blob: 984a4363197dc0ae24780bceac333afcd1640f23 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkFloatingPoint_DEFINED
9#define SkFloatingPoint_DEFINED
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "include/core/SkTypes.h"
12#include "include/private/SkFloatBits.h"
13#include "include/private/SkSafe_math.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include <float.h>
Mike Reed9236b022018-05-14 13:37:16 -040015#include <math.h>
Ben Wagner29f2eaf2018-06-15 13:58:41 -040016#include <cstring>
Brian Osman06768fc2018-10-31 13:30:52 -040017#include <limits>
Ben Wagner29f2eaf2018-06-15 13:58:41 -040018
bungeman@google.comd3fbd342014-04-15 15:52:07 +000019
mtkleine18fa442016-06-09 13:40:56 -070020#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
21 #include <xmmintrin.h>
22#elif defined(SK_ARM_HAS_NEON)
23 #include <arm_neon.h>
24#endif
25
bungeman@google.comd3fbd342014-04-15 15:52:07 +000026// For _POSIX_VERSION
27#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
28#include <unistd.h>
29#endif
30
Mike Reedf07b61f2019-04-23 16:31:13 -040031constexpr float SK_FloatSqrt2 = 1.41421356f;
32constexpr float SK_FloatPI = 3.14159265f;
33
bungeman@google.com0567f222012-07-25 17:00:47 +000034// C++98 cmath std::pow seems to be the earliest portable way to get float pow.
35// However, on Linux including cmath undefines isfinite.
36// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608
reed@android.com9c970452009-04-03 14:26:10 +000037static inline float sk_float_pow(float base, float exp) {
bungeman@google.com0567f222012-07-25 17:00:47 +000038 return powf(base, exp);
reed@android.com9c970452009-04-03 14:26:10 +000039}
40
bungeman028205b2015-07-29 12:34:25 -070041#define sk_float_sqrt(x) sqrtf(x)
42#define sk_float_sin(x) sinf(x)
43#define sk_float_cos(x) cosf(x)
44#define sk_float_tan(x) tanf(x)
45#define sk_float_floor(x) floorf(x)
46#define sk_float_ceil(x) ceilf(x)
bungemandd67e3d2016-03-10 13:39:30 -080047#define sk_float_trunc(x) truncf(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +000048#ifdef SK_BUILD_FOR_MAC
bungeman028205b2015-07-29 12:34:25 -070049# define sk_float_acos(x) static_cast<float>(acos(x))
50# define sk_float_asin(x) static_cast<float>(asin(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +000051#else
bungeman028205b2015-07-29 12:34:25 -070052# define sk_float_acos(x) acosf(x)
53# define sk_float_asin(x) asinf(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +000054#endif
bungeman028205b2015-07-29 12:34:25 -070055#define sk_float_atan2(y,x) atan2f(y,x)
56#define sk_float_abs(x) fabsf(x)
bungemand7dc76f2016-03-10 11:14:40 -080057#define sk_float_copysign(x, y) copysignf(x, y)
bungeman028205b2015-07-29 12:34:25 -070058#define sk_float_mod(x,y) fmodf(x,y)
59#define sk_float_exp(x) expf(x)
60#define sk_float_log(x) logf(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +000061
Mike Reedf07b61f2019-04-23 16:31:13 -040062constexpr float sk_float_degrees_to_radians(float degrees) {
63 return degrees * (SK_FloatPI / 180);
64}
65
66constexpr float sk_float_radians_to_degrees(float radians) {
67 return radians * (180 / SK_FloatPI);
68}
69
mtklein1831f992015-06-09 11:47:01 -070070#define sk_float_round(x) sk_float_floor((x) + 0.5f)
71
reed7729e562015-01-16 08:35:09 -080072// can't find log2f on android, but maybe that just a tool bug?
73#ifdef SK_BUILD_FOR_ANDROID
74 static inline float sk_float_log2(float x) {
75 const double inv_ln_2 = 1.44269504088896;
76 return (float)(log(x) * inv_ln_2);
77 }
78#else
79 #define sk_float_log2(x) log2f(x)
80#endif
81
Mike Reed9236b022018-05-14 13:37:16 -040082static inline bool sk_float_isfinite(float x) {
83 return SkFloatBits_IsFinite(SkFloat2Bits(x));
84}
85
Mike Reedf07b61f2019-04-23 16:31:13 -040086static inline bool sk_floats_are_finite(float a, float b) {
87 return sk_float_isfinite(a) && sk_float_isfinite(b);
88}
89
90static inline bool sk_floats_are_finite(const float array[], int count) {
91 float prod = 0;
92 for (int i = 0; i < count; ++i) {
93 prod *= array[i];
94 }
95 // At this point, prod will either be NaN or 0
96 return prod == 0; // if prod is NaN, this check will return false
97}
98
Mike Reed9236b022018-05-14 13:37:16 -040099static inline bool sk_float_isinf(float x) {
100 return SkFloatBits_IsInf(SkFloat2Bits(x));
101}
102
103static inline bool sk_float_isnan(float x) {
104 return !(x == x);
105}
reed@google.com61873a52011-12-05 21:47:25 +0000106
caryclark@google.com3b97af52013-04-23 11:56:44 +0000107#define sk_double_isnan(a) sk_float_isnan(a)
108
Mike Reed828f1d52017-08-09 11:06:53 -0400109#define SK_MaxS32FitsInFloat 2147483520
110#define SK_MinS32FitsInFloat -SK_MaxS32FitsInFloat
111
Mike Reed3d5a6b52018-01-31 15:55:47 -0500112#define SK_MaxS64FitsInFloat (SK_MaxS64 >> (63-24) << (63-24)) // 0x7fffff8000000000
113#define SK_MinS64FitsInFloat -SK_MaxS64FitsInFloat
114
Mike Reed828f1d52017-08-09 11:06:53 -0400115/**
116 * Return the closest int for the given float. Returns SK_MaxS32FitsInFloat for NaN.
117 */
118static inline int sk_float_saturate2int(float x) {
119 x = SkTMin<float>(x, SK_MaxS32FitsInFloat);
120 x = SkTMax<float>(x, SK_MinS32FitsInFloat);
121 return (int)x;
122}
123
Mike Reede0762232017-10-10 14:49:35 -0400124/**
125 * Return the closest int for the given double. Returns SK_MaxS32 for NaN.
126 */
127static inline int sk_double_saturate2int(double x) {
128 x = SkTMin<double>(x, SK_MaxS32);
129 x = SkTMax<double>(x, SK_MinS32);
130 return (int)x;
131}
132
Mike Reed3d5a6b52018-01-31 15:55:47 -0500133/**
134 * Return the closest int64_t for the given float. Returns SK_MaxS64FitsInFloat for NaN.
135 */
136static inline int64_t sk_float_saturate2int64(float x) {
137 x = SkTMin<float>(x, SK_MaxS64FitsInFloat);
138 x = SkTMax<float>(x, SK_MinS64FitsInFloat);
139 return (int64_t)x;
140}
141
Mike Reed828f1d52017-08-09 11:06:53 -0400142#define sk_float_floor2int(x) sk_float_saturate2int(sk_float_floor(x))
143#define sk_float_round2int(x) sk_float_saturate2int(sk_float_floor((x) + 0.5f))
144#define sk_float_ceil2int(x) sk_float_saturate2int(sk_float_ceil(x))
145
146#define sk_float_floor2int_no_saturate(x) (int)sk_float_floor(x)
147#define sk_float_round2int_no_saturate(x) (int)sk_float_floor((x) + 0.5f)
148#define sk_float_ceil2int_no_saturate(x) (int)sk_float_ceil(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000149
reed39393e32014-10-21 12:33:21 -0700150#define sk_double_floor(x) floor(x)
151#define sk_double_round(x) floor((x) + 0.5)
152#define sk_double_ceil(x) ceil(x)
153#define sk_double_floor2int(x) (int)floor(x)
Mike Reedee430912018-05-23 12:12:21 -0400154#define sk_double_round2int(x) (int)floor((x) + 0.5)
reed39393e32014-10-21 12:33:21 -0700155#define sk_double_ceil2int(x) (int)ceil(x)
156
Mike Klein7791b942017-09-18 09:46:16 -0400157// Cast double to float, ignoring any warning about too-large finite values being cast to float.
158// Clang thinks this is undefined, but it's actually implementation defined to return either
159// the largest float or infinity (one of the two bracketing representable floats). Good enough!
Mike Klein5e996b82017-09-21 13:56:05 -0400160#if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
Ben Wagner05c474b2017-09-15 17:48:08 -0400161__attribute__((no_sanitize("float-cast-overflow")))
162#endif
163static inline float sk_double_to_float(double x) {
164 return static_cast<float>(x);
165}
166
Brian Osman06768fc2018-10-31 13:30:52 -0400167#define SK_FloatNaN std::numeric_limits<float>::quiet_NaN()
168#define SK_FloatInfinity (+std::numeric_limits<float>::infinity())
169#define SK_FloatNegativeInfinity (-std::numeric_limits<float>::infinity())
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000170
Brian Salomond6f3f182019-05-31 09:24:40 -0400171#define SK_DoubleNaN std::numeric_limits<double>::quiet_NaN()
172
Mike Reed31cc6d72019-02-27 16:14:37 -0500173// Returns false if any of the floats are outside of [0...1]
174// Returns true if count is 0
175bool sk_floats_are_unit(const float array[], size_t count);
176
mtkleina766ca82016-01-26 07:40:30 -0800177static inline float sk_float_rsqrt_portable(float x) {
178 // Get initial estimate.
Mike Klein1e114f12016-09-29 11:15:15 -0400179 int i;
180 memcpy(&i, &x, 4);
mtkleina766ca82016-01-26 07:40:30 -0800181 i = 0x5F1FFFF9 - (i>>1);
Mike Klein1e114f12016-09-29 11:15:15 -0400182 float estimate;
183 memcpy(&estimate, &i, 4);
mtkleina766ca82016-01-26 07:40:30 -0800184
185 // One step of Newton's method to refine.
186 const float estimate_sq = estimate*estimate;
187 estimate *= 0.703952253f*(2.38924456f-x*estimate_sq);
188 return estimate;
189}
mtklein490b6152015-07-31 11:50:27 -0700190
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000191// Fast, approximate inverse square root.
192// Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster on SSE, 2x on NEON.
mtkleina766ca82016-01-26 07:40:30 -0800193static inline float sk_float_rsqrt(float x) {
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000194// We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got
195// it at compile time. This is going to be too fast to productively hide behind a function pointer.
196//
mtkleina766ca82016-01-26 07:40:30 -0800197// We do one step of Newton's method to refine the estimates in the NEON and portable paths. No
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000198// refinement is faster, but very innacurate. Two steps is more accurate, but slower than 1/sqrt.
jvanverth29c69792015-07-23 11:14:29 -0700199//
mtkleina766ca82016-01-26 07:40:30 -0800200// Optimized constants in the portable path courtesy of http://rrrola.wz.cz/inv_sqrt.html
mtkleinb9c47f92015-07-23 08:37:02 -0700201#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
202 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x)));
djsollen21769c52014-08-01 05:32:32 -0700203#elif defined(SK_ARM_HAS_NEON)
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000204 // Get initial estimate.
205 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doing everything 2x.
206 float32x2_t estimate = vrsqrte_f32(xx);
207
208 // One step of Newton's method to refine.
209 const float32x2_t estimate_sq = vmul_f32(estimate, estimate);
210 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq));
211 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places.
212#else
mtkleina766ca82016-01-26 07:40:30 -0800213 return sk_float_rsqrt_portable(x);
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000214#endif
215}
216
joshualitt922c8b12015-08-07 09:55:23 -0700217// This is the number of significant digits we can print in a string such that when we read that
218// string back we get the floating point number we expect. The minimum value C requires is 6, but
219// most compilers support 9
220#ifdef FLT_DECIMAL_DIG
221#define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG
222#else
223#define SK_FLT_DECIMAL_DIG 9
224#endif
225
Mike Reedfaffa862018-02-12 11:46:19 -0500226// IEEE defines how float divide behaves for non-finite values and zero-denoms, but C does not
227// so we have a helper that suppresses the possible undefined-behavior warnings.
228
229#ifdef __clang__
230__attribute__((no_sanitize("float-divide-by-zero")))
231#endif
232static inline float sk_ieee_float_divide(float numer, float denom) {
233 return numer / denom;
234}
235
Mike Reed9236b022018-05-14 13:37:16 -0400236#ifdef __clang__
237__attribute__((no_sanitize("float-divide-by-zero")))
238#endif
239static inline double sk_ieee_double_divide(double numer, double denom) {
240 return numer / denom;
241}
242
Mike Kleina4c277b2018-11-06 14:24:55 -0500243// While we clean up divide by zero, we'll replace places that do divide by zero with this TODO.
244static inline float sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(float n, float d) {
245 return sk_ieee_float_divide(n,d);
246}
247static inline float sk_ieee_double_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(double n, double d) {
248 return sk_ieee_double_divide(n,d);
249}
250
reed@android.com8a1c16f2008-12-17 15:59:43 +0000251#endif