Home | History | Annotate | Download | only in private
      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 #include "SkSafe_math.h"
     15 #include <float.h>
     16 
     17 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
     18     #include <xmmintrin.h>
     19 #elif defined(SK_ARM_HAS_NEON)
     20     #include <arm_neon.h>
     21 #endif
     22 
     23 // For _POSIX_VERSION
     24 #if defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
     25 #include <unistd.h>
     26 #endif
     27 
     28 #include "SkFloatBits.h"
     29 
     30 // C++98 cmath std::pow seems to be the earliest portable way to get float pow.
     31 // However, on Linux including cmath undefines isfinite.
     32 // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608
     33 static inline float sk_float_pow(float base, float exp) {
     34     return powf(base, exp);
     35 }
     36 
     37 #define sk_float_sqrt(x)        sqrtf(x)
     38 #define sk_float_sin(x)         sinf(x)
     39 #define sk_float_cos(x)         cosf(x)
     40 #define sk_float_tan(x)         tanf(x)
     41 #define sk_float_floor(x)       floorf(x)
     42 #define sk_float_ceil(x)        ceilf(x)
     43 #define sk_float_trunc(x)       truncf(x)
     44 #ifdef SK_BUILD_FOR_MAC
     45 #    define sk_float_acos(x)    static_cast<float>(acos(x))
     46 #    define sk_float_asin(x)    static_cast<float>(asin(x))
     47 #else
     48 #    define sk_float_acos(x)    acosf(x)
     49 #    define sk_float_asin(x)    asinf(x)
     50 #endif
     51 #define sk_float_atan2(y,x)     atan2f(y,x)
     52 #define sk_float_abs(x)         fabsf(x)
     53 #define sk_float_copysign(x, y) copysignf(x, y)
     54 #define sk_float_mod(x,y)       fmodf(x,y)
     55 #define sk_float_exp(x)         expf(x)
     56 #define sk_float_log(x)         logf(x)
     57 
     58 #define sk_float_round(x) sk_float_floor((x) + 0.5f)
     59 
     60 // can't find log2f on android, but maybe that just a tool bug?
     61 #ifdef SK_BUILD_FOR_ANDROID
     62     static inline float sk_float_log2(float x) {
     63         const double inv_ln_2 = 1.44269504088896;
     64         return (float)(log(x) * inv_ln_2);
     65     }
     66 #else
     67     #define sk_float_log2(x)        log2f(x)
     68 #endif
     69 
     70 #ifdef SK_BUILD_FOR_WIN
     71     #define sk_float_isfinite(x)    _finite(x)
     72     #define sk_float_isnan(x)       _isnan(x)
     73     static inline int sk_float_isinf(float x) {
     74         int32_t bits = SkFloat2Bits(x);
     75         return (bits << 1) == (0xFF << 24);
     76     }
     77 #else
     78     #define sk_float_isfinite(x)    isfinite(x)
     79     #define sk_float_isnan(x)       isnan(x)
     80     #define sk_float_isinf(x)       isinf(x)
     81 #endif
     82 
     83 #define sk_double_isnan(a)          sk_float_isnan(a)
     84 
     85 #ifdef SK_USE_FLOATBITS
     86     #define sk_float_floor2int(x)   SkFloatToIntFloor(x)
     87     #define sk_float_round2int(x)   SkFloatToIntRound(x)
     88     #define sk_float_ceil2int(x)    SkFloatToIntCeil(x)
     89 #else
     90     #define sk_float_floor2int(x)   (int)sk_float_floor(x)
     91     #define sk_float_round2int(x)   (int)sk_float_floor((x) + 0.5f)
     92     #define sk_float_ceil2int(x)    (int)sk_float_ceil(x)
     93 #endif
     94 
     95 #define sk_double_floor(x)          floor(x)
     96 #define sk_double_round(x)          floor((x) + 0.5)
     97 #define sk_double_ceil(x)           ceil(x)
     98 #define sk_double_floor2int(x)      (int)floor(x)
     99 #define sk_double_round2int(x)      (int)floor((x) + 0.5f)
    100 #define sk_double_ceil2int(x)       (int)ceil(x)
    101 
    102 static const uint32_t kIEEENotANumber = 0x7fffffff;
    103 #define SK_FloatNaN                 (*SkTCast<const float*>(&kIEEENotANumber))
    104 #define SK_FloatInfinity            (+(float)INFINITY)
    105 #define SK_FloatNegativeInfinity    (-(float)INFINITY)
    106 
    107 static inline float sk_float_rsqrt_portable(float x) {
    108     // Get initial estimate.
    109     int i;
    110     memcpy(&i, &x, 4);
    111     i = 0x5F1FFFF9 - (i>>1);
    112     float estimate;
    113     memcpy(&estimate, &i, 4);
    114 
    115     // One step of Newton's method to refine.
    116     const float estimate_sq = estimate*estimate;
    117     estimate *= 0.703952253f*(2.38924456f-x*estimate_sq);
    118     return estimate;
    119 }
    120 
    121 // Fast, approximate inverse square root.
    122 // Compare to name-brand "1.0f / sk_float_sqrt(x)".  Should be around 10x faster on SSE, 2x on NEON.
    123 static inline float sk_float_rsqrt(float x) {
    124 // We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got
    125 // it at compile time.  This is going to be too fast to productively hide behind a function pointer.
    126 //
    127 // We do one step of Newton's method to refine the estimates in the NEON and portable paths.  No
    128 // refinement is faster, but very innacurate.  Two steps is more accurate, but slower than 1/sqrt.
    129 //
    130 // Optimized constants in the portable path courtesy of http://rrrola.wz.cz/inv_sqrt.html
    131 #if SK_CPU_SSE_LEVEL >= SK_CPU_SSE_LEVEL_SSE1
    132     return _mm_cvtss_f32(_mm_rsqrt_ss(_mm_set_ss(x)));
    133 #elif defined(SK_ARM_HAS_NEON)
    134     // Get initial estimate.
    135     const float32x2_t xx = vdup_n_f32(x);  // Clever readers will note we're doing everything 2x.
    136     float32x2_t estimate = vrsqrte_f32(xx);
    137 
    138     // One step of Newton's method to refine.
    139     const float32x2_t estimate_sq = vmul_f32(estimate, estimate);
    140     estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq));
    141     return vget_lane_f32(estimate, 0);  // 1 will work fine too; the answer's in both places.
    142 #else
    143     return sk_float_rsqrt_portable(x);
    144 #endif
    145 }
    146 
    147 // This is the number of significant digits we can print in a string such that when we read that
    148 // string back we get the floating point number we expect.  The minimum value C requires is 6, but
    149 // most compilers support 9
    150 #ifdef FLT_DECIMAL_DIG
    151 #define SK_FLT_DECIMAL_DIG FLT_DECIMAL_DIG
    152 #else
    153 #define SK_FLT_DECIMAL_DIG 9
    154 #endif
    155 
    156 #endif
    157