1 /*
2  * Copyright (c) 2016, Alliance for Open Media. All rights reserved
3  *
4  * This source code is subject to the terms of the BSD 2 Clause License and
5  * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6  * was not distributed with this source code in the LICENSE file, you can
7  * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8  * Media Patent License 1.0 was not distributed with this source code in the
9  * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
10  */
11 //
12 // Endian related functions.
13 
14 #ifndef AOM_AOM_UTIL_ENDIAN_INL_H_
15 #define AOM_AOM_UTIL_ENDIAN_INL_H_
16 
17 #include <stdlib.h>
18 
19 #include "config/aom_config.h"
20 
21 #include "aom/aom_integer.h"
22 
23 #if defined(__GNUC__)
24 #define LOCAL_GCC_VERSION ((__GNUC__ << 8) | __GNUC_MINOR__)
25 #define LOCAL_GCC_PREREQ(maj, min) (LOCAL_GCC_VERSION >= (((maj) << 8) | (min)))
26 #else
27 #define LOCAL_GCC_VERSION 0
28 #define LOCAL_GCC_PREREQ(maj, min) 0
29 #endif
30 
31 // handle clang compatibility
32 #ifndef __has_builtin
33 #define __has_builtin(x) 0
34 #endif
35 
36 // some endian fix (e.g.: mips-gcc doesn't define __BIG_ENDIAN__)
37 #if !defined(WORDS_BIGENDIAN) &&                   \
38     (defined(__BIG_ENDIAN__) || defined(_M_PPC) || \
39      (defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)))
40 #define WORDS_BIGENDIAN
41 #endif
42 
43 #if defined(WORDS_BIGENDIAN)
44 #define HToLE32 BSwap32
45 #define HToLE16 BSwap16
46 #define HToBE64(x) (x)
47 #define HToBE32(x) (x)
48 #else
49 #define HToLE32(x) (x)
50 #define HToLE16(x) (x)
51 #define HToBE64(X) BSwap64(X)
52 #define HToBE32(X) BSwap32(X)
53 #endif
54 
55 #if LOCAL_GCC_PREREQ(4, 8) || __has_builtin(__builtin_bswap16)
56 #define HAVE_BUILTIN_BSWAP16
57 #endif
58 
59 #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap32)
60 #define HAVE_BUILTIN_BSWAP32
61 #endif
62 
63 #if LOCAL_GCC_PREREQ(4, 3) || __has_builtin(__builtin_bswap64)
64 #define HAVE_BUILTIN_BSWAP64
65 #endif
66 
67 #if HAVE_MIPS32 && defined(__mips__) && !defined(__mips64) && \
68     defined(__mips_isa_rev) && (__mips_isa_rev >= 2) && (__mips_isa_rev < 6)
69 #define AOM_USE_MIPS32_R2
70 #endif
71 
BSwap16(uint16_t x)72 static INLINE uint16_t BSwap16(uint16_t x) {
73 #if defined(HAVE_BUILTIN_BSWAP16)
74   return __builtin_bswap16(x);
75 #elif defined(_MSC_VER)
76   return _byteswap_ushort(x);
77 #else
78   // gcc will recognize a 'rorw $8, ...' here:
79   return (x >> 8) | ((x & 0xff) << 8);
80 #endif  // HAVE_BUILTIN_BSWAP16
81 }
82 
BSwap32(uint32_t x)83 static INLINE uint32_t BSwap32(uint32_t x) {
84 #if defined(AOM_USE_MIPS32_R2)
85   uint32_t ret;
86   __asm__ volatile(
87       "wsbh   %[ret], %[x]          \n\t"
88       "rotr   %[ret], %[ret],  16   \n\t"
89       : [ret] "=r"(ret)
90       : [x] "r"(x));
91   return ret;
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));
97   return swapped_bytes;
98 #elif defined(_MSC_VER)
99   return (uint32_t)_byteswap_ulong(x);
100 #else
101   return (x >> 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000) | (x << 24);
102 #endif  // HAVE_BUILTIN_BSWAP32
103 }
104 
BSwap64(uint64_t x)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);
118   return x;
119 #endif  // HAVE_BUILTIN_BSWAP64
120 }
121 
122 #endif  // AOM_AOM_UTIL_ENDIAN_INL_H_
123