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#ifdef SK_CAN_USE_FLOAT
16
17#include <math.h>
18#include <float.h>
19#include "SkFloatBits.h"
20
21// If math.h had powf(float, float), I could remove this wrapper
22static inline float sk_float_pow(float base, float exp) {
23    return static_cast<float>(pow(static_cast<double>(base),
24                                  static_cast<double>(exp)));
25}
26
27static inline float sk_float_copysign(float x, float y) {
28    int32_t xbits = SkFloat2Bits(x);
29    int32_t ybits = SkFloat2Bits(y);
30    return SkBits2Float((xbits & 0x7FFFFFFF) | (ybits & 0x80000000));
31}
32
33#ifdef SK_BUILD_FOR_WINCE
34    #define sk_float_sqrt(x)        (float)::sqrt(x)
35    #define sk_float_sin(x)         (float)::sin(x)
36    #define sk_float_cos(x)         (float)::cos(x)
37    #define sk_float_tan(x)         (float)::tan(x)
38    #define sk_float_acos(x)        (float)::acos(x)
39    #define sk_float_asin(x)        (float)::asin(x)
40    #define sk_float_atan2(y,x)     (float)::atan2(y,x)
41    #define sk_float_abs(x)         (float)::fabs(x)
42    #define sk_float_mod(x,y)       (float)::fmod(x,y)
43    #define sk_float_exp(x)         (float)::exp(x)
44    #define sk_float_log(x)         (float)::log(x)
45    #define sk_float_floor(x)       (float)::floor(x)
46    #define sk_float_ceil(x)        (float)::ceil(x)
47#else
48    #define sk_float_sqrt(x)        sqrtf(x)
49    #define sk_float_sin(x)         sinf(x)
50    #define sk_float_cos(x)         cosf(x)
51    #define sk_float_tan(x)         tanf(x)
52    #define sk_float_floor(x)       floorf(x)
53    #define sk_float_ceil(x)        ceilf(x)
54#ifdef SK_BUILD_FOR_MAC
55    #define sk_float_acos(x)        static_cast<float>(acos(x))
56    #define sk_float_asin(x)        static_cast<float>(asin(x))
57#else
58    #define sk_float_acos(x)        acosf(x)
59    #define sk_float_asin(x)        asinf(x)
60#endif
61    #define sk_float_atan2(y,x)     atan2f(y,x)
62    #define sk_float_abs(x)         fabsf(x)
63    #define sk_float_mod(x,y)       fmodf(x,y)
64    #define sk_float_exp(x)         expf(x)
65    #define sk_float_log(x)         logf(x)
66#endif
67
68#ifdef SK_BUILD_FOR_WIN
69    #define sk_float_isfinite(x)    _finite(x)
70    #define sk_float_isnan(x)       _isnan(x)
71    static inline int sk_float_isinf(float x) {
72        int32_t bits = SkFloat2Bits(x);
73        return (bits << 1) == (0xFF << 24);
74    }
75#else
76    #define sk_float_isfinite(x)    isfinite(x)
77    #define sk_float_isnan(x)       isnan(x)
78    #define sk_float_isinf(x)       isinf(x)
79#endif
80
81#ifdef SK_USE_FLOATBITS
82    #define sk_float_floor2int(x)   SkFloatToIntFloor(x)
83    #define sk_float_round2int(x)   SkFloatToIntRound(x)
84    #define sk_float_ceil2int(x)    SkFloatToIntCeil(x)
85#else
86    #define sk_float_floor2int(x)   (int)sk_float_floor(x)
87    #define sk_float_round2int(x)   (int)sk_float_floor((x) + 0.5f)
88    #define sk_float_ceil2int(x)    (int)sk_float_ceil(x)
89#endif
90
91#endif
92#endif
93