Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkMath.h
Go to the documentation of this file.
1/*
2 * Copyright 2006 The Android Open Source Project
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 SkMath_DEFINED
9#define SkMath_DEFINED
10
13
14#include <cstdint>
15#include <climits>
16
17// Max Signed 16 bit value
18static constexpr int16_t SK_MaxS16 = INT16_MAX;
19static constexpr int16_t SK_MinS16 = -SK_MaxS16;
20
21static constexpr int32_t SK_MaxS32 = INT32_MAX;
22static constexpr int32_t SK_MinS32 = -SK_MaxS32;
23static constexpr int32_t SK_NaN32 = INT32_MIN;
24
25static constexpr int64_t SK_MaxS64 = INT64_MAX;
26static constexpr int64_t SK_MinS64 = -SK_MaxS64;
27
28// 64bit -> 32bit utilities
29
30// Handy util that can be passed two ints, and will automatically promote to
31// 64bits before the multiply, so the caller doesn't have to remember to cast
32// e.g. (int64_t)a * b;
33static inline int64_t sk_64_mul(int64_t a, int64_t b) {
34 return a * b;
35}
36
37static inline constexpr int32_t SkLeftShift(int32_t value, int32_t shift) {
38 return (int32_t) ((uint32_t) value << shift);
39}
40
41static inline constexpr int64_t SkLeftShift(int64_t value, int32_t shift) {
42 return (int64_t) ((uint64_t) value << shift);
43}
44
45///////////////////////////////////////////////////////////////////////////////
46
47/**
48 * Returns true if value is a power of 2. Does not explicitly check for
49 * value <= 0.
50 */
51template <typename T> constexpr inline bool SkIsPow2(T value) {
52 return (value & (value - 1)) == 0;
53}
54
55///////////////////////////////////////////////////////////////////////////////
56
57/**
58 * Return a*b/((1 << shift) - 1), rounding any fractional bits.
59 * Only valid if a and b are unsigned and <= 32767 and shift is > 0 and <= 8
60 */
61static inline unsigned SkMul16ShiftRound(U16CPU a, U16CPU b, int shift) {
62 SkASSERT(a <= 32767);
63 SkASSERT(b <= 32767);
64 SkASSERT(shift > 0 && shift <= 8);
65 unsigned prod = a*b + (1 << (shift - 1));
66 return (prod + (prod >> shift)) >> shift;
67}
68
69/**
70 * Return a*b/255, rounding any fractional bits.
71 * Only valid if a and b are unsigned and <= 32767.
72 */
74 return SkMul16ShiftRound(a, b, 8);
75}
76
77#endif
#define SkASSERT(cond)
Definition SkAssert.h:116
unsigned U16CPU
Definition SkCPUTypes.h:23
unsigned U8CPU
Definition SkCPUTypes.h:18
static unsigned SkMul16ShiftRound(U16CPU a, U16CPU b, int shift)
Definition SkMath.h:61
static constexpr int64_t SK_MaxS64
Definition SkMath.h:25
static U8CPU SkMulDiv255Round(U16CPU a, U16CPU b)
Definition SkMath.h:73
static constexpr int32_t SkLeftShift(int32_t value, int32_t shift)
Definition SkMath.h:37
constexpr bool SkIsPow2(T value)
Definition SkMath.h:51
static constexpr int32_t SK_NaN32
Definition SkMath.h:23
static int64_t sk_64_mul(int64_t a, int64_t b)
Definition SkMath.h:33
static constexpr int32_t SK_MinS32
Definition SkMath.h:22
static constexpr int16_t SK_MaxS16
Definition SkMath.h:18
static constexpr int64_t SK_MinS64
Definition SkMath.h:26
static constexpr int32_t SK_MaxS32
Definition SkMath.h:21
static constexpr int16_t SK_MinS16
Definition SkMath.h:19
static bool b
struct MyStruct a[10]
uint8_t value
#define T