2008-12-17 15:59:43 +00:00
|
|
|
/*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Copyright 2006 The Android Open Source Project
|
2008-12-17 15:59:43 +00:00
|
|
|
*
|
2011-07-28 14:26:00 +00:00
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
2008-12-17 15:59:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkFloatingPoint_DEFINED
|
|
|
|
#define SkFloatingPoint_DEFINED
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypes.h"
|
|
|
|
#include "include/private/SkFloatBits.h"
|
|
|
|
#include "include/private/SkSafe_math.h"
|
2008-12-17 15:59:43 +00:00
|
|
|
#include <float.h>
|
2018-05-14 17:37:16 +00:00
|
|
|
#include <math.h>
|
2019-07-10 16:16:39 +00:00
|
|
|
#include <cmath>
|
2018-06-15 17:58:41 +00:00
|
|
|
#include <cstring>
|
2018-10-31 17:30:52 +00:00
|
|
|
#include <limits>
|
2018-06-15 17:58:41 +00:00
|
|
|
|
2014-04-15 15:52:07 +00:00
|
|
|
|
2016-06-09 20:40:56 +00:00
|
|
|
#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
|
|
|
|
#include <xmmintrin.h>
|
|
|
|
#elif defined(SK_ARM_HAS_NEON)
|
|
|
|
#include <arm_neon.h>
|
|
|
|
#endif
|
|
|
|
|
2014-04-15 15:52:07 +00:00
|
|
|
// For _POSIX_VERSION
|
|
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2019-04-23 20:31:13 +00:00
|
|
|
constexpr float SK_FloatSqrt2 = 1.41421356f;
|
|
|
|
constexpr float SK_FloatPI = 3.14159265f;
|
2019-08-02 13:57:04 +00:00
|
|
|
constexpr double SK_DoublePI = 3.14159265358979323846264338327950288;
|
2019-04-23 20:31:13 +00:00
|
|
|
|
2012-07-25 17:00:47 +00:00
|
|
|
// C++98 cmath std::pow seems to be the earliest portable way to get float pow.
|
|
|
|
// However, on Linux including cmath undefines isfinite.
|
|
|
|
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608
|
2009-04-03 14:26:10 +00:00
|
|
|
static inline float sk_float_pow(float base, float exp) {
|
2012-07-25 17:00:47 +00:00
|
|
|
return powf(base, exp);
|
2009-04-03 14:26:10 +00:00
|
|
|
}
|
|
|
|
|
2015-07-29 19:34:25 +00:00
|
|
|
#define sk_float_sqrt(x) sqrtf(x)
|
|
|
|
#define sk_float_sin(x) sinf(x)
|
|
|
|
#define sk_float_cos(x) cosf(x)
|
|
|
|
#define sk_float_tan(x) tanf(x)
|
|
|
|
#define sk_float_floor(x) floorf(x)
|
|
|
|
#define sk_float_ceil(x) ceilf(x)
|
2016-03-10 21:39:30 +00:00
|
|
|
#define sk_float_trunc(x) truncf(x)
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifdef SK_BUILD_FOR_MAC
|
2015-07-29 19:34:25 +00:00
|
|
|
# define sk_float_acos(x) static_cast<float>(acos(x))
|
|
|
|
# define sk_float_asin(x) static_cast<float>(asin(x))
|
2008-12-17 15:59:43 +00:00
|
|
|
#else
|
2015-07-29 19:34:25 +00:00
|
|
|
# define sk_float_acos(x) acosf(x)
|
|
|
|
# define sk_float_asin(x) asinf(x)
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|
2015-07-29 19:34:25 +00:00
|
|
|
#define sk_float_atan2(y,x) atan2f(y,x)
|
|
|
|
#define sk_float_abs(x) fabsf(x)
|
2016-03-10 19:14:40 +00:00
|
|
|
#define sk_float_copysign(x, y) copysignf(x, y)
|
2015-07-29 19:34:25 +00:00
|
|
|
#define sk_float_mod(x,y) fmodf(x,y)
|
|
|
|
#define sk_float_exp(x) expf(x)
|
|
|
|
#define sk_float_log(x) logf(x)
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2019-04-23 20:31:13 +00:00
|
|
|
constexpr float sk_float_degrees_to_radians(float degrees) {
|
|
|
|
return degrees * (SK_FloatPI / 180);
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr float sk_float_radians_to_degrees(float radians) {
|
|
|
|
return radians * (180 / SK_FloatPI);
|
|
|
|
}
|
|
|
|
|
2015-06-09 18:47:01 +00:00
|
|
|
#define sk_float_round(x) sk_float_floor((x) + 0.5f)
|
|
|
|
|
2015-01-16 16:35:09 +00:00
|
|
|
// can't find log2f on android, but maybe that just a tool bug?
|
|
|
|
#ifdef SK_BUILD_FOR_ANDROID
|
|
|
|
static inline float sk_float_log2(float x) {
|
|
|
|
const double inv_ln_2 = 1.44269504088896;
|
|
|
|
return (float)(log(x) * inv_ln_2);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define sk_float_log2(x) log2f(x)
|
|
|
|
#endif
|
|
|
|
|
2018-05-14 17:37:16 +00:00
|
|
|
static inline bool sk_float_isfinite(float x) {
|
|
|
|
return SkFloatBits_IsFinite(SkFloat2Bits(x));
|
|
|
|
}
|
|
|
|
|
2019-04-23 20:31:13 +00:00
|
|
|
static inline bool sk_floats_are_finite(float a, float b) {
|
|
|
|
return sk_float_isfinite(a) && sk_float_isfinite(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool sk_floats_are_finite(const float array[], int count) {
|
|
|
|
float prod = 0;
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
prod *= array[i];
|
|
|
|
}
|
|
|
|
// At this point, prod will either be NaN or 0
|
|
|
|
return prod == 0; // if prod is NaN, this check will return false
|
|
|
|
}
|
|
|
|
|
2018-05-14 17:37:16 +00:00
|
|
|
static inline bool sk_float_isinf(float x) {
|
|
|
|
return SkFloatBits_IsInf(SkFloat2Bits(x));
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool sk_float_isnan(float x) {
|
|
|
|
return !(x == x);
|
|
|
|
}
|
2011-12-05 21:47:25 +00:00
|
|
|
|
2013-04-23 11:56:44 +00:00
|
|
|
#define sk_double_isnan(a) sk_float_isnan(a)
|
|
|
|
|
2017-08-09 15:06:53 +00:00
|
|
|
#define SK_MaxS32FitsInFloat 2147483520
|
|
|
|
#define SK_MinS32FitsInFloat -SK_MaxS32FitsInFloat
|
|
|
|
|
2018-01-31 20:55:47 +00:00
|
|
|
#define SK_MaxS64FitsInFloat (SK_MaxS64 >> (63-24) << (63-24)) // 0x7fffff8000000000
|
|
|
|
#define SK_MinS64FitsInFloat -SK_MaxS64FitsInFloat
|
|
|
|
|
2017-08-09 15:06:53 +00:00
|
|
|
/**
|
|
|
|
* Return the closest int for the given float. Returns SK_MaxS32FitsInFloat for NaN.
|
|
|
|
*/
|
|
|
|
static inline int sk_float_saturate2int(float x) {
|
2020-02-07 15:36:46 +00:00
|
|
|
x = x < SK_MaxS32FitsInFloat ? x : SK_MaxS32FitsInFloat;
|
|
|
|
x = x > SK_MinS32FitsInFloat ? x : SK_MinS32FitsInFloat;
|
2017-08-09 15:06:53 +00:00
|
|
|
return (int)x;
|
|
|
|
}
|
|
|
|
|
2017-10-10 18:49:35 +00:00
|
|
|
/**
|
|
|
|
* Return the closest int for the given double. Returns SK_MaxS32 for NaN.
|
|
|
|
*/
|
|
|
|
static inline int sk_double_saturate2int(double x) {
|
2020-02-07 15:36:46 +00:00
|
|
|
x = x < SK_MaxS32 ? x : SK_MaxS32;
|
|
|
|
x = x > SK_MinS32 ? x : SK_MinS32;
|
2017-10-10 18:49:35 +00:00
|
|
|
return (int)x;
|
|
|
|
}
|
|
|
|
|
2018-01-31 20:55:47 +00:00
|
|
|
/**
|
|
|
|
* Return the closest int64_t for the given float. Returns SK_MaxS64FitsInFloat for NaN.
|
|
|
|
*/
|
|
|
|
static inline int64_t sk_float_saturate2int64(float x) {
|
2020-02-07 15:36:46 +00:00
|
|
|
x = x < SK_MaxS64FitsInFloat ? x : SK_MaxS64FitsInFloat;
|
|
|
|
x = x > SK_MinS64FitsInFloat ? x : SK_MinS64FitsInFloat;
|
2018-01-31 20:55:47 +00:00
|
|
|
return (int64_t)x;
|
|
|
|
}
|
|
|
|
|
2017-08-09 15:06:53 +00:00
|
|
|
#define sk_float_floor2int(x) sk_float_saturate2int(sk_float_floor(x))
|
|
|
|
#define sk_float_round2int(x) sk_float_saturate2int(sk_float_floor((x) + 0.5f))
|
|
|
|
#define sk_float_ceil2int(x) sk_float_saturate2int(sk_float_ceil(x))
|
|
|
|
|
|
|
|
#define sk_float_floor2int_no_saturate(x) (int)sk_float_floor(x)
|
|
|
|
#define sk_float_round2int_no_saturate(x) (int)sk_float_floor((x) + 0.5f)
|
|
|
|
#define sk_float_ceil2int_no_saturate(x) (int)sk_float_ceil(x)
|
2008-12-17 15:59:43 +00:00
|
|
|
|
2014-10-21 19:33:21 +00:00
|
|
|
#define sk_double_floor(x) floor(x)
|
|
|
|
#define sk_double_round(x) floor((x) + 0.5)
|
|
|
|
#define sk_double_ceil(x) ceil(x)
|
|
|
|
#define sk_double_floor2int(x) (int)floor(x)
|
2018-05-23 16:12:21 +00:00
|
|
|
#define sk_double_round2int(x) (int)floor((x) + 0.5)
|
2014-10-21 19:33:21 +00:00
|
|
|
#define sk_double_ceil2int(x) (int)ceil(x)
|
|
|
|
|
2017-09-18 13:46:16 +00:00
|
|
|
// Cast double to float, ignoring any warning about too-large finite values being cast to float.
|
|
|
|
// Clang thinks this is undefined, but it's actually implementation defined to return either
|
|
|
|
// the largest float or infinity (one of the two bracketing representable floats). Good enough!
|
2020-07-06 13:24:17 +00:00
|
|
|
SK_ATTRIBUTE(no_sanitize("float-cast-overflow"))
|
2017-09-15 21:48:08 +00:00
|
|
|
static inline float sk_double_to_float(double x) {
|
|
|
|
return static_cast<float>(x);
|
|
|
|
}
|
|
|
|
|
2018-10-31 17:30:52 +00:00
|
|
|
#define SK_FloatNaN std::numeric_limits<float>::quiet_NaN()
|
|
|
|
#define SK_FloatInfinity (+std::numeric_limits<float>::infinity())
|
|
|
|
#define SK_FloatNegativeInfinity (-std::numeric_limits<float>::infinity())
|
2013-11-08 20:14:16 +00:00
|
|
|
|
2019-05-31 13:24:40 +00:00
|
|
|
#define SK_DoubleNaN std::numeric_limits<double>::quiet_NaN()
|
|
|
|
|
2019-02-27 21:14:37 +00:00
|
|
|
// Returns false if any of the floats are outside of [0...1]
|
|
|
|
// Returns true if count is 0
|
|
|
|
bool sk_floats_are_unit(const float array[], size_t count);
|
|
|
|
|
2016-01-26 15:40:30 +00:00
|
|
|
static inline float sk_float_rsqrt_portable(float x) {
|
|
|
|
// Get initial estimate.
|
2016-09-29 15:15:15 +00:00
|
|
|
int i;
|
|
|
|
memcpy(&i, &x, 4);
|
2016-01-26 15:40:30 +00:00
|
|
|
i = 0x5F1FFFF9 - (i>>1);
|
2016-09-29 15:15:15 +00:00
|
|
|
float estimate;
|
|
|
|
memcpy(&estimate, &i, 4);
|
2016-01-26 15:40:30 +00:00
|
|
|
|
|
|
|
// One step of Newton's method to refine.
|
|
|
|
const float estimate_sq = estimate*estimate;
|
|
|
|
estimate *= 0.703952253f*(2.38924456f-x*estimate_sq);
|
|
|
|
return estimate;
|
|
|
|
}
|
2015-07-31 18:50:27 +00:00
|
|
|
|
2013-11-08 20:14:16 +00:00
|
|
|
// Fast, approximate inverse square root.
|
|
|
|
// Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster on SSE, 2x on NEON.
|
2016-01-26 15:40:30 +00:00
|
|
|
static inline float sk_float_rsqrt(float x) {
|
2013-11-08 20:14:16 +00:00
|
|
|
// We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got
|
|
|
|
// it at compile time. This is going to be too fast to productively hide behind a function pointer.
|
|
|
|
//
|
2016-01-26 15:40:30 +00:00
|
|
|
// We do one step of Newton's method to refine the estimates in the NEON and portable paths. No
|
2013-11-08 20:14:16 +00:00
|
|
|
// refinement is faster, but very innacurate. Two steps is more accurate, but slower than 1/sqrt.
|
2015-07-23 18:14:29 +00:00
|
|
|
//
|
2016-01-26 15:40:30 +00:00
|
|
|
// Optimized constants in the portable path courtesy of http://rrrola.wz.cz/inv_sqrt.html
|
2015-07-23 15:37:02 +00:00
|
|
|
#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
|
|
|
|
return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x)));
|
2014-08-01 12:32:32 +00:00
|
|
|
#elif defined(SK_ARM_HAS_NEON)
|
2013-11-08 20:14:16 +00:00
|
|
|
// Get initial estimate.
|
|
|
|
const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doing everything 2x.
|
|
|
|
float32x2_t estimate = vrsqrte_f32(xx);
|
|
|
|
|
|
|
|
// One step of Newton's method to refine.
|
|
|
|
const float32x2_t estimate_sq = vmul_f32(estimate, estimate);
|
|
|
|
estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq));
|
|
|
|
return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places.
|
|
|
|
#else
|
2016-01-26 15:40:30 +00:00
|
|
|
return sk_float_rsqrt_portable(x);
|
2013-11-08 20:14:16 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2015-08-07 16:55:23 +00:00
|
|
|
// This is the number of significant digits we can print in a string such that when we read that
|
|
|
|
// string back we get the floating point number we expect. The minimum value C requires is 6, but
|
|
|
|
// most compilers support 9
|
|
|
|
#ifdef FLT_DECIMAL_DIG
|
|
|
|
#define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG
|
|
|
|
#else
|
|
|
|
#define SK_FLT_DECIMAL_DIG 9
|
|
|
|
#endif
|
|
|
|
|
2018-02-12 16:46:19 +00:00
|
|
|
// IEEE defines how float divide behaves for non-finite values and zero-denoms, but C does not
|
|
|
|
// so we have a helper that suppresses the possible undefined-behavior warnings.
|
|
|
|
|
2020-07-06 13:24:17 +00:00
|
|
|
SK_ATTRIBUTE(no_sanitize("float-divide-by-zero"))
|
2018-02-12 16:46:19 +00:00
|
|
|
static inline float sk_ieee_float_divide(float numer, float denom) {
|
|
|
|
return numer / denom;
|
|
|
|
}
|
|
|
|
|
2020-07-06 13:24:17 +00:00
|
|
|
SK_ATTRIBUTE(no_sanitize("float-divide-by-zero"))
|
2018-05-14 17:37:16 +00:00
|
|
|
static inline double sk_ieee_double_divide(double numer, double denom) {
|
|
|
|
return numer / denom;
|
|
|
|
}
|
|
|
|
|
2018-11-06 19:24:55 +00:00
|
|
|
// While we clean up divide by zero, we'll replace places that do divide by zero with this TODO.
|
|
|
|
static inline float sk_ieee_float_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(float n, float d) {
|
|
|
|
return sk_ieee_float_divide(n,d);
|
|
|
|
}
|
|
|
|
static inline float sk_ieee_double_divide_TODO_IS_DIVIDE_BY_ZERO_SAFE_HERE(double n, double d) {
|
|
|
|
return sk_ieee_double_divide(n,d);
|
|
|
|
}
|
|
|
|
|
2019-07-10 16:16:39 +00:00
|
|
|
static inline float sk_fmaf(float f, float m, float a) {
|
|
|
|
#if defined(FP_FAST_FMA)
|
|
|
|
return std::fmaf(f,m,a);
|
|
|
|
#else
|
|
|
|
return f*m+a;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|