blob: d3ebdc26e272115cb114fbee993cc66c32ba86bb [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00002 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00003 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00004 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00006 */
7
8#ifndef SkFloatingPoint_DEFINED
9#define SkFloatingPoint_DEFINED
10
Mike Kleind9187c02018-09-07 17:32:47 +000011#include "../private/SkFloatBits.h"
Hal Canary3b347232018-09-06 11:51:43 -040012#include "SkTypes.h"
Mike Kleind9187c02018-09-07 17:32:47 +000013#include "SkSafe_math.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#include <float.h>
Mike Reed9236b022018-05-14 13:37:16 -040015#include <math.h>
Ben Wagner29f2eaf2018-06-15 13:58:41 -040016#include <cstring>
17
bungeman@google.comd3fbd342014-04-15 15:52:07 +000018
mtkleine18fa442016-06-09 13:40:56 -070019#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
20 #include <xmmintrin.h>
21#elif defined(SK_ARM_HAS_NEON)
22 #include <arm_neon.h>
23#endif
24
bungeman@google.comd3fbd342014-04-15 15:52:07 +000025// For _POSIX_VERSION
26#if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
27#include <unistd.h>
28#endif
29
bungeman@google.com0567f222012-07-25 17:00:47 +000030// C++98 cmath std::pow seems to be the earliest portable way to get float pow.
31// However, on Linux including cmath undefines isfinite.
32// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608
reed@android.com9c970452009-04-03 14:26:10 +000033static inline float sk_float_pow(float base, float exp) {
bungeman@google.com0567f222012-07-25 17:00:47 +000034 return powf(base, exp);
reed@android.com9c970452009-04-03 14:26:10 +000035}
36
bungeman028205b2015-07-29 12:34:25 -070037#define sk_float_sqrt(x) sqrtf(x)
38#define sk_float_sin(x) sinf(x)
39#define sk_float_cos(x) cosf(x)
40#define sk_float_tan(x) tanf(x)
41#define sk_float_floor(x) floorf(x)
42#define sk_float_ceil(x) ceilf(x)
bungemandd67e3d2016-03-10 13:39:30 -080043#define sk_float_trunc(x) truncf(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +000044#ifdef SK_BUILD_FOR_MAC
bungeman028205b2015-07-29 12:34:25 -070045# define sk_float_acos(x) static_cast<float>(acos(x))
46# define sk_float_asin(x) static_cast<float>(asin(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +000047#else
bungeman028205b2015-07-29 12:34:25 -070048# define sk_float_acos(x) acosf(x)
49# define sk_float_asin(x) asinf(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +000050#endif
bungeman028205b2015-07-29 12:34:25 -070051#define sk_float_atan2(y,x) atan2f(y,x)
52#define sk_float_abs(x) fabsf(x)
bungemand7dc76f2016-03-10 11:14:40 -080053#define sk_float_copysign(x, y) copysignf(x, y)
bungeman028205b2015-07-29 12:34:25 -070054#define sk_float_mod(x,y) fmodf(x,y)
55#define sk_float_exp(x) expf(x)
56#define sk_float_log(x) logf(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +000057
mtklein1831f992015-06-09 11:47:01 -070058#define sk_float_round(x) sk_float_floor((x) + 0.5f)
59
reed7729e562015-01-16 08:35:09 -080060// can't find log2f on android, but maybe that just a tool bug?
61#ifdef SK_BUILD_FOR_ANDROID
62 static inline float sk_float_log2(float x) {
63 const double inv_ln_2 = 1.44269504088896;
64 return (float)(log(x) * inv_ln_2);
65 }
66#else
67 #define sk_float_log2(x) log2f(x)
68#endif
69
Mike Reed9236b022018-05-14 13:37:16 -040070static inline bool sk_float_isfinite(float x) {
71 return SkFloatBits_IsFinite(SkFloat2Bits(x));
72}
73
74static inline bool sk_float_isinf(float x) {
75 return SkFloatBits_IsInf(SkFloat2Bits(x));
76}
77
78static inline bool sk_float_isnan(float x) {
79 return !(x == x);
80}
reed@google.com61873a52011-12-05 21:47:25 +000081
caryclark@google.com3b97af52013-04-23 11:56:44 +000082#define sk_double_isnan(a) sk_float_isnan(a)
83
Mike Reed828f1d52017-08-09 11:06:53 -040084#define SK_MaxS32FitsInFloat 2147483520
85#define SK_MinS32FitsInFloat -SK_MaxS32FitsInFloat
86
Mike Reed3d5a6b52018-01-31 15:55:47 -050087#define SK_MaxS64FitsInFloat (SK_MaxS64 >> (63-24) << (63-24)) // 0x7fffff8000000000
88#define SK_MinS64FitsInFloat -SK_MaxS64FitsInFloat
89
Mike Reed828f1d52017-08-09 11:06:53 -040090/**
91 * Return the closest int for the given float. Returns SK_MaxS32FitsInFloat for NaN.
92 */
93static inline int sk_float_saturate2int(float x) {
94 x = SkTMin<float>(x, SK_MaxS32FitsInFloat);
95 x = SkTMax<float>(x, SK_MinS32FitsInFloat);
96 return (int)x;
97}
98
Mike Reede0762232017-10-10 14:49:35 -040099/**
100 * Return the closest int for the given double. Returns SK_MaxS32 for NaN.
101 */
102static inline int sk_double_saturate2int(double x) {
103 x = SkTMin<double>(x, SK_MaxS32);
104 x = SkTMax<double>(x, SK_MinS32);
105 return (int)x;
106}
107
Mike Reed3d5a6b52018-01-31 15:55:47 -0500108/**
109 * Return the closest int64_t for the given float. Returns SK_MaxS64FitsInFloat for NaN.
110 */
111static inline int64_t sk_float_saturate2int64(float x) {
112 x = SkTMin<float>(x, SK_MaxS64FitsInFloat);
113 x = SkTMax<float>(x, SK_MinS64FitsInFloat);
114 return (int64_t)x;
115}
116
Mike Reed828f1d52017-08-09 11:06:53 -0400117#define sk_float_floor2int(x) sk_float_saturate2int(sk_float_floor(x))
118#define sk_float_round2int(x) sk_float_saturate2int(sk_float_floor((x) + 0.5f))
119#define sk_float_ceil2int(x) sk_float_saturate2int(sk_float_ceil(x))
120
121#define sk_float_floor2int_no_saturate(x) (int)sk_float_floor(x)
122#define sk_float_round2int_no_saturate(x) (int)sk_float_floor((x) + 0.5f)
123#define sk_float_ceil2int_no_saturate(x) (int)sk_float_ceil(x)
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124
reed39393e32014-10-21 12:33:21 -0700125#define sk_double_floor(x) floor(x)
126#define sk_double_round(x) floor((x) + 0.5)
127#define sk_double_ceil(x) ceil(x)
128#define sk_double_floor2int(x) (int)floor(x)
Mike Reedee430912018-05-23 12:12:21 -0400129#define sk_double_round2int(x) (int)floor((x) + 0.5)
reed39393e32014-10-21 12:33:21 -0700130#define sk_double_ceil2int(x) (int)ceil(x)
131
Mike Klein7791b942017-09-18 09:46:16 -0400132// Cast double to float, ignoring any warning about too-large finite values being cast to float.
133// Clang thinks this is undefined, but it's actually implementation defined to return either
134// the largest float or infinity (one of the two bracketing representable floats). Good enough!
Mike Klein5e996b82017-09-21 13:56:05 -0400135#if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
Ben Wagner05c474b2017-09-15 17:48:08 -0400136__attribute__((no_sanitize("float-cast-overflow")))
137#endif
138static inline float sk_double_to_float(double x) {
139 return static_cast<float>(x);
140}
141
Mike Klein12f25a22018-06-05 10:46:05 -0400142#define SK_FloatNaN NAN
143#define SK_FloatInfinity (+INFINITY)
144#define SK_FloatNegativeInfinity (-INFINITY)
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000145
mtkleina766ca82016-01-26 07:40:30 -0800146static inline float sk_float_rsqrt_portable(float x) {
147 // Get initial estimate.
Mike Klein1e114f12016-09-29 11:15:15 -0400148 int i;
149 memcpy(&i, &x, 4);
mtkleina766ca82016-01-26 07:40:30 -0800150 i = 0x5F1FFFF9 - (i>>1);
Mike Klein1e114f12016-09-29 11:15:15 -0400151 float estimate;
152 memcpy(&estimate, &i, 4);
mtkleina766ca82016-01-26 07:40:30 -0800153
154 // One step of Newton's method to refine.
155 const float estimate_sq = estimate*estimate;
156 estimate *= 0.703952253f*(2.38924456f-x*estimate_sq);
157 return estimate;
158}
mtklein490b6152015-07-31 11:50:27 -0700159
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000160// Fast, approximate inverse square root.
161// 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 -0800162static inline float sk_float_rsqrt(float x) {
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000163// We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got
164// it at compile time. This is going to be too fast to productively hide behind a function pointer.
165//
mtkleina766ca82016-01-26 07:40:30 -0800166// 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 +0000167// refinement is faster, but very innacurate. Two steps is more accurate, but slower than 1/sqrt.
jvanverth29c69792015-07-23 11:14:29 -0700168//
mtkleina766ca82016-01-26 07:40:30 -0800169// Optimized constants in the portable path courtesy of http://rrrola.wz.cz/inv_sqrt.html
mtkleinb9c47f92015-07-23 08:37:02 -0700170#if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
171 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x)));
djsollen21769c52014-08-01 05:32:32 -0700172#elif defined(SK_ARM_HAS_NEON)
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000173 // Get initial estimate.
174 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doing everything 2x.
175 float32x2_t estimate = vrsqrte_f32(xx);
176
177 // One step of Newton's method to refine.
178 const float32x2_t estimate_sq = vmul_f32(estimate, estimate);
179 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq));
180 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places.
181#else
mtkleina766ca82016-01-26 07:40:30 -0800182 return sk_float_rsqrt_portable(x);
commit-bot@chromium.org11e5b972013-11-08 20:14:16 +0000183#endif
184}
185
joshualitt922c8b12015-08-07 09:55:23 -0700186// This is the number of significant digits we can print in a string such that when we read that
187// string back we get the floating point number we expect. The minimum value C requires is 6, but
188// most compilers support 9
189#ifdef FLT_DECIMAL_DIG
190#define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG
191#else
192#define SK_FLT_DECIMAL_DIG 9
193#endif
194
Mike Reedfaffa862018-02-12 11:46:19 -0500195// IEEE defines how float divide behaves for non-finite values and zero-denoms, but C does not
196// so we have a helper that suppresses the possible undefined-behavior warnings.
197
198#ifdef __clang__
199__attribute__((no_sanitize("float-divide-by-zero")))
200#endif
201static inline float sk_ieee_float_divide(float numer, float denom) {
202 return numer / denom;
203}
204
Mike Reed9236b022018-05-14 13:37:16 -0400205#ifdef __clang__
206__attribute__((no_sanitize("float-divide-by-zero")))
207#endif
208static inline double sk_ieee_double_divide(double numer, double denom) {
209 return numer / denom;
210}
211
reed@android.com8a1c16f2008-12-17 15:59:43 +0000212#endif