1 /*
2 * Copyright (C) 2006, 2007 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #ifndef WTF_MathExtras_h
27 #define WTF_MathExtras_h
28
29 #include <math.h>
30
31 #if PLATFORM(WIN_OS)
32
33 #include "kjs/JSImmediate.h"
34 #include <limits>
35
36 #if HAVE(FLOAT_H)
37 #include <float.h>
38 #endif
39
40 #endif
41
42 #ifndef M_PI
43 const double piDouble = 3.14159265358979323846;
44 const float piFloat = 3.14159265358979323846f;
45 #else
46 const double piDouble = M_PI;
47 const float piFloat = static_cast<float>(M_PI);
48 #endif
49
50 #ifndef M_PI_4
51 const double piOverFourDouble = 0.785398163397448309616;
52 const float piOverFourFloat = 0.785398163397448309616f;
53 #else
54 const double piOverFourDouble = M_PI_4;
55 const float piOverFourFloat = static_cast<float>(M_PI_4);
56 #endif // !BUILDING_KDE__
57
58 #if defined(WTF_COMPILER_MSVC)
59
60 #ifndef BUILDING_KDE__
isinf(double num)61 inline bool isinf(double num)
62 {
63 return !_finite(num) && !_isnan(num);
64 }
isnan(double num)65 inline bool isnan(double num)
66 {
67 return _isnan(num);
68 }
lround(double num)69 inline long lround(double num)
70 {
71 return num > 0 ? num + 0.5 : ceil(num - 0.5);
72 }
lroundf(float num)73 inline long lroundf(float num)
74 {
75 return num > 0 ? num + 0.5f : ceilf(num - 0.5f);
76 }
round(double num)77 inline double round(double num)
78 {
79 return num > 0 ? floor(num + 0.5) : ceil(num - 0.5);
80 }
roundf(float num)81 inline float roundf(float num)
82 {
83 return num > 0 ? floorf(num + 0.5f) : ceilf(num - 0.5f);
84 }
signbit(double num)85 inline bool signbit(double num)
86 {
87 return _copysign(1.0, num) < 0;
88 }
89 #endif
90
91 #if _MSC_VER < 1920
92 #ifndef BUILDING_KDE__
93 // FIXME: where to get std::numeric_limits from?
94 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values.
wtf_atan2(double x,double y)95 inline double wtf_atan2(double x, double y)
96 {
97 static double posInf = std::numeric_limits<double>::infinity();
98 static double negInf = -std::numeric_limits<double>::infinity();
99
100 double result = KJS::NaN;
101
102 if (x == posInf && y == posInf) {
103 result = piOverFourDouble;
104 } else if (x == posInf && y == negInf) {
105 result = 3 * piOverFourDouble;
106 } else if (x == negInf && y == posInf) {
107 result = -piOverFourDouble;
108 } else if (x == negInf && y == negInf) {
109 result = -3 * piOverFourDouble;
110 } else {
111 result = ::atan2(x, y);
112 }
113
114 return result;
115 }
116 #else // !BUILDING_KDE__
117
118 #define wtf_atan2(x, y) atan2(x, y)
119
120 #endif // !BUILDING_KDE__
121
122 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
wtf_fmod(double x,double y)123 inline double wtf_fmod(double x, double y)
124 {
125 return (!isinf(x) && isinf(y)) ? x : fmod(x, y);
126 }
127
128 #define fmod(x, y) wtf_fmod(x, y)
129
130 #define atan2(x, y) wtf_atan2(x, y)
131 #endif // _MSC_VER < 1920
132 #endif // #if defined(WTF_COMPILER_MSVC)
133
deg2rad(double d)134 inline double deg2rad(double d)
135 {
136 return d * piDouble / 180.0;
137 }
rad2deg(double r)138 inline double rad2deg(double r)
139 {
140 return r * 180.0 / piDouble;
141 }
deg2grad(double d)142 inline double deg2grad(double d)
143 {
144 return d * 400.0 / 360.0;
145 }
grad2deg(double g)146 inline double grad2deg(double g)
147 {
148 return g * 360.0 / 400.0;
149 }
rad2grad(double r)150 inline double rad2grad(double r)
151 {
152 return r * 200.0 / piDouble;
153 }
grad2rad(double g)154 inline double grad2rad(double g)
155 {
156 return g * piDouble / 200.0;
157 }
158
deg2rad(float d)159 inline float deg2rad(float d)
160 {
161 return d * piFloat / 180.0f;
162 }
rad2deg(float r)163 inline float rad2deg(float r)
164 {
165 return r * 180.0f / piFloat;
166 }
deg2grad(float d)167 inline float deg2grad(float d)
168 {
169 return d * 400.0f / 360.0f;
170 }
grad2deg(float g)171 inline float grad2deg(float g)
172 {
173 return g * 360.0f / 400.0f;
174 }
rad2grad(float r)175 inline float rad2grad(float r)
176 {
177 return r * 200.0f / piFloat;
178 }
grad2rad(float g)179 inline float grad2rad(float g)
180 {
181 return g * piFloat / 200.0f;
182 }
183
184 #endif // #ifndef WTF_MathExtras_h
185