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