epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkFloatingPoint_DEFINED |
| 11 | #define SkFloatingPoint_DEFINED |
| 12 | |
| 13 | #include "SkTypes.h" |
| 14 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include <math.h> |
| 16 | #include <float.h> |
bungeman@google.com | d3fbd34 | 2014-04-15 15:52:07 +0000 | [diff] [blame] | 17 | |
| 18 | // For _POSIX_VERSION |
| 19 | #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
| 20 | #include <unistd.h> |
| 21 | #endif |
| 22 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #include "SkFloatBits.h" |
| 24 | |
bungeman@google.com | 0567f22 | 2012-07-25 17:00:47 +0000 | [diff] [blame] | 25 | // C++98 cmath std::pow seems to be the earliest portable way to get float pow. |
| 26 | // However, on Linux including cmath undefines isfinite. |
| 27 | // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608 |
reed@android.com | 9c97045 | 2009-04-03 14:26:10 +0000 | [diff] [blame] | 28 | static inline float sk_float_pow(float base, float exp) { |
bungeman@google.com | 0567f22 | 2012-07-25 17:00:47 +0000 | [diff] [blame] | 29 | return powf(base, exp); |
reed@android.com | 9c97045 | 2009-04-03 14:26:10 +0000 | [diff] [blame] | 30 | } |
| 31 | |
reed@android.com | eebf5cb | 2010-02-09 18:30:59 +0000 | [diff] [blame] | 32 | static inline float sk_float_copysign(float x, float y) { |
bungeman@google.com | d3fbd34 | 2014-04-15 15:52:07 +0000 | [diff] [blame] | 33 | // c++11 contains a 'float copysign(float, float)' function in <cmath>. |
bungeman | ef59adb | 2014-09-17 11:16:50 -0700 | [diff] [blame] | 34 | // clang-cl reports __cplusplus for clang, not the __cplusplus vc++ version _MSC_VER would report. |
bungeman | 8dfdfff | 2014-10-15 13:53:54 -0700 | [diff] [blame] | 35 | #if (defined(_MSC_VER) && defined(__clang__)) |
| 36 | # define SK_BUILD_WITH_CLANG_CL 1 |
| 37 | #else |
| 38 | # define SK_BUILD_WITH_CLANG_CL 0 |
| 39 | #endif |
| 40 | #if (!SK_BUILD_WITH_CLANG_CL && __cplusplus >= 201103L) || (_MSC_VER >= 1800) |
bungeman | b7069e9 | 2015-07-21 14:14:30 -0700 | [diff] [blame] | 41 | return copysignf(x, y); |
bungeman@google.com | d3fbd34 | 2014-04-15 15:52:07 +0000 | [diff] [blame] | 42 | |
| 43 | // Posix has demanded 'float copysignf(float, float)' (from C99) since Issue 6. |
| 44 | #elif defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L |
| 45 | return copysignf(x, y); |
| 46 | |
| 47 | // Visual studio prior to 13 only has 'double _copysign(double, double)'. |
| 48 | #elif defined(_MSC_VER) |
bungeman@google.com | 51b0d0b | 2014-04-15 16:08:29 +0000 | [diff] [blame] | 49 | return (float)_copysign(x, y); |
bungeman@google.com | d3fbd34 | 2014-04-15 15:52:07 +0000 | [diff] [blame] | 50 | |
| 51 | // Otherwise convert to bits and extract sign. |
| 52 | #else |
reed@android.com | eebf5cb | 2010-02-09 18:30:59 +0000 | [diff] [blame] | 53 | int32_t xbits = SkFloat2Bits(x); |
| 54 | int32_t ybits = SkFloat2Bits(y); |
| 55 | return SkBits2Float((xbits & 0x7FFFFFFF) | (ybits & 0x80000000)); |
bungeman@google.com | d3fbd34 | 2014-04-15 15:52:07 +0000 | [diff] [blame] | 56 | #endif |
reed@android.com | eebf5cb | 2010-02-09 18:30:59 +0000 | [diff] [blame] | 57 | } |
| 58 | |
bungeman | 028205b | 2015-07-29 12:34:25 -0700 | [diff] [blame] | 59 | #define sk_float_sqrt(x) sqrtf(x) |
| 60 | #define sk_float_sin(x) sinf(x) |
| 61 | #define sk_float_cos(x) cosf(x) |
| 62 | #define sk_float_tan(x) tanf(x) |
| 63 | #define sk_float_floor(x) floorf(x) |
| 64 | #define sk_float_ceil(x) ceilf(x) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | #ifdef SK_BUILD_FOR_MAC |
bungeman | 028205b | 2015-07-29 12:34:25 -0700 | [diff] [blame] | 66 | # define sk_float_acos(x) static_cast<float>(acos(x)) |
| 67 | # define sk_float_asin(x) static_cast<float>(asin(x)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | #else |
bungeman | 028205b | 2015-07-29 12:34:25 -0700 | [diff] [blame] | 69 | # define sk_float_acos(x) acosf(x) |
| 70 | # define sk_float_asin(x) asinf(x) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | #endif |
bungeman | 028205b | 2015-07-29 12:34:25 -0700 | [diff] [blame] | 72 | #define sk_float_atan2(y,x) atan2f(y,x) |
| 73 | #define sk_float_abs(x) fabsf(x) |
| 74 | #define sk_float_mod(x,y) fmodf(x,y) |
| 75 | #define sk_float_exp(x) expf(x) |
| 76 | #define sk_float_log(x) logf(x) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 77 | |
mtklein | 1831f99 | 2015-06-09 11:47:01 -0700 | [diff] [blame] | 78 | #define sk_float_round(x) sk_float_floor((x) + 0.5f) |
| 79 | |
reed | 7729e56 | 2015-01-16 08:35:09 -0800 | [diff] [blame] | 80 | // can't find log2f on android, but maybe that just a tool bug? |
| 81 | #ifdef SK_BUILD_FOR_ANDROID |
| 82 | static inline float sk_float_log2(float x) { |
| 83 | const double inv_ln_2 = 1.44269504088896; |
| 84 | return (float)(log(x) * inv_ln_2); |
| 85 | } |
| 86 | #else |
| 87 | #define sk_float_log2(x) log2f(x) |
| 88 | #endif |
| 89 | |
reed@google.com | 61873a5 | 2011-12-05 21:47:25 +0000 | [diff] [blame] | 90 | #ifdef SK_BUILD_FOR_WIN |
| 91 | #define sk_float_isfinite(x) _finite(x) |
reed@google.com | 5ae777d | 2011-12-06 20:18:05 +0000 | [diff] [blame] | 92 | #define sk_float_isnan(x) _isnan(x) |
| 93 | static inline int sk_float_isinf(float x) { |
| 94 | int32_t bits = SkFloat2Bits(x); |
| 95 | return (bits << 1) == (0xFF << 24); |
| 96 | } |
reed@google.com | 61873a5 | 2011-12-05 21:47:25 +0000 | [diff] [blame] | 97 | #else |
| 98 | #define sk_float_isfinite(x) isfinite(x) |
reed@google.com | 5ae777d | 2011-12-06 20:18:05 +0000 | [diff] [blame] | 99 | #define sk_float_isnan(x) isnan(x) |
| 100 | #define sk_float_isinf(x) isinf(x) |
reed@google.com | 61873a5 | 2011-12-05 21:47:25 +0000 | [diff] [blame] | 101 | #endif |
| 102 | |
caryclark@google.com | 3b97af5 | 2013-04-23 11:56:44 +0000 | [diff] [blame] | 103 | #define sk_double_isnan(a) sk_float_isnan(a) |
| 104 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | #ifdef SK_USE_FLOATBITS |
| 106 | #define sk_float_floor2int(x) SkFloatToIntFloor(x) |
| 107 | #define sk_float_round2int(x) SkFloatToIntRound(x) |
| 108 | #define sk_float_ceil2int(x) SkFloatToIntCeil(x) |
| 109 | #else |
| 110 | #define sk_float_floor2int(x) (int)sk_float_floor(x) |
| 111 | #define sk_float_round2int(x) (int)sk_float_floor((x) + 0.5f) |
| 112 | #define sk_float_ceil2int(x) (int)sk_float_ceil(x) |
| 113 | #endif |
| 114 | |
reed | 39393e3 | 2014-10-21 12:33:21 -0700 | [diff] [blame] | 115 | #define sk_double_floor(x) floor(x) |
| 116 | #define sk_double_round(x) floor((x) + 0.5) |
| 117 | #define sk_double_ceil(x) ceil(x) |
| 118 | #define sk_double_floor2int(x) (int)floor(x) |
| 119 | #define sk_double_round2int(x) (int)floor((x) + 0.5f) |
| 120 | #define sk_double_ceil2int(x) (int)ceil(x) |
| 121 | |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 122 | extern const uint32_t gIEEENotANumber; |
| 123 | extern const uint32_t gIEEEInfinity; |
bsalomon@google.com | 50c79d8 | 2013-01-08 20:31:53 +0000 | [diff] [blame] | 124 | extern const uint32_t gIEEENegativeInfinity; |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 125 | |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame] | 126 | #define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber)) |
| 127 | #define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity)) |
| 128 | #define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfinity)) |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 129 | |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 130 | static inline float sk_float_rsqrt_portable(float x) { |
| 131 | // Get initial estimate. |
| 132 | int i = *SkTCast<int*>(&x); |
| 133 | i = 0x5F1FFFF9 - (i>>1); |
| 134 | float estimate = *SkTCast<float*>(&i); |
| 135 | |
| 136 | // One step of Newton's method to refine. |
| 137 | const float estimate_sq = estimate*estimate; |
| 138 | estimate *= 0.703952253f*(2.38924456f-x*estimate_sq); |
| 139 | return estimate; |
| 140 | } |
mtklein | 490b615 | 2015-07-31 11:50:27 -0700 | [diff] [blame] | 141 | |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 142 | // Fast, approximate inverse square root. |
| 143 | // 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] | 144 | static inline float sk_float_rsqrt(float x) { |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 145 | // We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got |
| 146 | // it at compile time. This is going to be too fast to productively hide behind a function pointer. |
| 147 | // |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 148 | // 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] | 149 | // 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] | 150 | // |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 151 | // 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] | 152 | #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 |
| 153 | return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); |
djsollen | 21769c5 | 2014-08-01 05:32:32 -0700 | [diff] [blame] | 154 | #elif defined(SK_ARM_HAS_NEON) |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 155 | // Get initial estimate. |
| 156 | const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doing everything 2x. |
| 157 | float32x2_t estimate = vrsqrte_f32(xx); |
| 158 | |
| 159 | // One step of Newton's method to refine. |
| 160 | const float32x2_t estimate_sq = vmul_f32(estimate, estimate); |
| 161 | estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); |
| 162 | return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places. |
| 163 | #else |
mtklein | a766ca8 | 2016-01-26 07:40:30 -0800 | [diff] [blame] | 164 | return sk_float_rsqrt_portable(x); |
commit-bot@chromium.org | 11e5b97 | 2013-11-08 20:14:16 +0000 | [diff] [blame] | 165 | #endif |
| 166 | } |
| 167 | |
joshualitt | 922c8b1 | 2015-08-07 09:55:23 -0700 | [diff] [blame] | 168 | // This is the number of significant digits we can print in a string such that when we read that |
| 169 | // string back we get the floating point number we expect. The minimum value C requires is 6, but |
| 170 | // most compilers support 9 |
| 171 | #ifdef FLT_DECIMAL_DIG |
| 172 | #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG |
| 173 | #else |
| 174 | #define SK_FLT_DECIMAL_DIG 9 |
| 175 | #endif |
| 176 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 177 | #endif |