Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Functions
FloatingPointTest.cpp File Reference
#include "include/private/base/SkAssert.h"
#include "include/private/base/SkFloatingPoint.h"
#include "src/base/SkUtils.h"
#include "tests/Test.h"
#include <array>
#include <cfloat>
#include <cmath>
#include <cstdint>
#include <cstring>
#include <limits>

Go to the source code of this file.

Functions

 DEF_TEST (DoubleNearlyZero, reporter)
 
 DEF_TEST (DoubleNearlyEqualUlps, reporter)
 
 DEF_TEST (BitCastDoubleRoundTrip, reporter)
 
 DEF_TEST (FMA, reporter)
 
 DEF_TEST (Midpoint, reporter)
 

Function Documentation

◆ DEF_TEST() [1/5]

DEF_TEST ( BitCastDoubleRoundTrip  ,
reporter   
)

Definition at line 111 of file FloatingPointTest.cpp.

111 {
112 std::array<double, 5> testCases = {0.0, 1.0, -13.0, 1.234567890123456, -543210.987654321};
113
114 for (size_t i = 0; i < testCases.size(); i++) {
115 double input = testCases[i];
116 uint64_t bits = sk_bit_cast<uint64_t>(input);
117 double output = sk_bit_cast<double>(bits);
118 REPORTER_ASSERT(reporter, input == output, "%.16f is not exactly %.16f", input, output);
119 }
120
121 {
122 uint64_t bits = sk_bit_cast<uint64_t>((double) NAN);
123 double output = sk_bit_cast<double>(bits);
124 REPORTER_ASSERT(reporter, std::isnan(output), "%.16f is not nan", output);
125 }
126 {
127 uint64_t bits = sk_bit_cast<uint64_t>((double) INFINITY);
128 double output = sk_bit_cast<double>(bits);
129 REPORTER_ASSERT(reporter, !SkIsFinite(output), "%.16f is not infinity", output);
130 }
131}
reporter
static bool SkIsFinite(T x, Pack... values)
#define REPORTER_ASSERT(r, cond,...)
Definition Test.h:286

◆ DEF_TEST() [2/5]

DEF_TEST ( DoubleNearlyEqualUlps  ,
reporter   
)

Definition at line 47 of file FloatingPointTest.cpp.

47 {
48 // Our tolerance is looser than DBL_EPSILON
53 REPORTER_ASSERT(reporter, sk_doubles_nearly_equal_ulps(100.5, 100.5 - DBL_EPSILON));
54 REPORTER_ASSERT(reporter, sk_doubles_nearly_equal_ulps(100.5, 100.5 + DBL_EPSILON));
55
56 // Our tolerance is tighter than FLT_EPSILON
59 REPORTER_ASSERT(reporter, !sk_doubles_nearly_equal_ulps(100.5, 100.5 - FLT_EPSILON));
60 REPORTER_ASSERT(reporter, !sk_doubles_nearly_equal_ulps(100.5, 100.5 + FLT_EPSILON));
63
68
72
73 // Test values upto the edge of infinity.
74 const double biggest = std::numeric_limits<double>::max();
75 auto almostBiggest = [&](int n) {
76 double almostBiggest = biggest;
77 for (int i = 0; i < n; ++i) {
78 almostBiggest = std::nextafter(almostBiggest, -INFINITY);
79 }
80 return almostBiggest;
81 };
82 const double nextBiggest = almostBiggest(1);
84 REPORTER_ASSERT(reporter, sk_doubles_nearly_equal_ulps(biggest, almostBiggest(16)));
85 REPORTER_ASSERT(reporter, !sk_doubles_nearly_equal_ulps(biggest, almostBiggest(17)));
86
87 // One ulp less would be infinity.
88 const uint64_t smallestNANPattern =
89 0b0'11111111111'0000000000000000000000000000000000000000000000000001;
90 double smallestNAN;
91 memcpy(&smallestNAN, &smallestNANPattern, sizeof(double));
92 SkASSERT(std::isnan(smallestNAN));
93 SkASSERT(biggest != nextBiggest);
94
95 // Sanity check.
97
98 // Make sure to return false along the edge of infinity.
101 REPORTER_ASSERT(reporter, !sk_doubles_nearly_equal_ulps(smallestNAN, INFINITY));
102
103 const double smallest = std::numeric_limits<double>::denorm_min();
107 REPORTER_ASSERT(reporter, sk_doubles_nearly_equal_ulps(8*smallest, -8*smallest));
108 REPORTER_ASSERT(reporter, !sk_doubles_nearly_equal_ulps(8*smallest, -9*smallest));
109}
#define SkASSERT(cond)
Definition SkAssert.h:116
bool sk_doubles_nearly_equal_ulps(double a, double b, uint8_t maxUlpsDiff=16)

◆ DEF_TEST() [3/5]

DEF_TEST ( DoubleNearlyZero  ,
reporter   
)

Definition at line 20 of file FloatingPointTest.cpp.

◆ DEF_TEST() [4/5]

DEF_TEST ( FMA  ,
reporter   
)

Definition at line 133 of file FloatingPointTest.cpp.

133 {
134 // 0b0'01111111111'00'0000000000'0000000000'0000000010'0000000000'0000000000
135 double over1 = 1+4.656612873e-10;
136
137 // 0b0'01111111110'11'1111111111'1111111111'1111111100'0000000000'0000000000
138 double under1 = 1-4.656612873e-10;
139
140 // Precision loss
141 // -------------- becomes 1; extra bits are rounded off.
142 double x = std::fma(1, -1, over1 * under1);
143
144 // Precision maintained
145 // ------------- becomes 1 - 2^-62; extra bits are maintained
146 double y = std::fma(over1, under1, -1);
147
149 REPORTER_ASSERT(reporter, y == -exp2(-62));
150}
double y
double x

◆ DEF_TEST() [5/5]

DEF_TEST ( Midpoint  ,
reporter   
)

Definition at line 152 of file FloatingPointTest.cpp.

152 {
153 const float smallest = std::numeric_limits<float>::denorm_min();
154 REPORTER_ASSERT(reporter, sk_float_midpoint(smallest, smallest) == smallest);
155 REPORTER_ASSERT(reporter, sk_float_midpoint(smallest, -smallest) == 0);
156
157 const float biggest = std::numeric_limits<float>::max();
158 REPORTER_ASSERT(reporter, sk_float_midpoint(biggest, biggest) == biggest);
159 REPORTER_ASSERT(reporter, sk_float_midpoint(biggest, -biggest) == 0);
160
161}
static constexpr float sk_float_midpoint(float a, float b)