OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2006 The Android Open Source Project | 3 * Copyright 2006 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkFloatingPoint_DEFINED | 10 #ifndef SkFloatingPoint_DEFINED |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 #define sk_double_ceil2int(x) (int)ceil(x) | 120 #define sk_double_ceil2int(x) (int)ceil(x) |
121 | 121 |
122 extern const uint32_t gIEEENotANumber; | 122 extern const uint32_t gIEEENotANumber; |
123 extern const uint32_t gIEEEInfinity; | 123 extern const uint32_t gIEEEInfinity; |
124 extern const uint32_t gIEEENegativeInfinity; | 124 extern const uint32_t gIEEENegativeInfinity; |
125 | 125 |
126 #define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber)) | 126 #define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber)) |
127 #define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity)) | 127 #define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity)) |
128 #define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfini
ty)) | 128 #define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfini
ty)) |
129 | 129 |
130 static inline float sk_float_rsqrt_portable(float x) { | 130 // We forward declare this to break an #include cycle. |
131 // Get initial estimate. | 131 // (SkScalar -> SkFloatingPoint -> SkOpts.h -> SkXfermode -> SkColor -> SkScalar
) |
132 int i = *SkTCast<int*>(&x); | 132 namespace SkOpts { extern float (*rsqrt)(float); } |
133 i = 0x5F1FFFF9 - (i>>1); | |
134 float estimate = *SkTCast<float*>(&i); | |
135 | |
136 // One step of Newton's method to refine. | |
137 const float estimate_sq = estimate*estimate; | |
138 estimate *= 0.703952253f*(2.38924456f-x*estimate_sq); | |
139 return estimate; | |
140 } | |
141 | 133 |
142 // Fast, approximate inverse square root. | 134 // Fast, approximate inverse square root. |
143 // Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster
on SSE, 2x on NEON. | 135 // Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster
on SSE, 2x on NEON. |
144 static inline float sk_float_rsqrt(float x) { | 136 static inline float sk_float_rsqrt(const float x) { |
145 // We want all this inlined, so we'll inline SIMD and just take the hit when we
don't know we've got | 137 // We want all this inlined, so we'll inline SIMD and just take the hit when we
don't know we've got |
146 // it at compile time. This is going to be too fast to productively hide behind
a function pointer. | 138 // it at compile time. This is going to be too fast to productively hide behind
a function pointer. |
147 // | 139 // |
148 // We do one step of Newton's method to refine the estimates in the NEON and por
table paths. No | 140 // We do one step of Newton's method to refine the estimates in the NEON and nul
l paths. No |
149 // refinement is faster, but very innacurate. Two steps is more accurate, but s
lower than 1/sqrt. | 141 // refinement is faster, but very innacurate. Two steps is more accurate, but s
lower than 1/sqrt. |
150 // | 142 // |
151 // Optimized constants in the portable path courtesy of http://rrrola.wz.cz/inv_
sqrt.html | 143 // Optimized constants in the null path courtesy of http://rrrola.wz.cz/inv_sqrt
.html |
152 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 | 144 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 |
153 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); | 145 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); |
154 #elif defined(SK_ARM_HAS_NEON) | 146 #elif defined(SK_ARM_HAS_NEON) |
155 // Get initial estimate. | 147 // Get initial estimate. |
156 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doi
ng everything 2x. | 148 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doi
ng everything 2x. |
157 float32x2_t estimate = vrsqrte_f32(xx); | 149 float32x2_t estimate = vrsqrte_f32(xx); |
158 | 150 |
159 // One step of Newton's method to refine. | 151 // One step of Newton's method to refine. |
160 const float32x2_t estimate_sq = vmul_f32(estimate, estimate); | 152 const float32x2_t estimate_sq = vmul_f32(estimate, estimate); |
161 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); | 153 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); |
162 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in
both places. | 154 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in
both places. |
163 #else | 155 #else |
164 return sk_float_rsqrt_portable(x); | 156 // Perhaps runtime-detected NEON, or a portable fallback. |
| 157 return SkOpts::rsqrt(x); |
165 #endif | 158 #endif |
166 } | 159 } |
167 | 160 |
168 // This is the number of significant digits we can print in a string such that w
hen we read that | 161 // This is the number of significant digits we can print in a string such that w
hen we read that |
169 // string back we get the floating point number we expect. The minimum value C
requires is 6, but | 162 // string back we get the floating point number we expect. The minimum value C
requires is 6, but |
170 // most compilers support 9 | 163 // most compilers support 9 |
171 #ifdef FLT_DECIMAL_DIG | 164 #ifdef FLT_DECIMAL_DIG |
172 #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG | 165 #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG |
173 #else | 166 #else |
174 #define SK_FLT_DECIMAL_DIG 9 | 167 #define SK_FLT_DECIMAL_DIG 9 |
175 #endif | 168 #endif |
176 | 169 |
177 #endif | 170 #endif |
OLD | NEW |