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.

141 lines
3.4KB

  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 swapping routines
  23. */
  24. #ifndef FFMPEG_BSWAP_H
  25. #define FFMPEG_BSWAP_H
  26. #include <stdint.h>
  27. #include "config.h"
  28. #include "common.h"
  29. #ifdef HAVE_BYTESWAP_H
  30. #include <byteswap.h>
  31. #else
  32. #ifdef ARCH_X86_64
  33. # define LEGACY_REGS "=Q"
  34. #else
  35. # define LEGACY_REGS "=q"
  36. #endif
  37. static av_always_inline uint16_t bswap_16(uint16_t x)
  38. {
  39. #if defined(ARCH_X86)
  40. __asm("rorw $8, %0" :
  41. LEGACY_REGS (x) :
  42. "0" (x));
  43. #elif defined(ARCH_SH4)
  44. __asm__("swap.b %0,%0":"=r"(x):"0"(x));
  45. #else
  46. x= (x>>8) | (x<<8);
  47. #endif
  48. return x;
  49. }
  50. static av_always_inline uint32_t bswap_32(uint32_t x)
  51. {
  52. #if defined(ARCH_X86)
  53. #ifdef HAVE_BSWAP
  54. __asm("bswap %0":
  55. "=r" (x) :
  56. #else
  57. __asm("xchgb %b0,%h0\n"
  58. "rorl $16,%0 \n"
  59. "xchgb %b0,%h0":
  60. LEGACY_REGS (x) :
  61. #endif
  62. "0" (x));
  63. #elif defined(ARCH_SH4)
  64. __asm__("swap.b %0,%0\n"
  65. "swap.w %0,%0\n"
  66. "swap.b %0,%0\n"
  67. :"=r"(x):"0"(x));
  68. #elif defined(ARCH_ARM)
  69. uint32_t t;
  70. __asm__ ("eor %1, %0, %0, ror #16 \n\t"
  71. "bic %1, %1, #0xFF0000 \n\t"
  72. "mov %0, %0, ror #8 \n\t"
  73. "eor %0, %0, %1, lsr #8 \n\t"
  74. : "+r"(x), "+r"(t));
  75. #elif defined(ARCH_BFIN)
  76. unsigned tmp;
  77. asm("%1 = %0 >> 8 (V); \n\t"
  78. "%0 = %0 << 8 (V); \n\t"
  79. "%0 = %0 | %1; \n\t"
  80. "%0 = PACK(%0.L, %0.H); \n\t"
  81. : "+d"(x), "=&d"(tmp));
  82. #else
  83. x= ((x<<8)&0xFF00FF00) | ((x>>8)&0x00FF00FF);
  84. x= (x>>16) | (x<<16);
  85. #endif
  86. return x;
  87. }
  88. static inline uint64_t bswap_64(uint64_t x)
  89. {
  90. #if 0
  91. x= ((x<< 8)&0xFF00FF00FF00FF00ULL) | ((x>> 8)&0x00FF00FF00FF00FFULL);
  92. x= ((x<<16)&0xFFFF0000FFFF0000ULL) | ((x>>16)&0x0000FFFF0000FFFFULL);
  93. return (x>>32) | (x<<32);
  94. #elif defined(ARCH_X86_64)
  95. __asm("bswap %0":
  96. "=r" (x) :
  97. "0" (x));
  98. return x;
  99. #else
  100. union {
  101. uint64_t ll;
  102. uint32_t l[2];
  103. } w, r;
  104. w.ll = x;
  105. r.l[0] = bswap_32 (w.l[1]);
  106. r.l[1] = bswap_32 (w.l[0]);
  107. return r.ll;
  108. #endif
  109. }
  110. #endif /* !HAVE_BYTESWAP_H */
  111. // be2me ... BigEndian to MachineEndian
  112. // le2me ... LittleEndian to MachineEndian
  113. #ifdef WORDS_BIGENDIAN
  114. #define be2me_16(x) (x)
  115. #define be2me_32(x) (x)
  116. #define be2me_64(x) (x)
  117. #define le2me_16(x) bswap_16(x)
  118. #define le2me_32(x) bswap_32(x)
  119. #define le2me_64(x) bswap_64(x)
  120. #else
  121. #define be2me_16(x) bswap_16(x)
  122. #define be2me_32(x) bswap_32(x)
  123. #define be2me_64(x) bswap_64(x)
  124. #define le2me_16(x) (x)
  125. #define le2me_32(x) (x)
  126. #define le2me_64(x) (x)
  127. #endif
  128. #endif /* FFMPEG_BSWAP_H */