• 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 "SkMath.h"
12 
13 /**
14  *  Return the integer square root of value, with a bias of bitBias
15  */
16 int32_t SkSqrtBits(int32_t value, int bitBias);
17 
18 /** Return the integer square root of n, treated as a SkFixed (16.16)
19  */
SkSqrt32(int32_t n)20 static inline int32_t SkSqrt32(int32_t n) { return SkSqrtBits(n, 15); }
21 
22 /**
23  *  Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches)
24  */
SkClampPos(int value)25 static inline int SkClampPos(int value) {
26     return value & ~(value >> 31);
27 }
28 
29 /**
30  * Stores numer/denom and numer%denom into div and mod respectively.
31  */
32 template <typename In, typename Out>
SkTDivMod(In numer,In denom,Out * div,Out * mod)33 inline void SkTDivMod(In numer, In denom, Out* div, Out* mod) {
34 #ifdef SK_CPU_ARM32
35     // If we wrote this as in the else branch, GCC won't fuse the two into one
36     // divmod call, but rather a div call followed by a divmod.  Silly!  This
37     // version is just as fast as calling __aeabi_[u]idivmod manually, but with
38     // prettier code.
39     //
40     // This benches as around 2x faster than the code in the else branch.
41     const In d = numer/denom;
42     *div = static_cast<Out>(d);
43     *mod = static_cast<Out>(numer-d*denom);
44 #else
45     // On x86 this will just be a single idiv.
46     *div = static_cast<Out>(numer/denom);
47     *mod = static_cast<Out>(numer%denom);
48 #endif
49 }
50 
51 /** Returns -1 if n < 0, else returns 0
52  */
53 #define SkExtractSign(n)    ((int32_t)(n) >> 31)
54 
55 /** If sign == -1, returns -n, else sign must be 0, and returns n.
56  Typically used in conjunction with SkExtractSign().
57  */
SkApplySign(int32_t n,int32_t sign)58 static inline int32_t SkApplySign(int32_t n, int32_t sign) {
59     SkASSERT(sign == 0 || sign == -1);
60     return (n ^ sign) - sign;
61 }
62 
63 /** Return x with the sign of y */
SkCopySign32(int32_t x,int32_t y)64 static inline int32_t SkCopySign32(int32_t x, int32_t y) {
65     return SkApplySign(x, SkExtractSign(x ^ y));
66 }
67 
68 /** Given a positive value and a positive max, return the value
69  pinned against max.
70  Note: only works as long as max - value doesn't wrap around
71  @return max if value >= max, else value
72  */
SkClampUMax(unsigned value,unsigned max)73 static inline unsigned SkClampUMax(unsigned value, unsigned max) {
74     if (value > max) {
75         value = max;
76     }
77     return value;
78 }
79 
80 // If a signed int holds min_int (e.g. 0x80000000) it is undefined what happens when
81 // we negate it (even though we *know* we're 2's complement and we'll get the same
82 // value back). So we create this helper function that casts to size_t (unsigned) first,
83 // to avoid the complaint.
sk_negate_to_size_t(int32_t value)84 static inline size_t sk_negate_to_size_t(int32_t value) {
85 #if defined(_MSC_VER)
86 #pragma warning(push)
87 #pragma warning(disable : 4146)  // Thanks MSVC, we know what we're negating an unsigned
88 #endif
89     return -static_cast<size_t>(value);
90 #if defined(_MSC_VER)
91 #pragma warning(pop)
92 #endif
93 }
94 
95 ///////////////////////////////////////////////////////////////////////////////
96 
97 /** Return a*b/255, truncating away any fractional bits. Only valid if both
98  a and b are 0..255
99  */
SkMulDiv255Trunc(U8CPU a,U8CPU b)100 static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
101     SkASSERT((uint8_t)a == a);
102     SkASSERT((uint8_t)b == b);
103     unsigned prod = a*b + 1;
104     return (prod + (prod >> 8)) >> 8;
105 }
106 
107 /** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if
108  both a and b are 0..255. The expected result equals (a * b + 254) / 255.
109  */
SkMulDiv255Ceiling(U8CPU a,U8CPU b)110 static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) {
111     SkASSERT((uint8_t)a == a);
112     SkASSERT((uint8_t)b == b);
113     unsigned prod = a*b + 255;
114     return (prod + (prod >> 8)) >> 8;
115 }
116 
117 /** Just the rounding step in SkDiv255Round: round(value / 255)
118  */
SkDiv255Round(unsigned prod)119 static inline unsigned SkDiv255Round(unsigned prod) {
120     prod += 128;
121     return (prod + (prod >> 8)) >> 8;
122 }
123 
SkPinToUnitFloat(float x)124 static inline float SkPinToUnitFloat(float x) {
125     return SkTMin(SkTMax(x, 0.0f), 1.0f);
126 }
127 
128 /**
129  * Swap byte order of a 4-byte value, e.g. 0xaarrggbb -> 0xbbggrraa.
130  */
131 #if defined(_MSC_VER)
132     #include <stdlib.h>
SkBSwap32(uint32_t v)133     static inline uint32_t SkBSwap32(uint32_t v) { return _byteswap_ulong(v); }
134 #else
SkBSwap32(uint32_t v)135     static inline uint32_t SkBSwap32(uint32_t v) { return __builtin_bswap32(v); }
136 #endif
137 
138 //! Returns the number of leading zero bits (0...32)
139 int SkCLZ_portable(uint32_t);
140 
141 #ifndef SkCLZ
142     #if defined(SK_BUILD_FOR_WIN)
143         #include <intrin.h>
144 
SkCLZ(uint32_t mask)145         static inline int SkCLZ(uint32_t mask) {
146             if (mask) {
147                 unsigned long index;
148                 _BitScanReverse(&index, mask);
149                 // Suppress this bogus /analyze warning. The check for non-zero
150                 // guarantees that _BitScanReverse will succeed.
151 #pragma warning(suppress : 6102) // Using 'index' from failed function call
152                 return index ^ 0x1F;
153             } else {
154                 return 32;
155             }
156         }
157     #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
SkCLZ(uint32_t mask)158         static inline int SkCLZ(uint32_t mask) {
159             // __builtin_clz(0) is undefined, so we have to detect that case.
160             return mask ? __builtin_clz(mask) : 32;
161         }
162     #else
163         #define SkCLZ(x)    SkCLZ_portable(x)
164     #endif
165 #endif
166 
167 /**
168  *  Returns the smallest power-of-2 that is >= the specified value. If value
169  *  is already a power of 2, then it is returned unchanged. It is undefined
170  *  if value is <= 0.
171  */
SkNextPow2(int value)172 static inline int SkNextPow2(int value) {
173     SkASSERT(value > 0);
174     return 1 << (32 - SkCLZ(value - 1));
175 }
176 
177 /**
178 *  Returns the largest power-of-2 that is <= the specified value. If value
179 *  is already a power of 2, then it is returned unchanged. It is undefined
180 *  if value is <= 0.
181 */
SkPrevPow2(int value)182 static inline int SkPrevPow2(int value) {
183     SkASSERT(value > 0);
184     return 1 << (32 - SkCLZ(value >> 1));
185 }
186 
187 /**
188  *  Returns the log2 of the specified value, were that value to be rounded up
189  *  to the next power of 2. It is undefined to pass 0. Examples:
190  *  SkNextLog2(1) -> 0
191  *  SkNextLog2(2) -> 1
192  *  SkNextLog2(3) -> 2
193  *  SkNextLog2(4) -> 2
194  *  SkNextLog2(5) -> 3
195  */
SkNextLog2(uint32_t value)196 static inline int SkNextLog2(uint32_t value) {
197     SkASSERT(value != 0);
198     return 32 - SkCLZ(value - 1);
199 }
200 
201 /**
202 *  Returns the log2 of the specified value, were that value to be rounded down
203 *  to the previous power of 2. It is undefined to pass 0. Examples:
204 *  SkPrevLog2(1) -> 0
205 *  SkPrevLog2(2) -> 1
206 *  SkPrevLog2(3) -> 1
207 *  SkPrevLog2(4) -> 2
208 *  SkPrevLog2(5) -> 2
209 */
SkPrevLog2(uint32_t value)210 static inline int SkPrevLog2(uint32_t value) {
211     SkASSERT(value != 0);
212     return 32 - SkCLZ(value >> 1);
213 }
214 
215 ///////////////////////////////////////////////////////////////////////////////
216 
217 /**
218  *  Return the next power of 2 >= n.
219  */
GrNextPow2(uint32_t n)220 static inline uint32_t GrNextPow2(uint32_t n) {
221     return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
222 }
223 
224 /**
225  * Returns the next power of 2 >= n or n if the next power of 2 can't be represented by size_t.
226  */
GrNextSizePow2(size_t n)227 static inline size_t GrNextSizePow2(size_t n) {
228     constexpr int kNumSizeTBits = 8 * sizeof(size_t);
229     constexpr size_t kHighBitSet = size_t(1) << (kNumSizeTBits - 1);
230 
231     if (!n) {
232         return 1;
233     } else if (n >= kHighBitSet) {
234         return n;
235     }
236 
237     n--;
238     uint32_t shift = 1;
239     while (shift < kNumSizeTBits) {
240         n |= n >> shift;
241         shift <<= 1;
242     }
243     return n + 1;
244 }
245 
246 // conservative check. will return false for very large values that "could" fit
SkFitsInFixed(T x)247 template <typename T> static inline bool SkFitsInFixed(T x) {
248     return SkTAbs(x) <= 32767.0f;
249 }
250 
251 #endif
252