port.h (1674B)
1 /* Copyright 2013 Google Inc. All Rights Reserved. 2 3 Distributed under MIT license. 4 See file LICENSE for detail or copy at https://opensource.org/licenses/MIT 5 */ 6 7 /* Helper function for bit twiddling and macros for branch prediction. */ 8 9 #ifndef WOFF2_PORT_H_ 10 #define WOFF2_PORT_H_ 11 12 #include <assert.h> 13 14 namespace woff2 { 15 16 typedef unsigned int uint32; 17 18 inline int Log2Floor(uint32 n) { 19 #if defined(__GNUC__) 20 return n == 0 ? -1 : 31 ^ __builtin_clz(n); 21 #else 22 if (n == 0) 23 return -1; 24 int log = 0; 25 uint32 value = n; 26 for (int i = 4; i >= 0; --i) { 27 int shift = (1 << i); 28 uint32 x = value >> shift; 29 if (x != 0) { 30 value = x; 31 log += shift; 32 } 33 } 34 assert(value == 1); 35 return log; 36 #endif 37 } 38 39 } // namespace woff2 40 41 /* Compatibility with non-clang compilers. */ 42 #ifndef __has_builtin 43 #define __has_builtin(x) 0 44 #endif 45 46 #if (__GNUC__ > 2) || (__GNUC__ == 2 && __GNUC_MINOR__ > 95) || \ 47 (defined(__llvm__) && __has_builtin(__builtin_expect)) 48 #define PREDICT_FALSE(x) (__builtin_expect(x, 0)) 49 #define PREDICT_TRUE(x) (__builtin_expect(!!(x), 1)) 50 #else 51 #define PREDICT_FALSE(x) (x) 52 #define PREDICT_TRUE(x) (x) 53 #endif 54 55 #if (defined(__ARM_ARCH) && (__ARM_ARCH == 7)) || \ 56 (defined(M_ARM) && (M_ARM == 7)) || \ 57 defined(__aarch64__) || defined(__ARM64_ARCH_8__) || defined(__i386) || \ 58 defined(_M_IX86) || defined(__x86_64__) || defined(_M_X64) 59 #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) 60 #define WOFF_LITTLE_ENDIAN 61 #elif defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__) 62 #define WOFF_BIG_ENDIAN 63 #endif /* endianness */ 64 #endif /* CPU whitelist */ 65 66 #endif // WOFF2_PORT_H_