]> granicus.if.org Git - libvpx/blob - vpx_util/endian_inl.h
Merge "Improve the second-level sub-pixel motion search"
[libvpx] / vpx_util / endian_inl.h
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 #ifdef __clang__
29 # define LOCAL_CLANG_VERSION ((__clang_major__ << 8) | __clang_minor__)
30 # define LOCAL_CLANG_PREREQ(maj, min) \
31     (LOCAL_CLANG_VERSION >= (((maj) << 8) | (min)))
32 #else
33 # define LOCAL_CLANG_VERSION 0
34 # define LOCAL_CLANG_PREREQ(maj, min) 0
35 #endif  // __clang__
36
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
42 #endif
43
44 #if defined(WORDS_BIGENDIAN)
45 #define HToLE32 BSwap32
46 #define HToLE16 BSwap16
47 #define HToBE64(x) (x)
48 #define HToBE32(x) (x)
49 #else
50 #define HToLE32(x) (x)
51 #define HToLE16(x) (x)
52 #define HToBE64(X) BSwap64(X)
53 #define HToBE32(X) BSwap32(X)
54 #endif
55
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
60 #endif
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
64 #endif
65
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
69 #endif
70
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);
76 #else
77   // gcc will recognize a 'rorw $8, ...' here:
78   return (x >> 8) | ((x & 0xff) << 8);
79 #endif  // HAVE_BUILTIN_BSWAP16
80 }
81
82 static INLINE uint32_t BSwap32(uint32_t x) {
83 #if defined(VPX_USE_MIPS32_R2)
84   uint32_t ret;
85   __asm__ volatile (
86     "wsbh   %[ret], %[x]          \n\t"
87     "rotr   %[ret], %[ret],  16   \n\t"
88     : [ret]"=r"(ret)
89     : [x]"r"(x)
90   );
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
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  // VPX_UTIL_ENDIAN_INL_H_