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.

133 lines
2.5KB

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