• 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/core/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 
124 /**
125  * Swap byte order of a 4-byte value, e.g. 0xaarrggbb -> 0xbbggrraa.
126  */
127 #if defined(_MSC_VER)
128     #include <stdlib.h>
SkBSwap32(uint32_t v)129     static inline uint32_t SkBSwap32(uint32_t v) { return _byteswap_ulong(v); }
130 #else
SkBSwap32(uint32_t v)131     static inline uint32_t SkBSwap32(uint32_t v) { return __builtin_bswap32(v); }
132 #endif
133 
134 //! Returns the number of leading zero bits (0...32)
135 int SkCLZ_portable(uint32_t);
136 
137 #ifndef SkCLZ
138     #if defined(SK_BUILD_FOR_WIN)
139         #include <intrin.h>
140 
SkCLZ(uint32_t mask)141         static inline int SkCLZ(uint32_t mask) {
142             if (mask) {
143                 unsigned long index;
144                 _BitScanReverse(&index, mask);
145                 // Suppress this bogus /analyze warning. The check for non-zero
146                 // guarantees that _BitScanReverse will succeed.
147 #pragma warning(suppress : 6102) // Using 'index' from failed function call
148                 return index ^ 0x1F;
149             } else {
150                 return 32;
151             }
152         }
153     #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
SkCLZ(uint32_t mask)154         static inline int SkCLZ(uint32_t mask) {
155             // __builtin_clz(0) is undefined, so we have to detect that case.
156             return mask ? __builtin_clz(mask) : 32;
157         }
158     #else
159         #define SkCLZ(x)    SkCLZ_portable(x)
160     #endif
161 #endif
162 
163 /**
164  *  Returns the smallest power-of-2 that is >= the specified value. If value
165  *  is already a power of 2, then it is returned unchanged. It is undefined
166  *  if value is <= 0.
167  */
SkNextPow2(int value)168 static inline int SkNextPow2(int value) {
169     SkASSERT(value > 0);
170     return 1 << (32 - SkCLZ(value - 1));
171 }
172 
173 /**
174 *  Returns the largest power-of-2 that is <= the specified value. If value
175 *  is already a power of 2, then it is returned unchanged. It is undefined
176 *  if value is <= 0.
177 */
SkPrevPow2(int value)178 static inline int SkPrevPow2(int value) {
179     SkASSERT(value > 0);
180     return 1 << (32 - SkCLZ(value >> 1));
181 }
182 
183 /**
184  *  Returns the log2 of the specified value, were that value to be rounded up
185  *  to the next power of 2. It is undefined to pass 0. Examples:
186  *  SkNextLog2(1) -> 0
187  *  SkNextLog2(2) -> 1
188  *  SkNextLog2(3) -> 2
189  *  SkNextLog2(4) -> 2
190  *  SkNextLog2(5) -> 3
191  */
SkNextLog2(uint32_t value)192 static inline int SkNextLog2(uint32_t value) {
193     SkASSERT(value != 0);
194     return 32 - SkCLZ(value - 1);
195 }
196 
197 /**
198 *  Returns the log2 of the specified value, were that value to be rounded down
199 *  to the previous power of 2. It is undefined to pass 0. Examples:
200 *  SkPrevLog2(1) -> 0
201 *  SkPrevLog2(2) -> 1
202 *  SkPrevLog2(3) -> 1
203 *  SkPrevLog2(4) -> 2
204 *  SkPrevLog2(5) -> 2
205 */
SkPrevLog2(uint32_t value)206 static inline int SkPrevLog2(uint32_t value) {
207     SkASSERT(value != 0);
208     return 32 - SkCLZ(value >> 1);
209 }
210 
211 ///////////////////////////////////////////////////////////////////////////////
212 
213 /**
214  *  Return the smallest power-of-2 >= n.
215  */
GrNextPow2(uint32_t n)216 static inline uint32_t GrNextPow2(uint32_t n) {
217     return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
218 }
219 
220 /**
221  * Returns the next power of 2 >= n or n if the next power of 2 can't be represented by size_t.
222  */
GrNextSizePow2(size_t n)223 static inline size_t GrNextSizePow2(size_t n) {
224     constexpr int kNumSizeTBits = 8 * sizeof(size_t);
225     constexpr size_t kHighBitSet = size_t(1) << (kNumSizeTBits - 1);
226 
227     if (!n) {
228         return 1;
229     } else if (n >= kHighBitSet) {
230         return n;
231     }
232 
233     n--;
234     uint32_t shift = 1;
235     while (shift < kNumSizeTBits) {
236         n |= n >> shift;
237         shift <<= 1;
238     }
239     return n + 1;
240 }
241 
242 // conservative check. will return false for very large values that "could" fit
SkFitsInFixed(T x)243 template <typename T> static inline bool SkFitsInFixed(T x) {
244     return SkTAbs(x) <= 32767.0f;
245 }
246 
247 #endif
248