You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

100 lines
2.6KB

  1. #ifndef __BSWAP_H__
  2. #define __BSWAP_H__
  3. #ifdef HAVE_BYTESWAP_H
  4. #include <byteswap.h>
  5. #else
  6. #include <inttypes.h> /* for __WORDSIZE */
  7. #ifdef ARCH_X86
  8. inline static unsigned short ByteSwap16(unsigned short x)
  9. {
  10. __asm("xchgb %b0,%h0" :
  11. "=q" (x) :
  12. "0" (x));
  13. return x;
  14. }
  15. #define bswap_16(x) ByteSwap16(x)
  16. inline static unsigned int ByteSwap32(unsigned int x)
  17. {
  18. #if __CPU__ > 386
  19. __asm("bswap %0":
  20. "=r" (x) :
  21. #else
  22. __asm("xchgb %b0,%h0\n"
  23. " rorl $16,%0\n"
  24. " xchgb %b0,%h0":
  25. "=q" (x) :
  26. #endif
  27. "0" (x));
  28. return x;
  29. }
  30. #define bswap_32(x) ByteSwap32(x)
  31. inline static unsigned long long int ByteSwap64(unsigned long long int x)
  32. {
  33. register union { __extension__ unsigned long long int __ll;
  34. unsigned long int __l[2]; } __x;
  35. asm("xchgl %0,%1":
  36. "=r"(__x.__l[0]),"=r"(__x.__l[1]):
  37. "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
  38. return __x.__ll;
  39. }
  40. #define bswap_64(x) ByteSwap64(x)
  41. #else
  42. #define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
  43. // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
  44. #define bswap_32(x) \
  45. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  46. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  47. #if __WORDSIZE >= 64
  48. # define bswap_64(x) \
  49. ((((x) & 0xff00000000000000ull) >> 56) \
  50. | (((x) & 0x00ff000000000000ull) >> 40) \
  51. | (((x) & 0x0000ff0000000000ull) >> 24) \
  52. | (((x) & 0x000000ff00000000ull) >> 8) \
  53. | (((x) & 0x00000000ff000000ull) << 8) \
  54. | (((x) & 0x0000000000ff0000ull) << 24) \
  55. | (((x) & 0x000000000000ff00ull) << 40) \
  56. | (((x) & 0x00000000000000ffull) << 56))
  57. #else
  58. #define bswap_64(x) \
  59. (__extension__ \
  60. ({ union { __extension__ unsigned long long int __ll; \
  61. unsigned long int __l[2]; } __w, __r; \
  62. __w.__ll = (x); \
  63. __r.__l[0] = bswap_32 (__w.__l[1]); \
  64. __r.__l[1] = bswap_32 (__w.__l[0]); \
  65. __r.__ll; }))
  66. #endif
  67. #endif /* !ARCH_X86 */
  68. #endif /* !HAVE_BYTESWAP_H */
  69. // be2me ... BigEndian to MachineEndian
  70. // le2me ... LittleEndian to MachineEndian
  71. #ifdef WORDS_BIGENDIAN
  72. #define be2me_16(x) (x)
  73. #define be2me_32(x) (x)
  74. #define be2me_64(x) (x)
  75. #define le2me_16(x) bswap_16(x)
  76. #define le2me_32(x) bswap_32(x)
  77. #define le2me_64(x) bswap_64(x)
  78. #else
  79. #define be2me_16(x) bswap_16(x)
  80. #define be2me_32(x) bswap_32(x)
  81. #define be2me_64(x) bswap_64(x)
  82. #define le2me_16(x) (x)
  83. #define le2me_32(x) (x)
  84. #define le2me_64(x) (x)
  85. #endif
  86. #endif /* __BSWAP_H__ */