Debian Codegraph @

src/3rdparty/javascriptcore/JavaScriptCore/wtf/MathExtras.h

raw symbols SCIP
1 /*
2 * Copyright (C) 2006, 2007, 2008 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 <float.h>
30 #include <limits>
31 #include <cmath>
32 #include <stdlib.h>
33
34 #if OS(SOLARIS)
35 #include <ieeefp.h>
36 #endif
37
38 #if OS(OPENBSD)
39 #include <sys/types.h>
40 #include <machine/ieee.h>
41 #endif
42
43 #if (OS(QNX) && defined(_CPPLIB_VER)) || COMPILER(INTEL)
44 // FIXME: Look into a way to have cmath import its functions into both the standard and global
45 // namespace. For now, we include math.h since the QNX cmath header only imports its functions
46 // into the standard namespace.
47 #include <math.h>
48 // These macros from math.h conflict with the real functions in the std namespace.
49 #undef signbit
50 #undef isnan
51 #undef isinf
52 #undef isfinite
53 #endif
54
55 #ifndef M_PI
56 const double piDouble = 3.14159265358979323846;
57 const float piFloat = 3.14159265358979323846f;
58 #else
59 const double piDouble = M_PI;
60 const float piFloat = static_cast<float>(M_PI);
61 #endif
62
63 #ifndef M_PI_4
64 const double piOverFourDouble = 0.785398163397448309616;
65 const float piOverFourFloat = 0.785398163397448309616f;
66 #else
67 const double piOverFourDouble = M_PI_4;
68 const float piOverFourFloat = static_cast<float>(M_PI_4);
69 #endif
70
71 #if OS(DARWIN)
72
73 // Work around a bug in the Mac OS X libc where ceil(-0.1) return +0.
74 inline double wtf_ceil(double x) { return copysign(ceil(x), x); }
75
76 #define ceil(x) wtf_ceil(x)
77
78 #endif
79
80 #if OS(SOLARIS) && __cplusplus < 201103L
81
82 namespace std {
83
84 #ifndef isfinite
85 inline bool isfinite(double x) { return finite(x) && !isnand(x); }
86 #endif
87 #ifndef signbit
88 inline bool signbit(double x) { return copysign(1.0, x) < 0; }
89 #endif
90 #ifndef isinf
91 inline bool isinf(double x) { return !finite(x) && !isnand(x); }
92 #endif
93
94 } // namespace std
95
96 #endif
97
98 #if OS(OPENBSD) && __cplusplus < 201103L
99
100 namespace std {
101
102 #ifndef isfinite
103 inline bool isfinite(double x) { return finite(x); }
104 #endif
105 #ifndef signbit
106 inline bool signbit(double x) { struct ieee_double *p = (struct ieee_double *)&x; return p->dbl_sign; }
107 #endif
108
109 } // namespace std
110
111 #endif
112
113 #if (COMPILER(MSVC) && _MSC_VER < 1800) || COMPILER(RVCT)
114
115 // We must not do 'num + 0.5' or 'num - 0.5' because they can cause precision loss.
116 static double round(double num)
117 {
118 double integer = ceil(num);
119 if (num > 0)
120 return integer - num > 0.5 ? integer - 1.0 : integer;
121 return integer - num >= 0.5 ? integer - 1.0 : integer;
122 }
123 static float roundf(float num)
124 {
125 float integer = ceilf(num);
126 if (num > 0)
127 return integer - num > 0.5f ? integer - 1.0f : integer;
128 return integer - num >= 0.5f ? integer - 1.0f : integer;
129 }
130 inline long long llround(double num) { return static_cast<long long>(round(num)); }
131 inline long long llroundf(float num) { return static_cast<long long>(roundf(num)); }
132 inline long lround(double num) { return static_cast<long>(round(num)); }
133 inline long lroundf(float num) { return static_cast<long>(roundf(num)); }
134 inline double trunc(double num) { return num > 0 ? floor(num) : ceil(num); }
135
136 #endif
137
138 #if COMPILER(MSVC)
139
140 namespace std {
141
142 inline bool isinf(double num) { return !_finite(num) && !_isnan(num); }
143 inline bool isnan(double num) { return !!_isnan(num); }
144 inline bool isfinite(double x) { return _finite(x); }
145 #if _MSC_VER < 1800
146 inline bool signbit(double num) { return _copysign(1.0, num) < 0; }
147 #endif
148
149 } // namespace std
150
151 inline double nextafter(double x, double y) { return _nextafter(x, y); }
152 inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x + FLT_EPSILON; }
153
154 inline double copysign(double x, double y) { return _copysign(x, y); }
155
156 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values.
157 inline double wtf_atan2(double x, double y)
158 {
159 double posInf = std::numeric_limits<double>::infinity();
160 double negInf = -std::numeric_limits<double>::infinity();
161 double nan = std::numeric_limits<double>::quiet_NaN();
162
163 double result = nan;
164
165 if (x == posInf && y == posInf)
166 result = piOverFourDouble;
167 else if (x == posInf && y == negInf)
168 result = 3 * piOverFourDouble;
169 else if (x == negInf && y == posInf)
170 result = -piOverFourDouble;
171 else if (x == negInf && y == negInf)
172 result = -3 * piOverFourDouble;
173 else
174 result = ::atan2(x, y);
175
176 return result;
177 }
178
179 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x.
180 extern "C" inline double wtf_fmod(double x, double y) { return (!std::isinf(x) && std::isinf(y)) ? x : fmod(x, y); }
181
182 // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead of 1.
183 extern "C" inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); }
184
185 #define atan2(x, y) wtf_atan2(x, y)
186 #define fmod(x, y) wtf_fmod(x, y)
187 #define pow(x, y) wtf_pow(x, y)
188
189 #endif // COMPILER(MSVC)
190
191 inline double deg2rad(double d) { return d * piDouble / 180.0; }
192 inline double rad2deg(double r) { return r * 180.0 / piDouble; }
193 inline double deg2grad(double d) { return d * 400.0 / 360.0; }
194 inline double grad2deg(double g) { return g * 360.0 / 400.0; }
195 inline double turn2deg(double t) { return t * 360.0; }
196 inline double deg2turn(double d) { return d / 360.0; }
197 inline double rad2grad(double r) { return r * 200.0 / piDouble; }
198 inline double grad2rad(double g) { return g * piDouble / 200.0; }
199
200 inline float deg2rad(float d) { return d * piFloat / 180.0f; }
201 inline float rad2deg(float r) { return r * 180.0f / piFloat; }
202 inline float deg2grad(float d) { return d * 400.0f / 360.0f; }
203 inline float grad2deg(float g) { return g * 360.0f / 400.0f; }
204 inline float turn2deg(float t) { return t * 360.0f; }
205 inline float deg2turn(float d) { return d / 360.0f; }
206 inline float rad2grad(float r) { return r * 200.0f / piFloat; }
207 inline float grad2rad(float g) { return g * piFloat / 200.0f; }
208
209 #if COMPILER(MINGW64) && (!defined(__MINGW64_VERSION_RC) || __MINGW64_VERSION_RC < 1)
210 inline double wtf_pow(double x, double y)
211 {
212 // MinGW-w64 has a custom implementation for pow.
213 // This handles certain special cases that are different.
214 if ((x == 0.0 || std::isinf(x)) && std::isfinite(y)) {
215 double f;
216 if (modf(y, &f) != 0.0)
217 return ((x == 0.0) ^ (y > 0.0)) ? std::numeric_limits<double>::infinity() : 0.0;
218 }
219
220 if (x == 2.0) {
221 int yInt = static_cast<int>(y);
222 if (y == yInt)
223 return ldexp(1.0, yInt);
224 }
225
226 return pow(x, y);
227 }
228 #define pow(x, y) wtf_pow(x, y)
229 #endif // COMPILER(MINGW64) && (!defined(__MINGW64_VERSION_RC) || __MINGW64_VERSION_RC < 1)
230
231 #endif // #ifndef WTF_MathExtras_h