bswap.h 3.3 KB

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