SkFloatingPoint.h revision 50c79d886bf435d3a9cad056885370e2c3f526ad
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#include "SkFloatBits.h"
18
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
22static inline float sk_float_pow(float base, float exp) {
23    return powf(base, exp);
24}
25
26static 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
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
54    #define sk_float_acos(x)        static_cast<float>(acos(x))
55    #define sk_float_asin(x)        static_cast<float>(asin(x))
56#else
57    #define sk_float_acos(x)        acosf(x)
58    #define sk_float_asin(x)        asinf(x)
59#endif
60    #define sk_float_atan2(y,x)     atan2f(y,x)
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)
65#endif
66
67#ifdef SK_BUILD_FOR_WIN
68    #define sk_float_isfinite(x)    _finite(x)
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    }
74#else
75    #define sk_float_isfinite(x)    isfinite(x)
76    #define sk_float_isnan(x)       isnan(x)
77    #define sk_float_isinf(x)       isinf(x)
78#endif
79
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
90extern const uint32_t gIEEENotANumber;
91extern const uint32_t gIEEEInfinity;
92extern const uint32_t gIEEENegativeInfinity;
93
94#define SK_FloatNaN                 (*reinterpret_cast<const float*>(&gIEEENotANumber))
95#define SK_FloatInfinity            (*reinterpret_cast<const float*>(&gIEEEInfinity))
96#define SK_FloatNegativeInfinity    (*reinterpret_cast<const float*>(&gIEEENegativeInfinity))
97#endif
98