2011-07-28 14:26:00 +00:00
|
|
|
|
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
|
|
|
*/
|
|
|
|
|
2011-07-28 14:26:00 +00:00
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifndef SkFloatingPoint_DEFINED
|
|
|
|
#define SkFloatingPoint_DEFINED
|
|
|
|
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
|
|
|
#include <math.h>
|
|
|
|
#include <float.h>
|
2014-04-15 15:52:07 +00:00
|
|
|
|
|
|
|
// For _POSIX_VERSION
|
|
|
|
#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#include "SkFloatBits.h"
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-02-09 18:30:59 +00:00
|
|
|
static inline float sk_float_copysign(float x, float y) {
|
2014-04-15 15:52:07 +00:00
|
|
|
// c++11 contains a 'float copysign(float, float)' function in <cmath>.
|
2014-09-17 18:16:50 +00:00
|
|
|
// clang-cl reports __cplusplus for clang, not the __cplusplus vc++ version _MSC_VER would report.
|
2014-10-15 20:53:54 +00:00
|
|
|
#if (defined(_MSC_VER) && defined(__clang__))
|
|
|
|
# define SK_BUILD_WITH_CLANG_CL 1
|
|
|
|
#else
|
|
|
|
# define SK_BUILD_WITH_CLANG_CL 0
|
|
|
|
#endif
|
|
|
|
#if (!SK_BUILD_WITH_CLANG_CL && __cplusplus >= 201103L) || (_MSC_VER >= 1800)
|
2015-07-21 21:14:30 +00:00
|
|
|
return copysignf(x, y);
|
2014-04-15 15:52:07 +00:00
|
|
|
|
|
|
|
// Posix has demanded 'float copysignf(float, float)' (from C99) since Issue 6.
|
|
|
|
#elif defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
|
|
|
|
return copysignf(x, y);
|
|
|
|
|
|
|
|
// Visual studio prior to 13 only has 'double _copysign(double, double)'.
|
|
|
|
#elif defined(_MSC_VER)
|
2014-04-15 16:08:29 +00:00
|
|
|
return (float)_copysign(x, y);
|
2014-04-15 15:52:07 +00:00
|
|
|
|
|
|
|
// Otherwise convert to bits and extract sign.
|
|
|
|
#else
|
2010-02-09 18:30:59 +00:00
|
|
|
int32_t xbits = SkFloat2Bits(x);
|
|
|
|
int32_t ybits = SkFloat2Bits(y);
|
|
|
|
return SkBits2Float((xbits & 0x7FFFFFFF) | (ybits & 0x80000000));
|
2014-04-15 15:52:07 +00:00
|
|
|
#endif
|
2010-02-09 18:30:59 +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)
|
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)
|
|
|
|
#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
|
|
|
|
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
|
|
|
|
|
2011-12-05 21:47:25 +00:00
|
|
|
#ifdef SK_BUILD_FOR_WIN
|
|
|
|
#define sk_float_isfinite(x) _finite(x)
|
2011-12-06 20:18:05 +00:00
|
|
|
#define sk_float_isnan(x) _isnan(x)
|
|
|
|
static inline int sk_float_isinf(float x) {
|
|
|
|
int32_t bits = SkFloat2Bits(x);
|
|
|
|
return (bits << 1) == (0xFF << 24);
|
|
|
|
}
|
2011-12-05 21:47:25 +00:00
|
|
|
#else
|
|
|
|
#define sk_float_isfinite(x) isfinite(x)
|
2011-12-06 20:18:05 +00:00
|
|
|
#define sk_float_isnan(x) isnan(x)
|
|
|
|
#define sk_float_isinf(x) isinf(x)
|
2011-12-05 21:47:25 +00:00
|
|
|
#endif
|
|
|
|
|
2013-04-23 11:56:44 +00:00
|
|
|
#define sk_double_isnan(a) sk_float_isnan(a)
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#ifdef SK_USE_FLOATBITS
|
|
|
|
#define sk_float_floor2int(x) SkFloatToIntFloor(x)
|
|
|
|
#define sk_float_round2int(x) SkFloatToIntRound(x)
|
|
|
|
#define sk_float_ceil2int(x) SkFloatToIntCeil(x)
|
|
|
|
#else
|
|
|
|
#define sk_float_floor2int(x) (int)sk_float_floor(x)
|
|
|
|
#define sk_float_round2int(x) (int)sk_float_floor((x) + 0.5f)
|
|
|
|
#define sk_float_ceil2int(x) (int)sk_float_ceil(x)
|
|
|
|
#endif
|
|
|
|
|
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)
|
|
|
|
#define sk_double_round2int(x) (int)floor((x) + 0.5f)
|
|
|
|
#define sk_double_ceil2int(x) (int)ceil(x)
|
|
|
|
|
2012-11-02 19:50:26 +00:00
|
|
|
extern const uint32_t gIEEENotANumber;
|
|
|
|
extern const uint32_t gIEEEInfinity;
|
2013-01-08 20:31:53 +00:00
|
|
|
extern const uint32_t gIEEENegativeInfinity;
|
2012-11-02 19:50:26 +00:00
|
|
|
|
2013-02-07 18:58:35 +00:00
|
|
|
#define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber))
|
|
|
|
#define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity))
|
|
|
|
#define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfinity))
|
2013-11-08 20:14:16 +00:00
|
|
|
|
2016-01-22 19:51:40 +00:00
|
|
|
// We forward declare this to break an #include cycle.
|
|
|
|
// (SkScalar -> SkFloatingPoint -> SkOpts.h -> SkXfermode -> SkColor -> SkScalar)
|
|
|
|
namespace SkOpts { extern float (*rsqrt)(float); }
|
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-22 19:51:40 +00:00
|
|
|
static inline float sk_float_rsqrt(const 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-22 19:51:40 +00:00
|
|
|
// We do one step of Newton's method to refine the estimates in the NEON and null 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-22 19:51:40 +00:00
|
|
|
// Optimized constants in the null 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-22 19:51:40 +00:00
|
|
|
// Perhaps runtime-detected NEON, or a portable fallback.
|
|
|
|
return SkOpts::rsqrt(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
|
|
|
|
|
2008-12-17 15:59:43 +00:00
|
|
|
#endif
|