Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(768)

Side by Side Diff: include/private/SkFloatingPoint.h

Issue 1616013003: de-proc sk_float_rsqrt (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: missed Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | src/core/SkOpts.h » ('j') | src/opts/SkFloatingPoint_opts.h » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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.
131 // (SkScalar -> SkFloatingPoint -> SkOpts.h -> SkXfermode -> SkColor -> SkScalar )
132 namespace SkOpts { extern float (*rsqrt)(float); }
133
134 // Fast, approximate inverse square root. 130 // 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. 131 // 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) { 132 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 133 // 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. 134 // it at compile time. This is going to be too fast to productively hide behind a function pointer.
139 // 135 //
140 // We do one step of Newton's method to refine the estimates in the NEON and nul l paths. No 136 // 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. 137 // refinement is faster, but very innacurate. Two steps is more accurate, but s lower than 1/sqrt.
142 // 138 //
143 // Optimized constants in the null path courtesy of http://rrrola.wz.cz/inv_sqrt .html 139 // 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 140 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
145 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); 141 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x)));
146 #elif defined(SK_ARM_HAS_NEON) 142 #elif defined(SK_ARM_HAS_NEON)
147 // Get initial estimate. 143 // Get initial estimate.
148 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doi ng everything 2x. 144 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); 145 float32x2_t estimate = vrsqrte_f32(xx);
150 146
151 // One step of Newton's method to refine. 147 // One step of Newton's method to refine.
152 const float32x2_t estimate_sq = vmul_f32(estimate, estimate); 148 const float32x2_t estimate_sq = vmul_f32(estimate, estimate);
153 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); 149 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. 150 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places.
155 #else 151 #else
156 // Perhaps runtime-detected NEON, or a portable fallback. 152 // Get initial estimate.
157 return SkOpts::rsqrt(x); 153 int i = *SkTCast<int*>(&x);
154 i = 0x5F1FFFF9 - (i>>1);
155 float estimate = *SkTCast<float*>(&i);
156
157 // One step of Newton's method to refine.
158 const float estimate_sq = estimate*estimate;
159 estimate *= 0.703952253f*(2.38924456f-x*estimate_sq);
160 return estimate;
158 #endif 161 #endif
159 } 162 }
160 163
161 // This is the number of significant digits we can print in a string such that w hen we read that 164 // 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 165 // string back we get the floating point number we expect. The minimum value C requires is 6, but
163 // most compilers support 9 166 // most compilers support 9
164 #ifdef FLT_DECIMAL_DIG 167 #ifdef FLT_DECIMAL_DIG
165 #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG 168 #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG
166 #else 169 #else
167 #define SK_FLT_DECIMAL_DIG 9 170 #define SK_FLT_DECIMAL_DIG 9
168 #endif 171 #endif
169 172
170 #endif 173 #endif
OLDNEW
« no previous file with comments | « no previous file | src/core/SkOpts.h » ('j') | src/opts/SkFloatingPoint_opts.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698