OLD | NEW |
| (Empty) |
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 | |
28 static inline float sk_float_pow(float base, float exp) { | |
29 return powf(base, exp); | |
30 } | |
31 | |
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 copysignf(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 #define sk_float_sqrt(x) sqrtf(x) | |
60 #define sk_float_sin(x) sinf(x) | |
61 #define sk_float_cos(x) cosf(x) | |
62 #define sk_float_tan(x) tanf(x) | |
63 #define sk_float_floor(x) floorf(x) | |
64 #define sk_float_ceil(x) ceilf(x) | |
65 #ifdef SK_BUILD_FOR_MAC | |
66 # define sk_float_acos(x) static_cast<float>(acos(x)) | |
67 # define sk_float_asin(x) static_cast<float>(asin(x)) | |
68 #else | |
69 # define sk_float_acos(x) acosf(x) | |
70 # define sk_float_asin(x) asinf(x) | |
71 #endif | |
72 #define sk_float_atan2(y,x) atan2f(y,x) | |
73 #define sk_float_abs(x) fabsf(x) | |
74 #define sk_float_mod(x,y) fmodf(x,y) | |
75 #define sk_float_exp(x) expf(x) | |
76 #define sk_float_log(x) logf(x) | |
77 | |
78 #define sk_float_round(x) sk_float_floor((x) + 0.5f) | |
79 | |
80 // can't find log2f on android, but maybe that just a tool bug? | |
81 #ifdef SK_BUILD_FOR_ANDROID | |
82 static inline float sk_float_log2(float x) { | |
83 const double inv_ln_2 = 1.44269504088896; | |
84 return (float)(log(x) * inv_ln_2); | |
85 } | |
86 #else | |
87 #define sk_float_log2(x) log2f(x) | |
88 #endif | |
89 | |
90 #ifdef SK_BUILD_FOR_WIN | |
91 #define sk_float_isfinite(x) _finite(x) | |
92 #define sk_float_isnan(x) _isnan(x) | |
93 static inline int sk_float_isinf(float x) { | |
94 int32_t bits = SkFloat2Bits(x); | |
95 return (bits << 1) == (0xFF << 24); | |
96 } | |
97 #else | |
98 #define sk_float_isfinite(x) isfinite(x) | |
99 #define sk_float_isnan(x) isnan(x) | |
100 #define sk_float_isinf(x) isinf(x) | |
101 #endif | |
102 | |
103 #define sk_double_isnan(a) sk_float_isnan(a) | |
104 | |
105 #ifdef SK_USE_FLOATBITS | |
106 #define sk_float_floor2int(x) SkFloatToIntFloor(x) | |
107 #define sk_float_round2int(x) SkFloatToIntRound(x) | |
108 #define sk_float_ceil2int(x) SkFloatToIntCeil(x) | |
109 #else | |
110 #define sk_float_floor2int(x) (int)sk_float_floor(x) | |
111 #define sk_float_round2int(x) (int)sk_float_floor((x) + 0.5f) | |
112 #define sk_float_ceil2int(x) (int)sk_float_ceil(x) | |
113 #endif | |
114 | |
115 #define sk_double_floor(x) floor(x) | |
116 #define sk_double_round(x) floor((x) + 0.5) | |
117 #define sk_double_ceil(x) ceil(x) | |
118 #define sk_double_floor2int(x) (int)floor(x) | |
119 #define sk_double_round2int(x) (int)floor((x) + 0.5f) | |
120 #define sk_double_ceil2int(x) (int)ceil(x) | |
121 | |
122 extern const uint32_t gIEEENotANumber; | |
123 extern const uint32_t gIEEEInfinity; | |
124 extern const uint32_t gIEEENegativeInfinity; | |
125 | |
126 #define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber)) | |
127 #define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity)) | |
128 #define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfini
ty)) | |
129 | |
130 // We forward declare this to break an #include cycle. | |
131 // (SkScalar -> SkFloatingPoint -> SkOpts.h -> SkXfermode -> SkColor -> SkScalar
) | |
132 namespace SkOpts { extern float (*rsqrt)(float); } | |
133 | |
134 // Fast, approximate inverse square root. | |
135 // Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster
on SSE, 2x on NEON. | |
136 static inline float sk_float_rsqrt(const float x) { | |
137 // We want all this inlined, so we'll inline SIMD and just take the hit when we
don't know we've got | |
138 // it at compile time. This is going to be too fast to productively hide behind
a function pointer. | |
139 // | |
140 // We do one step of Newton's method to refine the estimates in the NEON and nul
l paths. No | |
141 // refinement is faster, but very innacurate. Two steps is more accurate, but s
lower than 1/sqrt. | |
142 // | |
143 // Optimized constants in the null path courtesy of http://rrrola.wz.cz/inv_sqrt
.html | |
144 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 | |
145 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); | |
146 #elif defined(SK_ARM_HAS_NEON) | |
147 // Get initial estimate. | |
148 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doi
ng everything 2x. | |
149 float32x2_t estimate = vrsqrte_f32(xx); | |
150 | |
151 // One step of Newton's method to refine. | |
152 const float32x2_t estimate_sq = vmul_f32(estimate, estimate); | |
153 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); | |
154 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in
both places. | |
155 #else | |
156 // Perhaps runtime-detected NEON, or a portable fallback. | |
157 return SkOpts::rsqrt(x); | |
158 #endif | |
159 } | |
160 | |
161 // This is the number of significant digits we can print in a string such that w
hen we read that | |
162 // string back we get the floating point number we expect. The minimum value C
requires is 6, but | |
163 // most compilers support 9 | |
164 #ifdef FLT_DECIMAL_DIG | |
165 #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG | |
166 #else | |
167 #define SK_FLT_DECIMAL_DIG 9 | |
168 #endif | |
169 | |
170 #endif | |
OLD | NEW |