• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SkFloatingPoint_DEFINED
9 #define SkFloatingPoint_DEFINED
10 
11 #include "include/private/base/SkAttributes.h"
12 #include "include/private/base/SkFloatBits.h"
13 #include "include/private/base/SkMath.h"
14 
15 #include <cfloat>
16 #include <cmath>
17 #include <cstdint>
18 #include <cstring>
19 
20 constexpr float SK_FloatSqrt2 = 1.41421356f;
21 constexpr float SK_FloatPI    = 3.14159265f;
22 constexpr double SK_DoublePI  = 3.14159265358979323846264338327950288;
23 
24 // C++98 cmath std::pow seems to be the earliest portable way to get float pow.
25 // However, on Linux including cmath undefines isfinite.
26 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608
sk_float_pow(float base,float exp)27 static inline float sk_float_pow(float base, float exp) {
28     return powf(base, exp);
29 }
30 
31 #define sk_float_sqrt(x)        sqrtf(x)
32 #define sk_float_sin(x)         sinf(x)
33 #define sk_float_cos(x)         cosf(x)
34 #define sk_float_tan(x)         tanf(x)
35 #define sk_float_floor(x)       floorf(x)
36 #define sk_float_ceil(x)        ceilf(x)
37 #define sk_float_trunc(x)       truncf(x)
38 #ifdef SK_BUILD_FOR_MAC
39 #    define sk_float_acos(x)    static_cast<float>(acos(x))
40 #    define sk_float_asin(x)    static_cast<float>(asin(x))
41 #else
42 #    define sk_float_acos(x)    acosf(x)
43 #    define sk_float_asin(x)    asinf(x)
44 #endif
45 #define sk_float_atan2(y,x)     atan2f(y,x)
46 #define sk_float_abs(x)         fabsf(x)
47 #define sk_float_copysign(x, y) copysignf(x, y)
48 #define sk_float_mod(x,y)       fmodf(x,y)
49 #define sk_float_exp(x)         expf(x)
50 #define sk_float_log(x)         logf(x)
51 
sk_float_degrees_to_radians(float degrees)52 constexpr float sk_float_degrees_to_radians(float degrees) {
53     return degrees * (SK_FloatPI / 180);
54 }
55 
sk_float_radians_to_degrees(float radians)56 constexpr float sk_float_radians_to_degrees(float radians) {
57     return radians * (180 / SK_FloatPI);
58 }
59 
60 // floor(double+0.5) vs. floorf(float+0.5f) give comparable performance, but upcasting to double
61 // means tricky values like 0.49999997 and 2^24 get rounded correctly. If these were rounded
62 // as floatf(x + .5f), they would be 1 higher than expected.
63 #define sk_float_round(x) (float)sk_double_round((double)(x))
64 
65 // can't find log2f on android, but maybe that just a tool bug?
66 #ifdef SK_BUILD_FOR_ANDROID
sk_float_log2(float x)67     static inline float sk_float_log2(float x) {
68         const double inv_ln_2 = 1.44269504088896;
69         return (float)(log(x) * inv_ln_2);
70     }
71 #else
72     #define sk_float_log2(x)        log2f(x)
73 #endif
74 
sk_float_isfinite(float x)75 static inline bool sk_float_isfinite(float x) {
76     return SkFloatBits_IsFinite(SkFloat2Bits(x));
77 }
78 
sk_floats_are_finite(float a,float b)79 static inline bool sk_floats_are_finite(float a, float b) {
80     return sk_float_isfinite(a) && sk_float_isfinite(b);
81 }
82 
sk_floats_are_finite(const float array[],int count)83 static inline bool sk_floats_are_finite(const float array[], int count) {
84     float prod = 0;
85     for (int i = 0; i < count; ++i) {
86         prod *= array[i];
87     }
88     // At this point, prod will either be NaN or 0
89     return prod == 0;   // if prod is NaN, this check will return false
90 }
91 
sk_float_isinf(float x)92 static inline bool sk_float_isinf(float x) {
93     return SkFloatBits_IsInf(SkFloat2Bits(x));
94 }
95 
sk_float_isnan(float x)96 static inline bool sk_float_isnan(float x) {
97     return !(x == x);
98 }
99 
100 #define sk_double_isnan(a)          sk_float_isnan(a)
101 
102 #define SK_MaxS32FitsInFloat    2147483520
103 #define SK_MinS32FitsInFloat    -SK_MaxS32FitsInFloat
104 
105 #define SK_MaxS64FitsInFloat    (SK_MaxS64 >> (63-24) << (63-24))   // 0x7fffff8000000000
106 #define SK_MinS64FitsInFloat    -SK_MaxS64FitsInFloat
107 
108 /**
109  *  Return the closest int for the given float. Returns SK_MaxS32FitsInFloat for NaN.
110  */
sk_float_saturate2int(float x)111 static inline int sk_float_saturate2int(float x) {
112     x = x < SK_MaxS32FitsInFloat ? x : SK_MaxS32FitsInFloat;
113     x = x > SK_MinS32FitsInFloat ? x : SK_MinS32FitsInFloat;
114     return (int)x;
115 }
116 
117 /**
118  *  Return the closest int for the given double. Returns SK_MaxS32 for NaN.
119  */
sk_double_saturate2int(double x)120 static inline int sk_double_saturate2int(double x) {
121     x = x < SK_MaxS32 ? x : SK_MaxS32;
122     x = x > SK_MinS32 ? x : SK_MinS32;
123     return (int)x;
124 }
125 
126 /**
127  *  Return the closest int64_t for the given float. Returns SK_MaxS64FitsInFloat for NaN.
128  */
sk_float_saturate2int64(float x)129 static inline int64_t sk_float_saturate2int64(float x) {
130     x = x < SK_MaxS64FitsInFloat ? x : SK_MaxS64FitsInFloat;
131     x = x > SK_MinS64FitsInFloat ? x : SK_MinS64FitsInFloat;
132     return (int64_t)x;
133 }
134 
135 #define sk_float_floor2int(x)   sk_float_saturate2int(sk_float_floor(x))
136 #define sk_float_round2int(x)   sk_float_saturate2int(sk_float_round(x))
137 #define sk_float_ceil2int(x)    sk_float_saturate2int(sk_float_ceil(x))
138 
139 #define sk_float_floor2int_no_saturate(x)   (int)sk_float_floor(x)
140 #define sk_float_round2int_no_saturate(x)   (int)sk_float_round(x)
141 #define sk_float_ceil2int_no_saturate(x)    (int)sk_float_ceil(x)
142 
143 #define sk_double_floor(x)          floor(x)
144 #define sk_double_round(x)          floor((x) + 0.5)
145 #define sk_double_ceil(x)           ceil(x)
146 #define sk_double_floor2int(x)      (int)sk_double_floor(x)
147 #define sk_double_round2int(x)      (int)sk_double_round(x)
148 #define sk_double_ceil2int(x)       (int)sk_double_ceil(x)
149 
150 // Cast double to float, ignoring any warning about too-large finite values being cast to float.
151 // Clang thinks this is undefined, but it's actually implementation defined to return either
152 // the largest float or infinity (one of the two bracketing representable floats).  Good enough!
153 SK_NO_SANITIZE("float-cast-overflow")
sk_double_to_float(double x)154 static inline float sk_double_to_float(double x) {
155     return static_cast<float>(x);
156 }
157 
158 #define SK_FloatNaN                 std::numeric_limits<float>::quiet_NaN()
159 #define SK_FloatInfinity            (+std::numeric_limits<float>::infinity())
160 #define SK_FloatNegativeInfinity    (-std::numeric_limits<float>::infinity())
161 
162 #define SK_DoubleNaN                std::numeric_limits<double>::quiet_NaN()
163 
164 // Returns false if any of the floats are outside of [0...1]
165 // Returns true if count is 0
166 bool sk_floats_are_unit(const float array[], size_t count);
167 
sk_float_rsqrt_portable(float x)168 static inline float sk_float_rsqrt_portable(float x) { return 1.0f / sk_float_sqrt(x); }
sk_float_rsqrt(float x)169 static inline float sk_float_rsqrt         (float x) { return 1.0f / sk_float_sqrt(x); }
170 
171 // Returns the log2 of the provided value, were that value to be rounded up to the next power of 2.
172 // Returns 0 if value <= 0:
173 // Never returns a negative number, even if value is NaN.
174 //
175 //     sk_float_nextlog2((-inf..1]) -> 0
176 //     sk_float_nextlog2((1..2]) -> 1
177 //     sk_float_nextlog2((2..4]) -> 2
178 //     sk_float_nextlog2((4..8]) -> 3
179 //     ...
sk_float_nextlog2(float x)180 static inline int sk_float_nextlog2(float x) {
181     uint32_t bits = (uint32_t)SkFloat2Bits(x);
182     bits += (1u << 23) - 1u;  // Increment the exponent for non-powers-of-2.
183     int exp = ((int32_t)bits >> 23) - 127;
184     return exp & ~(exp >> 31);  // Return 0 for negative or denormalized floats, and exponents < 0.
185 }
186 
187 // This is the number of significant digits we can print in a string such that when we read that
188 // string back we get the floating point number we expect.  The minimum value C requires is 6, but
189 // most compilers support 9
190 #ifdef FLT_DECIMAL_DIG
191 #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG
192 #else
193 #define SK_FLT_DECIMAL_DIG 9
194 #endif
195 
196 // IEEE defines how float divide behaves for non-finite values and zero-denoms, but C does not
197 // so we have a helper that suppresses the possible undefined-behavior warnings.
198 
199 SK_NO_SANITIZE("float-divide-by-zero")
sk_ieee_float_divide(float numer,float denom)200 static inline float sk_ieee_float_divide(float numer, float denom) {
201     return numer / denom;
202 }
203 
204 SK_NO_SANITIZE("float-divide-by-zero")
sk_ieee_double_divide(double numer,double denom)205 static inline double sk_ieee_double_divide(double numer, double denom) {
206     return numer / denom;
207 }
208 
209 // While we clean up divide by zero, we'll replace places that do divide by zero with this TODO.
sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(float n,float d)210 static inline float sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(float n, float d) {
211     return sk_ieee_float_divide(n,d);
212 }
213 
sk_fmaf(float f,float m,float a)214 static inline float sk_fmaf(float f, float m, float a) {
215 #if defined(FP_FAST_FMA)
216     return std::fmaf(f,m,a);
217 #else
218     return f*m+a;
219 #endif
220 }
221 
222 // Returns true iff the provided number is within a small epsilon of 0.
223 bool sk_double_nearly_zero(double a);
224 
225 // Comparing floating point numbers is complicated. This helper only works if one or none
226 // of the two inputs is not very close to zero. It also does not work if both inputs could be NaN.
227 // The term "ulps" stands for "units of least precision". Read the following for more nuance:
228 //   https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
229 bool sk_doubles_nearly_equal_ulps(double a, double b, uint8_t max_ulps_diff=16);
230 
231 #endif
232