blob: a7aa50cf9f061ca307a015d981de790b726f28d0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkFloatingPoint_DEFINED
11#define SkFloatingPoint_DEFINED
12
13#include "SkTypes.h"
14
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include <math.h>
16#include <float.h>
bungeman@google.comd3fbd342014-04-15 15:52:07 +000017
mtkleine18fa442016-06-09 13:40:56 -070018#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
19 #include <xmmintrin.h>
20#elif defined(SK_ARM_HAS_NEON)
21 #include <arm_neon.h>
22#endif
23
bungeman@google.comd3fbd342014-04-15 15:52:07 +000024// For _POSIX_VERSION
25#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
26#include <unistd.h>
27#endif
28
reed@android.com8a1c16f2008-12-17 15:59:43 +000029#include "SkFloatBits.h"
30
bungeman@google.com0567f222012-07-25 17:00:47 +000031// C++98 cmath std::pow seems to be the earliest portable way to get float pow.
32// However, on Linux including cmath undefines isfinite.
33// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608
reed@android.com9c970452009-04-03 14:26:10 +000034static inline float sk_float_pow(float base, float exp) {
bungeman@google.com0567f222012-07-25 17:00:47 +000035 return powf(base, exp);
reed@android.com9c970452009-04-03 14:26:10 +000036}
37
bungeman028205b2015-07-29 12:34:25 -070038#define sk_float_sqrt(x) sqrtf(x)
39#define sk_float_sin(x) sinf(x)
40#define sk_float_cos(x) cosf(x)
41#define sk_float_tan(x) tanf(x)
42#define sk_float_floor(x) floorf(x)
43#define sk_float_ceil(x) ceilf(x)
bungemandd67e3d2016-03-10 13:39:30 -080044#define sk_float_trunc(x) truncf(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +000045#ifdef SK_BUILD_FOR_MAC
bungeman028205b2015-07-29 12:34:25 -070046# define sk_float_acos(x) static_cast<float>(acos(x))
47# define sk_float_asin(x) static_cast<float>(asin(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +000048#else
bungeman028205b2015-07-29 12:34:25 -070049# define sk_float_acos(x) acosf(x)
50# define sk_float_asin(x) asinf(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +000051#endif
bungeman028205b2015-07-29 12:34:25 -070052#define sk_float_atan2(y,x) atan2f(y,x)
53#define sk_float_abs(x) fabsf(x)
bungemand7dc76f2016-03-10 11:14:40 -080054#define sk_float_copysign(x, y) copysignf(x, y)
bungeman028205b2015-07-29 12:34:25 -070055#define sk_float_mod(x,y) fmodf(x,y)
56#define sk_float_exp(x) expf(x)
57#define sk_float_log(x) logf(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +000058
mtklein1831f992015-06-09 11:47:01 -070059#define sk_float_round(x) sk_float_floor((x) + 0.5f)
60
reed7729e562015-01-16 08:35:09 -080061// can't find log2f on android, but maybe that just a tool bug?
62#ifdef SK_BUILD_FOR_ANDROID
63 static inline float sk_float_log2(float x) {
64 const double inv_ln_2 = 1.44269504088896;
65 return (float)(log(x) * inv_ln_2);
66 }
67#else
68 #define sk_float_log2(x) log2f(x)
69#endif
70
reed@google.com61873a52011-12-05 21:47:25 +000071#ifdef SK_BUILD_FOR_WIN
72 #define sk_float_isfinite(x) _finite(x)
reed@google.com5ae777d2011-12-06 20:18:05 +000073 #define sk_float_isnan(x) _isnan(x)
74 static inline int sk_float_isinf(float x) {
75 int32_t bits = SkFloat2Bits(x);
76 return (bits << 1) == (0xFF << 24);
77 }
reed@google.com61873a52011-12-05 21:47:25 +000078#else
79 #define sk_float_isfinite(x) isfinite(x)
reed@google.com5ae777d2011-12-06 20:18:05 +000080 #define sk_float_isnan(x) isnan(x)
81 #define sk_float_isinf(x) isinf(x)
reed@google.com61873a52011-12-05 21:47:25 +000082#endif
83
caryclark@google.com3b97af52013-04-23 11:56:44 +000084#define sk_double_isnan(a) sk_float_isnan(a)
85
reed@android.com8a1c16f2008-12-17 15:59:43 +000086#ifdef SK_USE_FLOATBITS
87 #define sk_float_floor2int(x) SkFloatToIntFloor(x)
88 #define sk_float_round2int(x) SkFloatToIntRound(x)
89 #define sk_float_ceil2int(x) SkFloatToIntCeil(x)
90#else
91 #define sk_float_floor2int(x) (int)sk_float_floor(x)
92 #define sk_float_round2int(x) (int)sk_float_floor((x) + 0.5f)
93 #define sk_float_ceil2int(x) (int)sk_float_ceil(x)
94#endif
95
reed39393e32014-10-21 12:33:21 -070096#define sk_double_floor(x) floor(x)
97#define sk_double_round(x) floor((x) + 0.5)
98#define sk_double_ceil(x) ceil(x)
99#define sk_double_floor2int(x) (int)floor(x)
100#define sk_double_round2int(x) (int)floor((x) + 0.5f)
101#define sk_double_ceil2int(x) (int)ceil(x)
102
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000103extern const uint32_t gIEEENotANumber;
104extern const uint32_t gIEEEInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +0000105extern const uint32_t gIEEENegativeInfinity;
bsalomon@google.com92b6a942012-11-02 19:50:26 +0000106
djsollen@google.comefbe8e92013-02-07 18:58:35 +0000107#define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber))
108#define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity))
109#define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfinity))
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000110
mtkleina766ca82016-01-26 07:40:30 -0800111static inline float sk_float_rsqrt_portable(float x) {
112 // Get initial estimate.
113 int i = *SkTCast<int*>(&x);
114 i = 0x5F1FFFF9 - (i>>1);
115 float estimate = *SkTCast<float*>(&i);
116
117 // One step of Newton's method to refine.
118 const float estimate_sq = estimate*estimate;
119 estimate *= 0.703952253f*(2.38924456f-x*estimate_sq);
120 return estimate;
121}
mtklein490b6152015-07-31 11:50:27 -0700122
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000123// Fast, approximate inverse square root.
124// 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 -0800125static inline float sk_float_rsqrt(float x) {
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000126// We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got
127// it at compile time. This is going to be too fast to productively hide behind a function pointer.
128//
mtkleina766ca82016-01-26 07:40:30 -0800129// 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 +0000130// refinement is faster, but very innacurate. Two steps is more accurate, but slower than 1/sqrt.
jvanverth29c69792015-07-23 11:14:29 -0700131//
mtkleina766ca82016-01-26 07:40:30 -0800132// Optimized constants in the portable path courtesy of http://rrrola.wz.cz/inv_sqrt.html
mtkleinb9c47f92015-07-23 08:37:02 -0700133#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
134 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x)));
djsollen21769c52014-08-01 05:32:32 -0700135#elif defined(SK_ARM_HAS_NEON)
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000136 // Get initial estimate.
137 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doing everything 2x.
138 float32x2_t estimate = vrsqrte_f32(xx);
139
140 // One step of Newton's method to refine.
141 const float32x2_t estimate_sq = vmul_f32(estimate, estimate);
142 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq));
143 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places.
144#else
mtkleina766ca82016-01-26 07:40:30 -0800145 return sk_float_rsqrt_portable(x);
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000146#endif
147}
148
joshualitt922c8b12015-08-07 09:55:23 -0700149// This is the number of significant digits we can print in a string such that when we read that
150// string back we get the floating point number we expect. The minimum value C requires is 6, but
151// most compilers support 9
152#ifdef FLT_DECIMAL_DIG
153#define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG
154#else
155#define SK_FLT_DECIMAL_DIG 9
156#endif
157
reed@android.com8a1c16f2008-12-17 15:59:43 +0000158#endif