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.

139 lines
3.2KB

  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. static av_always_inline uint16_t bswap_16(uint16_t x)
  35. {
  36. #if defined(ARCH_X86)
  37. __asm("rorw $8, %0" :
  38. LEGACY_REGS (x) :
  39. "0" (x));
  40. #elif defined(ARCH_SH4)
  41. __asm__("swap.b %0,%0":"=r"(x):"0"(x));
  42. #else
  43. x= (x>>8) | (x<<8);
  44. #endif
  45. return x;
  46. }
  47. static av_always_inline uint32_t bswap_32(uint32_t x)
  48. {
  49. #if defined(ARCH_X86)
  50. #if __CPU__ != 386
  51. __asm("bswap %0":
  52. "=r" (x) :
  53. #else
  54. __asm("xchgb %b0,%h0\n"
  55. " rorl $16,%0\n"
  56. " xchgb %b0,%h0":
  57. LEGACY_REGS (x) :
  58. #endif
  59. "0" (x));
  60. #elif defined(ARCH_SH4)
  61. __asm__(
  62. "swap.b %0,%0\n"
  63. "swap.w %0,%0\n"
  64. "swap.b %0,%0\n"
  65. :"=r"(x):"0"(x));
  66. #elif defined(ARCH_ARM)
  67. uint32_t t;
  68. __asm__ (
  69. "eor %1, %0, %0, ror #16 \n\t"
  70. "bic %1, %1, #0xFF0000 \n\t"
  71. "mov %0, %0, ror #8 \n\t"
  72. "eor %0, %0, %1, lsr #8 \n\t"
  73. : "+r"(x), "+r"(t));
  74. #elif defined(ARCH_BFIN)
  75. unsigned tmp;
  76. asm("%1 = %0 >> 8 (V);\n\t"
  77. "%0 = %0 << 8 (V);\n\t"
  78. "%0 = %0 | %1;\n\t"
  79. "%0 = PACK(%0.L, %0.H);\n\t"
  80. : "+d"(x), "=&d"(tmp));
  81. #else
  82. x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
  83. x= (x>>16) | (x<<16);
  84. #endif
  85. return x;
  86. }
  87. static inline uint64_t bswap_64(uint64_t x)
  88. {
  89. #if 0
  90. x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
  91. x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
  92. return (x>>32) | (x<<32);
  93. #elif defined(ARCH_X86_64)
  94. __asm("bswap %0":
  95. "=r" (x) :
  96. "0" (x));
  97. return x;
  98. #else
  99. union {
  100. uint64_t ll;
  101. uint32_t l[2];
  102. } w, r;
  103. w.ll = x;
  104. r.l[0] = bswap_32 (w.l[1]);
  105. r.l[1] = bswap_32 (w.l[0]);
  106. return r.ll;
  107. #endif
  108. }
  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__ */