reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 1 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 2 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 3 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 4 | * Use of this source code is governed by a BSD-style license that can be |
| 5 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #ifndef SkFloatingPoint_DEFINED |
| 9 | #define SkFloatingPoint_DEFINED |
| 10 | |
Hal Canary | 3b34723 | 2018-09-06 11:51:43 -0400 | [diff] [blame^] | 11 | #include "SkFloatBits.h" |
| 12 | #include "SkMath.h" |
Mike Klein | e9f78b4 | 2016-11-22 08:57:45 -0500 | [diff] [blame] | 13 | #include "SkSafe_math.h" |
Hal Canary | 3b34723 | 2018-09-06 11:51:43 -0400 | [diff] [blame^] | 14 | #include "SkTypes.h" |
| 15 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 16 | #include <float.h> |
Mike Reed | 9236b02 | 2018-05-14 13:37:16 -0400 | [diff] [blame] | 17 | #include <math.h> |
Ben Wagner | 29f2eaf | 2018-06-15 13:58:41 -0400 | [diff] [blame] | 18 | #include <cstring> |
| 19 | |
bungeman@google.com | d3fbd34 | 2014-04-15 15:52:07 +0000 | [diff] [blame] | 20 | |
mtklein | e18fa44 | 2016-06-09 13:40:56 -0700 | [diff] [blame] | 21 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 |
| 22 | #include <xmmintrin.h> |
| 23 | #elif defined(SK_ARM_HAS_NEON) |
| 24 | #include <arm_neon.h> |
| 25 | #endif |
| 26 | |
bungeman@google.com | d3fbd34 | 2014-04-15 15:52:07 +0000 | [diff] [blame] | 27 | // For _POSIX_VERSION |
| 28 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 29 | #include <unistd.h> |
| 30 | #endif |
| 31 | |
bungeman@google.com | 0567f22 | 2012-07-25 17:00:47 +0000 | [diff] [blame] | 32 | // C++98 cmath std::pow seems to be the earliest portable way to get float pow. |
| 33 | // However, on Linux including cmath undefines isfinite. |
| 34 | // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608 |
reed@android.com | 9c97045 | 2009-04-03 14:26:10 +0000 | [diff] [blame] | 35 | static inline float sk_float_pow(float base, float exp) { |
bungeman@google.com | 0567f22 | 2012-07-25 17:00:47 +0000 | [diff] [blame] | 36 | return powf(base, exp); |
reed@android.com | 9c97045 | 2009-04-03 14:26:10 +0000 | [diff] [blame] | 37 | } |
| 38 | |
bungeman | 028205b | 2015-07-29 12:34:25 -0700 | [diff] [blame] | 39 | #define sk_float_sqrt(x) sqrtf(x) |
| 40 | #define sk_float_sin(x) sinf(x) |
| 41 | #define sk_float_cos(x) cosf(x) |
| 42 | #define sk_float_tan(x) tanf(x) |
| 43 | #define sk_float_floor(x) floorf(x) |
| 44 | #define sk_float_ceil(x) ceilf(x) |
bungeman | dd67e3d | 2016-03-10 13:39:30 -0800 | [diff] [blame] | 45 | #define sk_float_trunc(x) truncf(x) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 46 | #ifdef SK_BUILD_FOR_MAC |
bungeman | 028205b | 2015-07-29 12:34:25 -0700 | [diff] [blame] | 47 | # define sk_float_acos(x) static_cast<float>(acos(x)) |
| 48 | # define sk_float_asin(x) static_cast<float>(asin(x)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | #else |
bungeman | 028205b | 2015-07-29 12:34:25 -0700 | [diff] [blame] | 50 | # define sk_float_acos(x) acosf(x) |
| 51 | # define sk_float_asin(x) asinf(x) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | #endif |
bungeman | 028205b | 2015-07-29 12:34:25 -0700 | [diff] [blame] | 53 | #define sk_float_atan2(y,x) atan2f(y,x) |
| 54 | #define sk_float_abs(x) fabsf(x) |
bungeman | d7dc76f | 2016-03-10 11:14:40 -0800 | [diff] [blame] | 55 | #define sk_float_copysign(x, y) copysignf(x, y) |
bungeman | 028205b | 2015-07-29 12:34:25 -0700 | [diff] [blame] | 56 | #define sk_float_mod(x,y) fmodf(x,y) |
| 57 | #define sk_float_exp(x) expf(x) |
| 58 | #define sk_float_log(x) logf(x) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 59 | |
mtklein | 1831f99 | 2015-06-09 11:47:01 -0700 | [diff] [blame] | 60 | #define sk_float_round(x) sk_float_floor((x) + 0.5f) |
| 61 | |
reed | 7729e56 | 2015-01-16 08:35:09 -0800 | [diff] [blame] | 62 | // can't find log2f on android, but maybe that just a tool bug? |
| 63 | #ifdef SK_BUILD_FOR_ANDROID |
| 64 | static inline float sk_float_log2(float x) { |
| 65 | const double inv_ln_2 = 1.44269504088896; |
| 66 | return (float)(log(x) * inv_ln_2); |
| 67 | } |
| 68 | #else |
| 69 | #define sk_float_log2(x) log2f(x) |
| 70 | #endif |
| 71 | |
Mike Reed | 9236b02 | 2018-05-14 13:37:16 -0400 | [diff] [blame] | 72 | static inline bool sk_float_isfinite(float x) { |
| 73 | return SkFloatBits_IsFinite(SkFloat2Bits(x)); |
| 74 | } |
| 75 | |
| 76 | static inline bool sk_float_isinf(float x) { |
| 77 | return SkFloatBits_IsInf(SkFloat2Bits(x)); |
| 78 | } |
| 79 | |
| 80 | static inline bool sk_float_isnan(float x) { |
| 81 | return !(x == x); |
| 82 | } |
reed@google.com | 61873a5 | 2011-12-05 21:47:25 +0000 | [diff] [blame] | 83 | |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 84 | #define sk_double_isnan(a) sk_float_isnan(a) |
| 85 | |
Mike Reed | 828f1d5 | 2017-08-09 11:06:53 -0400 | [diff] [blame] | 86 | #define SK_MaxS32FitsInFloat 2147483520 |
| 87 | #define SK_MinS32FitsInFloat -SK_MaxS32FitsInFloat |
| 88 | |
Mike Reed | 3d5a6b5 | 2018-01-31 15:55:47 -0500 | [diff] [blame] | 89 | #define SK_MaxS64FitsInFloat (SK_MaxS64 >> (63-24) << (63-24)) // 0x7fffff8000000000 |
| 90 | #define SK_MinS64FitsInFloat -SK_MaxS64FitsInFloat |
| 91 | |
Mike Reed | 828f1d5 | 2017-08-09 11:06:53 -0400 | [diff] [blame] | 92 | /** |
| 93 | * Return the closest int for the given float. Returns SK_MaxS32FitsInFloat for NaN. |
| 94 | */ |
| 95 | static inline int sk_float_saturate2int(float x) { |
| 96 | x = SkTMin<float>(x, SK_MaxS32FitsInFloat); |
| 97 | x = SkTMax<float>(x, SK_MinS32FitsInFloat); |
| 98 | return (int)x; |
| 99 | } |
| 100 | |
Mike Reed | e076223 | 2017-10-10 14:49:35 -0400 | [diff] [blame] | 101 | /** |
| 102 | * Return the closest int for the given double. Returns SK_MaxS32 for NaN. |
| 103 | */ |
| 104 | static inline int sk_double_saturate2int(double x) { |
| 105 | x = SkTMin<double>(x, SK_MaxS32); |
| 106 | x = SkTMax<double>(x, SK_MinS32); |
| 107 | return (int)x; |
| 108 | } |
| 109 | |
Mike Reed | 3d5a6b5 | 2018-01-31 15:55:47 -0500 | [diff] [blame] | 110 | /** |
| 111 | * Return the closest int64_t for the given float. Returns SK_MaxS64FitsInFloat for NaN. |
| 112 | */ |
| 113 | static inline int64_t sk_float_saturate2int64(float x) { |
| 114 | x = SkTMin<float>(x, SK_MaxS64FitsInFloat); |
| 115 | x = SkTMax<float>(x, SK_MinS64FitsInFloat); |
| 116 | return (int64_t)x; |
| 117 | } |
| 118 | |
Mike Reed | 828f1d5 | 2017-08-09 11:06:53 -0400 | [diff] [blame] | 119 | #define sk_float_floor2int(x) sk_float_saturate2int(sk_float_floor(x)) |
| 120 | #define sk_float_round2int(x) sk_float_saturate2int(sk_float_floor((x) + 0.5f)) |
| 121 | #define sk_float_ceil2int(x) sk_float_saturate2int(sk_float_ceil(x)) |
| 122 | |
| 123 | #define sk_float_floor2int_no_saturate(x) (int)sk_float_floor(x) |
| 124 | #define sk_float_round2int_no_saturate(x) (int)sk_float_floor((x) + 0.5f) |
| 125 | #define sk_float_ceil2int_no_saturate(x) (int)sk_float_ceil(x) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 126 | |
reed | 39393e3 | 2014-10-21 12:33:21 -0700 | [diff] [blame] | 127 | #define sk_double_floor(x) floor(x) |
| 128 | #define sk_double_round(x) floor((x) + 0.5) |
| 129 | #define sk_double_ceil(x) ceil(x) |
| 130 | #define sk_double_floor2int(x) (int)floor(x) |
Mike Reed | ee43091 | 2018-05-23 12:12:21 -0400 | [diff] [blame] | 131 | #define sk_double_round2int(x) (int)floor((x) + 0.5) |
reed | 39393e3 | 2014-10-21 12:33:21 -0700 | [diff] [blame] | 132 | #define sk_double_ceil2int(x) (int)ceil(x) |
| 133 | |
Mike Klein | 7791b94 | 2017-09-18 09:46:16 -0400 | [diff] [blame] | 134 | // Cast double to float, ignoring any warning about too-large finite values being cast to float. |
| 135 | // Clang thinks this is undefined, but it's actually implementation defined to return either |
| 136 | // the largest float or infinity (one of the two bracketing representable floats). Good enough! |
Mike Klein | 5e996b8 | 2017-09-21 13:56:05 -0400 | [diff] [blame] | 137 | #if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007 |
Ben Wagner | 05c474b | 2017-09-15 17:48:08 -0400 | [diff] [blame] | 138 | __attribute__((no_sanitize("float-cast-overflow"))) |
| 139 | #endif |
| 140 | static inline float sk_double_to_float(double x) { |
| 141 | return static_cast<float>(x); |
| 142 | } |
| 143 | |
Mike Klein | 12f25a2 | 2018-06-05 10:46:05 -0400 | [diff] [blame] | 144 | #define SK_FloatNaN NAN |
| 145 | #define SK_FloatInfinity (+INFINITY) |
| 146 | #define SK_FloatNegativeInfinity (-INFINITY) |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 147 | |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 148 | static inline float sk_float_rsqrt_portable(float x) { |
| 149 | // Get initial estimate. |
Mike Klein | 1e114f1 | 2016-09-29 11:15:15 -0400 | [diff] [blame] | 150 | int i; |
| 151 | memcpy(&i, &x, 4); |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 152 | i = 0x5F1FFFF9 - (i>>1); |
Mike Klein | 1e114f1 | 2016-09-29 11:15:15 -0400 | [diff] [blame] | 153 | float estimate; |
| 154 | memcpy(&estimate, &i, 4); |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 155 | |
| 156 | // One step of Newton's method to refine. |
| 157 | const float estimate_sq = estimate*estimate; |
| 158 | estimate *= 0.703952253f*(2.38924456f-x*estimate_sq); |
| 159 | return estimate; |
| 160 | } |
mtklein | 490b615 | 2015-07-31 11:50:27 -0700 | [diff] [blame] | 161 | |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 162 | // Fast, approximate inverse square root. |
| 163 | // Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster on SSE, 2x on NEON. |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 164 | static inline float sk_float_rsqrt(float x) { |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 165 | // We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got |
| 166 | // it at compile time. This is going to be too fast to productively hide behind a function pointer. |
| 167 | // |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 168 | // We do one step of Newton's method to refine the estimates in the NEON and portable paths. No |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 169 | // refinement is faster, but very innacurate. Two steps is more accurate, but slower than 1/sqrt. |
jvanverth | 29c6979 | 2015-07-23 11:14:29 -0700 | [diff] [blame] | 170 | // |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 171 | // Optimized constants in the portable path courtesy of http://rrrola.wz.cz/inv_sqrt.html |
mtklein | b9c47f9 | 2015-07-23 08:37:02 -0700 | [diff] [blame] | 172 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 |
| 173 | return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); |
djsollen | 21769c5 | 2014-08-01 05:32:32 -0700 | [diff] [blame] | 174 | #elif defined(SK_ARM_HAS_NEON) |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 175 | // Get initial estimate. |
| 176 | const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doing everything 2x. |
| 177 | float32x2_t estimate = vrsqrte_f32(xx); |
| 178 | |
| 179 | // One step of Newton's method to refine. |
| 180 | const float32x2_t estimate_sq = vmul_f32(estimate, estimate); |
| 181 | estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); |
| 182 | return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places. |
| 183 | #else |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 184 | return sk_float_rsqrt_portable(x); |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 185 | #endif |
| 186 | } |
| 187 | |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 188 | // This is the number of significant digits we can print in a string such that when we read that |
| 189 | // string back we get the floating point number we expect. The minimum value C requires is 6, but |
| 190 | // most compilers support 9 |
| 191 | #ifdef FLT_DECIMAL_DIG |
| 192 | #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG |
| 193 | #else |
| 194 | #define SK_FLT_DECIMAL_DIG 9 |
| 195 | #endif |
| 196 | |
Mike Reed | faffa86 | 2018-02-12 11:46:19 -0500 | [diff] [blame] | 197 | // IEEE defines how float divide behaves for non-finite values and zero-denoms, but C does not |
| 198 | // so we have a helper that suppresses the possible undefined-behavior warnings. |
| 199 | |
| 200 | #ifdef __clang__ |
| 201 | __attribute__((no_sanitize("float-divide-by-zero"))) |
| 202 | #endif |
| 203 | static inline float sk_ieee_float_divide(float numer, float denom) { |
| 204 | return numer / denom; |
| 205 | } |
| 206 | |
Mike Reed | 9236b02 | 2018-05-14 13:37:16 -0400 | [diff] [blame] | 207 | #ifdef __clang__ |
| 208 | __attribute__((no_sanitize("float-divide-by-zero"))) |
| 209 | #endif |
| 210 | static inline double sk_ieee_double_divide(double numer, double denom) { |
| 211 | return numer / denom; |
| 212 | } |
| 213 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 214 | #endif |