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.

176 lines
3.8KB

  1. /*
  2. * copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file bswap.h
  22. * byte swap.
  23. */
  24. #ifndef __BSWAP_H__
  25. #define __BSWAP_H__
  26. #ifdef HAVE_BYTESWAP_H
  27. #include <byteswap.h>
  28. #else
  29. #ifdef ARCH_X86_64
  30. # define LEGACY_REGS "=Q"
  31. #else
  32. # define LEGACY_REGS "=q"
  33. #endif
  34. #if defined(ARCH_X86)
  35. static av_always_inline uint16_t bswap_16(uint16_t x)
  36. {
  37. __asm("rorw $8, %0" :
  38. LEGACY_REGS (x) :
  39. "0" (x));
  40. return x;
  41. }
  42. static av_always_inline uint32_t bswap_32(uint32_t x)
  43. {
  44. #if __CPU__ != 386
  45. __asm("bswap %0":
  46. "=r" (x) :
  47. #else
  48. __asm("xchgb %b0,%h0\n"
  49. " rorl $16,%0\n"
  50. " xchgb %b0,%h0":
  51. LEGACY_REGS (x) :
  52. #endif
  53. "0" (x));
  54. return x;
  55. }
  56. static inline uint64_t bswap_64(uint64_t x)
  57. {
  58. #ifdef ARCH_X86_64
  59. __asm("bswap %0":
  60. "=r" (x) :
  61. "0" (x));
  62. return x;
  63. #else
  64. union {
  65. uint64_t ll;
  66. struct {
  67. uint32_t l,h;
  68. } l;
  69. } r;
  70. r.l.l = bswap_32 (x);
  71. r.l.h = bswap_32 (x>>32);
  72. return r.ll;
  73. #endif
  74. }
  75. #elif defined(ARCH_SH4)
  76. static av_always_inline uint16_t bswap_16(uint16_t x) {
  77. __asm__("swap.b %0,%0":"=r"(x):"0"(x));
  78. return x;
  79. }
  80. static av_always_inline uint32_t bswap_32(uint32_t x) {
  81. __asm__(
  82. "swap.b %0,%0\n"
  83. "swap.w %0,%0\n"
  84. "swap.b %0,%0\n"
  85. :"=r"(x):"0"(x));
  86. return x;
  87. }
  88. static inline uint64_t bswap_64(uint64_t x)
  89. {
  90. union {
  91. uint64_t ll;
  92. struct {
  93. uint32_t l,h;
  94. } l;
  95. } r;
  96. r.l.l = bswap_32 (x);
  97. r.l.h = bswap_32 (x>>32);
  98. return r.ll;
  99. }
  100. #else
  101. static av_always_inline uint16_t bswap_16(uint16_t x){
  102. return (x>>8) | (x<<8);
  103. }
  104. #ifdef ARCH_ARM
  105. static av_always_inline uint32_t bswap_32(uint32_t x){
  106. uint32_t t;
  107. __asm__ (
  108. "eor %1, %0, %0, ror #16 \n\t"
  109. "bic %1, %1, #0xFF0000 \n\t"
  110. "mov %0, %0, ror #8 \n\t"
  111. "eor %0, %0, %1, lsr #8 \n\t"
  112. : "+r"(x), "+r"(t));
  113. return x;
  114. }
  115. #else
  116. static av_always_inline uint32_t bswap_32(uint32_t x){
  117. x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
  118. return (x>>16) | (x<<16);
  119. }
  120. #endif
  121. static inline uint64_t bswap_64(uint64_t x)
  122. {
  123. #if 0
  124. x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
  125. x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
  126. return (x>>32) | (x<<32);
  127. #else
  128. union {
  129. uint64_t ll;
  130. uint32_t l[2];
  131. } w, r;
  132. w.ll = x;
  133. r.l[0] = bswap_32 (w.l[1]);
  134. r.l[1] = bswap_32 (w.l[0]);
  135. return r.ll;
  136. #endif
  137. }
  138. #endif /* defined(ARCH_X86) */
  139. #endif /* !HAVE_BYTESWAP_H */
  140. // be2me ... BigEndian to MachineEndian
  141. // le2me ... LittleEndian to MachineEndian
  142. #ifdef WORDS_BIGENDIAN
  143. #define be2me_16(x) (x)
  144. #define be2me_32(x) (x)
  145. #define be2me_64(x) (x)
  146. #define le2me_16(x) bswap_16(x)
  147. #define le2me_32(x) bswap_32(x)
  148. #define le2me_64(x) bswap_64(x)
  149. #else
  150. #define be2me_16(x) bswap_16(x)
  151. #define be2me_32(x) bswap_32(x)
  152. #define be2me_64(x) bswap_64(x)
  153. #define le2me_16(x) (x)
  154. #define le2me_32(x) (x)
  155. #define le2me_64(x) (x)
  156. #endif
  157. #endif /* __BSWAP_H__ */