1
0

Endian.h 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <https://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) || defined(_MIPSEL) || \
  29. defined(MIPSEL)
  30. #define Endian_LITTLE
  31. #elif defined(__SPU__) || defined(__powerpc64__) || defined(__ppc64__) || \
  32. defined(__PPC64__) || defined(_ARCH_PPC64) || defined(__powerpc__) || \
  33. defined(__ppc__) || defined(__PPC__) || defined(_ARCH_PPC) || \
  34. defined(__sparcv9__) || defined(__sparcv9) || defined(__sparc_v8__) || \
  35. defined(__sparc__) || defined(__sparc) || defined(_MIPSEB) || defined(MIPSEB)
  36. #define Endian_BIG
  37. #else
  38. #error Unable to determine byte order of target machine, please specify -DEndian_BIG=1 or \
  39. -DEndian_LITTLE=1
  40. #endif
  41. #if defined(Endian_LITTLE)
  42. #define Endian_isBigEndian() 0
  43. #elif defined(Endian_BIG)
  44. #define Endian_isBigEndian() 1
  45. #endif
  46. #if defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 8
  47. #define Endian_byteSwapNoCast16(x) __builtin_bswap16(x)
  48. #else
  49. #define Endian_byteSwapNoCast16(x) \
  50. (__extension__ ({ \
  51. uint16_t _x = (x); \
  52. _x >> 8 | _x << 8; \
  53. }))
  54. #endif
  55. #define Endian_byteSwapNoCast32(x) __builtin_bswap32(x)
  56. #define Endian_byteSwapNoCast64(x) __builtin_bswap64(x)
  57. // Make sure the size is right.
  58. #define Endian_byteSwap16(x) ((uint16_t)Endian_byteSwapNoCast16(((uint16_t)(x))))
  59. #define Endian_byteSwap32(x) ((uint32_t)Endian_byteSwapNoCast32(((uint32_t)(x))))
  60. #define Endian_byteSwap64(x) ((uint64_t)Endian_byteSwapNoCast64(((uint64_t)(x))))
  61. #if defined(Endian_BIG)
  62. #define Endian_hostToLittleEndian16(input) Endian_byteSwap16(input)
  63. #define Endian_hostToBigEndian16(input) (input)
  64. #define Endian_hostToLittleEndian32(input) Endian_byteSwap32(input)
  65. #define Endian_hostToBigEndian32(input) (input)
  66. #define Endian_hostToLittleEndian64(input) Endian_byteSwap64(input)
  67. #define Endian_hostToBigEndian64(input) (input)
  68. #elif defined(Endian_LITTLE)
  69. #define Endian_hostToLittleEndian16(input) (input)
  70. #define Endian_hostToBigEndian16(input) Endian_byteSwap16(input)
  71. #define Endian_hostToLittleEndian32(input) (input)
  72. #define Endian_hostToBigEndian32(input) Endian_byteSwap32(input)
  73. #define Endian_hostToLittleEndian64(input) (input)
  74. #define Endian_hostToBigEndian64(input) Endian_byteSwap64(input)
  75. #endif
  76. // looks silly but some macros want to be able to do generic swapping for any common int type.
  77. #define Endian_littleEndianToHost8(x) (x)
  78. #define Endian_bigEndianToHost8(x) (x)
  79. #define Endian_hostToLittleEndian8(x) (x)
  80. #define Endian_hostToBigEndian8(x) (x)
  81. #define Endian_littleEndianToHost16(x) Endian_hostToLittleEndian16(x)
  82. #define Endian_bigEndianToHost16(x) Endian_hostToBigEndian16(x)
  83. #define Endian_littleEndianToHost32(x) Endian_hostToLittleEndian32(x)
  84. #define Endian_bigEndianToHost32(x) Endian_hostToBigEndian32(x)
  85. #define Endian_littleEndianToHost64(x) Endian_hostToLittleEndian64(x)
  86. #define Endian_bigEndianToHost64(x) Endian_hostToBigEndian64(x)
  87. #endif