Flutter Engine
The Flutter Engine
Macros | Functions
SkMathPriv.h File Reference
#include "include/private/base/SkAssert.h"
#include "include/private/base/SkCPUTypes.h"
#include "include/private/base/SkTemplates.h"
#include <cstddef>
#include <cstdint>

Go to the source code of this file.

Macros

#define SkExtractSign(n)   ((int32_t)(n) >> 31)
 

Functions

int32_t SkSqrtBits (int32_t value, int bitBias)
 
static int32_t SkSqrt32 (int32_t n)
 
static int SkClampPos (int value)
 
template<typename In , typename Out >
void SkTDivMod (In numer, In denom, Out *div, Out *mod)
 
static int32_t SkApplySign (int32_t n, int32_t sign)
 
static int32_t SkCopySign32 (int32_t x, int32_t y)
 
static unsigned SkClampUMax (unsigned value, unsigned max)
 
static size_t sk_negate_to_size_t (int32_t value)
 
static U8CPU SkMulDiv255Trunc (U8CPU a, U8CPU b)
 
static U8CPU SkMulDiv255Ceiling (U8CPU a, U8CPU b)
 
static unsigned SkDiv255Round (unsigned prod)
 
static uint32_t SkBSwap32 (uint32_t v)
 
int SkPopCount_portable (uint32_t n)
 
static int SkPopCount (uint32_t n)
 
int SkNthSet (uint32_t target, int n)
 
constexpr int SkCLZ_portable (uint32_t x)
 Returns the number of leading zero bits (0...32) More...
 
static int SkCLZ (uint32_t mask)
 
constexpr int SkCTZ_portable (uint32_t x)
 Returns the number of trailing zero bits (0...32) More...
 
static int SkCTZ (uint32_t mask)
 
static int SkNextLog2 (uint32_t value)
 
constexpr int SkNextLog2_portable (uint32_t value)
 
static int SkPrevLog2 (uint32_t value)
 
constexpr int SkPrevLog2_portable (uint32_t value)
 
static int SkNextPow2 (int value)
 
constexpr int SkNextPow2_portable (int value)
 
static int SkPrevPow2 (int value)
 
constexpr int SkPrevPow2_portable (int value)
 
static uint32_t GrNextPow2 (uint32_t n)
 
static size_t GrNextSizePow2 (size_t n)
 
template<typename T >
static bool SkFitsInFixed (T x)
 

Macro Definition Documentation

◆ SkExtractSign

#define SkExtractSign (   n)    ((int32_t)(n) >> 31)

Returns -1 if n < 0, else returns 0

Definition at line 45 of file SkMathPriv.h.

Function Documentation

◆ GrNextPow2()

static uint32_t GrNextPow2 ( uint32_t  n)
inlinestatic

Return the smallest power-of-2 >= n.

Definition at line 302 of file SkMathPriv.h.

302 {
303 return n ? (1 << (32 - SkCLZ(n - 1))) : 1;
304}
static int SkCLZ(uint32_t mask)
Definition: SkMathPriv.h:186

◆ GrNextSizePow2()

static size_t GrNextSizePow2 ( size_t  n)
inlinestatic

Returns the next power of 2 >= n or n if the next power of 2 can't be represented by size_t.

Definition at line 309 of file SkMathPriv.h.

309 {
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}

◆ sk_negate_to_size_t()

static size_t sk_negate_to_size_t ( int32_t  value)
inlinestatic

Definition at line 76 of file SkMathPriv.h.

76 {
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}
uint8_t value

◆ SkApplySign()

static int32_t SkApplySign ( int32_t  n,
int32_t  sign 
)
inlinestatic

If sign == -1, returns -n, else sign must be 0, and returns n. Typically used in conjunction with SkExtractSign().

Definition at line 50 of file SkMathPriv.h.

50 {
51 SkASSERT(sign == 0 || sign == -1);
52 return (n ^ sign) - sign;
53}
#define SkASSERT(cond)
Definition: SkAssert.h:116
static int sign(SkScalar x)
Definition: SkPath.cpp:2205

◆ SkBSwap32()

static uint32_t SkBSwap32 ( uint32_t  v)
inlinestatic

Swap byte order of a 4-byte value, e.g. 0xaarrggbb -> 0xbbggrraa.

Definition at line 123 of file SkMathPriv.h.

123{ return __builtin_bswap32(v); }

◆ SkClampPos()

static int SkClampPos ( int  value)
inlinestatic

Returns (value < 0 ? 0 : value) efficiently (i.e. no compares or branches)

Definition at line 30 of file SkMathPriv.h.

30 {
31 return value & ~(value >> 31);
32}

◆ SkClampUMax()

static unsigned SkClampUMax ( unsigned  value,
unsigned  max 
)
inlinestatic

Given a positive value and a positive max, return the value pinned against max. Note: only works as long as max - value doesn't wrap around

Returns
max if value >= max, else value

Definition at line 65 of file SkMathPriv.h.

65 {
66 if (value > max) {
67 value = max;
68 }
69 return value;
70}
static float max(float r, float g, float b)
Definition: hsl.cpp:49

◆ SkCLZ()

static int SkCLZ ( uint32_t  mask)
inlinestatic

Definition at line 186 of file SkMathPriv.h.

186 {
187 return SkCLZ_portable(mask);
188 }
constexpr int SkCLZ_portable(uint32_t x)
Returns the number of leading zero bits (0...32)
Definition: SkMathPriv.h:149

◆ SkCLZ_portable()

constexpr int SkCLZ_portable ( uint32_t  x)
constexpr

Returns the number of leading zero bits (0...32)

Definition at line 149 of file SkMathPriv.h.

149 {
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}
double y
double x

◆ SkCopySign32()

static int32_t SkCopySign32 ( int32_t  x,
int32_t  y 
)
inlinestatic

Return x with the sign of y

Definition at line 56 of file SkMathPriv.h.

56 {
57 return SkApplySign(x, SkExtractSign(x ^ y));
58}
static int32_t SkApplySign(int32_t n, int32_t sign)
Definition: SkMathPriv.h:50
#define SkExtractSign(n)
Definition: SkMathPriv.h:45

◆ SkCTZ()

static int SkCTZ ( uint32_t  mask)
inlinestatic

Definition at line 224 of file SkMathPriv.h.

224 {
225 return SkCTZ_portable(mask);
226 }
constexpr int SkCTZ_portable(uint32_t x)
Returns the number of trailing zero bits (0...32)
Definition: SkMathPriv.h:193

◆ SkCTZ_portable()

constexpr int SkCTZ_portable ( uint32_t  x)
constexpr

Returns the number of trailing zero bits (0...32)

Definition at line 193 of file SkMathPriv.h.

193 {
194 return 32 - SkCLZ_portable(~x & (x - 1));
195}

◆ SkDiv255Round()

static unsigned SkDiv255Round ( unsigned  prod)
inlinestatic

Just the rounding step in SkDiv255Round: round(value / 255)

Definition at line 111 of file SkMathPriv.h.

111 {
112 prod += 128;
113 return (prod + (prod >> 8)) >> 8;
114}

◆ SkFitsInFixed()

template<typename T >
static bool SkFitsInFixed ( T  x)
inlinestatic

Definition at line 329 of file SkMathPriv.h.

329 {
330 return SkTAbs(x) <= 32767.0f;
331}
static T SkTAbs(T value)
Definition: SkTemplates.h:43

◆ SkMulDiv255Ceiling()

static U8CPU SkMulDiv255Ceiling ( U8CPU  a,
U8CPU  b 
)
inlinestatic

Return (a*b)/255, taking the ceiling of any fractional bits. Only valid if both a and b are 0..255. The expected result equals (a * b + 254) / 255.

Definition at line 102 of file SkMathPriv.h.

102 {
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}
static bool b
struct MyStruct a[10]

◆ SkMulDiv255Trunc()

static U8CPU SkMulDiv255Trunc ( U8CPU  a,
U8CPU  b 
)
inlinestatic

Return a*b/255, truncating away any fractional bits. Only valid if both a and b are 0..255

Definition at line 92 of file SkMathPriv.h.

92 {
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}

◆ SkNextLog2()

static int SkNextLog2 ( uint32_t  value)
inlinestatic

Returns the log2 of the specified value, were that value to be rounded up to the next power of 2. It is undefined to pass 0. Examples: SkNextLog2(1) -> 0 SkNextLog2(2) -> 1 SkNextLog2(3) -> 2 SkNextLog2(4) -> 2 SkNextLog2(5) -> 3

Definition at line 238 of file SkMathPriv.h.

238 {
239 SkASSERT(value != 0);
240 return 32 - SkCLZ(value - 1);
241}

◆ SkNextLog2_portable()

constexpr int SkNextLog2_portable ( uint32_t  value)
constexpr

Definition at line 243 of file SkMathPriv.h.

243 {
244 SkASSERT(value != 0);
245 return 32 - SkCLZ_portable(value - 1);
246}

◆ SkNextPow2()

static int SkNextPow2 ( int  value)
inlinestatic

Returns the smallest power-of-2 that is >= the specified value. If value is already a power of 2, then it is returned unchanged. It is undefined if value is <= 0.

Definition at line 272 of file SkMathPriv.h.

272 {
273 SkASSERT(value > 0);
274 return 1 << SkNextLog2(static_cast<uint32_t>(value));
275}
static int SkNextLog2(uint32_t value)
Definition: SkMathPriv.h:238

◆ SkNextPow2_portable()

constexpr int SkNextPow2_portable ( int  value)
constexpr

Definition at line 277 of file SkMathPriv.h.

277 {
278 SkASSERT(value > 0);
279 return 1 << SkNextLog2_portable(static_cast<uint32_t>(value));
280}
constexpr int SkNextLog2_portable(uint32_t value)
Definition: SkMathPriv.h:243

◆ SkNthSet()

int SkNthSet ( uint32_t  target,
int  n 
)

Definition at line 53 of file SkMathPriv.cpp.

53 {
55
56 for (int i = 0; i < n; ++i) {
57 target &= (target - 1); // Remove the lowest bit in the integer.
58 }
59
60 return SkCTZ(target);
61}
static int SkPopCount(uint32_t n)
Definition: SkMathPriv.h:136
static int SkCTZ(uint32_t mask)
Definition: SkMathPriv.h:224
uint32_t * target

◆ SkPopCount()

static int SkPopCount ( uint32_t  n)
inlinestatic

Definition at line 136 of file SkMathPriv.h.

136 {
137 return SkPopCount_portable(n);
138 }
int SkPopCount_portable(uint32_t n)
Definition: SkMathPriv.cpp:42

◆ SkPopCount_portable()

int SkPopCount_portable ( uint32_t  n)

Definition at line 42 of file SkMathPriv.cpp.

42 {
43 int count = 0;
44
45 while (n) {
46 n &= (n - 1); // Remove the lowest bit in the integer.
47 count++;
48 }
49 return count;
50}
int count
Definition: FontMgrTest.cpp:50

◆ SkPrevLog2()

static int SkPrevLog2 ( uint32_t  value)
inlinestatic

Returns the log2 of the specified value, were that value to be rounded down to the previous power of 2. It is undefined to pass 0. Examples: SkPrevLog2(1) -> 0 SkPrevLog2(2) -> 1 SkPrevLog2(3) -> 1 SkPrevLog2(4) -> 2 SkPrevLog2(5) -> 2

Definition at line 257 of file SkMathPriv.h.

257 {
258 SkASSERT(value != 0);
259 return 32 - SkCLZ(value >> 1);
260}

◆ SkPrevLog2_portable()

constexpr int SkPrevLog2_portable ( uint32_t  value)
constexpr

Definition at line 262 of file SkMathPriv.h.

262 {
263 SkASSERT(value != 0);
264 return 32 - SkCLZ_portable(value >> 1);
265}

◆ SkPrevPow2()

static int SkPrevPow2 ( int  value)
inlinestatic

Returns the largest power-of-2 that is <= the specified value. If value is already a power of 2, then it is returned unchanged. It is undefined if value is <= 0.

Definition at line 287 of file SkMathPriv.h.

287 {
288 SkASSERT(value > 0);
289 return 1 << SkPrevLog2(static_cast<uint32_t>(value));
290}
static int SkPrevLog2(uint32_t value)
Definition: SkMathPriv.h:257

◆ SkPrevPow2_portable()

constexpr int SkPrevPow2_portable ( int  value)
constexpr

Definition at line 292 of file SkMathPriv.h.

292 {
293 SkASSERT(value > 0);
294 return 1 << SkPrevLog2_portable(static_cast<uint32_t>(value));
295}
constexpr int SkPrevLog2_portable(uint32_t value)
Definition: SkMathPriv.h:262

◆ SkSqrt32()

static int32_t SkSqrt32 ( int32_t  n)
inlinestatic

Return the integer square root of n, treated as a SkFixed (16.16)

Definition at line 25 of file SkMathPriv.h.

25{ return SkSqrtBits(n, 15); }
int32_t SkSqrtBits(int32_t value, int bitBias)
Definition: SkMathPriv.cpp:18

◆ SkSqrtBits()

int32_t SkSqrtBits ( int32_t  value,
int  bitBias 
)

Return the integer square root of value, with a bias of bitBias

Definition at line 18 of file SkMathPriv.cpp.

18 {
19 SkASSERT(x >= 0 && count > 0 && (unsigned)count <= 30);
20
21 uint32_t root = 0;
22 uint32_t remHi = 0;
23 uint32_t remLo = x;
24
25 do {
26 root <<= 1;
27
28 remHi = (remHi<<2) | (remLo>>30);
29 remLo <<= 2;
30
31 uint32_t testDiv = (root << 1) + 1;
32 if (remHi >= testDiv) {
33 remHi -= testDiv;
34 root++;
35 }
36 } while (--count >= 0);
37
38 return root;
39}
string root
Definition: scale_cpu.py:20

◆ SkTDivMod()

template<typename In , typename Out >
void SkTDivMod ( In  numer,
In  denom,
Out *  div,
Out *  mod 
)
inline

Stores numer/denom and numerdenom into div and mod respectively.

Definition at line 38 of file SkMathPriv.h.

38 {
39 *div = static_cast<Out>(numer/denom);
40 *mod = static_cast<Out>(numer%denom);
41}