1 /*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkMathPriv_DEFINED
9 #define SkMathPriv_DEFINED
10
11 #include "include/private/base/SkAssert.h"
12 #include "include/private/base/SkCPUTypes.h"
13 #include "include/private/base/SkTemplates.h"
14
15 #include <cstddef>
16 #include <cstdint>
17
18 /**
19 * Return the integer square root of value, with a bias of bitBias
20 */
21 int32_t SkSqrtBits(int32_t value, int bitBias);
22
23 /** Return the integer square root of n, treated as a SkFixed (16.16)
24 */
SkSqrt32(int32_t n)25 static inline int32_t SkSqrt32(int32_t n) { return SkSqrtBits(n, 15); }
26
27 /**
28 * Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches)
29 */
SkClampPos(int value)30 static inline int SkClampPos(int value) {
31 return value & ~(value >> 31);
32 }
33
34 /**
35 * Stores numer/denom and numer%denom into div and mod respectively.
36 */
37 template <typename In, typename Out>
SkTDivMod(In numer,In denom,Out * div,Out * mod)38 inline void SkTDivMod(In numer, In denom, Out* div, Out* mod) {
39 *div = static_cast<Out>(numer/denom);
40 *mod = static_cast<Out>(numer%denom);
41 }
42
43 /** Returns -1 if n < 0, else returns 0
44 */
45 #define SkExtractSign(n) ((int32_t)(n) >> 31)
46
47 /** If sign == -1, returns -n, else sign must be 0, and returns n.
48 Typically used in conjunction with SkExtractSign().
49 */
SkApplySign(int32_t n,int32_t sign)50 static inline int32_t SkApplySign(int32_t n, int32_t sign) {
51 SkASSERT(sign == 0 || sign == -1);
52 return (n ^ sign) - sign;
53 }
54
55 /** Return x with the sign of y */
SkCopySign32(int32_t x,int32_t y)56 static inline int32_t SkCopySign32(int32_t x, int32_t y) {
57 return SkApplySign(x, SkExtractSign(x ^ y));
58 }
59
60 /** Given a positive value and a positive max, return the value
61 pinned against max.
62 Note: only works as long as max - value doesn't wrap around
63 @return max if value >= max, else value
64 */
SkClampUMax(unsigned value,unsigned max)65 static inline unsigned SkClampUMax(unsigned value, unsigned max) {
66 if (value > max) {
67 value = max;
68 }
69 return value;
70 }
71
72 // If a signed int holds min_int (e.g. 0x80000000) it is undefined what happens when
73 // we negate it (even though we *know* we're 2's complement and we'll get the same
74 // value back). So we create this helper function that casts to size_t (unsigned) first,
75 // to avoid the complaint.
sk_negate_to_size_t(int32_t value)76 static inline size_t sk_negate_to_size_t(int32_t value) {
77 #if defined(_MSC_VER)
78 #pragma warning(push)
79 #pragma warning(disable : 4146) // Thanks MSVC, we know what we're negating an unsigned
80 #endif
81 return -static_cast<size_t>(value);
82 #if defined(_MSC_VER)
83 #pragma warning(pop)
84 #endif
85 }
86
87 ///////////////////////////////////////////////////////////////////////////////
88
89 /** Return a*b/255, truncating away any fractional bits. Only valid if both
90 a and b are 0..255
91 */
SkMulDiv255Trunc(U8CPU a,U8CPU b)92 static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
93 SkASSERT((uint8_t)a == a);
94 SkASSERT((uint8_t)b == b);
95 unsigned prod = a*b + 1;
96 return (prod + (prod >> 8)) >> 8;
97 }
98
99 /** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if
100 both a and b are 0..255. The expected result equals (a * b + 254) / 255.
101 */
SkMulDiv255Ceiling(U8CPU a,U8CPU b)102 static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) {
103 SkASSERT((uint8_t)a == a);
104 SkASSERT((uint8_t)b == b);
105 unsigned prod = a*b + 255;
106 return (prod + (prod >> 8)) >> 8;
107 }
108
109 /** Just the rounding step in SkDiv255Round: round(value / 255)
110 */
SkDiv255Round(unsigned prod)111 static inline unsigned SkDiv255Round(unsigned prod) {
112 prod += 128;
113 return (prod + (prod >> 8)) >> 8;
114 }
115
116 /**
117 * Swap byte order of a 4-byte value, e.g. 0xaarrggbb -> 0xbbggrraa.
118 */
119 #if defined(_MSC_VER)
120 #include <stdlib.h>
SkBSwap32(uint32_t v)121 static inline uint32_t SkBSwap32(uint32_t v) { return _byteswap_ulong(v); }
122 #else
SkBSwap32(uint32_t v)123 static inline uint32_t SkBSwap32(uint32_t v) { return __builtin_bswap32(v); }
124 #endif
125
126 /*
127 * Return the number of set bits (i.e., the population count) in the provided uint32_t.
128 */
129 int SkPopCount_portable(uint32_t n);
130
131 #if defined(__GNUC__) || defined(__clang__)
SkPopCount(uint32_t n)132 static inline int SkPopCount(uint32_t n) {
133 return __builtin_popcount(n);
134 }
135 #else
SkPopCount(uint32_t n)136 static inline int SkPopCount(uint32_t n) {
137 return SkPopCount_portable(n);
138 }
139 #endif
140
141 /*
142 * Return the 0-based index of the nth bit set in target
143 * Returns 32 if there is no nth bit set.
144 */
145 int SkNthSet(uint32_t target, int n);
146
147 //! Returns the number of leading zero bits (0...32)
148 // From Hacker's Delight 2nd Edition
SkCLZ_portable(uint32_t x)149 constexpr int SkCLZ_portable(uint32_t x) {
150 int n = 32;
151 uint32_t y = x >> 16; if (y != 0) {n -= 16; x = y;}
152 y = x >> 8; if (y != 0) {n -= 8; x = y;}
153 y = x >> 4; if (y != 0) {n -= 4; x = y;}
154 y = x >> 2; if (y != 0) {n -= 2; x = y;}
155 y = x >> 1; if (y != 0) {return n - 2;}
156 return n - static_cast<int>(x);
157 }
158
159 static_assert(32 == SkCLZ_portable(0));
160 static_assert(31 == SkCLZ_portable(1));
161 static_assert( 1 == SkCLZ_portable(1 << 30));
162 static_assert( 1 == SkCLZ_portable((1 << 30) | (1 << 24) | 1));
163 static_assert( 0 == SkCLZ_portable(~0U));
164
165 #if defined(SK_BUILD_FOR_WIN)
166 #include <intrin.h>
167
SkCLZ(uint32_t mask)168 static inline int SkCLZ(uint32_t mask) {
169 if (mask) {
170 unsigned long index = 0;
171 _BitScanReverse(&index, mask);
172 // Suppress this bogus /analyze warning. The check for non-zero
173 // guarantees that _BitScanReverse will succeed.
174 #if defined(_MSC_VER)
175 #pragma warning(push)
176 #pragma warning(suppress : 6102) // Using 'index' from failed function call
177 #endif
178 return static_cast<int>(index ^ 0x1F);
179 #if defined(_MSC_VER)
180 #pragma warning(pop)
181 #endif
182 } else {
183 return 32;
184 }
185 }
186 #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
SkCLZ(uint32_t mask)187 static inline int SkCLZ(uint32_t mask) {
188 // __builtin_clz(0) is undefined, so we have to detect that case.
189 return mask ? __builtin_clz(mask) : 32;
190 }
191 #else
SkCLZ(uint32_t mask)192 static inline int SkCLZ(uint32_t mask) {
193 return SkCLZ_portable(mask);
194 }
195 #endif
196
197 //! Returns the number of trailing zero bits (0...32)
198 // From Hacker's Delight 2nd Edition
SkCTZ_portable(uint32_t x)199 constexpr int SkCTZ_portable(uint32_t x) {
200 return 32 - SkCLZ_portable(~x & (x - 1));
201 }
202
203 static_assert(32 == SkCTZ_portable(0));
204 static_assert( 0 == SkCTZ_portable(1));
205 static_assert(30 == SkCTZ_portable(1 << 30));
206 static_assert( 2 == SkCTZ_portable((1 << 30) | (1 << 24) | (1 << 2)));
207 static_assert( 0 == SkCTZ_portable(~0U));
208
209 #if defined(SK_BUILD_FOR_WIN)
210 #include <intrin.h>
211
SkCTZ(uint32_t mask)212 static inline int SkCTZ(uint32_t mask) {
213 if (mask) {
214 unsigned long index = 0;
215 _BitScanForward(&index, mask);
216 // Suppress this bogus /analyze warning. The check for non-zero
217 // guarantees that _BitScanReverse will succeed.
218 #if defined(_MSC_VER)
219 #pragma warning(push)
220 #pragma warning(suppress : 6102) // Using 'index' from failed function call
221 #endif
222 return static_cast<int>(index);
223 #if defined(_MSC_VER)
224 #pragma warning(pop)
225 #endif
226 } else {
227 return 32;
228 }
229 }
230 #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
SkCTZ(uint32_t mask)231 static inline int SkCTZ(uint32_t mask) {
232 // __builtin_ctz(0) is undefined, so we have to detect that case.
233 return mask ? __builtin_ctz(mask) : 32;
234 }
235 #else
SkCTZ(uint32_t mask)236 static inline int SkCTZ(uint32_t mask) {
237 return SkCTZ_portable(mask);
238 }
239 #endif
240
241 /**
242 * Returns the log2 of the specified value, were that value to be rounded up
243 * to the next power of 2. It is undefined to pass 0. Examples:
244 * SkNextLog2(1) -> 0
245 * SkNextLog2(2) -> 1
246 * SkNextLog2(3) -> 2
247 * SkNextLog2(4) -> 2
248 * SkNextLog2(5) -> 3
249 */
SkNextLog2(uint32_t value)250 static inline int SkNextLog2(uint32_t value) {
251 SkASSERT(value != 0);
252 return 32 - SkCLZ(value - 1);
253 }
254
SkNextLog2_portable(uint32_t value)255 constexpr int SkNextLog2_portable(uint32_t value) {
256 SkASSERT(value != 0);
257 return 32 - SkCLZ_portable(value - 1);
258 }
259
260 /**
261 * Returns the log2 of the specified value, were that value to be rounded down
262 * to the previous power of 2. It is undefined to pass 0. Examples:
263 * SkPrevLog2(1) -> 0
264 * SkPrevLog2(2) -> 1
265 * SkPrevLog2(3) -> 1
266 * SkPrevLog2(4) -> 2
267 * SkPrevLog2(5) -> 2
268 */
SkPrevLog2(uint32_t value)269 static inline int SkPrevLog2(uint32_t value) {
270 SkASSERT(value != 0);
271 return 32 - SkCLZ(value >> 1);
272 }
273
SkPrevLog2_portable(uint32_t value)274 constexpr int SkPrevLog2_portable(uint32_t value) {
275 SkASSERT(value != 0);
276 return 32 - SkCLZ_portable(value >> 1);
277 }
278
279 /**
280 * Returns the smallest power-of-2 that is >= the specified value. If value
281 * is already a power of 2, then it is returned unchanged. It is undefined
282 * if value is <= 0.
283 */
SkNextPow2(int value)284 static inline int SkNextPow2(int value) {
285 SkASSERT(value > 0);
286 return 1 << SkNextLog2(static_cast<uint32_t>(value));
287 }
288
SkNextPow2_portable(int value)289 constexpr int SkNextPow2_portable(int value) {
290 SkASSERT(value > 0);
291 return 1 << SkNextLog2_portable(static_cast<uint32_t>(value));
292 }
293
294 /**
295 * Returns the largest power-of-2 that is <= the specified value. If value
296 * is already a power of 2, then it is returned unchanged. It is undefined
297 * if value is <= 0.
298 */
SkPrevPow2(int value)299 static inline int SkPrevPow2(int value) {
300 SkASSERT(value > 0);
301 return 1 << SkPrevLog2(static_cast<uint32_t>(value));
302 }
303
SkPrevPow2_portable(int value)304 constexpr int SkPrevPow2_portable(int value) {
305 SkASSERT(value > 0);
306 return 1 << SkPrevLog2_portable(static_cast<uint32_t>(value));
307 }
308
309 ///////////////////////////////////////////////////////////////////////////////
310
311 /**
312 * Returns the next power of 2 >= n or n if the next power of 2 can't be represented by size_t.
313 */
SkNextSizePow2(size_t n)314 constexpr size_t SkNextSizePow2(size_t n) {
315 constexpr int kNumSizeTBits = 8 * sizeof(size_t);
316 constexpr size_t kHighBitSet = size_t(1) << (kNumSizeTBits - 1);
317
318 if (!n) {
319 return 1;
320 } else if (n >= kHighBitSet) {
321 return n;
322 }
323
324 n--;
325 uint32_t shift = 1;
326 while (shift < kNumSizeTBits) {
327 n |= n >> shift;
328 shift <<= 1;
329 }
330 return n + 1;
331 }
332
333 // conservative check. will return false for very large values that "could" fit
SkFitsInFixed(T x)334 template <typename T> static inline bool SkFitsInFixed(T x) {
335 return SkTAbs(x) <= 32767.0f;
336 }
337
338 #endif
339