bswap.h 3.0 KB

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