Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Macros | Functions | Variables
SkFloatingPoint.h File Reference
#include "include/private/base/SkAttributes.h"
#include "include/private/base/SkMath.h"
#include <cmath>
#include <cstdint>
#include <limits>
#include <type_traits>

Go to the source code of this file.

Macros

#define sk_float_round(x)   (float)sk_double_round((double)(x))
 
#define sk_float_floor2int(x)   sk_float_saturate2int(std::floor(x))
 
#define sk_float_round2int(x)   sk_float_saturate2int(sk_float_round(x))
 
#define sk_float_ceil2int(x)   sk_float_saturate2int(std::ceil(x))
 
#define sk_float_floor2int_no_saturate(x)   ((int)std::floor(x))
 
#define sk_float_round2int_no_saturate(x)   ((int)sk_float_round(x))
 
#define sk_float_ceil2int_no_saturate(x)   ((int)std::ceil(x))
 
#define sk_double_round(x)   (std::floor((x) + 0.5))
 
#define sk_double_floor2int(x)   ((int)std::floor(x))
 
#define sk_double_round2int(x)   ((int)std::round(x))
 
#define sk_double_ceil2int(x)   ((int)std::ceil(x))
 

Functions

static constexpr int sk_float_sgn (float x)
 
static constexpr float sk_float_degrees_to_radians (float degrees)
 
static constexpr float sk_float_radians_to_degrees (float radians)
 
template<typename T , std::enable_if_t< std::is_floating_point_v< T >, bool > = true>
static bool SkIsNaN (T x)
 
template<typename T , typename... Pack, std::enable_if_t< std::is_floating_point_v< T >, bool > = true>
static bool SkIsFinite (T x, Pack... values)
 
template<typename T , std::enable_if_t< std::is_floating_point_v< T >, bool > = true>
static bool SkIsFinite (const T array[], int count)
 
static constexpr int sk_float_saturate2int (float x)
 
static constexpr int sk_double_saturate2int (double x)
 
static constexpr int64_t sk_float_saturate2int64 (float x)
 
static constexpr float sk_double_to_float (double x)
 
static constexpr float sk_float_midpoint (float a, float b)
 
static float sk_float_rsqrt_portable (float x)
 
static float sk_float_rsqrt (float x)
 
static constexpr float sk_ieee_float_divide (float numer, float denom)
 
static constexpr double sk_ieee_double_divide (double numer, double denom)
 
bool sk_double_nearly_zero (double a)
 
bool sk_doubles_nearly_equal_ulps (double a, double b, uint8_t maxUlpsDiff=16)
 

Variables

constexpr float SK_FloatSqrt2 = 1.41421356f
 
constexpr float SK_FloatPI = 3.14159265f
 
constexpr double SK_DoublePI = 3.14159265358979323846264338327950288
 
constexpr int SK_MaxS32FitsInFloat = 2147483520
 
constexpr int SK_MinS32FitsInFloat = -SK_MaxS32FitsInFloat
 
constexpr int64_t SK_MaxS64FitsInFloat = SK_MaxS64 >> (63-24) << (63-24)
 
constexpr int64_t SK_MinS64FitsInFloat = -SK_MaxS64FitsInFloat
 
constexpr float SK_FloatNaN = std::numeric_limits<float>::quiet_NaN()
 
constexpr float SK_FloatInfinity = std::numeric_limits<float>::infinity()
 
constexpr float SK_FloatNegativeInfinity = -SK_FloatInfinity
 
constexpr double SK_DoubleNaN = std::numeric_limits<double>::quiet_NaN()
 

Macro Definition Documentation

◆ sk_double_ceil2int

#define sk_double_ceil2int (   x)    ((int)std::ceil(x))

Definition at line 113 of file SkFloatingPoint.h.

◆ sk_double_floor2int

#define sk_double_floor2int (   x)    ((int)std::floor(x))

Definition at line 111 of file SkFloatingPoint.h.

◆ sk_double_round

#define sk_double_round (   x)    (std::floor((x) + 0.5))

Definition at line 110 of file SkFloatingPoint.h.

◆ sk_double_round2int

#define sk_double_round2int (   x)    ((int)std::round(x))

Definition at line 112 of file SkFloatingPoint.h.

◆ sk_float_ceil2int

#define sk_float_ceil2int (   x)    sk_float_saturate2int(std::ceil(x))

Definition at line 104 of file SkFloatingPoint.h.

◆ sk_float_ceil2int_no_saturate

#define sk_float_ceil2int_no_saturate (   x)    ((int)std::ceil(x))

Definition at line 108 of file SkFloatingPoint.h.

◆ sk_float_floor2int

#define sk_float_floor2int (   x)    sk_float_saturate2int(std::floor(x))

Definition at line 102 of file SkFloatingPoint.h.

◆ sk_float_floor2int_no_saturate

#define sk_float_floor2int_no_saturate (   x)    ((int)std::floor(x))

Definition at line 106 of file SkFloatingPoint.h.

◆ sk_float_round

#define sk_float_round (   x)    (float)sk_double_round((double)(x))

Definition at line 38 of file SkFloatingPoint.h.

◆ sk_float_round2int

#define sk_float_round2int (   x)    sk_float_saturate2int(sk_float_round(x))

Definition at line 103 of file SkFloatingPoint.h.

◆ sk_float_round2int_no_saturate

#define sk_float_round2int_no_saturate (   x)    ((int)sk_float_round(x))

Definition at line 107 of file SkFloatingPoint.h.

Function Documentation

◆ sk_double_nearly_zero()

bool sk_double_nearly_zero ( double  a)

Definition at line 54 of file SkFloatingPoint.cpp.

54 {
55 return a == 0 || fabs(a) < std::numeric_limits<float>::epsilon();
56}
struct MyStruct a[10]

◆ sk_double_saturate2int()

static constexpr int sk_double_saturate2int ( double  x)
staticconstexpr

Return the closest int for the given double. Returns SK_MaxS32 for NaN.

Definition at line 87 of file SkFloatingPoint.h.

87 {
88 x = x < SK_MaxS32 ? x : SK_MaxS32;
89 x = x > SK_MinS32 ? x : SK_MinS32;
90 return (int)x;
91}
static constexpr int32_t SK_MinS32
Definition SkMath.h:22
static constexpr int32_t SK_MaxS32
Definition SkMath.h:21
double x

◆ sk_double_to_float()

static constexpr float sk_double_to_float ( double  x)
staticconstexpr

Definition at line 119 of file SkFloatingPoint.h.

119 {
120 return static_cast<float>(x);
121}

◆ sk_doubles_nearly_equal_ulps()

bool sk_doubles_nearly_equal_ulps ( double  a,
double  b,
uint8_t  maxUlpsDiff = 16 
)

Definition at line 30 of file SkFloatingPoint.cpp.

30 {
31
32 // The maximum magnitude to construct the ulp tolerance. The proper magnitude for
33 // subnormal numbers is minMagnitude, which is 2^-1021, so if a and b are subnormal (having a
34 // magnitude of 0) use minMagnitude. If a or b are infinity or nan, then maxMagnitude will be
35 // +infinity. This means the tolerance will also be infinity, but the expression b - a below
36 // will either be NaN or infinity, so a tolerance of infinity doesn't matter.
37 static constexpr double minMagnitude = std::numeric_limits<double>::min();
38 const double maxMagnitude = std::max(std::max(magnitude(a), minMagnitude), magnitude(b));
39
40 // Given a magnitude, this is the factor that generates the ulp for that magnitude.
41 // In numbers, 2 ^ (-precision + 1) = 2 ^ -52.
42 static constexpr double ulpFactor = std::numeric_limits<double>::epsilon();
43
44 // The tolerance in ULPs given the maxMagnitude. Because the return statement must use <
45 // for comparison instead of <= to correctly handle infinities, bump maxUlpsDiff up to get
46 // the full maxUlpsDiff range.
47 const double tolerance = maxMagnitude * (ulpFactor * (maxUlpsDiff + 1));
48
49 // The expression a == b is mainly for handling infinities, but it also catches the exact
50 // equals.
51 return a == b || std::abs(b - a) < tolerance;
52}
static double magnitude(double a)
static bool b

◆ sk_float_degrees_to_radians()

static constexpr float sk_float_degrees_to_radians ( float  degrees)
staticconstexpr

Definition at line 27 of file SkFloatingPoint.h.

27 {
28 return degrees * (SK_FloatPI / 180);
29}
constexpr float SK_FloatPI

◆ sk_float_midpoint()

static constexpr float sk_float_midpoint ( float  a,
float  b 
)
staticconstexpr

Definition at line 130 of file SkFloatingPoint.h.

130 {
131 // Use double math to avoid underflow and overflow.
132 return static_cast<float>(0.5 * (static_cast<double>(a) + b));
133}

◆ sk_float_radians_to_degrees()

static constexpr float sk_float_radians_to_degrees ( float  radians)
staticconstexpr

Definition at line 31 of file SkFloatingPoint.h.

31 {
32 return radians * (180 / SK_FloatPI);
33}

◆ sk_float_rsqrt()

static float sk_float_rsqrt ( float  x)
inlinestatic

Definition at line 136 of file SkFloatingPoint.h.

136{ return 1.0f / std::sqrt(x); }

◆ sk_float_rsqrt_portable()

static float sk_float_rsqrt_portable ( float  x)
inlinestatic

Definition at line 135 of file SkFloatingPoint.h.

135{ return 1.0f / std::sqrt(x); }

◆ sk_float_saturate2int()

static constexpr int sk_float_saturate2int ( float  x)
staticconstexpr

Return the closest int for the given float. Returns SK_MaxS32FitsInFloat for NaN.

Definition at line 78 of file SkFloatingPoint.h.

78 {
81 return (int)x;
82}
constexpr int SK_MinS32FitsInFloat
constexpr int SK_MaxS32FitsInFloat

◆ sk_float_saturate2int64()

static constexpr int64_t sk_float_saturate2int64 ( float  x)
staticconstexpr

Return the closest int64_t for the given float. Returns SK_MaxS64FitsInFloat for NaN.

Definition at line 96 of file SkFloatingPoint.h.

96 {
99 return (int64_t)x;
100}
constexpr int64_t SK_MinS64FitsInFloat
constexpr int64_t SK_MaxS64FitsInFloat

◆ sk_float_sgn()

static constexpr int sk_float_sgn ( float  x)
staticconstexpr

Definition at line 23 of file SkFloatingPoint.h.

23 {
24 return (0.0f < x) - (x < 0.0f);
25}

◆ sk_ieee_double_divide()

static constexpr double sk_ieee_double_divide ( double  numer,
double  denom 
)
staticconstexpr

Definition at line 150 of file SkFloatingPoint.h.

150 {
151 return numer / denom;
152}

◆ sk_ieee_float_divide()

static constexpr float sk_ieee_float_divide ( float  numer,
float  denom 
)
staticconstexpr

Definition at line 145 of file SkFloatingPoint.h.

145 {
146 return numer / denom;
147}

◆ SkIsFinite() [1/2]

template<typename T , std::enable_if_t< std::is_floating_point_v< T >, bool > = true>
static bool SkIsFinite ( const T  array[],
int  count 
)
inlinestatic

Definition at line 58 of file SkFloatingPoint.h.

58 {
59 T x = array[0];
60 T prod = x - x;
61 for (int i = 1; i < count; ++i) {
62 prod *= array[i];
63 }
64 // At this point, `prod` will either be NaN or 0.
65 return prod == prod;
66}
int count
#define T

◆ SkIsFinite() [2/2]

template<typename T , typename... Pack, std::enable_if_t< std::is_floating_point_v< T >, bool > = true>
static bool SkIsFinite ( T  x,
Pack...  values 
)
inlinestatic

Definition at line 50 of file SkFloatingPoint.h.

50 {
51 T prod = x - x;
52 prod = (prod * ... * values);
53 // At this point, `prod` will either be NaN or 0.
54 return prod == prod;
55}

◆ SkIsNaN()

template<typename T , std::enable_if_t< std::is_floating_point_v< T >, bool > = true>
static bool SkIsNaN ( T  x)
inlinestatic

Definition at line 41 of file SkFloatingPoint.h.

41 {
42 return x != x;
43}

Variable Documentation

◆ SK_DoubleNaN

constexpr double SK_DoubleNaN = std::numeric_limits<double>::quiet_NaN()
inlineconstexpr

Definition at line 127 of file SkFloatingPoint.h.

◆ SK_DoublePI

constexpr double SK_DoublePI = 3.14159265358979323846264338327950288
inlineconstexpr

Definition at line 21 of file SkFloatingPoint.h.

◆ SK_FloatInfinity

constexpr float SK_FloatInfinity = std::numeric_limits<float>::infinity()
inlineconstexpr

Definition at line 124 of file SkFloatingPoint.h.

◆ SK_FloatNaN

constexpr float SK_FloatNaN = std::numeric_limits<float>::quiet_NaN()
inlineconstexpr

Definition at line 123 of file SkFloatingPoint.h.

◆ SK_FloatNegativeInfinity

constexpr float SK_FloatNegativeInfinity = -SK_FloatInfinity
inlineconstexpr

Definition at line 125 of file SkFloatingPoint.h.

◆ SK_FloatPI

constexpr float SK_FloatPI = 3.14159265f
inlineconstexpr

Definition at line 20 of file SkFloatingPoint.h.

◆ SK_FloatSqrt2

constexpr float SK_FloatSqrt2 = 1.41421356f
inlineconstexpr

Definition at line 19 of file SkFloatingPoint.h.

◆ SK_MaxS32FitsInFloat

constexpr int SK_MaxS32FitsInFloat = 2147483520
inlineconstexpr

Definition at line 68 of file SkFloatingPoint.h.

◆ SK_MaxS64FitsInFloat

constexpr int64_t SK_MaxS64FitsInFloat = SK_MaxS64 >> (63-24) << (63-24)
inlineconstexpr

Definition at line 72 of file SkFloatingPoint.h.

◆ SK_MinS32FitsInFloat

constexpr int SK_MinS32FitsInFloat = -SK_MaxS32FitsInFloat
inlineconstexpr

Definition at line 69 of file SkFloatingPoint.h.

◆ SK_MinS64FitsInFloat

constexpr int64_t SK_MinS64FitsInFloat = -SK_MaxS64FitsInFloat
inlineconstexpr

Definition at line 73 of file SkFloatingPoint.h.