endianness.h 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #ifndef ENDIANNESS_H
  2. #define ENDIANNESS_H
  3. #ifndef __LITTLE_ENDIAN
  4. #define __LITTLE_ENDIAN 1234
  5. #endif
  6. #ifndef __BIG_ENDIAN
  7. #define __BIG_ENDIAN 4321
  8. #endif
  9. #ifndef __BYTE_ORDER
  10. #ifdef __LITTLE_ENDIAN__
  11. #define __BYTE_ORDER __LITTLE_ENDIAN
  12. #else
  13. #ifdef __BIG_ENDIAN__
  14. #define __BYTE_ORDER __BIG_ENDIAN
  15. #endif
  16. #endif
  17. #endif
  18. #ifndef be16toh
  19. #if __BYTE_ORDER == __BIG_ENDIAN
  20. #define be16toh(x) (x)
  21. #else
  22. #define be16toh(x) ((((x) & 0xFF00) >> 8) | (((x) & 0x00FF) << 8))
  23. #endif
  24. #endif
  25. #ifndef __bswap_32
  26. #define __bswap_32(x) ((((x) & 0xFF000000) >> 24) \
  27. | (((x) & 0x00FF0000) >> 8) \
  28. | (((x) & 0x0000FF00) << 8) \
  29. | (((x) & 0x000000FF) << 24))
  30. #endif
  31. #ifndef be32toh
  32. #if __BYTE_ORDER == __BIG_ENDIAN
  33. #define be32toh(x) (x)
  34. #else
  35. #define be32toh(x) __bswap_32(x)
  36. #endif
  37. #endif
  38. #ifndef htobe32
  39. #define htobe32 be32toh
  40. #endif
  41. #ifndef le32toh
  42. #if __BYTE_ORDER == __BIG_ENDIAN
  43. #define le32toh(x) __bswap_32(x)
  44. #else
  45. #define le32toh(x) (x)
  46. #endif
  47. #endif
  48. #ifndef htole32
  49. #define htole32 le32toh
  50. #endif
  51. #ifndef __bswap_64
  52. #define __bswap_64(x) ((((x) & 0xFF00000000000000ull) >> 56) \
  53. | (((x) & 0x00FF000000000000ull) >> 40) \
  54. | (((x) & 0x0000FF0000000000ull) >> 24) \
  55. | (((x) & 0x000000FF00000000ull) >> 8) \
  56. | (((x) & 0x00000000FF000000ull) << 8) \
  57. | (((x) & 0x0000000000FF0000ull) << 24) \
  58. | (((x) & 0x000000000000FF00ull) << 40) \
  59. | (((x) & 0x00000000000000FFull) << 56))
  60. #endif
  61. #ifndef htobe64
  62. #if __BYTE_ORDER == __BIG_ENDIAN
  63. #define htobe64(x) (x)
  64. #else
  65. #define htobe64(x) __bswap_64(x)
  66. #endif
  67. #endif
  68. #ifndef be64toh
  69. #define be64toh htobe64
  70. #endif
  71. #ifndef le64toh
  72. #if __BYTE_ORDER == __LITTLE_ENDIAN
  73. #define le64toh(x) (x)
  74. #else
  75. #define le64toh(x) __bswap_64(x)
  76. #endif
  77. #endif
  78. #ifndef htole64
  79. #define htole64 le64toh
  80. #endif
  81. #endif /* ENDIANNESS_H */