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 |
11 #define SkFloatingPoint_DEFINED | 11 #define SkFloatingPoint_DEFINED |
12 | 12 |
13 #include "SkTypes.h" | 13 #include "SkTypes.h" |
| 14 #include "../private/SkOpts.h" |
14 | 15 |
15 #include <math.h> | 16 #include <math.h> |
16 #include <float.h> | 17 #include <float.h> |
17 | 18 |
18 // For _POSIX_VERSION | 19 // For _POSIX_VERSION |
19 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) | 20 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__)) |
20 #include <unistd.h> | 21 #include <unistd.h> |
21 #endif | 22 #endif |
22 | 23 |
23 #include "SkFloatBits.h" | 24 #include "SkFloatBits.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
120 #define sk_double_ceil2int(x) (int)ceil(x) | 121 #define sk_double_ceil2int(x) (int)ceil(x) |
121 | 122 |
122 extern const uint32_t gIEEENotANumber; | 123 extern const uint32_t gIEEENotANumber; |
123 extern const uint32_t gIEEEInfinity; | 124 extern const uint32_t gIEEEInfinity; |
124 extern const uint32_t gIEEENegativeInfinity; | 125 extern const uint32_t gIEEENegativeInfinity; |
125 | 126 |
126 #define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber)) | 127 #define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber)) |
127 #define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity)) | 128 #define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity)) |
128 #define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfini
ty)) | 129 #define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfini
ty)) |
129 | 130 |
130 namespace SkOpts { extern float (*rsqrt)(float); } | |
131 | |
132 // Fast, approximate inverse square root. | 131 // Fast, approximate inverse square root. |
133 // Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster
on SSE, 2x on NEON. | 132 // Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster
on SSE, 2x on NEON. |
134 static inline float sk_float_rsqrt(const float x) { | 133 static inline float sk_float_rsqrt(const float x) { |
135 // We want all this inlined, so we'll inline SIMD and just take the hit when we
don't know we've got | 134 // We want all this inlined, so we'll inline SIMD and just take the hit when we
don't know we've got |
136 // it at compile time. This is going to be too fast to productively hide behind
a function pointer. | 135 // it at compile time. This is going to be too fast to productively hide behind
a function pointer. |
137 // | 136 // |
138 // We do one step of Newton's method to refine the estimates in the NEON and nul
l paths. No | 137 // We do one step of Newton's method to refine the estimates in the NEON and nul
l paths. No |
139 // refinement is faster, but very innacurate. Two steps is more accurate, but s
lower than 1/sqrt. | 138 // refinement is faster, but very innacurate. Two steps is more accurate, but s
lower than 1/sqrt. |
140 // | 139 // |
141 // Optimized constants in the null path courtesy of http://rrrola.wz.cz/inv_sqrt
.html | 140 // Optimized constants in the null path courtesy of http://rrrola.wz.cz/inv_sqrt
.html |
142 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 | 141 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1 |
143 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); | 142 return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x))); |
144 #elif defined(SK_ARM_HAS_NEON) | 143 #elif defined(SK_ARM_HAS_NEON) |
145 // Get initial estimate. | 144 // Get initial estimate. |
146 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doi
ng everything 2x. | 145 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doi
ng everything 2x. |
147 float32x2_t estimate = vrsqrte_f32(xx); | 146 float32x2_t estimate = vrsqrte_f32(xx); |
148 | 147 |
149 // One step of Newton's method to refine. | 148 // One step of Newton's method to refine. |
150 const float32x2_t estimate_sq = vmul_f32(estimate, estimate); | 149 const float32x2_t estimate_sq = vmul_f32(estimate, estimate); |
151 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); | 150 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); |
152 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in
both places. | 151 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in
both places. |
153 #else | 152 #else |
154 // Perhaps runtime-detected NEON, or a portable fallback. | 153 // Perhaps runtime-detected NEON, or a portable fallback. |
155 return SkOpts::rsqrt(x); | 154 return SkOpts::rsqrt(x); |
156 #endif | 155 #endif |
157 } | 156 } |
158 | 157 |
159 #endif | 158 #endif |
OLD | NEW |