blob: 44a3eef98dc8c0272a324494e4ea0153dd4f7972 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
reed@android.com8a1c16f2008-12-17 15:59:43 +00002/*
epoger@google.comec3ed6a2011-07-28 14:26:00 +00003 * Copyright 2006 The Android Open Source Project
reed@android.com8a1c16f2008-12-17 15:59:43 +00004 *
epoger@google.comec3ed6a2011-07-28 14:26:00 +00005 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
reed@android.com8a1c16f2008-12-17 15:59:43 +00007 */
8
epoger@google.comec3ed6a2011-07-28 14:26:00 +00009
reed@android.com8a1c16f2008-12-17 15:59:43 +000010#ifndef SkFloatingPoint_DEFINED
11#define SkFloatingPoint_DEFINED
12
13#include "SkTypes.h"
14
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#include <math.h>
16#include <float.h>
17#include "SkFloatBits.h"
18
bungeman@google.com0567f222012-07-25 17:00:47 +000019// 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.com9c970452009-04-03 14:26:10 +000022static inline float sk_float_pow(float base, float exp) {
bungeman@google.com0567f222012-07-25 17:00:47 +000023 return powf(base, exp);
reed@android.com9c970452009-04-03 14:26:10 +000024}
25
reed@android.comeebf5cb2010-02-09 18:30:59 +000026static 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.com8a1c16f2008-12-17 15:59:43 +000032#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.comfc25abd2009-01-15 14:38:33 +000054 #define sk_float_acos(x) static_cast<float>(acos(x))
55 #define sk_float_asin(x) static_cast<float>(asin(x))
reed@android.com8a1c16f2008-12-17 15:59:43 +000056#else
57 #define sk_float_acos(x) acosf(x)
58 #define sk_float_asin(x) asinf(x)
59#endif
reed@google.com61873a52011-12-05 21:47:25 +000060 #define sk_float_atan2(y,x) atan2f(y,x)
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 #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.com8a1c16f2008-12-17 15:59:43 +000065#endif
66
reed@google.com61873a52011-12-05 21:47:25 +000067#ifdef SK_BUILD_FOR_WIN
68 #define sk_float_isfinite(x) _finite(x)
reed@google.com5ae777d2011-12-06 20:18:05 +000069 #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.com61873a52011-12-05 21:47:25 +000074#else
75 #define sk_float_isfinite(x) isfinite(x)
reed@google.com5ae777d2011-12-06 20:18:05 +000076 #define sk_float_isnan(x) isnan(x)
77 #define sk_float_isinf(x) isinf(x)
reed@google.com61873a52011-12-05 21:47:25 +000078#endif
79
caryclark@google.com3b97af52013-04-23 11:56:44 +000080#define sk_double_isnan(a) sk_float_isnan(a)
81
reed@android.com8a1c16f2008-12-17 15:59:43 +000082#ifdef SK_USE_FLOATBITS
83 #define sk_float_floor2int(x) SkFloatToIntFloor(x)
84 #define sk_float_round2int(x) SkFloatToIntRound(x)
85 #define sk_float_ceil2int(x) SkFloatToIntCeil(x)
86#else
87 #define sk_float_floor2int(x) (int)sk_float_floor(x)
88 #define sk_float_round2int(x) (int)sk_float_floor((x) + 0.5f)
89 #define sk_float_ceil2int(x) (int)sk_float_ceil(x)
90#endif
91
bsalomon@google.com92b6a942012-11-02 19:50:26 +000092extern const uint32_t gIEEENotANumber;
93extern const uint32_t gIEEEInfinity;
bsalomon@google.com50c79d82013-01-08 20:31:53 +000094extern const uint32_t gIEEENegativeInfinity;
bsalomon@google.com92b6a942012-11-02 19:50:26 +000095
djsollen@google.comefbe8e92013-02-07 18:58:35 +000096#define SK_FloatNaN (*SkTCast<const float*>(&gIEEENotANumber))
97#define SK_FloatInfinity (*SkTCast<const float*>(&gIEEEInfinity))
98#define SK_FloatNegativeInfinity (*SkTCast<const float*>(&gIEEENegativeInfinity))
reed@android.com8a1c16f2008-12-17 15:59:43 +000099#endif