1
2 /*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
9
10 #ifndef SkFloatingPoint_DEFINED
11 #define SkFloatingPoint_DEFINED
12
13 #include "SkTypes.h"
14
15 #include <math.h>
16 #include <float.h>
17
18 // For _POSIX_VERSION
19 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
20 #include <unistd.h>
21 #endif
22
23 #include "SkFloatBits.h"
24
25 // C++98 cmath std::pow seems to be the earliest portable way to get float pow.
26 // However, on Linux including cmath undefines isfinite.
27 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608
sk_float_pow(float base,float exp)28 static inline float sk_float_pow(float base, float exp) {
29 return powf(base, exp);
30 }
31
sk_float_copysign(float x,float y)32 static inline float sk_float_copysign(float x, float y) {
33 // c++11 contains a 'float copysign(float, float)' function in <cmath>.
34 // clang-cl reports __cplusplus for clang, not the __cplusplus vc++ version _MSC_VER would report.
35 #if (defined(_MSC_VER) && defined(__clang__))
36 # define SK_BUILD_WITH_CLANG_CL 1
37 #else
38 # define SK_BUILD_WITH_CLANG_CL 0
39 #endif
40 #if (!SK_BUILD_WITH_CLANG_CL && __cplusplus >= 201103L) || (_MSC_VER >= 1800)
41 return copysign(x, y);
42
43 // Posix has demanded 'float copysignf(float, float)' (from C99) since Issue 6.
44 #elif defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
45 return copysignf(x, y);
46
47 // Visual studio prior to 13 only has 'double _copysign(double, double)'.
48 #elif defined(_MSC_VER)
49 return (float)_copysign(x, y);
50
51 // Otherwise convert to bits and extract sign.
52 #else
53 int32_t xbits = SkFloat2Bits(x);
54 int32_t ybits = SkFloat2Bits(y);
55 return SkBits2Float((xbits & 0x7FFFFFFF) | (ybits & 0x80000000));
56 #endif
57 }
58
59 #ifdef SK_BUILD_FOR_WINCE
60 #define sk_float_sqrt(x) (float)::sqrt(x)
61 #define sk_float_sin(x) (float)::sin(x)
62 #define sk_float_cos(x) (float)::cos(x)
63 #define sk_float_tan(x) (float)::tan(x)
64 #define sk_float_acos(x) (float)::acos(x)
65 #define sk_float_asin(x) (float)::asin(x)
66 #define sk_float_atan2(y,x) (float)::atan2(y,x)
67 #define sk_float_abs(x) (float)::fabs(x)
68 #define sk_float_mod(x,y) (float)::fmod(x,y)
69 #define sk_float_exp(x) (float)::exp(x)
70 #define sk_float_log(x) (float)::log(x)
71 #define sk_float_floor(x) (float)::floor(x)
72 #define sk_float_ceil(x) (float)::ceil(x)
73 #else
74 #define sk_float_sqrt(x) sqrtf(x)
75 #define sk_float_sin(x) sinf(x)
76 #define sk_float_cos(x) cosf(x)
77 #define sk_float_tan(x) tanf(x)
78 #define sk_float_floor(x) floorf(x)
79 #define sk_float_ceil(x) ceilf(x)
80 #ifdef SK_BUILD_FOR_MAC
81 #define sk_float_acos(x) static_cast<float>(acos(x))
82 #define sk_float_asin(x) static_cast<float>(asin(x))
83 #else
84 #define sk_float_acos(x) acosf(x)
85 #define sk_float_asin(x) asinf(x)
86 #endif
87 #define sk_float_atan2(y,x) atan2f(y,x)
88 #define sk_float_abs(x) fabsf(x)
89 #define sk_float_mod(x,y) fmodf(x,y)
90 #define sk_float_exp(x) expf(x)
91 #define sk_float_log(x) logf(x)
92 #endif
93
94 // can't find log2f on android, but maybe that just a tool bug?
95 #ifdef SK_BUILD_FOR_ANDROID
sk_float_log2(float x)96 static inline float sk_float_log2(float x) {
97 const double inv_ln_2 = 1.44269504088896;
98 return (float)(log(x) * inv_ln_2);
99 }
100 #else
101 #define sk_float_log2(x) log2f(x)
102 #endif
103
104 #ifdef SK_BUILD_FOR_WIN
105 #define sk_float_isfinite(x) _finite(x)
106 #define sk_float_isnan(x) _isnan(x)
sk_float_isinf(float x)107 static inline int sk_float_isinf(float x) {
108 int32_t bits = SkFloat2Bits(x);
109 return (bits << 1) == (0xFF << 24);
110 }
111 #else
112 #define sk_float_isfinite(x) isfinite(x)
113 #define sk_float_isnan(x) isnan(x)
114 #define sk_float_isinf(x) isinf(x)
115 #endif
116
117 #define sk_double_isnan(a) sk_float_isnan(a)
118
119 #ifdef SK_USE_FLOATBITS
120 #define sk_float_floor2int(x) SkFloatToIntFloor(x)
121 #define sk_float_round2int(x) SkFloatToIntRound(x)
122 #define sk_float_ceil2int(x) SkFloatToIntCeil(x)
123 #else
124 #define sk_float_floor2int(x) (int)sk_float_floor(x)
125 #define sk_float_round2int(x) (int)sk_float_floor((x) + 0.5f)
126 #define sk_float_ceil2int(x) (int)sk_float_ceil(x)
127 #endif
128
129 #define sk_double_floor(x) floor(x)
130 #define sk_double_round(x) floor((x) + 0.5)
131 #define sk_double_ceil(x) ceil(x)
132 #define sk_double_floor2int(x) (int)floor(x)
133 #define sk_double_round2int(x) (int)floor((x) + 0.5f)
134 #define sk_double_ceil2int(x) (int)ceil(x)
135
136 extern const uint32_t gIEEENotANumber;
137 extern const uint32_t gIEEEInfinity;
138 extern const uint32_t gIEEENegativeInfinity;
139
140 #define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber))
141 #define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity))
142 #define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfinity))
143
144 #if defined(__SSE__)
145 #include <xmmintrin.h>
146 #elif defined(SK_ARM_HAS_NEON)
147 #include <arm_neon.h>
148 #endif
149
150 // Fast, approximate inverse square root.
151 // Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster on SSE, 2x on NEON.
sk_float_rsqrt(const float x)152 static inline float sk_float_rsqrt(const float x) {
153 // We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got
154 // it at compile time. This is going to be too fast to productively hide behind a function pointer.
155 //
156 // We do one step of Newton's method to refine the estimates in the NEON and null paths. No
157 // refinement is faster, but very innacurate. Two steps is more accurate, but slower than 1/sqrt.
158 #if defined(__SSE__)
159 float result;
160 _mm_store_ss(&result, _mm_rsqrt_ss(_mm_set_ss(x)));
161 return result;
162 #elif defined(SK_ARM_HAS_NEON)
163 // Get initial estimate.
164 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doing everything 2x.
165 float32x2_t estimate = vrsqrte_f32(xx);
166
167 // One step of Newton's method to refine.
168 const float32x2_t estimate_sq = vmul_f32(estimate, estimate);
169 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq));
170 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places.
171 #else
172 // Get initial estimate.
173 int i = *SkTCast<int*>(&x);
174 i = 0x5f3759df - (i>>1);
175 float estimate = *SkTCast<float*>(&i);
176
177 // One step of Newton's method to refine.
178 const float estimate_sq = estimate*estimate;
179 estimate *= (1.5f-0.5f*x*estimate_sq);
180 return estimate;
181 #endif
182 }
183
184 #endif
185