2
0

endian_inl.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Endian related functions.
  11. #ifndef VPX_VPX_UTIL_ENDIAN_INL_H_
  12. #define VPX_VPX_UTIL_ENDIAN_INL_H_
  13. #include <stdlib.h>
  14. #include "./vpx_config.h"
  15. #include "vpx/vpx_integer.h"
  16. #if defined(__GNUC__)
  17. #define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
  18. #define LOCAL_GCC_PREREQ(maj, min) (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
  19. #else
  20. #define LOCAL_GCC_VERSION 0
  21. #define LOCAL_GCC_PREREQ(maj, min) 0
  22. #endif
  23. // handle clang compatibility
  24. #ifndef __has_builtin
  25. #define __has_builtin(x) 0
  26. #endif
  27. // some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
  28. #if !defined(WORDS_BIGENDIAN) && \
  29. (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
  30. (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
  31. #define WORDS_BIGENDIAN
  32. #endif
  33. #if defined(WORDS_BIGENDIAN)
  34. #define HToLE32 BSwap32
  35. #define HToLE16 BSwap16
  36. #define HToBE64(x) (x)
  37. #define HToBE32(x) (x)
  38. #else
  39. #define HToLE32(x) (x)
  40. #define HToLE16(x) (x)
  41. #define HToBE64(X) BSwap64(X)
  42. #define HToBE32(X) BSwap32(X)
  43. #endif
  44. #if LOCAL_GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
  45. #define HAVE_BUILTIN_BSWAP16
  46. #endif
  47. #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32)
  48. #define HAVE_BUILTIN_BSWAP32
  49. #endif
  50. #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64)
  51. #define HAVE_BUILTIN_BSWAP64
  52. #endif
  53. #if HAVE_MIPS32 && defined(__mips__) && !defined(__mips64) && \
  54. defined(__mips_isa_rev) && (__mips_isa_rev >= 2) && (__mips_isa_rev < 6)
  55. #define VPX_USE_MIPS32_R2
  56. #endif
  57. static INLINE uint16_t BSwap16(uint16_t x) {
  58. #if defined(HAVE_BUILTIN_BSWAP16)
  59. return __builtin_bswap16(x);
  60. #elif defined(_MSC_VER)
  61. return _byteswap_ushort(x);
  62. #else
  63. // gcc will recognize a 'rorw $8, ...' here:
  64. return (x >> 8) | ((x & 0xff) << 8);
  65. #endif // HAVE_BUILTIN_BSWAP16
  66. }
  67. static INLINE uint32_t BSwap32(uint32_t x) {
  68. #if defined(VPX_USE_MIPS32_R2)
  69. uint32_t ret;
  70. __asm__ volatile(
  71. "wsbh %[ret], %[x] \n\t"
  72. "rotr %[ret], %[ret], 16 \n\t"
  73. : [ret] "=r"(ret)
  74. : [x] "r"(x));
  75. return ret;
  76. #elif defined(HAVE_BUILTIN_BSWAP32)
  77. return __builtin_bswap32(x);
  78. #elif defined(__i386__) || defined(__x86_64__)
  79. uint32_t swapped_bytes;
  80. __asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x));
  81. return swapped_bytes;
  82. #elif defined(_MSC_VER)
  83. return (uint32_t)_byteswap_ulong(x);
  84. #else
  85. return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
  86. #endif // HAVE_BUILTIN_BSWAP32
  87. }
  88. static INLINE uint64_t BSwap64(uint64_t x) {
  89. #if defined(HAVE_BUILTIN_BSWAP64)
  90. return __builtin_bswap64(x);
  91. #elif defined(__x86_64__)
  92. uint64_t swapped_bytes;
  93. __asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x));
  94. return swapped_bytes;
  95. #elif defined(_MSC_VER)
  96. return (uint64_t)_byteswap_uint64(x);
  97. #else // generic code for swapping 64-bit values (suggested by bdb@)
  98. x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32);
  99. x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16);
  100. x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8);
  101. return x;
  102. #endif // HAVE_BUILTIN_BSWAP64
  103. }
  104. #endif // VPX_VPX_UTIL_ENDIAN_INL_H_