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