|
1 |
|
2 /* |
|
3 * Copyright 2006 The Android Open Source Project |
|
4 * |
|
5 * Use of this source code is governed by a BSD-style license that can be |
|
6 * found in the LICENSE file. |
|
7 */ |
|
8 |
|
9 |
|
10 #ifndef SkFloatingPoint_DEFINED |
|
11 #define SkFloatingPoint_DEFINED |
|
12 |
|
13 #include "SkTypes.h" |
|
14 |
|
15 #include <math.h> |
|
16 #include <float.h> |
|
17 #include "SkFloatBits.h" |
|
18 |
|
19 // C++98 cmath std::pow seems to be the earliest portable way to get float pow. |
|
20 // However, on Linux including cmath undefines isfinite. |
|
21 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608 |
|
22 static inline float sk_float_pow(float base, float exp) { |
|
23 return powf(base, exp); |
|
24 } |
|
25 |
|
26 static inline float sk_float_copysign(float x, float y) { |
|
27 int32_t xbits = SkFloat2Bits(x); |
|
28 int32_t ybits = SkFloat2Bits(y); |
|
29 return SkBits2Float((xbits & 0x7FFFFFFF) | (ybits & 0x80000000)); |
|
30 } |
|
31 |
|
32 #ifdef SK_BUILD_FOR_WINCE |
|
33 #define sk_float_sqrt(x) (float)::sqrt(x) |
|
34 #define sk_float_sin(x) (float)::sin(x) |
|
35 #define sk_float_cos(x) (float)::cos(x) |
|
36 #define sk_float_tan(x) (float)::tan(x) |
|
37 #define sk_float_acos(x) (float)::acos(x) |
|
38 #define sk_float_asin(x) (float)::asin(x) |
|
39 #define sk_float_atan2(y,x) (float)::atan2(y,x) |
|
40 #define sk_float_abs(x) (float)::fabs(x) |
|
41 #define sk_float_mod(x,y) (float)::fmod(x,y) |
|
42 #define sk_float_exp(x) (float)::exp(x) |
|
43 #define sk_float_log(x) (float)::log(x) |
|
44 #define sk_float_floor(x) (float)::floor(x) |
|
45 #define sk_float_ceil(x) (float)::ceil(x) |
|
46 #else |
|
47 #define sk_float_sqrt(x) sqrtf(x) |
|
48 #define sk_float_sin(x) sinf(x) |
|
49 #define sk_float_cos(x) cosf(x) |
|
50 #define sk_float_tan(x) tanf(x) |
|
51 #define sk_float_floor(x) floorf(x) |
|
52 #define sk_float_ceil(x) ceilf(x) |
|
53 #ifdef SK_BUILD_FOR_MAC |
|
54 #define sk_float_acos(x) static_cast<float>(acos(x)) |
|
55 #define sk_float_asin(x) static_cast<float>(asin(x)) |
|
56 #else |
|
57 #define sk_float_acos(x) acosf(x) |
|
58 #define sk_float_asin(x) asinf(x) |
|
59 #endif |
|
60 #define sk_float_atan2(y,x) atan2f(y,x) |
|
61 #define sk_float_abs(x) fabsf(x) |
|
62 #define sk_float_mod(x,y) fmodf(x,y) |
|
63 #define sk_float_exp(x) expf(x) |
|
64 #define sk_float_log(x) logf(x) |
|
65 #endif |
|
66 |
|
67 #ifdef SK_BUILD_FOR_WIN |
|
68 #define sk_float_isfinite(x) _finite(x) |
|
69 #define sk_float_isnan(x) _isnan(x) |
|
70 static inline int sk_float_isinf(float x) { |
|
71 int32_t bits = SkFloat2Bits(x); |
|
72 return (bits << 1) == (0xFF << 24); |
|
73 } |
|
74 #else |
|
75 #define sk_float_isfinite(x) isfinite(x) |
|
76 #define sk_float_isnan(x) isnan(x) |
|
77 #define sk_float_isinf(x) isinf(x) |
|
78 #endif |
|
79 |
|
80 #define sk_double_isnan(a) sk_float_isnan(a) |
|
81 |
|
82 #ifdef SK_USE_FLOATBITS |
|
83 #define sk_float_floor2int(x) SkFloatToIntFloor(x) |
|
84 #define sk_float_round2int(x) SkFloatToIntRound(x) |
|
85 #define sk_float_ceil2int(x) SkFloatToIntCeil(x) |
|
86 #else |
|
87 #define sk_float_floor2int(x) (int)sk_float_floor(x) |
|
88 #define sk_float_round2int(x) (int)sk_float_floor((x) + 0.5f) |
|
89 #define sk_float_ceil2int(x) (int)sk_float_ceil(x) |
|
90 #endif |
|
91 |
|
92 extern const uint32_t gIEEENotANumber; |
|
93 extern const uint32_t gIEEEInfinity; |
|
94 extern const uint32_t gIEEENegativeInfinity; |
|
95 |
|
96 #define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber)) |
|
97 #define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity)) |
|
98 #define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfinity)) |
|
99 |
|
100 #if defined(__SSE__) |
|
101 #include <xmmintrin.h> |
|
102 #elif defined(__ARM_NEON__) |
|
103 #include <arm_neon.h> |
|
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 null paths. No |
|
113 // refinement is faster, but very innacurate. Two steps is more accurate, but slower 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 doing 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 |