OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 return log(num) / 0.693147180559945309417232121458176568; | 109 return log(num) / 0.693147180559945309417232121458176568; |
110 } | 110 } |
111 | 111 |
112 inline float log2f(float num) | 112 inline float log2f(float num) |
113 { | 113 { |
114 // This constant is roughly M_LN2, which is not provided by default on Windo
ws and Android. | 114 // This constant is roughly M_LN2, which is not provided by default on Windo
ws and Android. |
115 return logf(num) / 0.693147180559945309417232121458176568f; | 115 return logf(num) / 0.693147180559945309417232121458176568f; |
116 } | 116 } |
117 #endif | 117 #endif |
118 | 118 |
119 #if COMPILER(MSVC) && (_MSC_VER < 1800) | 119 #if COMPILER(MSVC) |
| 120 |
| 121 // VS2013 has most of the math functions now, but we still need to work |
| 122 // around various differences in behavior of Inf. |
| 123 |
| 124 #if _MSC_VER < 1800 |
120 | 125 |
121 namespace std { | 126 namespace std { |
122 | 127 |
123 inline bool isinf(double num) { return !_finite(num) && !_isnan(num); } | 128 inline bool isinf(double num) { return !_finite(num) && !_isnan(num); } |
124 inline bool isnan(double num) { return !!_isnan(num); } | 129 inline bool isnan(double num) { return !!_isnan(num); } |
125 inline bool isfinite(double x) { return _finite(x); } | 130 inline bool isfinite(double x) { return _finite(x); } |
126 inline bool signbit(double num) { return _copysign(1.0, num) < 0; } | 131 inline bool signbit(double num) { return _copysign(1.0, num) < 0; } |
127 | 132 |
128 } // namespace std | 133 } // namespace std |
129 | 134 |
130 inline double nextafter(double x, double y) { return _nextafter(x, y); } | 135 inline double nextafter(double x, double y) { return _nextafter(x, y); } |
131 inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x +
FLT_EPSILON; } | 136 inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x +
FLT_EPSILON; } |
132 | 137 |
133 inline double copysign(double x, double y) { return _copysign(x, y); } | 138 inline double copysign(double x, double y) { return _copysign(x, y); } |
134 | 139 |
| 140 #endif // _MSC_VER |
| 141 |
135 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN inst
ead of specific values. | 142 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN inst
ead of specific values. |
136 inline double wtf_atan2(double x, double y) | 143 inline double wtf_atan2(double x, double y) |
137 { | 144 { |
138 double posInf = std::numeric_limits<double>::infinity(); | 145 double posInf = std::numeric_limits<double>::infinity(); |
139 double negInf = -std::numeric_limits<double>::infinity(); | 146 double negInf = -std::numeric_limits<double>::infinity(); |
140 double nan = std::numeric_limits<double>::quiet_NaN(); | 147 double nan = std::numeric_limits<double>::quiet_NaN(); |
141 | 148 |
142 double result = nan; | 149 double result = nan; |
143 | 150 |
144 if (x == posInf && y == posInf) | 151 if (x == posInf && y == posInf) |
(...skipping 13 matching lines...) Expand all Loading... |
158 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN
instead of x. | 165 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN
instead of x. |
159 inline double wtf_fmod(double x, double y) { return (!std::isinf(x) && std::isin
f(y)) ? x : fmod(x, y); } | 166 inline double wtf_fmod(double x, double y) { return (!std::isinf(x) && std::isin
f(y)) ? x : fmod(x, y); } |
160 | 167 |
161 // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead
of 1. | 168 // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead
of 1. |
162 inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); } | 169 inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); } |
163 | 170 |
164 #define atan2(x, y) wtf_atan2(x, y) | 171 #define atan2(x, y) wtf_atan2(x, y) |
165 #define fmod(x, y) wtf_fmod(x, y) | 172 #define fmod(x, y) wtf_fmod(x, y) |
166 #define pow(x, y) wtf_pow(x, y) | 173 #define pow(x, y) wtf_pow(x, y) |
167 | 174 |
| 175 #if _MSC_VER < 1800 |
| 176 |
168 // MSVC's math functions do not bring lrint. | 177 // MSVC's math functions do not bring lrint. |
169 inline long int lrint(double flt) | 178 inline long int lrint(double flt) |
170 { | 179 { |
171 int64_t intgr; | 180 int64_t intgr; |
172 #if CPU(X86) | 181 #if CPU(X86) |
173 __asm { | 182 __asm { |
174 fld flt | 183 fld flt |
175 fistp intgr | 184 fistp intgr |
176 }; | 185 }; |
177 #else | 186 #else |
178 ASSERT(std::isfinite(flt)); | 187 ASSERT(std::isfinite(flt)); |
179 double rounded = round(flt); | 188 double rounded = round(flt); |
180 intgr = static_cast<int64_t>(rounded); | 189 intgr = static_cast<int64_t>(rounded); |
181 // If the fractional part is exactly 0.5, we need to check whether | 190 // If the fractional part is exactly 0.5, we need to check whether |
182 // the rounded result is even. If it is not we need to add 1 to | 191 // the rounded result is even. If it is not we need to add 1 to |
183 // negative values and subtract one from positive values. | 192 // negative values and subtract one from positive values. |
184 if ((fabs(intgr - flt) == 0.5) & intgr) | 193 if ((fabs(intgr - flt) == 0.5) & intgr) |
185 intgr -= ((intgr >> 62) | 1); // 1 with the sign of result, i.e. -1 or 1
. | 194 intgr -= ((intgr >> 62) | 1); // 1 with the sign of result, i.e. -1 or 1
. |
186 #endif | 195 #endif |
187 return static_cast<long int>(intgr); | 196 return static_cast<long int>(intgr); |
188 } | 197 } |
189 | 198 |
| 199 #endif // _MSC_VER |
| 200 |
190 #endif // COMPILER(MSVC) | 201 #endif // COMPILER(MSVC) |
191 | 202 |
192 inline double deg2rad(double d) { return d * piDouble / 180.0; } | 203 inline double deg2rad(double d) { return d * piDouble / 180.0; } |
193 inline double rad2deg(double r) { return r * 180.0 / piDouble; } | 204 inline double rad2deg(double r) { return r * 180.0 / piDouble; } |
194 inline double deg2grad(double d) { return d * 400.0 / 360.0; } | 205 inline double deg2grad(double d) { return d * 400.0 / 360.0; } |
195 inline double grad2deg(double g) { return g * 360.0 / 400.0; } | 206 inline double grad2deg(double g) { return g * 360.0 / 400.0; } |
196 inline double turn2deg(double t) { return t * 360.0; } | 207 inline double turn2deg(double t) { return t * 360.0; } |
197 inline double deg2turn(double d) { return d / 360.0; } | 208 inline double deg2turn(double d) { return d / 360.0; } |
198 inline double rad2grad(double r) { return r * 200.0 / piDouble; } | 209 inline double rad2grad(double r) { return r * 200.0 / piDouble; } |
199 inline double grad2rad(double g) { return g * piDouble / 200.0; } | 210 inline double grad2rad(double g) { return g * piDouble / 200.0; } |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 if (i >> 2) | 320 if (i >> 2) |
310 log2 += 2, i >>= 2; | 321 log2 += 2, i >>= 2; |
311 if (i >> 1) | 322 if (i >> 1) |
312 log2 += 1; | 323 log2 += 1; |
313 return log2; | 324 return log2; |
314 } | 325 } |
315 | 326 |
316 } // namespace WTF | 327 } // namespace WTF |
317 | 328 |
318 #endif // #ifndef WTF_MathExtras_h | 329 #endif // #ifndef WTF_MathExtras_h |
OLD | NEW |