1
0

endian.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #ifndef _ENDIAN_H
  2. #define _ENDIAN_H
  3. #define __LITTLE_ENDIAN 1234
  4. #define __BIG_ENDIAN 4321
  5. #define __PDP_ENDIAN 3412
  6. #include <bits/endian.h>
  7. #if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
  8. #define BIG_ENDIAN __BIG_ENDIAN
  9. #define LITTLE_ENDIAN __LITTLE_ENDIAN
  10. #define PDP_ENDIAN __PDP_ENDIAN
  11. #define BYTE_ORDER __BYTE_ORDER
  12. #include <stdint.h>
  13. #if __STDC_VERSION__ >= 199901L
  14. inline
  15. #endif
  16. static uint16_t __bswap16(uint16_t __x)
  17. {
  18. return __x<<8 | __x>>8;
  19. }
  20. #if __STDC_VERSION__ >= 199901L
  21. inline
  22. #endif
  23. static uint32_t __bswap32(uint32_t __x)
  24. {
  25. return __x>>24 | __x>>8&0xff00 | __x<<8&0xff0000 | __x<<24;
  26. }
  27. #if __STDC_VERSION__ >= 199901L
  28. inline
  29. #endif
  30. static uint64_t __bswap64(uint64_t __x)
  31. {
  32. return __bswap32(__x)+0ULL<<32 | __bswap32(__x>>32);
  33. }
  34. #if __BYTE_ORDER == __LITTLE_ENDIAN
  35. #define htobe16(x) __bswap16(x)
  36. #define be16toh(x) __bswap16(x)
  37. #define betoh16(x) __bswap16(x)
  38. #define htobe32(x) __bswap32(x)
  39. #define be32toh(x) __bswap32(x)
  40. #define betoh32(x) __bswap32(x)
  41. #define htobe64(x) __bswap64(x)
  42. #define be64toh(x) __bswap64(x)
  43. #define betoh64(x) __bswap64(x)
  44. #define htole16(x) (uint16_t)(x)
  45. #define le16toh(x) (uint16_t)(x)
  46. #define letoh16(x) (uint16_t)(x)
  47. #define htole32(x) (uint32_t)(x)
  48. #define le32toh(x) (uint32_t)(x)
  49. #define letoh32(x) (uint32_t)(x)
  50. #define htole64(x) (uint64_t)(x)
  51. #define le64toh(x) (uint64_t)(x)
  52. #define letoh64(x) (uint64_t)(x)
  53. #else
  54. #define htobe16(x) (uint16_t)(x)
  55. #define be16toh(x) (uint16_t)(x)
  56. #define betoh16(x) (uint16_t)(x)
  57. #define htobe32(x) (uint32_t)(x)
  58. #define be32toh(x) (uint32_t)(x)
  59. #define betoh32(x) (uint32_t)(x)
  60. #define htobe64(x) (uint64_t)(x)
  61. #define be64toh(x) (uint64_t)(x)
  62. #define betoh64(x) (uint64_t)(x)
  63. #define htole16(x) __bswap16(x)
  64. #define le16toh(x) __bswap16(x)
  65. #define letoh16(x) __bswap16(x)
  66. #define htole32(x) __bswap32(x)
  67. #define le32toh(x) __bswap32(x)
  68. #define letoh32(x) __bswap32(x)
  69. #define htole64(x) __bswap64(x)
  70. #define le64toh(x) __bswap64(x)
  71. #define letoh64(x) __bswap64(x)
  72. #endif
  73. #endif
  74. #endif