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