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) \
22 (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
24 # define LOCAL_GCC_VERSION 0
25 # define LOCAL_GCC_PREREQ(maj, min) 0
29 # define LOCAL_CLANG_VERSION ((__clang_major__ << 8) | __clang_minor__)
30 # define LOCAL_CLANG_PREREQ(maj, min) \
31 (LOCAL_CLANG_VERSION >= (((maj) << 8) | (min)))
33 # define LOCAL_CLANG_VERSION 0
34 # define LOCAL_CLANG_PREREQ(maj, min) 0
37 // some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
38 #if !defined(WORDS_BIGENDIAN) && \
39 (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
40 (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
41 #define WORDS_BIGENDIAN
44 #if defined(WORDS_BIGENDIAN)
45 #define HToLE32 BSwap32
46 #define HToLE16 BSwap16
47 #define HToBE64(x) (x)
48 #define HToBE32(x) (x)
50 #define HToLE32(x) (x)
51 #define HToLE16(x) (x)
52 #define HToBE64(X) BSwap64(X)
53 #define HToBE32(X) BSwap32(X)
56 // clang-3.3 and gcc-4.3 have builtin functions for swap32/swap64
57 #if LOCAL_GCC_PREREQ(4, 3) || LOCAL_CLANG_PREREQ(3, 3)
58 #define HAVE_BUILTIN_BSWAP32
59 #define HAVE_BUILTIN_BSWAP64
61 // clang-3.3 and gcc-4.8 have a builtin function for swap16
62 #if LOCAL_GCC_PREREQ(4, 8) || LOCAL_CLANG_PREREQ(3, 3)
63 #define HAVE_BUILTIN_BSWAP16
66 #if HAVE_MIPS32 && defined(__mips__) && !defined(__mips64) && \
67 defined(__mips_isa_rev) && (__mips_isa_rev >= 2) && (__mips_isa_rev < 6)
68 #define VPX_USE_MIPS32_R2
71 static INLINE
uint16_t BSwap16(uint16_t x
) {
72 #if defined(HAVE_BUILTIN_BSWAP16)
73 return __builtin_bswap16(x
);
74 #elif defined(_MSC_VER)
75 return _byteswap_ushort(x
);
77 // gcc will recognize a 'rorw $8, ...' here:
78 return (x
>> 8) | ((x
& 0xff) << 8);
79 #endif // HAVE_BUILTIN_BSWAP16
82 static INLINE
uint32_t BSwap32(uint32_t x
) {
83 #if defined(VPX_USE_MIPS32_R2)
86 "wsbh %[ret], %[x] \n\t"
87 "rotr %[ret], %[ret], 16 \n\t"
92 #elif defined(HAVE_BUILTIN_BSWAP32)
93 return __builtin_bswap32(x
);
94 #elif defined(__i386__) || defined(__x86_64__)
95 uint32_t swapped_bytes
;
96 __asm__
volatile("bswap %0" : "=r"(swapped_bytes
) : "0"(x
));
98 #elif defined(_MSC_VER)
99 return (uint32_t)_byteswap_ulong(x
);
101 return (x
>> 24) | ((x
>> 8) & 0xff00) | ((x
<< 8) & 0xff0000) | (x
<< 24);
102 #endif // HAVE_BUILTIN_BSWAP32
105 static INLINE
uint64_t BSwap64(uint64_t x
) {
106 #if defined(HAVE_BUILTIN_BSWAP64)
107 return __builtin_bswap64(x
);
108 #elif defined(__x86_64__)
109 uint64_t swapped_bytes
;
110 __asm__
volatile("bswapq %0" : "=r"(swapped_bytes
) : "0"(x
));
111 return swapped_bytes
;
112 #elif defined(_MSC_VER)
113 return (uint64_t)_byteswap_uint64(x
);
114 #else // generic code for swapping 64-bit values (suggested by bdb@)
115 x
= ((x
& 0xffffffff00000000ull
) >> 32) | ((x
& 0x00000000ffffffffull
) << 32);
116 x
= ((x
& 0xffff0000ffff0000ull
) >> 16) | ((x
& 0x0000ffff0000ffffull
) << 16);
117 x
= ((x
& 0xff00ff00ff00ff00ull
) >> 8) | ((x
& 0x00ff00ff00ff00ffull
) << 8);
119 #endif // HAVE_BUILTIN_BSWAP64
122 #endif // VPX_UTIL_ENDIAN_INL_H_