endian_inl.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_UTIL_ENDIAN_INL_H_
  12. #define 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) \
  19. (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
  20. #else
  21. # define LOCAL_GCC_VERSION 0
  22. # define LOCAL_GCC_PREREQ(maj, min) 0
  23. #endif
  24. // handle clang compatibility
  25. #ifndef __has_builtin
  26. # define __has_builtin(x) 0
  27. #endif
  28. // some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
  29. #if !defined(WORDS_BIGENDIAN) && \
  30. (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
  31. (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
  32. #define WORDS_BIGENDIAN
  33. #endif
  34. #if defined(WORDS_BIGENDIAN)
  35. #define HToLE32 BSwap32
  36. #define HToLE16 BSwap16
  37. #define HToBE64(x) (x)
  38. #define HToBE32(x) (x)
  39. #else
  40. #define HToLE32(x) (x)
  41. #define HToLE16(x) (x)
  42. #define HToBE64(X) BSwap64(X)
  43. #define HToBE32(X) BSwap32(X)
  44. #endif
  45. #if LOCAL_GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
  46. #define HAVE_BUILTIN_BSWAP16
  47. #endif
  48. #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32)
  49. #define HAVE_BUILTIN_BSWAP32
  50. #endif
  51. #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64)
  52. #define HAVE_BUILTIN_BSWAP64
  53. #endif
  54. #if HAVE_MIPS32 && defined(__mips__) && !defined(__mips64) && \
  55. defined(__mips_isa_rev) && (__mips_isa_rev >= 2) && (__mips_isa_rev < 6)
  56. #define VPX_USE_MIPS32_R2
  57. #endif
  58. static INLINE uint16_t BSwap16(uint16_t x) {
  59. #if defined(HAVE_BUILTIN_BSWAP16)
  60. return __builtin_bswap16(x);
  61. #elif defined(_MSC_VER)
  62. return _byteswap_ushort(x);
  63. #else
  64. // gcc will recognize a 'rorw $8, ...' here:
  65. return (x >> 8) | ((x & 0xff) << 8);
  66. #endif // HAVE_BUILTIN_BSWAP16
  67. }
  68. static INLINE uint32_t BSwap32(uint32_t x) {
  69. #if defined(VPX_USE_MIPS32_R2)
  70. uint32_t ret;
  71. __asm__ volatile (
  72. "wsbh %[ret], %[x] \n\t"
  73. "rotr %[ret], %[ret], 16 \n\t"
  74. : [ret]"=r"(ret)
  75. : [x]"r"(x)
  76. );
  77. return ret;
  78. #elif defined(HAVE_BUILTIN_BSWAP32)
  79. return __builtin_bswap32(x);
  80. #elif defined(__i386__) || defined(__x86_64__)
  81. uint32_t swapped_bytes;
  82. __asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x));
  83. return swapped_bytes;
  84. #elif defined(_MSC_VER)
  85. return (uint32_t)_byteswap_ulong(x);
  86. #else
  87. return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
  88. #endif // HAVE_BUILTIN_BSWAP32
  89. }
  90. static INLINE uint64_t BSwap64(uint64_t x) {
  91. #if defined(HAVE_BUILTIN_BSWAP64)
  92. return __builtin_bswap64(x);
  93. #elif defined(__x86_64__)
  94. uint64_t swapped_bytes;
  95. __asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x));
  96. return swapped_bytes;
  97. #elif defined(_MSC_VER)
  98. return (uint64_t)_byteswap_uint64(x);
  99. #else // generic code for swapping 64-bit values (suggested by bdb@)
  100. x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32);
  101. x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16);
  102. x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8);
  103. return x;
  104. #endif // HAVE_BUILTIN_BSWAP64
  105. }
  106. #endif // VPX_UTIL_ENDIAN_INL_H_