1 // Copyright 2014 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
10 // Endian related functions.
12 #ifndef VPX_UTIL_ENDIAN_INL_H_
13 #define VPX_UTIL_ENDIAN_INL_H_
16 #include "./vpx_config.h"
17 #include "vpx/vpx_integer.h"
20 #define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
21 #define LOCAL_GCC_PREREQ(maj, min) (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
23 #define LOCAL_GCC_VERSION 0
24 #define LOCAL_GCC_PREREQ(maj, min) 0
27 // handle clang compatibility
29 #define __has_builtin(x) 0
32 // some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
33 #if !defined(WORDS_BIGENDIAN) && \
34 (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
35 (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
36 #define WORDS_BIGENDIAN
39 #if defined(WORDS_BIGENDIAN)
40 #define HToLE32 BSwap32
41 #define HToLE16 BSwap16
42 #define HToBE64(x) (x)
43 #define HToBE32(x) (x)
45 #define HToLE32(x) (x)
46 #define HToLE16(x) (x)
47 #define HToBE64(X) BSwap64(X)
48 #define HToBE32(X) BSwap32(X)
51 #if LOCAL_GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
52 #define HAVE_BUILTIN_BSWAP16
55 #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32)
56 #define HAVE_BUILTIN_BSWAP32
59 #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64)
60 #define HAVE_BUILTIN_BSWAP64
63 #if HAVE_MIPS32 && defined(__mips__) && !defined(__mips64) && \
64 defined(__mips_isa_rev) && (__mips_isa_rev >= 2) && (__mips_isa_rev < 6)
65 #define VPX_USE_MIPS32_R2
68 static INLINE uint16_t BSwap16(uint16_t x) {
69 #if defined(HAVE_BUILTIN_BSWAP16)
70 return __builtin_bswap16(x);
71 #elif defined(_MSC_VER)
72 return _byteswap_ushort(x);
74 // gcc will recognize a 'rorw $8, ...' here:
75 return (x >> 8) | ((x & 0xff) << 8);
76 #endif // HAVE_BUILTIN_BSWAP16
79 static INLINE uint32_t BSwap32(uint32_t x) {
80 #if defined(VPX_USE_MIPS32_R2)
83 "wsbh %[ret], %[x] \n\t"
84 "rotr %[ret], %[ret], 16 \n\t"
88 #elif defined(HAVE_BUILTIN_BSWAP32)
89 return __builtin_bswap32(x);
90 #elif defined(__i386__) || defined(__x86_64__)
91 uint32_t swapped_bytes;
92 __asm__ volatile("bswap %0" : "=r"(swapped_bytes) : "0"(x));
94 #elif defined(_MSC_VER)
95 return (uint32_t)_byteswap_ulong(x);
97 return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
98 #endif // HAVE_BUILTIN_BSWAP32
101 static INLINE uint64_t BSwap64(uint64_t x) {
102 #if defined(HAVE_BUILTIN_BSWAP64)
103 return __builtin_bswap64(x);
104 #elif defined(__x86_64__)
105 uint64_t swapped_bytes;
106 __asm__ volatile("bswapq %0" : "=r"(swapped_bytes) : "0"(x));
107 return swapped_bytes;
108 #elif defined(_MSC_VER)
109 return (uint64_t)_byteswap_uint64(x);
110 #else // generic code for swapping 64-bit values (suggested by bdb@)
111 x = ((x & 0xffffffff00000000ull) >> 32) | ((x & 0x00000000ffffffffull) << 32);
112 x = ((x & 0xffff0000ffff0000ull) >> 16) | ((x & 0x0000ffff0000ffffull) << 16);
113 x = ((x & 0xff00ff00ff00ff00ull) >> 8) | ((x & 0x00ff00ff00ff00ffull) << 8);
115 #endif // HAVE_BUILTIN_BSWAP64
118 #endif // VPX_UTIL_ENDIAN_INL_H_