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 static INLINE
uint16_t BSwap16(uint16_t x
) {
67 #if defined(HAVE_BUILTIN_BSWAP16)
68 return __builtin_bswap16(x
);
69 #elif defined(_MSC_VER)
70 return _byteswap_ushort(x
);
72 // gcc will recognize a 'rorw $8, ...' here:
73 return (x
>> 8) | ((x
& 0xff) << 8);
74 #endif // HAVE_BUILTIN_BSWAP16
77 static INLINE
uint32_t BSwap32(uint32_t x
) {
81 "wsbh %[ret], %[x] \n\t"
82 "rotr %[ret], %[ret], 16 \n\t"
87 #elif defined(HAVE_BUILTIN_BSWAP32)
88 return __builtin_bswap32(x
);
89 #elif defined(__i386__) || defined(__x86_64__)
90 uint32_t swapped_bytes
;
91 __asm__
volatile("bswap %0" : "=r"(swapped_bytes
) : "0"(x
));
93 #elif defined(_MSC_VER)
94 return (uint32_t)_byteswap_ulong(x
);
96 return (x
>> 24) | ((x
>> 8) & 0xff00) | ((x
<< 8) & 0xff0000) | (x
<< 24);
97 #endif // HAVE_BUILTIN_BSWAP32
100 static INLINE
uint64_t BSwap64(uint64_t x
) {
101 #if defined(HAVE_BUILTIN_BSWAP64)
102 return __builtin_bswap64(x
);
103 #elif defined(__x86_64__)
104 uint64_t swapped_bytes
;
105 __asm__
volatile("bswapq %0" : "=r"(swapped_bytes
) : "0"(x
));
106 return swapped_bytes
;
107 #elif defined(_MSC_VER)
108 return (uint64_t)_byteswap_uint64(x
);
109 #else // generic code for swapping 64-bit values (suggested by bdb@)
110 x
= ((x
& 0xffffffff00000000ull
) >> 32) | ((x
& 0x00000000ffffffffull
) << 32);
111 x
= ((x
& 0xffff0000ffff0000ull
) >> 16) | ((x
& 0x0000ffff0000ffffull
) << 16);
112 x
= ((x
& 0xff00ff00ff00ff00ull
) >> 8) | ((x
& 0x00ff00ff00ff00ffull
) << 8);
114 #endif // HAVE_BUILTIN_BSWAP64
117 #endif // VPX_UTIL_ENDIAN_INL_H_