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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 #define sk_float_ceil2int(x) (int)sk_float_ceil(x) | 89 #define sk_float_ceil2int(x) (int)sk_float_ceil(x) |
90 #endif | 90 #endif |
91 | 91 |
92 extern const uint32_t gIEEENotANumber; | 92 extern const uint32_t gIEEENotANumber; |
93 extern const uint32_t gIEEEInfinity; | 93 extern const uint32_t gIEEEInfinity; |
94 extern const uint32_t gIEEENegativeInfinity; | 94 extern const uint32_t gIEEENegativeInfinity; |
95 | 95 |
96 #define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber)) | 96 #define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber)) |
97 #define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity)) | 97 #define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity)) |
98 #define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfini
ty)) | 98 #define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfini
ty)) |
| 99 |
| 100 #if defined(__SSE__) |
| 101 #include <xmmintrin.h> |
| 102 #elif defined(__ARM_NEON__) |
| 103 #include <arm_neon.h> |
99 #endif | 104 #endif |
| 105 |
| 106 // Fast, approximate inverse square root. |
| 107 // Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster
on SSE, 2x on NEON. |
| 108 static inline float sk_float_rsqrt(const float x) { |
| 109 // We want all this inlined, so we'll inline SIMD and just take the hit when we
don't know we've got |
| 110 // it at compile time. This is going to be too fast to productively hide behind
a function pointer. |
| 111 // |
| 112 // We do one step of Newton's method to refine the estimates in the NEON and nul
l paths. No |
| 113 // refinement is faster, but very innacurate. Two steps is more accurate, but s
lower than 1/sqrt. |
| 114 #if defined(__SSE__) |
| 115 float result; |
| 116 _mm_store_ss(&result, _mm_rsqrt_ss(_mm_set_ss(x))); |
| 117 return result; |
| 118 #elif defined(__ARM_NEON__) |
| 119 // Get initial estimate. |
| 120 const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doi
ng everything 2x. |
| 121 float32x2_t estimate = vrsqrte_f32(xx); |
| 122 |
| 123 // One step of Newton's method to refine. |
| 124 const float32x2_t estimate_sq = vmul_f32(estimate, estimate); |
| 125 estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); |
| 126 return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in
both places. |
| 127 #else |
| 128 // Get initial estimate. |
| 129 int i = *SkTCast<int*>(&x); |
| 130 i = 0x5f3759df - (i>>1); |
| 131 float estimate = *SkTCast<float*>(&i); |
| 132 |
| 133 // One step of Newton's method to refine. |
| 134 const float estimate_sq = estimate*estimate; |
| 135 estimate *= (1.5f-0.5f*x*estimate_sq); |
| 136 return estimate; |
| 137 #endif |
| 138 } |
| 139 |
| 140 #endif |
OLD | NEW |