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 // We forward declare this to break an #include cycle. | 130 static inline float sk_float_rsqrt_portable(float x) { |
131 // (SkScalar -> SkFloatingPoint -> SkOpts.h -> SkXfermode -> SkColor -> SkScalar
) | 131 // Get initial estimate. |
132 namespace SkOpts { extern float (*rsqrt)(float); } | 132 int i = *SkTCast<int*>(&x); |
| 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 } |
133 | 141 |
134 // Fast, approximate inverse square root. | 142 // 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. | 143 // 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) { | 144 static inline float sk_float_rsqrt(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 | 145 // 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. | 146 // it at compile time. This is going to be too fast to productively hide behind
a function pointer. |
139 // | 147 // |
140 // We do one step of Newton's method to refine the estimates in the NEON and nul
l paths. No | 148 // We do one step of Newton's method to refine the estimates in the NEON and por
table paths. No |
141 // refinement is faster, but very innacurate. Two steps is more accurate, but s
lower than 1/sqrt. | 149 // refinement is faster, but very innacurate. Two steps is more accurate, but s
lower than 1/sqrt. |
142 // | 150 // |
143 // Optimized constants in the null path courtesy of http://rrrola.wz.cz/inv_sqrt
.html | 151 // Optimized constants in the portable path courtesy of http://rrrola.wz.cz/inv_
sqrt.html |
144 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 | 152 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 |
145 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); | 153 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); |
146 #elif defined(SK_ARM_HAS_NEON) | 154 #elif defined(SK_ARM_HAS_NEON) |
147 // Get initial estimate. | 155 // Get initial estimate. |
148 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doi
ng everything 2x. | 156 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); | 157 float32x2_t estimate = vrsqrte_f32(xx); |
150 | 158 |
151 // One step of Newton's method to refine. | 159 // One step of Newton's method to refine. |
152 const float32x2_t estimate_sq = vmul_f32(estimate, estimate); | 160 const float32x2_t estimate_sq = vmul_f32(estimate, estimate); |
153 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); | 161 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. | 162 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in
both places. |
155 #else | 163 #else |
156 // Perhaps runtime-detected NEON, or a portable fallback. | 164 return sk_float_rsqrt_portable(x); |
157 return SkOpts::rsqrt(x); | |
158 #endif | 165 #endif |
159 } | 166 } |
160 | 167 |
161 // This is the number of significant digits we can print in a string such that w
hen we read that | 168 // 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 | 169 // string back we get the floating point number we expect. The minimum value C
requires is 6, but |
163 // most compilers support 9 | 170 // most compilers support 9 |
164 #ifdef FLT_DECIMAL_DIG | 171 #ifdef FLT_DECIMAL_DIG |
165 #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG | 172 #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG |
166 #else | 173 #else |
167 #define SK_FLT_DECIMAL_DIG 9 | 174 #define SK_FLT_DECIMAL_DIG 9 |
168 #endif | 175 #endif |
169 | 176 |
170 #endif | 177 #endif |
OLD | NEW |