endian.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #ifndef __ENDIAN_H
  24. #define __ENDIAN_H
  25. #include <sys/types.h>
  26. #ifndef LITTLE_ENDIAN
  27. #define LITTLE_ENDIAN 1234
  28. #endif
  29. #ifndef BIG_ENDIAN
  30. #define BIG_ENDIAN 4321
  31. #endif
  32. #if __POWERPC__
  33. #include <ppc_intrinsics.h>
  34. #endif
  35. #if defined(ARCH_ARM)
  36. #define BYTE_ORDER LITTLE_ENDIAN
  37. #endif
  38. #if defined(__i386__) || defined(_X86_)
  39. #define BYTE_ORDER LITTLE_ENDIAN
  40. #endif
  41. #ifndef BYTE_ORDER
  42. #error "need to get the BYTE_ORDER define from somewhere"
  43. #endif
  44. // define a macro that unconditionally swaps
  45. #define SWAP_32(x) \
  46. (((uint32_t)(x) << 24) | (((uint32_t)(x) & 0xff00) << 8) |(((uint32_t)(x) & 0x00ff0000) >> 8) | ((uint32_t)(x) >> 24))
  47. #define SWAP_16(x) \
  48. ((((uint16_t)(x) & 0xff) << 8) | ((uint16_t)(x) >> 8))
  49. // standard swap macros
  50. #if BYTE_ORDER == BIG_ENDIAN
  51. #define LE32(val) SWAP_32(val)
  52. #define LE16(val) SWAP_16(val)
  53. #define BE32(val) (val)
  54. #define BE16(val) (val)
  55. #else
  56. #define LE32(val) (val)
  57. #define LE16(val) (val)
  58. #define BE32(val) SWAP_32(val)
  59. #define BE16(val) SWAP_16(val)
  60. #endif
  61. #define LE32SWAP(var) (var) = LE32(var);
  62. #define LE16SWAP(var) (var) = LE16(var);
  63. #define BE32SWAP(var) (var) = BE32(var);
  64. #define BE16SWAP(var) (var) = BE16(var);
  65. /* classic network byte swap stuff */
  66. #define ntohs(n) BE16(n)
  67. #define htons(h) BE16(h)
  68. #define ntohl(n) BE32(n)
  69. #define htonl(h) BE32(h)
  70. // some memory access macros
  71. #if __POWERPC__
  72. #define READ_MEM_WORD(ptr) __lwbrx((word *)(ptr), 0)
  73. #define READ_MEM_HALFWORD(ptr) __lhbrx((halfword *)(ptr), 0)
  74. #define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
  75. #define WRITE_MEM_WORD(ptr, data) __stwbrx(data, (word *)(ptr), 0)
  76. #define WRITE_MEM_HALFWORD(ptr, data) __sthbrx(data, (halfword *)(ptr), 0)
  77. #define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
  78. #else
  79. #define READ_MEM_WORD(ptr) SWAPIT_32(*(word *)(ptr))
  80. #define READ_MEM_HALFWORD(ptr) SWAPIT_16(*(halfword *)(ptr))
  81. #define READ_MEM_BYTE(ptr) (*(byte *)(ptr))
  82. #define WRITE_MEM_WORD(ptr, data) (*(word *)(ptr) = SWAPIT_32(data))
  83. #define WRITE_MEM_HALFWORD(ptr, data) (*(halfword *)(ptr) = SWAPIT_16(data))
  84. #define WRITE_MEM_BYTE(ptr, data) (*(byte *)(ptr) = (data))
  85. #endif
  86. #endif