epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2006 The Android Open Source Project |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 4 | * |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 10 | #ifndef SkFloatingPoint_DEFINED |
| 11 | #define SkFloatingPoint_DEFINED |
| 12 | |
| 13 | #include "SkTypes.h" |
| 14 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #include <math.h> |
| 16 | #include <float.h> |
| 17 | #include "SkFloatBits.h" |
| 18 | |
bungeman@google.com | 0567f22 | 2012-07-25 17:00:47 +0000 | [diff] [blame] | 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 |
reed@android.com | 9c97045 | 2009-04-03 14:26:10 +0000 | [diff] [blame] | 22 | static inline float sk_float_pow(float base, float exp) { |
bungeman@google.com | 0567f22 | 2012-07-25 17:00:47 +0000 | [diff] [blame] | 23 | return powf(base, exp); |
reed@android.com | 9c97045 | 2009-04-03 14:26:10 +0000 | [diff] [blame] | 24 | } |
| 25 | |
reed@android.com | eebf5cb | 2010-02-09 18:30:59 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 |
reed@android.com | fc25abd | 2009-01-15 14:38:33 +0000 | [diff] [blame] | 54 | #define sk_float_acos(x) static_cast<float>(acos(x)) |
| 55 | #define sk_float_asin(x) static_cast<float>(asin(x)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | #else |
| 57 | #define sk_float_acos(x) acosf(x) |
| 58 | #define sk_float_asin(x) asinf(x) |
| 59 | #endif |
reed@google.com | 61873a5 | 2011-12-05 21:47:25 +0000 | [diff] [blame] | 60 | #define sk_float_atan2(y,x) atan2f(y,x) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | #endif |
| 66 | |
reed@google.com | 61873a5 | 2011-12-05 21:47:25 +0000 | [diff] [blame] | 67 | #ifdef SK_BUILD_FOR_WIN |
| 68 | #define sk_float_isfinite(x) _finite(x) |
reed@google.com | 5ae777d | 2011-12-06 20:18:05 +0000 | [diff] [blame] | 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 | } |
reed@google.com | 61873a5 | 2011-12-05 21:47:25 +0000 | [diff] [blame] | 74 | #else |
| 75 | #define sk_float_isfinite(x) isfinite(x) |
reed@google.com | 5ae777d | 2011-12-06 20:18:05 +0000 | [diff] [blame] | 76 | #define sk_float_isnan(x) isnan(x) |
| 77 | #define sk_float_isinf(x) isinf(x) |
reed@google.com | 61873a5 | 2011-12-05 21:47:25 +0000 | [diff] [blame] | 78 | #endif |
| 79 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | #ifdef SK_USE_FLOATBITS |
| 81 | #define sk_float_floor2int(x) SkFloatToIntFloor(x) |
| 82 | #define sk_float_round2int(x) SkFloatToIntRound(x) |
| 83 | #define sk_float_ceil2int(x) SkFloatToIntCeil(x) |
| 84 | #else |
| 85 | #define sk_float_floor2int(x) (int)sk_float_floor(x) |
| 86 | #define sk_float_round2int(x) (int)sk_float_floor((x) + 0.5f) |
| 87 | #define sk_float_ceil2int(x) (int)sk_float_ceil(x) |
| 88 | #endif |
| 89 | |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 90 | extern const uint32_t gIEEENotANumber; |
| 91 | extern const uint32_t gIEEEInfinity; |
bsalomon@google.com | 50c79d8 | 2013-01-08 20:31:53 +0000 | [diff] [blame] | 92 | extern const uint32_t gIEEENegativeInfinity; |
bsalomon@google.com | 92b6a94 | 2012-11-02 19:50:26 +0000 | [diff] [blame] | 93 | |
djsollen@google.com | efbe8e9 | 2013-02-07 18:58:35 +0000 | [diff] [blame^] | 94 | #define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber)) |
| 95 | #define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity)) |
| 96 | #define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfinity)) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | #endif |