Endian.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Endian_H
  16. #define Endian_H
  17. #include <stdint.h>
  18. #if defined(Endian_BIG) || defined(Endian_LITTLE)
  19. // fallthrough
  20. #elif defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && \
  21. __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  22. #define Endian_LITTLE
  23. #elif defined(__BYTE_ORDER__) && defined(__ORDER_BIG_ENDIAN__) && \
  24. __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  25. #define Endian_BIG
  26. #elif defined(__amd64__) || defined(__x86_64__) || defined(__AMD64__) || \
  27. defined(_M_X64) || defined(__amd64) || defined(__i386__) || defined(__x86__) || \
  28. defined(__X86__) || defined(_M_IX86) || defined(__i386)
  29. #define Endian_LITTLE
  30. #elif defined(__SPU__) || defined(__powerpc64__) || defined(__ppc64__) || \
  31. defined(__PPC64__) || defined(_ARCH_PPC64) || defined(__powerpc__) || \
  32. defined(__ppc__) || defined(__PPC__) || defined(_ARCH_PPC) || \
  33. defined(__sparcv9__) || defined(__sparcv9) || defined(__sparc_v8__) || \
  34. defined(__sparc__) || defined(__sparc)
  35. #define Endian_BIG
  36. #else
  37. #error Unable to determine byte order of target machine, please specify -DEndian_BIG=1 or \
  38. -DEndian_LITTLE=1
  39. #endif
  40. #if defined(Endian_LITTLE)
  41. #define Endian_isBigEndian() 0
  42. #elif defined(Endian_BIG)
  43. #define Endian_isBigEndian() 1
  44. #endif
  45. #if defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8
  46. #define Endian_byteSwapNoCast16(x) __builtin_bswap16(x)
  47. #else
  48. #define Endian_byteSwapNoCast16(x) \
  49. (__extension__ ({ \
  50. uint16_t _x = (x); \
  51. _x >> 8 | _x << 8; \
  52. }))
  53. #endif
  54. #define Endian_byteSwapNoCast32(x) __builtin_bswap32(x)
  55. #define Endian_byteSwapNoCast64(x) __builtin_bswap64(x)
  56. // Make sure the size is right.
  57. #define Endian_byteSwap16(x) ((uint16_t)Endian_byteSwapNoCast16(((uint16_t)(x))))
  58. #define Endian_byteSwap32(x) ((uint32_t)Endian_byteSwapNoCast32(((uint32_t)(x))))
  59. #define Endian_byteSwap64(x) ((uint64_t)Endian_byteSwapNoCast64(((uint64_t)(x))))
  60. #if defined(Endian_BIG)
  61. #define Endian_hostToLittleEndian16(input) Endian_byteSwap16(input)
  62. #define Endian_hostToBigEndian16(input) (input)
  63. #define Endian_hostToLittleEndian32(input) Endian_byteSwap32(input)
  64. #define Endian_hostToBigEndian32(input) (input)
  65. #define Endian_hostToLittleEndian64(input) Endian_byteSwap64(input)
  66. #define Endian_hostToBigEndian64(input) (input)
  67. #elif defined(Endian_LITTLE)
  68. #define Endian_hostToLittleEndian16(input) (input)
  69. #define Endian_hostToBigEndian16(input) Endian_byteSwap16(input)
  70. #define Endian_hostToLittleEndian32(input) (input)
  71. #define Endian_hostToBigEndian32(input) Endian_byteSwap32(input)
  72. #define Endian_hostToLittleEndian64(input) (input)
  73. #define Endian_hostToBigEndian64(input) Endian_byteSwap64(input)
  74. #endif
  75. // looks silly but some macros want to be able to do generic swapping for any common int type.
  76. #define Endian_littleEndianToHost8(x) (x)
  77. #define Endian_bigEndianToHost8(x) (x)
  78. #define Endian_hostToLittleEndian8(x) (x)
  79. #define Endian_hostToBigEndian8(x) (x)
  80. #define Endian_littleEndianToHost16(x) Endian_hostToLittleEndian16(x)
  81. #define Endian_bigEndianToHost16(x) Endian_hostToBigEndian16(x)
  82. #define Endian_littleEndianToHost32(x) Endian_hostToLittleEndian32(x)
  83. #define Endian_bigEndianToHost32(x) Endian_hostToBigEndian32(x)
  84. #define Endian_littleEndianToHost64(x) Endian_hostToLittleEndian64(x)
  85. #define Endian_bigEndianToHost64(x) Endian_hostToBigEndian64(x)
  86. #endif