michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #ifndef SkFloatingPoint_DEFINED michael@0: #define SkFloatingPoint_DEFINED michael@0: michael@0: #include "SkTypes.h" michael@0: michael@0: #include michael@0: #include michael@0: #include "SkFloatBits.h" michael@0: michael@0: // C++98 cmath std::pow seems to be the earliest portable way to get float pow. michael@0: // However, on Linux including cmath undefines isfinite. michael@0: // http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14608 michael@0: static inline float sk_float_pow(float base, float exp) { michael@0: return powf(base, exp); michael@0: } michael@0: michael@0: static inline float sk_float_copysign(float x, float y) { michael@0: int32_t xbits = SkFloat2Bits(x); michael@0: int32_t ybits = SkFloat2Bits(y); michael@0: return SkBits2Float((xbits & 0x7FFFFFFF) | (ybits & 0x80000000)); michael@0: } michael@0: michael@0: #ifdef SK_BUILD_FOR_WINCE michael@0: #define sk_float_sqrt(x) (float)::sqrt(x) michael@0: #define sk_float_sin(x) (float)::sin(x) michael@0: #define sk_float_cos(x) (float)::cos(x) michael@0: #define sk_float_tan(x) (float)::tan(x) michael@0: #define sk_float_acos(x) (float)::acos(x) michael@0: #define sk_float_asin(x) (float)::asin(x) michael@0: #define sk_float_atan2(y,x) (float)::atan2(y,x) michael@0: #define sk_float_abs(x) (float)::fabs(x) michael@0: #define sk_float_mod(x,y) (float)::fmod(x,y) michael@0: #define sk_float_exp(x) (float)::exp(x) michael@0: #define sk_float_log(x) (float)::log(x) michael@0: #define sk_float_floor(x) (float)::floor(x) michael@0: #define sk_float_ceil(x) (float)::ceil(x) michael@0: #else michael@0: #define sk_float_sqrt(x) sqrtf(x) michael@0: #define sk_float_sin(x) sinf(x) michael@0: #define sk_float_cos(x) cosf(x) michael@0: #define sk_float_tan(x) tanf(x) michael@0: #define sk_float_floor(x) floorf(x) michael@0: #define sk_float_ceil(x) ceilf(x) michael@0: #ifdef SK_BUILD_FOR_MAC michael@0: #define sk_float_acos(x) static_cast(acos(x)) michael@0: #define sk_float_asin(x) static_cast(asin(x)) michael@0: #else michael@0: #define sk_float_acos(x) acosf(x) michael@0: #define sk_float_asin(x) asinf(x) michael@0: #endif michael@0: #define sk_float_atan2(y,x) atan2f(y,x) michael@0: #define sk_float_abs(x) fabsf(x) michael@0: #define sk_float_mod(x,y) fmodf(x,y) michael@0: #define sk_float_exp(x) expf(x) michael@0: #define sk_float_log(x) logf(x) michael@0: #endif michael@0: michael@0: #ifdef SK_BUILD_FOR_WIN michael@0: #define sk_float_isfinite(x) _finite(x) michael@0: #define sk_float_isnan(x) _isnan(x) michael@0: static inline int sk_float_isinf(float x) { michael@0: int32_t bits = SkFloat2Bits(x); michael@0: return (bits << 1) == (0xFF << 24); michael@0: } michael@0: #else michael@0: #define sk_float_isfinite(x) isfinite(x) michael@0: #define sk_float_isnan(x) isnan(x) michael@0: #define sk_float_isinf(x) isinf(x) michael@0: #endif michael@0: michael@0: #define sk_double_isnan(a) sk_float_isnan(a) michael@0: michael@0: #ifdef SK_USE_FLOATBITS michael@0: #define sk_float_floor2int(x) SkFloatToIntFloor(x) michael@0: #define sk_float_round2int(x) SkFloatToIntRound(x) michael@0: #define sk_float_ceil2int(x) SkFloatToIntCeil(x) michael@0: #else michael@0: #define sk_float_floor2int(x) (int)sk_float_floor(x) michael@0: #define sk_float_round2int(x) (int)sk_float_floor((x) + 0.5f) michael@0: #define sk_float_ceil2int(x) (int)sk_float_ceil(x) michael@0: #endif michael@0: michael@0: extern const uint32_t gIEEENotANumber; michael@0: extern const uint32_t gIEEEInfinity; michael@0: extern const uint32_t gIEEENegativeInfinity; michael@0: michael@0: #define SK_FloatNaN (*SkTCast(&gIEEENotANumber)) michael@0: #define SK_FloatInfinity (*SkTCast(&gIEEEInfinity)) michael@0: #define SK_FloatNegativeInfinity (*SkTCast(&gIEEENegativeInfinity)) michael@0: michael@0: #if defined(__SSE__) michael@0: #include michael@0: #elif defined(__ARM_NEON__) michael@0: #include michael@0: #endif michael@0: michael@0: // Fast, approximate inverse square root. michael@0: // Compare to name-brand "1.0f / sk_float_sqrt(x)". Should be around 10x faster on SSE, 2x on NEON. michael@0: static inline float sk_float_rsqrt(const float x) { michael@0: // We want all this inlined, so we'll inline SIMD and just take the hit when we don't know we've got michael@0: // it at compile time. This is going to be too fast to productively hide behind a function pointer. michael@0: // michael@0: // We do one step of Newton's method to refine the estimates in the NEON and null paths. No michael@0: // refinement is faster, but very innacurate. Two steps is more accurate, but slower than 1/sqrt. michael@0: #if defined(__SSE__) michael@0: float result; michael@0: _mm_store_ss(&result, _mm_rsqrt_ss(_mm_set_ss(x))); michael@0: return result; michael@0: #elif defined(__ARM_NEON__) michael@0: // Get initial estimate. michael@0: const float32x2_t xx = vdup_n_f32(x); // Clever readers will note we're doing everything 2x. michael@0: float32x2_t estimate = vrsqrte_f32(xx); michael@0: michael@0: // One step of Newton's method to refine. michael@0: const float32x2_t estimate_sq = vmul_f32(estimate, estimate); michael@0: estimate = vmul_f32(estimate, vrsqrts_f32(xx, estimate_sq)); michael@0: return vget_lane_f32(estimate, 0); // 1 will work fine too; the answer's in both places. michael@0: #else michael@0: // Get initial estimate. michael@0: int i = *SkTCast(&x); michael@0: i = 0x5f3759df - (i>>1); michael@0: float estimate = *SkTCast(&i); michael@0: michael@0: // One step of Newton's method to refine. michael@0: const float estimate_sq = estimate*estimate; michael@0: estimate *= (1.5f-0.5f*x*estimate_sq); michael@0: return estimate; michael@0: #endif michael@0: } michael@0: michael@0: #endif