1/*
2 * Copyright (C) 2006 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SkFloatingPoint_DEFINED
18#define SkFloatingPoint_DEFINED
19
20#include "SkTypes.h"
21
22#ifdef SK_CAN_USE_FLOAT
23
24#include <math.h>
25#include <float.h>
26#include "SkFloatBits.h"
27
28// If math.h had powf(float, float), I could remove this wrapper
29static inline float sk_float_pow(float base, float exp) {
30    return static_cast<float>(pow(static_cast<double>(base),
31                                  static_cast<double>(exp)));
32}
33
34static inline float sk_float_copysign(float x, float y) {
35    int32_t xbits = SkFloat2Bits(x);
36    int32_t ybits = SkFloat2Bits(y);
37    return SkBits2Float((xbits & 0x7FFFFFFF) | (ybits & 0x80000000));
38}
39
40#ifdef SK_BUILD_FOR_WINCE
41    #define sk_float_sqrt(x)        (float)::sqrt(x)
42    #define sk_float_sin(x)         (float)::sin(x)
43    #define sk_float_cos(x)         (float)::cos(x)
44    #define sk_float_tan(x)         (float)::tan(x)
45    #define sk_float_acos(x)        (float)::acos(x)
46    #define sk_float_asin(x)        (float)::asin(x)
47    #define sk_float_atan2(y,x)     (float)::atan2(y,x)
48    #define sk_float_abs(x)         (float)::fabs(x)
49    #define sk_float_mod(x,y)       (float)::fmod(x,y)
50    #define sk_float_exp(x)         (float)::exp(x)
51    #define sk_float_log(x)         (float)::log(x)
52    #define sk_float_floor(x)       (float)::floor(x)
53    #define sk_float_ceil(x)        (float)::ceil(x)
54#else
55    #define sk_float_sqrt(x)        sqrtf(x)
56    #define sk_float_sin(x)         sinf(x)
57    #define sk_float_cos(x)         cosf(x)
58    #define sk_float_tan(x)         tanf(x)
59    #define sk_float_floor(x)       floorf(x)
60    #define sk_float_ceil(x)        ceilf(x)
61#ifdef SK_BUILD_FOR_MAC
62    #define sk_float_acos(x)        static_cast<float>(acos(x))
63    #define sk_float_asin(x)        static_cast<float>(asin(x))
64#else
65    #define sk_float_acos(x)        acosf(x)
66    #define sk_float_asin(x)        asinf(x)
67#endif
68    #define sk_float_atan2(y,x) atan2f(y,x)
69    #define sk_float_abs(x)         fabsf(x)
70    #define sk_float_mod(x,y)       fmodf(x,y)
71    #define sk_float_exp(x)         expf(x)
72    #define sk_float_log(x)         logf(x)
73    #define sk_float_isNaN(x)       _isnan(x)
74#endif
75
76#ifdef SK_USE_FLOATBITS
77    #define sk_float_floor2int(x)   SkFloatToIntFloor(x)
78    #define sk_float_round2int(x)   SkFloatToIntRound(x)
79    #define sk_float_ceil2int(x)    SkFloatToIntCeil(x)
80#else
81    #define sk_float_floor2int(x)   (int)sk_float_floor(x)
82    #define sk_float_round2int(x)   (int)sk_float_floor((x) + 0.5f)
83    #define sk_float_ceil2int(x)    (int)sk_float_ceil(x)
84#endif
85
86#endif
87#endif
88