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 #pragma warning(push)
175 #pragma warning(suppress : 6102) // Using 'index' from failed function call
176 return static_cast<int>(index ^ 0x1F);
177 #pragma warning(pop)
178 } else {
179 return 32;
180 }
181 }
182 #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
SkCLZ(uint32_t mask)183 static inline int SkCLZ(uint32_t mask) {
184 // __builtin_clz(0) is undefined, so we have to detect that case.
185 return mask ? __builtin_clz(mask) : 32;
186 }
187 #else
SkCLZ(uint32_t mask)188 static inline int SkCLZ(uint32_t mask) {
189 return SkCLZ_portable(mask);
190 }
191 #endif
192
193 //! Returns the number of trailing zero bits (0...32)
194 // From Hacker's Delight 2nd Edition
SkCTZ_portable(uint32_t x)195 constexpr int SkCTZ_portable(uint32_t x) {
196 return 32 - SkCLZ_portable(~x & (x - 1));
197 }
198
199 static_assert(32 == SkCTZ_portable(0));
200 static_assert( 0 == SkCTZ_portable(1));
201 static_assert(30 == SkCTZ_portable(1 << 30));
202 static_assert( 2 == SkCTZ_portable((1 << 30) | (1 << 24) | (1 << 2)));
203 static_assert( 0 == SkCTZ_portable(~0U));
204
205 #if defined(SK_BUILD_FOR_WIN)
206 #include <intrin.h>
207
SkCTZ(uint32_t mask)208 static inline int SkCTZ(uint32_t mask) {
209 if (mask) {
210 unsigned long index = 0;
211 _BitScanForward(&index, mask);
212 // Suppress this bogus /analyze warning. The check for non-zero
213 // guarantees that _BitScanReverse will succeed.
214 #pragma warning(push)
215 #pragma warning(suppress : 6102) // Using 'index' from failed function call
216 return static_cast<int>(index);
217 #pragma warning(pop)
218 } else {
219 return 32;
220 }
221 }
222 #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
SkCTZ(uint32_t mask)223 static inline int SkCTZ(uint32_t mask) {
224 // __builtin_ctz(0) is undefined, so we have to detect that case.
225 return mask ? __builtin_ctz(mask) : 32;
226 }
227 #else
SkCTZ(uint32_t mask)228 static inline int SkCTZ(uint32_t mask) {
229 return SkCTZ_portable(mask);
230 }
231 #endif
232
233 /**
234 * Returns the log2 of the specified value, were that value to be rounded up
235 * to the next power of 2. It is undefined to pass 0. Examples:
236 * SkNextLog2(1) -> 0
237 * SkNextLog2(2) -> 1
238 * SkNextLog2(3) -> 2
239 * SkNextLog2(4) -> 2
240 * SkNextLog2(5) -> 3
241 */
SkNextLog2(uint32_t value)242 static inline int SkNextLog2(uint32_t value) {
243 SkASSERT(value != 0);
244 return 32 - SkCLZ(value - 1);
245 }
246
SkNextLog2_portable(uint32_t value)247 constexpr int SkNextLog2_portable(uint32_t value) {
248 SkASSERT(value != 0);
249 return 32 - SkCLZ_portable(value - 1);
250 }
251
252 /**
253 * Returns the log2 of the specified value, were that value to be rounded down
254 * to the previous power of 2. It is undefined to pass 0. Examples:
255 * SkPrevLog2(1) -> 0
256 * SkPrevLog2(2) -> 1
257 * SkPrevLog2(3) -> 1
258 * SkPrevLog2(4) -> 2
259 * SkPrevLog2(5) -> 2
260 */
SkPrevLog2(uint32_t value)261 static inline int SkPrevLog2(uint32_t value) {
262 SkASSERT(value != 0);
263 return 32 - SkCLZ(value >> 1);
264 }
265
SkPrevLog2_portable(uint32_t value)266 constexpr int SkPrevLog2_portable(uint32_t value) {
267 SkASSERT(value != 0);
268 return 32 - SkCLZ_portable(value >> 1);
269 }
270
271 /**
272 * Returns the smallest power-of-2 that is >= the specified value. If value
273 * is already a power of 2, then it is returned unchanged. It is undefined
274 * if value is <= 0.
275 */
SkNextPow2(int value)276 static inline int SkNextPow2(int value) {
277 SkASSERT(value > 0);
278 return 1 << SkNextLog2(static_cast<uint32_t>(value));
279 }
280
SkNextPow2_portable(int value)281 constexpr int SkNextPow2_portable(int value) {
282 SkASSERT(value > 0);
283 return 1 << SkNextLog2_portable(static_cast<uint32_t>(value));
284 }
285
286 /**
287 * Returns the largest power-of-2 that is <= the specified value. If value
288 * is already a power of 2, then it is returned unchanged. It is undefined
289 * if value is <= 0.
290 */
SkPrevPow2(int value)291 static inline int SkPrevPow2(int value) {
292 SkASSERT(value > 0);
293 return 1 << SkPrevLog2(static_cast<uint32_t>(value));
294 }
295
SkPrevPow2_portable(int value)296 constexpr int SkPrevPow2_portable(int value) {
297 SkASSERT(value > 0);
298 return 1 << SkPrevLog2_portable(static_cast<uint32_t>(value));
299 }
300
301 ///////////////////////////////////////////////////////////////////////////////
302
303 /**
304 * Returns the next power of 2 >= n or n if the next power of 2 can't be represented by size_t.
305 */
SkNextSizePow2(size_t n)306 constexpr size_t SkNextSizePow2(size_t n) {
307 constexpr int kNumSizeTBits = 8 * sizeof(size_t);
308 constexpr size_t kHighBitSet = size_t(1) << (kNumSizeTBits - 1);
309
310 if (!n) {
311 return 1;
312 } else if (n >= kHighBitSet) {
313 return n;
314 }
315
316 n--;
317 uint32_t shift = 1;
318 while (shift < kNumSizeTBits) {
319 n |= n >> shift;
320 shift <<= 1;
321 }
322 return n + 1;
323 }
324
325 // conservative check. will return false for very large values that "could" fit
SkFitsInFixed(T x)326 template <typename T> static inline bool SkFitsInFixed(T x) {
327 return SkTAbs(x) <= 32767.0f;
328 }
329
330 #endif
331