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.

130 lines
2.6KB

  1. /**
  2. * @file bswap.h
  3. * byte swap.
  4. */
  5. #ifndef __BSWAP_H__
  6. #define __BSWAP_H__
  7. #ifdef HAVE_BYTESWAP_H
  8. #include <byteswap.h>
  9. #else
  10. #ifdef ARCH_X86
  11. static inline unsigned short ByteSwap16(unsigned short x)
  12. {
  13. __asm("xchgb %b0,%h0" :
  14. "=q" (x) :
  15. "0" (x));
  16. return x;
  17. }
  18. #define bswap_16(x) ByteSwap16(x)
  19. static inline unsigned int ByteSwap32(unsigned int x)
  20. {
  21. #if __CPU__ > 386
  22. __asm("bswap %0":
  23. "=r" (x) :
  24. #else
  25. __asm("xchgb %b0,%h0\n"
  26. " rorl $16,%0\n"
  27. " xchgb %b0,%h0":
  28. "=q" (x) :
  29. #endif
  30. "0" (x));
  31. return x;
  32. }
  33. #define bswap_32(x) ByteSwap32(x)
  34. static inline unsigned long long int ByteSwap64(unsigned long long int x)
  35. {
  36. register union { __extension__ uint64_t __ll;
  37. uint32_t __l[2]; } __x;
  38. asm("xchgl %0,%1":
  39. "=r"(__x.__l[0]),"=r"(__x.__l[1]):
  40. "0"(bswap_32((unsigned long)x)),"1"(bswap_32((unsigned long)(x>>32))));
  41. return __x.__ll;
  42. }
  43. #define bswap_64(x) ByteSwap64(x)
  44. #elif defined(ARCH_SH4)
  45. static inline uint16_t ByteSwap16(uint16_t x) {
  46. __asm__("swap.b %0,%0":"=r"(x):"0"(x));
  47. return x;
  48. }
  49. static inline uint32_t ByteSwap32(uint32_t x) {
  50. __asm__(
  51. "swap.b %0,%0\n"
  52. "swap.w %0,%0\n"
  53. "swap.b %0,%0\n"
  54. :"=r"(x):"0"(x));
  55. return x;
  56. }
  57. #define bswap_16(x) ByteSwap16(x)
  58. #define bswap_32(x) ByteSwap32(x)
  59. static inline uint64_t ByteSwap64(uint64_t x)
  60. {
  61. union {
  62. uint64_t ll;
  63. struct {
  64. uint32_t l,h;
  65. } l;
  66. } r;
  67. r.l.l = bswap_32 (x);
  68. r.l.h = bswap_32 (x>>32);
  69. return r.ll;
  70. }
  71. #define bswap_64(x) ByteSwap64(x)
  72. #else
  73. #define bswap_16(x) (((x) & 0x00ff) << 8 | ((x) & 0xff00) >> 8)
  74. // code from bits/byteswap.h (C) 1997, 1998 Free Software Foundation, Inc.
  75. #define bswap_32(x) \
  76. ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >> 8) | \
  77. (((x) & 0x0000ff00) << 8) | (((x) & 0x000000ff) << 24))
  78. static inline uint64_t ByteSwap64(uint64_t x)
  79. {
  80. union {
  81. uint64_t ll;
  82. uint32_t l[2];
  83. } w, r;
  84. w.ll = x;
  85. r.l[0] = bswap_32 (w.l[1]);
  86. r.l[1] = bswap_32 (w.l[0]);
  87. return r.ll;
  88. }
  89. #define bswap_64(x) ByteSwap64(x)
  90. #endif /* !ARCH_X86 */
  91. #endif /* !HAVE_BYTESWAP_H */
  92. // be2me ... BigEndian to MachineEndian
  93. // le2me ... LittleEndian to MachineEndian
  94. #ifdef WORDS_BIGENDIAN
  95. #define be2me_16(x) (x)
  96. #define be2me_32(x) (x)
  97. #define be2me_64(x) (x)
  98. #define le2me_16(x) bswap_16(x)
  99. #define le2me_32(x) bswap_32(x)
  100. #define le2me_64(x) bswap_64(x)
  101. #else
  102. #define be2me_16(x) bswap_16(x)
  103. #define be2me_32(x) bswap_32(x)
  104. #define be2me_64(x) bswap_64(x)
  105. #define le2me_16(x) (x)
  106. #define le2me_32(x) (x)
  107. #define le2me_64(x) (x)
  108. #endif
  109. #endif /* __BSWAP_H__ */