• 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 #if defined(SK_BUILD_FOR_IOS) && (defined(SK_BUILD_FOR_ARM32) || defined(SK_BUILD_FOR_ARM64))
14 // iOS on ARM starts processes with the Flush-To-Zero (FTZ) and
15 // Denormals-Are-Zero (DAZ) bits in the fpscr register set.
16 // Algorithms that rely on denormalized numbers need alternative implementations.
17 // This can also be controlled in SSE with the MXCSR register,
18 // x87 with FSTCW/FLDCW, and mips with FCSR. This should be detected at runtime,
19 // or the library built one way or the other more generally (by the build).
20 #define SK_CPU_FLUSH_TO_ZERO
21 #endif
22 
23 /** Returns -1 if n < 0, else returns 0
24  */
25 #define SkExtractSign(n)    ((int32_t)(n) >> 31)
26 
27 /** If sign == -1, returns -n, else sign must be 0, and returns n.
28  Typically used in conjunction with SkExtractSign().
29  */
SkApplySign(int32_t n,int32_t sign)30 static inline int32_t SkApplySign(int32_t n, int32_t sign) {
31     SkASSERT(sign == 0 || sign == -1);
32     return (n ^ sign) - sign;
33 }
34 
35 /** Return x with the sign of y */
SkCopySign32(int32_t x,int32_t y)36 static inline int32_t SkCopySign32(int32_t x, int32_t y) {
37     return SkApplySign(x, SkExtractSign(x ^ y));
38 }
39 
40 /** Given a positive value and a positive max, return the value
41  pinned against max.
42  Note: only works as long as max - value doesn't wrap around
43  @return max if value >= max, else value
44  */
SkClampUMax(unsigned value,unsigned max)45 static inline unsigned SkClampUMax(unsigned value, unsigned max) {
46     if (value > max) {
47         value = max;
48     }
49     return value;
50 }
51 
52 ///////////////////////////////////////////////////////////////////////////////
53 
54 /** Return a*b/255, truncating away any fractional bits. Only valid if both
55  a and b are 0..255
56  */
SkMulDiv255Trunc(U8CPU a,U8CPU b)57 static inline U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b) {
58     SkASSERT((uint8_t)a == a);
59     SkASSERT((uint8_t)b == b);
60     unsigned prod = a*b + 1;
61     return (prod + (prod >> 8)) >> 8;
62 }
63 
64 /** Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if
65  both a and b are 0..255. The expected result equals (a * b + 254) / 255.
66  */
SkMulDiv255Ceiling(U8CPU a,U8CPU b)67 static inline U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b) {
68     SkASSERT((uint8_t)a == a);
69     SkASSERT((uint8_t)b == b);
70     unsigned prod = a*b + 255;
71     return (prod + (prod >> 8)) >> 8;
72 }
73 
74 /** Just the rounding step in SkDiv255Round: round(value / 255)
75  */
SkDiv255Round(unsigned prod)76 static inline unsigned SkDiv255Round(unsigned prod) {
77     prod += 128;
78     return (prod + (prod >> 8)) >> 8;
79 }
80 
SkPinToUnitFloat(float x)81 static inline float SkPinToUnitFloat(float x) {
82     return SkTMin(SkTMax(x, 0.0f), 1.0f);
83 }
84 
85 /**
86  * Swap byte order of a 4-byte value, e.g. 0xaarrggbb -> 0xbbggrraa.
87  */
88 #if defined(_MSC_VER)
89     #include <intrin.h>
SkBSwap32(uint32_t v)90     static inline uint32_t SkBSwap32(uint32_t v) { return _byteswap_ulong(v); }
91 #else
SkBSwap32(uint32_t v)92     static inline uint32_t SkBSwap32(uint32_t v) { return __builtin_bswap32(v); }
93 #endif
94 
95 //! Returns the number of leading zero bits (0...32)
96 int SkCLZ_portable(uint32_t);
97 
98 #ifndef SkCLZ
99     #if defined(SK_BUILD_FOR_WIN32)
100         #include <intrin.h>
101 
SkCLZ(uint32_t mask)102         static inline int SkCLZ(uint32_t mask) {
103             if (mask) {
104                 unsigned long index;
105                 _BitScanReverse(&index, mask);
106                 // Suppress this bogus /analyze warning. The check for non-zero
107                 // guarantees that _BitScanReverse will succeed.
108 #pragma warning(suppress : 6102) // Using 'index' from failed function call
109                 return index ^ 0x1F;
110             } else {
111                 return 32;
112             }
113         }
114     #elif defined(SK_CPU_ARM32) || defined(__GNUC__) || defined(__clang__)
SkCLZ(uint32_t mask)115         static inline int SkCLZ(uint32_t mask) {
116             // __builtin_clz(0) is undefined, so we have to detect that case.
117             return mask ? __builtin_clz(mask) : 32;
118         }
119     #else
120         #define SkCLZ(x)    SkCLZ_portable(x)
121     #endif
122 #endif
123 
124 /**
125  *  Returns the smallest power-of-2 that is >= the specified value. If value
126  *  is already a power of 2, then it is returned unchanged. It is undefined
127  *  if value is <= 0.
128  */
SkNextPow2(int value)129 static inline int SkNextPow2(int value) {
130     SkASSERT(value > 0);
131     return 1 << (32 - SkCLZ(value - 1));
132 }
133 
134 /**
135 *  Returns the largest power-of-2 that is <= the specified value. If value
136 *  is already a power of 2, then it is returned unchanged. It is undefined
137 *  if value is <= 0.
138 */
SkPrevPow2(int value)139 static inline int SkPrevPow2(int value) {
140     SkASSERT(value > 0);
141     return 1 << (32 - SkCLZ(value >> 1));
142 }
143 
144 /**
145  *  Returns the log2 of the specified value, were that value to be rounded up
146  *  to the next power of 2. It is undefined to pass 0. Examples:
147  *  SkNextLog2(1) -> 0
148  *  SkNextLog2(2) -> 1
149  *  SkNextLog2(3) -> 2
150  *  SkNextLog2(4) -> 2
151  *  SkNextLog2(5) -> 3
152  */
SkNextLog2(uint32_t value)153 static inline int SkNextLog2(uint32_t value) {
154     SkASSERT(value != 0);
155     return 32 - SkCLZ(value - 1);
156 }
157 
158 /**
159 *  Returns the log2 of the specified value, were that value to be rounded down
160 *  to the previous power of 2. It is undefined to pass 0. Examples:
161 *  SkPrevLog2(1) -> 0
162 *  SkPrevLog2(2) -> 1
163 *  SkPrevLog2(3) -> 1
164 *  SkPrevLog2(4) -> 2
165 *  SkPrevLog2(5) -> 2
166 */
SkPrevLog2(uint32_t value)167 static inline int SkPrevLog2(uint32_t value) {
168     SkASSERT(value != 0);
169     return 32 - SkCLZ(value >> 1);
170 }
171 
172 ///////////////////////////////////////////////////////////////////////////////
173 
174 /**
175  *  Return the next power of 2 >= n.
176  */
GrNextPow2(uint32_t n)177 static inline uint32_t GrNextPow2(uint32_t n) {
178     return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
179 }
180 
181 /**
182  * Returns the next power of 2 >= n or n if the next power of 2 can't be represented by size_t.
183  */
GrNextSizePow2(size_t n)184 static inline size_t GrNextSizePow2(size_t n) {
185     constexpr int kNumSizeTBits = 8 * sizeof(size_t);
186     constexpr size_t kHighBitSet = size_t(1) << (kNumSizeTBits - 1);
187 
188     if (!n) {
189         return 1;
190     } else if (n >= kHighBitSet) {
191         return n;
192     }
193 
194     n--;
195     uint32_t shift = 1;
196     while (shift < kNumSizeTBits) {
197         n |= n >> shift;
198         shift <<= 1;
199     }
200     return n + 1;
201 }
202 
203 #endif
204