Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkMathPriv.h
Go to the documentation of this file.
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
14
15#include <cstddef>
16#include <cstdint>
17
18/**
19 * Return the integer square root of value, with a bias of bitBias
20 */
21int32_t SkSqrtBits(int32_t value, int bitBias);
22
23/** Return the integer square root of n, treated as a SkFixed (16.16)
24 */
25static 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 */
30static 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 */
37template <typename In, typename Out>
38inline 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 */
50static 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 */
56static 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 */
65static 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.
76static 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 */
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 */
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 */
111static 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>
121 static inline uint32_t SkBSwap32(uint32_t v) { return _byteswap_ulong(v); }
122#else
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 */
129int SkPopCount_portable(uint32_t n);
130
131#if defined(__GNUC__) || defined(__clang__)
132 static inline int SkPopCount(uint32_t n) {
133 return __builtin_popcount(n);
134 }
135#else
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 */
145int SkNthSet(uint32_t target, int n);
146
147//! Returns the number of leading zero bits (0...32)
148// From Hacker's Delight 2nd Edition
149constexpr 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
159static_assert(32 == SkCLZ_portable(0));
160static_assert(31 == SkCLZ_portable(1));
161static_assert( 1 == SkCLZ_portable(1 << 30));
162static_assert( 1 == SkCLZ_portable((1 << 30) | (1 << 24) | 1));
163static_assert( 0 == SkCLZ_portable(~0U));
164
165#if defined(SK_BUILD_FOR_WIN)
166 #include <intrin.h>
167
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__)
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
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
193constexpr int SkCTZ_portable(uint32_t x) {
194 return 32 - SkCLZ_portable(~x & (x - 1));
195}
196
197static_assert(32 == SkCTZ_portable(0));
198static_assert( 0 == SkCTZ_portable(1));
199static_assert(30 == SkCTZ_portable(1 << 30));
200static_assert( 2 == SkCTZ_portable((1 << 30) | (1 << 24) | (1 << 2)));
201static_assert( 0 == SkCTZ_portable(~0U));
202
203#if defined(SK_BUILD_FOR_WIN)
204 #include <intrin.h>
205
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__)
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
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 */
238static inline int SkNextLog2(uint32_t value) {
239 SkASSERT(value != 0);
240 return 32 - SkCLZ(value - 1);
241}
242
243constexpr 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*/
257static inline int SkPrevLog2(uint32_t value) {
258 SkASSERT(value != 0);
259 return 32 - SkCLZ(value >> 1);
260}
261
262constexpr 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 */
272static inline int SkNextPow2(int value) {
273 SkASSERT(value > 0);
274 return 1 << SkNextLog2(static_cast<uint32_t>(value));
275}
276
277constexpr 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*/
287static inline int SkPrevPow2(int value) {
288 SkASSERT(value > 0);
289 return 1 << SkPrevLog2(static_cast<uint32_t>(value));
290}
291
292constexpr 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 */
302static 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 */
309static 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
329template <typename T> static inline bool SkFitsInFixed(T x) {
330 return SkTAbs(x) <= 32767.0f;
331}
332
333#endif
#define SkASSERT(cond)
Definition SkAssert.h:116
unsigned U8CPU
Definition SkCPUTypes.h:18
static int SkPopCount(uint32_t n)
Definition SkMathPriv.h:136
constexpr int SkPrevPow2_portable(int value)
Definition SkMathPriv.h:292
static bool SkFitsInFixed(T x)
Definition SkMathPriv.h:329
static unsigned SkDiv255Round(unsigned prod)
Definition SkMathPriv.h:111
static int SkPrevLog2(uint32_t value)
Definition SkMathPriv.h:257
constexpr int SkNextPow2_portable(int value)
Definition SkMathPriv.h:277
static uint32_t SkBSwap32(uint32_t v)
Definition SkMathPriv.h:123
constexpr int SkPrevLog2_portable(uint32_t value)
Definition SkMathPriv.h:262
constexpr int SkCTZ_portable(uint32_t x)
Returns the number of trailing zero bits (0...32)
Definition SkMathPriv.h:193
static int SkClampPos(int value)
Definition SkMathPriv.h:30
static int SkPrevPow2(int value)
Definition SkMathPriv.h:287
static int SkCTZ(uint32_t mask)
Definition SkMathPriv.h:224
static int32_t SkApplySign(int32_t n, int32_t sign)
Definition SkMathPriv.h:50
static U8CPU SkMulDiv255Trunc(U8CPU a, U8CPU b)
Definition SkMathPriv.h:92
static unsigned SkClampUMax(unsigned value, unsigned max)
Definition SkMathPriv.h:65
static U8CPU SkMulDiv255Ceiling(U8CPU a, U8CPU b)
Definition SkMathPriv.h:102
void SkTDivMod(In numer, In denom, Out *div, Out *mod)
Definition SkMathPriv.h:38
static int32_t SkCopySign32(int32_t x, int32_t y)
Definition SkMathPriv.h:56
constexpr int SkCLZ_portable(uint32_t x)
Returns the number of leading zero bits (0...32)
Definition SkMathPriv.h:149
static size_t sk_negate_to_size_t(int32_t value)
Definition SkMathPriv.h:76
int32_t SkSqrtBits(int32_t value, int bitBias)
#define SkExtractSign(n)
Definition SkMathPriv.h:45
static uint32_t GrNextPow2(uint32_t n)
Definition SkMathPriv.h:302
int SkPopCount_portable(uint32_t n)
static int32_t SkSqrt32(int32_t n)
Definition SkMathPriv.h:25
constexpr int SkNextLog2_portable(uint32_t value)
Definition SkMathPriv.h:243
static int SkCLZ(uint32_t mask)
Definition SkMathPriv.h:186
int SkNthSet(uint32_t target, int n)
static int SkNextPow2(int value)
Definition SkMathPriv.h:272
static int SkNextLog2(uint32_t value)
Definition SkMathPriv.h:238
static size_t GrNextSizePow2(size_t n)
Definition SkMathPriv.h:309
static int sign(SkScalar x)
Definition SkPath.cpp:2141
static T SkTAbs(T value)
Definition SkTemplates.h:43
static bool b
struct MyStruct a[10]
uint8_t value
uint32_t * target
static float max(float r, float g, float b)
Definition hsl.cpp:49
double y
double x
#define T