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.

87 lines
2.1KB

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