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 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 inline bool isfinite(double x) { return finite(x); } | 61 inline bool isfinite(double x) { return finite(x); } |
62 #endif | 62 #endif |
63 #ifndef signbit | 63 #ifndef signbit |
64 inline bool signbit(double x) { struct ieee_double *p = (struct ieee_double *)&x
; return p->dbl_sign; } | 64 inline bool signbit(double x) { struct ieee_double *p = (struct ieee_double *)&x
; return p->dbl_sign; } |
65 #endif | 65 #endif |
66 | 66 |
67 } // namespace std | 67 } // namespace std |
68 | 68 |
69 #endif | 69 #endif |
70 | 70 |
71 #if COMPILER(MSVC) && (_MSC_VER < 1800) | |
72 | |
73 // We must not do 'num + 0.5' or 'num - 0.5' because they can cause precision lo
ss. | |
74 static double round(double num) | |
75 { | |
76 double integer = ceil(num); | |
77 if (num > 0) | |
78 return integer - num > 0.5 ? integer - 1.0 : integer; | |
79 return integer - num >= 0.5 ? integer - 1.0 : integer; | |
80 } | |
81 static float roundf(float num) | |
82 { | |
83 float integer = ceilf(num); | |
84 if (num > 0) | |
85 return integer - num > 0.5f ? integer - 1.0f : integer; | |
86 return integer - num >= 0.5f ? integer - 1.0f : integer; | |
87 } | |
88 inline long long llround(double num) { return static_cast<long long>(round(num))
; } | |
89 inline long long llroundf(float num) { return static_cast<long long>(roundf(num)
); } | |
90 inline long lround(double num) { return static_cast<long>(round(num)); } | |
91 inline long lroundf(float num) { return static_cast<long>(roundf(num)); } | |
92 inline double trunc(double num) { return num > 0 ? floor(num) : ceil(num); } | |
93 | |
94 #endif | |
95 | |
96 #if OS(ANDROID) || COMPILER(MSVC) | 71 #if OS(ANDROID) || COMPILER(MSVC) |
97 // ANDROID and MSVC's math.h does not currently supply log2 or log2f. | 72 // ANDROID and MSVC's math.h does not currently supply log2 or log2f. |
98 inline double log2(double num) | 73 inline double log2(double num) |
99 { | 74 { |
100 // This constant is roughly M_LN2, which is not provided by default on Windo
ws and Android. | 75 // This constant is roughly M_LN2, which is not provided by default on Windo
ws and Android. |
101 return log(num) / 0.693147180559945309417232121458176568; | 76 return log(num) / 0.693147180559945309417232121458176568; |
102 } | 77 } |
103 | 78 |
104 inline float log2f(float num) | 79 inline float log2f(float num) |
105 { | 80 { |
106 // This constant is roughly M_LN2, which is not provided by default on Windo
ws and Android. | 81 // This constant is roughly M_LN2, which is not provided by default on Windo
ws and Android. |
107 return logf(num) / 0.693147180559945309417232121458176568f; | 82 return logf(num) / 0.693147180559945309417232121458176568f; |
108 } | 83 } |
109 #endif | 84 #endif |
110 | 85 |
111 #if COMPILER(MSVC) | 86 #if COMPILER(MSVC) |
112 | 87 |
113 // VS2013 has most of the math functions now, but we still need to work | 88 // VS2013 has most of the math functions now, but we still need to work |
114 // around various differences in behavior of Inf. | 89 // around various differences in behavior of Inf. |
115 | 90 |
116 #if _MSC_VER < 1800 | |
117 | |
118 namespace std { | |
119 | |
120 inline bool isinf(double num) { return !_finite(num) && !_isnan(num); } | |
121 inline bool isnan(double num) { return !!_isnan(num); } | |
122 inline bool isfinite(double x) { return _finite(x); } | |
123 inline bool signbit(double num) { return _copysign(1.0, num) < 0; } | |
124 | |
125 } // namespace std | |
126 | |
127 inline double nextafter(double x, double y) { return _nextafter(x, y); } | |
128 inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x +
FLT_EPSILON; } | |
129 | |
130 inline double copysign(double x, double y) { return _copysign(x, y); } | |
131 | |
132 #endif // _MSC_VER | |
133 | |
134 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN inst
ead of specific values. | 91 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN inst
ead of specific values. |
135 inline double wtf_atan2(double x, double y) | 92 inline double wtf_atan2(double x, double y) |
136 { | 93 { |
137 double posInf = std::numeric_limits<double>::infinity(); | 94 double posInf = std::numeric_limits<double>::infinity(); |
138 double negInf = -std::numeric_limits<double>::infinity(); | 95 double negInf = -std::numeric_limits<double>::infinity(); |
139 double nan = std::numeric_limits<double>::quiet_NaN(); | 96 double nan = std::numeric_limits<double>::quiet_NaN(); |
140 | 97 |
141 double result = nan; | 98 double result = nan; |
142 | 99 |
143 if (x == posInf && y == posInf) | 100 if (x == posInf && y == posInf) |
(...skipping 13 matching lines...) Expand all Loading... |
157 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN
instead of x. | 114 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN
instead of x. |
158 inline double wtf_fmod(double x, double y) { return (!std::isinf(x) && std::isin
f(y)) ? x : fmod(x, y); } | 115 inline double wtf_fmod(double x, double y) { return (!std::isinf(x) && std::isin
f(y)) ? x : fmod(x, y); } |
159 | 116 |
160 // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead
of 1. | 117 // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead
of 1. |
161 inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); } | 118 inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); } |
162 | 119 |
163 #define atan2(x, y) wtf_atan2(x, y) | 120 #define atan2(x, y) wtf_atan2(x, y) |
164 #define fmod(x, y) wtf_fmod(x, y) | 121 #define fmod(x, y) wtf_fmod(x, y) |
165 #define pow(x, y) wtf_pow(x, y) | 122 #define pow(x, y) wtf_pow(x, y) |
166 | 123 |
167 #if _MSC_VER < 1800 | |
168 | |
169 // MSVC's math functions do not bring lrint. | |
170 inline long int lrint(double flt) | |
171 { | |
172 int64_t intgr; | |
173 #if CPU(X86) | |
174 __asm { | |
175 fld flt | |
176 fistp intgr | |
177 }; | |
178 #else | |
179 ASSERT(std::isfinite(flt)); | |
180 double rounded = round(flt); | |
181 intgr = static_cast<int64_t>(rounded); | |
182 // If the fractional part is exactly 0.5, we need to check whether | |
183 // the rounded result is even. If it is not we need to add 1 to | |
184 // negative values and subtract one from positive values. | |
185 if ((fabs(intgr - flt) == 0.5) & intgr) | |
186 intgr -= ((intgr >> 62) | 1); // 1 with the sign of result, i.e. -1 or 1
. | |
187 #endif | |
188 return static_cast<long int>(intgr); | |
189 } | |
190 | |
191 #endif // _MSC_VER | |
192 | |
193 #endif // COMPILER(MSVC) | 124 #endif // COMPILER(MSVC) |
194 | 125 |
195 inline double deg2rad(double d) { return d * piDouble / 180.0; } | 126 inline double deg2rad(double d) { return d * piDouble / 180.0; } |
196 inline double rad2deg(double r) { return r * 180.0 / piDouble; } | 127 inline double rad2deg(double r) { return r * 180.0 / piDouble; } |
197 inline double deg2grad(double d) { return d * 400.0 / 360.0; } | 128 inline double deg2grad(double d) { return d * 400.0 / 360.0; } |
198 inline double grad2deg(double g) { return g * 360.0 / 400.0; } | 129 inline double grad2deg(double g) { return g * 360.0 / 400.0; } |
199 inline double turn2deg(double t) { return t * 360.0; } | 130 inline double turn2deg(double t) { return t * 360.0; } |
200 inline double deg2turn(double d) { return d / 360.0; } | 131 inline double deg2turn(double d) { return d / 360.0; } |
201 inline double rad2grad(double r) { return r * 200.0 / piDouble; } | 132 inline double rad2grad(double r) { return r * 200.0 / piDouble; } |
202 inline double grad2rad(double g) { return g * piDouble / 200.0; } | 133 inline double grad2rad(double g) { return g * piDouble / 200.0; } |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 if (i >> 2) | 351 if (i >> 2) |
421 log2 += 2, i >>= 2; | 352 log2 += 2, i >>= 2; |
422 if (i >> 1) | 353 if (i >> 1) |
423 log2 += 1; | 354 log2 += 1; |
424 return log2; | 355 return log2; |
425 } | 356 } |
426 | 357 |
427 } // namespace WTF | 358 } // namespace WTF |
428 | 359 |
429 #endif // #ifndef WTF_MathExtras_h | 360 #endif // #ifndef WTF_MathExtras_h |
OLD | NEW |