1 /*
2 * Copyright 2006 The Android Open Source Project
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkFloatingPoint_DEFINED
9 #define SkFloatingPoint_DEFINED
10
11 #include "../private/SkFloatBits.h"
12 #include "SkTypes.h"
13 #include "SkSafe_math.h"
14 #include <float.h>
15
16 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
17 #include <xmmintrin.h>
18 #elif defined(SK_ARM_HAS_NEON)
19 #include <arm_neon.h>
20 #endif
21
22 // For _POSIX_VERSION
23 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
24 #include <unistd.h>
25 #endif
26
27 // C++98 cmath std::pow seems to be the earliest portable way to get float pow.
28 // However, on Linux including cmath undefines isfinite.
29 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608
sk_float_pow(float base,float exp)30 static inline float sk_float_pow(float base, float exp) {
31 return powf(base, exp);
32 }
33
34 #define sk_float_sqrt(x) sqrtf(x)
35 #define sk_float_sin(x) sinf(x)
36 #define sk_float_cos(x) cosf(x)
37 #define sk_float_tan(x) tanf(x)
38 #define sk_float_floor(x) floorf(x)
39 #define sk_float_ceil(x) ceilf(x)
40 #define sk_float_trunc(x) truncf(x)
41 #ifdef SK_BUILD_FOR_MAC
42 # define sk_float_acos(x) static_cast<float>(acos(x))
43 # define sk_float_asin(x) static_cast<float>(asin(x))
44 #else
45 # define sk_float_acos(x) acosf(x)
46 # define sk_float_asin(x) asinf(x)
47 #endif
48 #define sk_float_atan2(y,x) atan2f(y,x)
49 #define sk_float_abs(x) fabsf(x)
50 #define sk_float_copysign(x, y) copysignf(x, y)
51 #define sk_float_mod(x,y) fmodf(x,y)
52 #define sk_float_exp(x) expf(x)
53 #define sk_float_log(x) logf(x)
54
55 #define sk_float_round(x) sk_float_floor((x) + 0.5f)
56
57 // can't find log2f on android, but maybe that just a tool bug?
58 #ifdef SK_BUILD_FOR_ANDROID
sk_float_log2(float x)59 static inline float sk_float_log2(float x) {
60 const double inv_ln_2 = 1.44269504088896;
61 return (float)(log(x) * inv_ln_2);
62 }
63 #else
64 #define sk_float_log2(x) log2f(x)
65 #endif
66
67 #ifdef SK_BUILD_FOR_WIN
68 #define sk_float_isfinite(x) _finite(x)
69 #define sk_float_isnan(x) _isnan(x)
sk_float_isinf(float x)70 static inline int sk_float_isinf(float x) {
71 return x && (x + x == x);
72 }
73 #else
74 #define sk_float_isfinite(x) isfinite(x)
75 #define sk_float_isnan(x) isnan(x)
76 #define sk_float_isinf(x) isinf(x)
77 #endif
78
79 #define sk_double_isnan(a) sk_float_isnan(a)
80
81 #define SK_MaxS32FitsInFloat 2147483520
82 #define SK_MinS32FitsInFloat -SK_MaxS32FitsInFloat
83
84 #define SK_MaxS64FitsInFloat (SK_MaxS64 >> (63-24) << (63-24)) // 0x7fffff8000000000
85 #define SK_MinS64FitsInFloat -SK_MaxS64FitsInFloat
86
87 /**
88 * Return the closest int for the given float. Returns SK_MaxS32FitsInFloat for NaN.
89 */
sk_float_saturate2int(float x)90 static inline int sk_float_saturate2int(float x) {
91 x = SkTMin<float>(x, SK_MaxS32FitsInFloat);
92 x = SkTMax<float>(x, SK_MinS32FitsInFloat);
93 return (int)x;
94 }
95
96 /**
97 * Return the closest int for the given double. Returns SK_MaxS32 for NaN.
98 */
sk_double_saturate2int(double x)99 static inline int sk_double_saturate2int(double x) {
100 x = SkTMin<double>(x, SK_MaxS32);
101 x = SkTMax<double>(x, SK_MinS32);
102 return (int)x;
103 }
104
105 /**
106 * Return the closest int64_t for the given float. Returns SK_MaxS64FitsInFloat for NaN.
107 */
sk_float_saturate2int64(float x)108 static inline int64_t sk_float_saturate2int64(float x) {
109 x = SkTMin<float>(x, SK_MaxS64FitsInFloat);
110 x = SkTMax<float>(x, SK_MinS64FitsInFloat);
111 return (int64_t)x;
112 }
113
114 #define sk_float_floor2int(x) sk_float_saturate2int(sk_float_floor(x))
115 #define sk_float_round2int(x) sk_float_saturate2int(sk_float_floor((x) + 0.5f))
116 #define sk_float_ceil2int(x) sk_float_saturate2int(sk_float_ceil(x))
117
118 #define sk_float_floor2int_no_saturate(x) (int)sk_float_floor(x)
119 #define sk_float_round2int_no_saturate(x) (int)sk_float_floor((x) + 0.5f)
120 #define sk_float_ceil2int_no_saturate(x) (int)sk_float_ceil(x)
121
122 #define sk_double_floor(x) floor(x)
123 #define sk_double_round(x) floor((x) + 0.5)
124 #define sk_double_ceil(x) ceil(x)
125 #define sk_double_floor2int(x) (int)floor(x)
126 #define sk_double_round2int(x) (int)floor((x) + 0.5f)
127 #define sk_double_ceil2int(x) (int)ceil(x)
128
129 // Cast double to float, ignoring any warning about too-large finite values being cast to float.
130 // Clang thinks this is undefined, but it's actually implementation defined to return either
131 // the largest float or infinity (one of the two bracketing representable floats). Good enough!
132 #if defined(__clang__) && (__clang_major__ * 1000 + __clang_minor__) >= 3007
133 __attribute__((no_sanitize("float-cast-overflow")))
134 #endif
sk_double_to_float(double x)135 static inline float sk_double_to_float(double x) {
136 return static_cast<float>(x);
137 }
138
139 static const uint32_t kIEEENotANumber = 0x7fffffff;
140 #define SK_FloatNaN (*SkTCast<const float*>(&kIEEENotANumber))
141 #define SK_FloatInfinity (+(float)INFINITY)
142 #define SK_FloatNegativeInfinity (-(float)INFINITY)
143
sk_float_rsqrt_portable(float x)144 static inline float sk_float_rsqrt_portable(float x) {
145 // Get initial estimate.
146 int i;
147 memcpy(&i, &x, 4);
148 i = 0x5F1FFFF9 - (i>>1);
149 float estimate;
150 memcpy(&estimate, &i, 4);
151
152 // One step of Newton's method to refine.
153 const float estimate_sq = estimate*estimate;
154 estimate *= 0.703952253f*(2.38924456f-x*estimate_sq);
155 return estimate;
156 }
157
158 // Fast, approximate inverse square root.
159 // Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster on SSE, 2x on NEON.
sk_float_rsqrt(float x)160 static inline float sk_float_rsqrt(float x) {
161 // We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got
162 // it at compile time. This is going to be too fast to productively hide behind a function pointer.
163 //
164 // We do one step of Newton's method to refine the estimates in the NEON and portable paths. No
165 // refinement is faster, but very innacurate. Two steps is more accurate, but slower than 1/sqrt.
166 //
167 // Optimized constants in the portable path courtesy of http://rrrola.wz.cz/inv_sqrt.html
168 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
169 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x)));
170 #elif defined(SK_ARM_HAS_NEON)
171 // Get initial estimate.
172 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doing everything 2x.
173 float32x2_t estimate = vrsqrte_f32(xx);
174
175 // One step of Newton's method to refine.
176 const float32x2_t estimate_sq = vmul_f32(estimate, estimate);
177 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq));
178 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places.
179 #else
180 return sk_float_rsqrt_portable(x);
181 #endif
182 }
183
184 // This is the number of significant digits we can print in a string such that when we read that
185 // string back we get the floating point number we expect. The minimum value C requires is 6, but
186 // most compilers support 9
187 #ifdef FLT_DECIMAL_DIG
188 #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG
189 #else
190 #define SK_FLT_DECIMAL_DIG 9
191 #endif
192
193 #endif
194