OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkMathPriv_DEFINED | 8 #ifndef SkMathPriv_DEFINED |
9 #define SkMathPriv_DEFINED | 9 #define SkMathPriv_DEFINED |
10 | 10 |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 /** | 85 /** |
86 * Swap byte order of a 4-byte value, e.g. 0xaarrggbb -> 0xbbggrraa. | 86 * Swap byte order of a 4-byte value, e.g. 0xaarrggbb -> 0xbbggrraa. |
87 */ | 87 */ |
88 #if defined(_MSC_VER) | 88 #if defined(_MSC_VER) |
89 #include <intrin.h> | 89 #include <intrin.h> |
90 static inline uint32_t SkBSwap32(uint32_t v) { return _byteswap_ulong(v); } | 90 static inline uint32_t SkBSwap32(uint32_t v) { return _byteswap_ulong(v); } |
91 #else | 91 #else |
92 static inline uint32_t SkBSwap32(uint32_t v) { return __builtin_bswap32(v);
} | 92 static inline uint32_t SkBSwap32(uint32_t v) { return __builtin_bswap32(v);
} |
93 #endif | 93 #endif |
94 | 94 |
| 95 //! Returns the number of leading zero bits (0...32) |
| 96 int SkCLZ_portable(uint32_t); |
| 97 |
| 98 #ifndef SkCLZ |
| 99 #if defined(SK_BUILD_FOR_WIN32) |
| 100 #include <intrin.h> |
| 101 |
| 102 static inline int SkCLZ(uint32_t mask) { |
| 103 if (mask) { |
| 104 unsigned long index; |
| 105 _BitScanReverse(&index, mask); |
| 106 // Suppress this bogus /analyze warning. The check for non-zero |
| 107 // guarantees that _BitScanReverse will succeed. |
| 108 #pragma warning(suppress : 6102) // Using 'index' from failed function call |
| 109 return index ^ 0x1F; |
| 110 } else { |
| 111 return 32; |
| 112 } |
| 113 } |
| 114 #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__) |
| 115 static inline int SkCLZ(uint32_t mask) { |
| 116 // __builtin_clz(0) is undefined, so we have to detect that case. |
| 117 return mask ? __builtin_clz(mask) : 32; |
| 118 } |
| 119 #else |
| 120 #define SkCLZ(x) SkCLZ_portable(x) |
| 121 #endif |
95 #endif | 122 #endif |
| 123 |
| 124 /** |
| 125 * Returns the smallest power-of-2 that is >= the specified value. If value |
| 126 * is already a power of 2, then it is returned unchanged. It is undefined |
| 127 * if value is <= 0. |
| 128 */ |
| 129 static inline int SkNextPow2(int value) { |
| 130 SkASSERT(value > 0); |
| 131 return 1 << (32 - SkCLZ(value - 1)); |
| 132 } |
| 133 |
| 134 /** |
| 135 * Returns the log2 of the specified value, were that value to be rounded up |
| 136 * to the next power of 2. It is undefined to pass 0. Examples: |
| 137 * SkNextLog2(1) -> 0 |
| 138 * SkNextLog2(2) -> 1 |
| 139 * SkNextLog2(3) -> 2 |
| 140 * SkNextLog2(4) -> 2 |
| 141 * SkNextLog2(5) -> 3 |
| 142 */ |
| 143 static inline int SkNextLog2(uint32_t value) { |
| 144 SkASSERT(value != 0); |
| 145 return 32 - SkCLZ(value - 1); |
| 146 } |
| 147 |
| 148 /////////////////////////////////////////////////////////////////////////////// |
| 149 |
| 150 /** |
| 151 * Return the next power of 2 >= n. |
| 152 */ |
| 153 static inline uint32_t GrNextPow2(uint32_t n) { |
| 154 return n ? (1 << (32 - SkCLZ(n - 1))) : 1; |
| 155 } |
| 156 |
| 157 static inline int GrNextPow2(int n) { |
| 158 SkASSERT(n >= 0); // this impl only works for non-neg. |
| 159 return n ? (1 << (32 - SkCLZ(n - 1))) : 1; |
| 160 } |
| 161 #endif |
OLD | NEW |