Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / skia / include / core / SkFloatingPoint.h
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
18 // For _POSIX_VERSION
19 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
20 #include <unistd.h>
21 #endif
22
23 #include "SkFloatBits.h"
24
25 // C++98 cmath std::pow seems to be the earliest portable way to get float pow.
26 // However, on Linux including cmath undefines isfinite.
27 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608
28 static inline float sk_float_pow(float base, float exp) {
29     return powf(base, exp);
30 }
31
32 static inline float sk_float_copysign(float x, float y) {
33 // c++11 contains a 'float copysign(float, float)' function in <cmath>.
34 #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
35     return copysign(x, y);
36
37 // Posix has demanded 'float copysignf(float, float)' (from C99) since Issue 6.
38 #elif defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L
39     return copysignf(x, y);
40
41 // Visual studio prior to 13 only has 'double _copysign(double, double)'.
42 #elif defined(_MSC_VER)
43     return (float)_copysign(x, y);
44
45 // Otherwise convert to bits and extract sign.
46 #else
47     int32_t xbits = SkFloat2Bits(x);
48     int32_t ybits = SkFloat2Bits(y);
49     return SkBits2Float((xbits & 0x7FFFFFFF) | (ybits & 0x80000000));
50 #endif
51 }
52
53 #ifdef SK_BUILD_FOR_WINCE
54     #define sk_float_sqrt(x)        (float)::sqrt(x)
55     #define sk_float_sin(x)         (float)::sin(x)
56     #define sk_float_cos(x)         (float)::cos(x)
57     #define sk_float_tan(x)         (float)::tan(x)
58     #define sk_float_acos(x)        (float)::acos(x)
59     #define sk_float_asin(x)        (float)::asin(x)
60     #define sk_float_atan2(y,x)     (float)::atan2(y,x)
61     #define sk_float_abs(x)         (float)::fabs(x)
62     #define sk_float_mod(x,y)       (float)::fmod(x,y)
63     #define sk_float_exp(x)         (float)::exp(x)
64     #define sk_float_log(x)         (float)::log(x)
65     #define sk_float_floor(x)       (float)::floor(x)
66     #define sk_float_ceil(x)        (float)::ceil(x)
67 #else
68     #define sk_float_sqrt(x)        sqrtf(x)
69     #define sk_float_sin(x)         sinf(x)
70     #define sk_float_cos(x)         cosf(x)
71     #define sk_float_tan(x)         tanf(x)
72     #define sk_float_floor(x)       floorf(x)
73     #define sk_float_ceil(x)        ceilf(x)
74 #ifdef SK_BUILD_FOR_MAC
75     #define sk_float_acos(x)        static_cast<float>(acos(x))
76     #define sk_float_asin(x)        static_cast<float>(asin(x))
77 #else
78     #define sk_float_acos(x)        acosf(x)
79     #define sk_float_asin(x)        asinf(x)
80 #endif
81     #define sk_float_atan2(y,x)     atan2f(y,x)
82     #define sk_float_abs(x)         fabsf(x)
83     #define sk_float_mod(x,y)       fmodf(x,y)
84     #define sk_float_exp(x)         expf(x)
85     #define sk_float_log(x)         logf(x)
86 #endif
87
88 #ifdef SK_BUILD_FOR_WIN
89     #define sk_float_isfinite(x)    _finite(x)
90     #define sk_float_isnan(x)       _isnan(x)
91     static inline int sk_float_isinf(float x) {
92         int32_t bits = SkFloat2Bits(x);
93         return (bits << 1) == (0xFF << 24);
94     }
95 #else
96     #define sk_float_isfinite(x)    isfinite(x)
97     #define sk_float_isnan(x)       isnan(x)
98     #define sk_float_isinf(x)       isinf(x)
99 #endif
100
101 #define sk_double_isnan(a)          sk_float_isnan(a)
102
103 #ifdef SK_USE_FLOATBITS
104     #define sk_float_floor2int(x)   SkFloatToIntFloor(x)
105     #define sk_float_round2int(x)   SkFloatToIntRound(x)
106     #define sk_float_ceil2int(x)    SkFloatToIntCeil(x)
107 #else
108     #define sk_float_floor2int(x)   (int)sk_float_floor(x)
109     #define sk_float_round2int(x)   (int)sk_float_floor((x) + 0.5f)
110     #define sk_float_ceil2int(x)    (int)sk_float_ceil(x)
111 #endif
112
113 extern const uint32_t gIEEENotANumber;
114 extern const uint32_t gIEEEInfinity;
115 extern const uint32_t gIEEENegativeInfinity;
116
117 #define SK_FloatNaN                 (*SkTCast<const float*>(&gIEEENotANumber))
118 #define SK_FloatInfinity            (*SkTCast<const float*>(&gIEEEInfinity))
119 #define SK_FloatNegativeInfinity    (*SkTCast<const float*>(&gIEEENegativeInfinity))
120
121 #if defined(__SSE__)
122 #include <xmmintrin.h>
123 #elif defined(SK_ARM_HAS_NEON)
124 #include <arm_neon.h>
125 #endif
126
127 // Fast, approximate inverse square root.
128 // Compare to name-brand "1.0f / sk_float_sqrt(x)".  Should be around 10x faster on SSE, 2x on NEON.
129 static inline float sk_float_rsqrt(const float x) {
130 // We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got
131 // it at compile time.  This is going to be too fast to productively hide behind a function pointer.
132 //
133 // We do one step of Newton's method to refine the estimates in the NEON and null paths.  No
134 // refinement is faster, but very innacurate.  Two steps is more accurate, but slower than 1/sqrt.
135 #if defined(__SSE__)
136     float result;
137     _mm_store_ss(&result, _mm_rsqrt_ss(_mm_set_ss(x)));
138     return result;
139 #elif defined(SK_ARM_HAS_NEON)
140     // Get initial estimate.
141     const float32x2_t xx = vdup_n_f32(x);  // Clever readers will note we're doing everything 2x.
142     float32x2_t estimate = vrsqrte_f32(xx);
143
144     // One step of Newton's method to refine.
145     const float32x2_t estimate_sq = vmul_f32(estimate, estimate);
146     estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq));
147     return vget_lane_f32(estimate, 0);  // 1 will work fine too; the answer's in both places.
148 #else
149     // Get initial estimate.
150     int i = *SkTCast<int*>(&x);
151     i = 0x5f3759df - (i>>1);
152     float estimate = *SkTCast<float*>(&i);
153
154     // One step of Newton's method to refine.
155     const float estimate_sq = estimate*estimate;
156     estimate *= (1.5f-0.5f*x*estimate_sq);
157     return estimate;
158 #endif
159 }
160
161 #endif