| OLD | NEW |
| 1 | 1 |
| 2 /* | 2 /* |
| 3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
| 4 * | 4 * |
| 5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
| 6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
| 7 */ | 7 */ |
| 8 | 8 |
| 9 | 9 |
| 10 #ifndef SkMath_DEFINED | 10 #ifndef SkMath_DEFINED |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 | 54 |
| 55 /** | 55 /** |
| 56 * Return the integer square root of value, with a bias of bitBias | 56 * Return the integer square root of value, with a bias of bitBias |
| 57 */ | 57 */ |
| 58 int32_t SkSqrtBits(int32_t value, int bitBias); | 58 int32_t SkSqrtBits(int32_t value, int bitBias); |
| 59 | 59 |
| 60 /** Return the integer square root of n, treated as a SkFixed (16.16) | 60 /** Return the integer square root of n, treated as a SkFixed (16.16) |
| 61 */ | 61 */ |
| 62 #define SkSqrt32(n) SkSqrtBits(n, 15) | 62 #define SkSqrt32(n) SkSqrtBits(n, 15) |
| 63 | 63 |
| 64 //! Returns the number of leading zero bits (0...32) | |
| 65 int SkCLZ_portable(uint32_t); | |
| 66 | |
| 67 #ifndef SkCLZ | |
| 68 #if defined(_MSC_VER) | |
| 69 #include <intrin.h> | |
| 70 | |
| 71 static inline int SkCLZ(uint32_t mask) { | |
| 72 if (mask) { | |
| 73 DWORD index; | |
| 74 _BitScanReverse(&index, mask); | |
| 75 // Suppress this bogus /analyze warning. The check for non-zero | |
| 76 // guarantees that _BitScanReverse will succeed. | |
| 77 #pragma warning(suppress : 6102) // Using 'index' from failed function call | |
| 78 return index ^ 0x1F; | |
| 79 } else { | |
| 80 return 32; | |
| 81 } | |
| 82 } | |
| 83 #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__) | |
| 84 static inline int SkCLZ(uint32_t mask) { | |
| 85 // __builtin_clz(0) is undefined, so we have to detect that case. | |
| 86 return mask ? __builtin_clz(mask) : 32; | |
| 87 } | |
| 88 #else | |
| 89 #define SkCLZ(x) SkCLZ_portable(x) | |
| 90 #endif | |
| 91 #endif | |
| 92 | |
| 93 /** | 64 /** |
| 94 * Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches) | 65 * Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches) |
| 95 */ | 66 */ |
| 96 static inline int SkClampPos(int value) { | 67 static inline int SkClampPos(int value) { |
| 97 return value & ~(value >> 31); | 68 return value & ~(value >> 31); |
| 98 } | 69 } |
| 99 | 70 |
| 100 /** Given an integer and a positive (max) integer, return the value | 71 /** Given an integer and a positive (max) integer, return the value |
| 101 * pinned against 0 and max, inclusive. | 72 * pinned against 0 and max, inclusive. |
| 102 * @param value The value we want returned pinned between [0...max] | 73 * @param value The value we want returned pinned between [0...max] |
| 103 * @param max The positive max value | 74 * @param max The positive max value |
| 104 * @return 0 if value < 0, max if value > max, else value | 75 * @return 0 if value < 0, max if value > max, else value |
| 105 */ | 76 */ |
| 106 static inline int SkClampMax(int value, int max) { | 77 static inline int SkClampMax(int value, int max) { |
| 107 // ensure that max is positive | 78 // ensure that max is positive |
| 108 SkASSERT(max >= 0); | 79 SkASSERT(max >= 0); |
| 109 if (value < 0) { | 80 if (value < 0) { |
| 110 value = 0; | 81 value = 0; |
| 111 } | 82 } |
| 112 if (value > max) { | 83 if (value > max) { |
| 113 value = max; | 84 value = max; |
| 114 } | 85 } |
| 115 return value; | 86 return value; |
| 116 } | 87 } |
| 117 | 88 |
| 118 /** | 89 /** |
| 119 * Returns the smallest power-of-2 that is >= the specified value. If value | |
| 120 * is already a power of 2, then it is returned unchanged. It is undefined | |
| 121 * if value is <= 0. | |
| 122 */ | |
| 123 static inline int SkNextPow2(int value) { | |
| 124 SkASSERT(value > 0); | |
| 125 return 1 << (32 - SkCLZ(value - 1)); | |
| 126 } | |
| 127 | |
| 128 /** | |
| 129 * Returns the log2 of the specified value, were that value to be rounded up | |
| 130 * to the next power of 2. It is undefined to pass 0. Examples: | |
| 131 * SkNextLog2(1) -> 0 | |
| 132 * SkNextLog2(2) -> 1 | |
| 133 * SkNextLog2(3) -> 2 | |
| 134 * SkNextLog2(4) -> 2 | |
| 135 * SkNextLog2(5) -> 3 | |
| 136 */ | |
| 137 static inline int SkNextLog2(uint32_t value) { | |
| 138 SkASSERT(value != 0); | |
| 139 return 32 - SkCLZ(value - 1); | |
| 140 } | |
| 141 | |
| 142 /** | |
| 143 * Returns true if value is a power of 2. Does not explicitly check for | 90 * Returns true if value is a power of 2. Does not explicitly check for |
| 144 * value <= 0. | 91 * value <= 0. |
| 145 */ | 92 */ |
| 146 template <typename T> inline bool SkIsPow2(T value) { | 93 template <typename T> inline bool SkIsPow2(T value) { |
| 147 return (value & (value - 1)) == 0; | 94 return (value & (value - 1)) == 0; |
| 148 } | 95 } |
| 149 | 96 |
| 150 /////////////////////////////////////////////////////////////////////////////// | 97 /////////////////////////////////////////////////////////////////////////////// |
| 151 | 98 |
| 152 /** | 99 /** |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 188 *div = static_cast<Out>(d); | 135 *div = static_cast<Out>(d); |
| 189 *mod = static_cast<Out>(numer-d*denom); | 136 *mod = static_cast<Out>(numer-d*denom); |
| 190 #else | 137 #else |
| 191 // On x86 this will just be a single idiv. | 138 // On x86 this will just be a single idiv. |
| 192 *div = static_cast<Out>(numer/denom); | 139 *div = static_cast<Out>(numer/denom); |
| 193 *mod = static_cast<Out>(numer%denom); | 140 *mod = static_cast<Out>(numer%denom); |
| 194 #endif | 141 #endif |
| 195 } | 142 } |
| 196 | 143 |
| 197 #endif | 144 #endif |
| OLD | NEW |