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.

143 lines
2.8KB

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