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.

127 lines
3.3KB

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