bswap.h 3.2 KB

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