Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
SkFloatingPoint.cpp File Reference
#include "include/private/base/SkFloatingPoint.h"
#include <algorithm>
#include <cmath>
#include <cstring>
#include <limits>

Go to the source code of this file.

Functions

static double magnitude (double a)
 
bool sk_doubles_nearly_equal_ulps (double a, double b, uint8_t maxUlpsDiff)
 
bool sk_double_nearly_zero (double a)
 

Function Documentation

◆ magnitude()

static double magnitude ( double  a)
static

Definition at line 19 of file SkFloatingPoint.cpp.

19 {
20 static constexpr int64_t extractMagnitude =
21 0b0'11111111111'0000000000000000000000000000000000000000000000000000;
22 int64_t bits;
23 memcpy(&bits, &a, sizeof(bits));
24 bits &= extractMagnitude;
25 double out;
26 memcpy(&out, &bits, sizeof(out));
27 return out;
28}
struct MyStruct a[10]

◆ 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}

◆ sk_doubles_nearly_equal_ulps()

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

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