1 | /* |
2 | * Copyright (C) 2006, 2007, 2008, 2009, 2010 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 |
28 | |
29 | #include <algorithm> |
30 | #include <cmath> |
31 | #include <float.h> |
32 | #include <limits> |
33 | #include <stdint.h> |
34 | #include <stdlib.h> |
35 | #include <wtf/StdLibExtras.h> |
36 | |
37 | #if OS(SOLARIS) |
38 | #include <ieeefp.h> |
39 | #endif |
40 | |
41 | #if OS(OPENBSD) |
42 | #include <sys/types.h> |
43 | #include <machine/ieee.h> |
44 | #endif |
45 | |
46 | #if OS(QNX) && defined(_CPPLIB_VER) |
47 | // FIXME: Look into a way to have cmath import its functions into both the standard and global |
48 | // namespace. For now, we include math.h since the QNX cmath header only imports its functions |
49 | // into the standard namespace. |
50 | #include <math.h> |
51 | // These macros from math.h conflict with the real functions in the std namespace. |
52 | #undef signbit |
53 | #undef isnan |
54 | #undef isinf |
55 | #undef isfinite |
56 | #endif |
57 | |
58 | #ifndef M_PI |
59 | const double piDouble = 3.14159265358979323846; |
60 | const float piFloat = 3.14159265358979323846f; |
61 | #else |
62 | const double piDouble = M_PI; |
63 | const float piFloat = static_cast<float>(M_PI); |
64 | #endif |
65 | |
66 | #ifndef M_PI_2 |
67 | const double piOverTwoDouble = 1.57079632679489661923; |
68 | const float piOverTwoFloat = 1.57079632679489661923f; |
69 | #else |
70 | const double piOverTwoDouble = M_PI_2; |
71 | const float piOverTwoFloat = static_cast<float>(M_PI_2); |
72 | #endif |
73 | |
74 | #ifndef M_PI_4 |
75 | const double piOverFourDouble = 0.785398163397448309616; |
76 | const float piOverFourFloat = 0.785398163397448309616f; |
77 | #else |
78 | const double piOverFourDouble = M_PI_4; |
79 | const float piOverFourFloat = static_cast<float>(M_PI_4); |
80 | #endif |
81 | |
82 | #if OS(DARWIN) |
83 | |
84 | // Work around a bug in the Mac OS X libc where ceil(-0.1) return +0. |
85 | inline double wtf_ceil(double x) { return copysign(ceil(x), x); } |
86 | |
87 | #define ceil(x) wtf_ceil(x) |
88 | |
89 | #endif |
90 | |
91 | #if OS(SOLARIS) && __cplusplus < 201103L |
92 | |
93 | namespace std { |
94 | |
95 | #ifndef isfinite |
96 | inline bool isfinite(double x) { return finite(x) && !isnand(x); } |
97 | #endif |
98 | #ifndef signbit |
99 | inline bool signbit(double x) { return copysign(1.0, x) < 0; } |
100 | #endif |
101 | #ifndef isinf |
102 | inline bool isinf(double x) { return !finite(x) && !isnand(x); } |
103 | #endif |
104 | |
105 | } // namespace std |
106 | |
107 | #endif |
108 | |
109 | #if OS(OPENBSD) && __cplusplus < 201103L |
110 | |
111 | namespace std { |
112 | |
113 | #ifndef isfinite |
114 | inline bool isfinite(double x) { return finite(x); } |
115 | #endif |
116 | #ifndef signbit |
117 | inline bool signbit(double x) { struct ieee_double *p = (struct ieee_double *)&x; return p->dbl_sign; } |
118 | #endif |
119 | |
120 | } // namespace std |
121 | |
122 | #endif |
123 | |
124 | #if COMPILER(MSVC) |
125 | |
126 | #if _MSC_VER < 1800 |
127 | // We must not do 'num + 0.5' or 'num - 0.5' because they can cause precision loss. |
128 | static double round(double num) |
129 | { |
130 | double integer = ceil(num); |
131 | if (num > 0) |
132 | return integer - num > 0.5 ? integer - 1.0 : integer; |
133 | return integer - num >= 0.5 ? integer - 1.0 : integer; |
134 | } |
135 | static float roundf(float num) |
136 | { |
137 | float integer = ceilf(num); |
138 | if (num > 0) |
139 | return integer - num > 0.5f ? integer - 1.0f : integer; |
140 | return integer - num >= 0.5f ? integer - 1.0f : integer; |
141 | } |
142 | #endif |
143 | inline long long llround(double num) { return static_cast<long long>(round(num)); } |
144 | inline long long llroundf(float num) { return static_cast<long long>(roundf(num)); } |
145 | inline long lround(double num) { return static_cast<long>(round(num)); } |
146 | inline long lroundf(float num) { return static_cast<long>(roundf(num)); } |
147 | |
148 | #endif |
149 | |
150 | #if COMPILER(MSVC) && COMPILER(MSVC12_OR_LOWER) |
151 | // MSVC's math.h does not currently supply log2 or log2f. |
152 | inline double log2(double num) |
153 | { |
154 | // This constant is roughly M_LN2, which is not provided by default on Windows. |
155 | return log(num) / 0.693147180559945309417232121458176568; |
156 | } |
157 | |
158 | inline float log2f(float num) |
159 | { |
160 | // This constant is roughly M_LN2, which is not provided by default on Windows. |
161 | return logf(num) / 0.693147180559945309417232121458176568f; |
162 | } |
163 | #endif |
164 | |
165 | #if COMPILER(MSVC) |
166 | // The 64bit version of abs() is already defined in stdlib.h which comes with VC10 |
167 | #if COMPILER(MSVC9_OR_LOWER) |
168 | inline long long abs(long long num) { return _abs64(num); } |
169 | #endif |
170 | |
171 | #if COMPILER(MSVC12_OR_LOWER) |
172 | |
173 | inline double nextafter(double x, double y) { return _nextafter(x, y); } |
174 | inline float nextafterf(float x, float y) { return x > y ? x - FLT_EPSILON : x + FLT_EPSILON; } |
175 | |
176 | inline double copysign(double x, double y) { return _copysign(x, y); } |
177 | |
178 | #endif // COMPILER(MSVC12_OR_LOWER) |
179 | |
180 | // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN instead of specific values. |
181 | inline double wtf_atan2(double x, double y) |
182 | { |
183 | double posInf = std::numeric_limits<double>::infinity(); |
184 | double negInf = -std::numeric_limits<double>::infinity(); |
185 | double nan = std::numeric_limits<double>::quiet_NaN(); |
186 | |
187 | double result = nan; |
188 | |
189 | if (x == posInf && y == posInf) |
190 | result = piOverFourDouble; |
191 | else if (x == posInf && y == negInf) |
192 | result = 3 * piOverFourDouble; |
193 | else if (x == negInf && y == posInf) |
194 | result = -piOverFourDouble; |
195 | else if (x == negInf && y == negInf) |
196 | result = -3 * piOverFourDouble; |
197 | else |
198 | result = ::atan2(x, y); |
199 | |
200 | return result; |
201 | } |
202 | |
203 | // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN instead of x. |
204 | inline double wtf_fmod(double x, double y) { return (!std::isinf(x) && std::isinf(y)) ? x : fmod(x, y); } |
205 | |
206 | // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead of 1. |
207 | inline double wtf_pow(double x, double y) { return y == 0 ? 1 : pow(x, y); } |
208 | |
209 | #define atan2(x, y) wtf_atan2(x, y) |
210 | #define fmod(x, y) wtf_fmod(x, y) |
211 | #define pow(x, y) wtf_pow(x, y) |
212 | |
213 | #if COMPILER(MSVC12_OR_LOWER) |
214 | |
215 | // MSVC's math functions do not bring lrint. |
216 | inline long int lrint(double flt) |
217 | { |
218 | int64_t intgr; |
219 | #if CPU(X86) |
220 | __asm { |
221 | fld flt |
222 | fistp intgr |
223 | }; |
224 | #else |
225 | ASSERT(std::isfinite(flt)); |
226 | double rounded = round(flt); |
227 | intgr = static_cast<int64_t>(rounded); |
228 | // If the fractional part is exactly 0.5, we need to check whether |
229 | // the rounded result is even. If it is not we need to add 1 to |
230 | // negative values and subtract one from positive values. |
231 | if ((fabs(intgr - flt) == 0.5) & intgr) |
232 | intgr -= ((intgr >> 62) | 1); // 1 with the sign of result, i.e. -1 or 1. |
233 | #endif |
234 | return static_cast<long int>(intgr); |
235 | } |
236 | |
237 | #endif // COMPILER(MSVC12_OR_LOWER) |
238 | #endif // COMPILER(MSVC) |
239 | |
240 | inline double deg2rad(double d) { return d * piDouble / 180.0; } |
241 | inline double rad2deg(double r) { return r * 180.0 / piDouble; } |
242 | inline double deg2grad(double d) { return d * 400.0 / 360.0; } |
243 | inline double grad2deg(double g) { return g * 360.0 / 400.0; } |
244 | inline double turn2deg(double t) { return t * 360.0; } |
245 | inline double deg2turn(double d) { return d / 360.0; } |
246 | inline double rad2grad(double r) { return r * 200.0 / piDouble; } |
247 | inline double grad2rad(double g) { return g * piDouble / 200.0; } |
248 | |
249 | inline float deg2rad(float d) { return d * piFloat / 180.0f; } |
250 | inline float rad2deg(float r) { return r * 180.0f / piFloat; } |
251 | inline float deg2grad(float d) { return d * 400.0f / 360.0f; } |
252 | inline float grad2deg(float g) { return g * 360.0f / 400.0f; } |
253 | inline float turn2deg(float t) { return t * 360.0f; } |
254 | inline float deg2turn(float d) { return d / 360.0f; } |
255 | inline float rad2grad(float r) { return r * 200.0f / piFloat; } |
256 | inline float grad2rad(float g) { return g * piFloat / 200.0f; } |
257 | |
258 | // std::numeric_limits<T>::min() returns the smallest positive value for floating point types |
259 | template<typename T> inline T defaultMinimumForClamp() { return std::numeric_limits<T>::min(); } |
260 | template<> inline float defaultMinimumForClamp() { return -std::numeric_limits<float>::max(); } |
261 | template<> inline double defaultMinimumForClamp() { return -std::numeric_limits<double>::max(); } |
262 | template<typename T> inline T defaultMaximumForClamp() { return std::numeric_limits<T>::max(); } |
263 | |
264 | template<typename T> inline T clampTo(double value, T min = defaultMinimumForClamp<T>(), T max = defaultMaximumForClamp<T>()) |
265 | { |
266 | if (value >= static_cast<double>(max)) |
267 | return max; |
268 | if (value <= static_cast<double>(min)) |
269 | return min; |
270 | return static_cast<T>(value); |
271 | } |
272 | template<> long long int clampTo(double, long long int, long long int); // clampTo does not support long long ints. |
273 | |
274 | inline int clampToInteger(double value) |
275 | { |
276 | return clampTo<int>(value); |
277 | } |
278 | |
279 | inline float clampToFloat(double value) |
280 | { |
281 | return clampTo<float>(value); |
282 | } |
283 | |
284 | inline int clampToPositiveInteger(double value) |
285 | { |
286 | return clampTo<int>(value, min: 0); |
287 | } |
288 | |
289 | inline int clampToInteger(float value) |
290 | { |
291 | return clampTo<int>(value); |
292 | } |
293 | |
294 | inline int clampToInteger(unsigned x) |
295 | { |
296 | const unsigned intMax = static_cast<unsigned>(std::numeric_limits<int>::max()); |
297 | |
298 | if (x >= intMax) |
299 | return std::numeric_limits<int>::max(); |
300 | return static_cast<int>(x); |
301 | } |
302 | |
303 | inline bool isWithinIntRange(float x) |
304 | { |
305 | return x > static_cast<float>(std::numeric_limits<int>::min()) && x < static_cast<float>(std::numeric_limits<int>::max()); |
306 | } |
307 | |
308 | template<typename T> inline bool hasOneBitSet(T value) |
309 | { |
310 | return !((value - 1) & value) && value; |
311 | } |
312 | |
313 | template<typename T> inline bool hasZeroOrOneBitsSet(T value) |
314 | { |
315 | return !((value - 1) & value); |
316 | } |
317 | |
318 | template<typename T> inline bool hasTwoOrMoreBitsSet(T value) |
319 | { |
320 | return !hasZeroOrOneBitsSet(value); |
321 | } |
322 | |
323 | template <typename T> inline unsigned getLSBSet(T value) |
324 | { |
325 | unsigned result = 0; |
326 | |
327 | while (value >>= 1) |
328 | ++result; |
329 | |
330 | return result; |
331 | } |
332 | |
333 | template<typename T> inline T timesThreePlusOneDividedByTwo(T value) |
334 | { |
335 | // Mathematically equivalent to: |
336 | // (value * 3 + 1) / 2; |
337 | // or: |
338 | // (unsigned)ceil(value * 1.5)); |
339 | // This form is not prone to internal overflow. |
340 | return value + (value >> 1) + (value & 1); |
341 | } |
342 | |
343 | #ifndef UINT64_C |
344 | #if COMPILER(MSVC) |
345 | #define UINT64_C(c) c ## ui64 |
346 | #else |
347 | #define UINT64_C(c) c ## ull |
348 | #endif |
349 | #endif |
350 | |
351 | #if COMPILER(MINGW64) && (!defined(__MINGW64_VERSION_RC) || __MINGW64_VERSION_RC < 1) |
352 | inline double wtf_pow(double x, double y) |
353 | { |
354 | // MinGW-w64 has a custom implementation for pow. |
355 | // This handles certain special cases that are different. |
356 | if ((x == 0.0 || std::isinf(x)) && std::isfinite(y)) { |
357 | double f; |
358 | if (modf(y, &f) != 0.0) |
359 | return ((x == 0.0) ^ (y > 0.0)) ? std::numeric_limits<double>::infinity() : 0.0; |
360 | } |
361 | |
362 | if (x == 2.0) { |
363 | int yInt = static_cast<int>(y); |
364 | if (y == yInt) |
365 | return ldexp(1.0, yInt); |
366 | } |
367 | |
368 | return pow(x, y); |
369 | } |
370 | #define pow(x, y) wtf_pow(x, y) |
371 | #endif // COMPILER(MINGW64) && (!defined(__MINGW64_VERSION_RC) || __MINGW64_VERSION_RC < 1) |
372 | |
373 | |
374 | // decompose 'number' to its sign, exponent, and mantissa components. |
375 | // The result is interpreted as: |
376 | // (sign ? -1 : 1) * pow(2, exponent) * (mantissa / (1 << 52)) |
377 | inline void decomposeDouble(double number, bool& sign, int32_t& exponent, uint64_t& mantissa) |
378 | { |
379 | ASSERT(std::isfinite(number)); |
380 | |
381 | sign = std::signbit(x: number); |
382 | |
383 | uint64_t bits = WTF::bitwise_cast<uint64_t>(from: number); |
384 | exponent = (static_cast<int32_t>(bits >> 52) & 0x7ff) - 0x3ff; |
385 | mantissa = bits & 0xFFFFFFFFFFFFFull; |
386 | |
387 | // Check for zero/denormal values; if so, adjust the exponent, |
388 | // if not insert the implicit, omitted leading 1 bit. |
389 | if (exponent == -0x3ff) |
390 | exponent = mantissa ? -0x3fe : 0; |
391 | else |
392 | mantissa |= 0x10000000000000ull; |
393 | } |
394 | |
395 | // Calculate d % 2^{64}. |
396 | inline void doubleToInteger(double d, unsigned long long& value) |
397 | { |
398 | if (std::isnan(x: d) || std::isinf(x: d)) |
399 | value = 0; |
400 | else { |
401 | // -2^{64} < fmodValue < 2^{64}. |
402 | double fmodValue = fmod(x: trunc(x: d), y: -2.0 * std::numeric_limits<long long>::min()); |
403 | if (fmodValue >= 0) { |
404 | // 0 <= fmodValue < 2^{64}. |
405 | // 0 <= value < 2^{64}. This cast causes no loss. |
406 | value = static_cast<unsigned long long>(fmodValue); |
407 | } else { |
408 | // -2^{64} < fmodValue < 0. |
409 | // 0 < fmodValueInUnsignedLongLong < 2^{64}. This cast causes no loss. |
410 | unsigned long long fmodValueInUnsignedLongLong = static_cast<unsigned long long>(-fmodValue); |
411 | // -1 < (std::numeric_limits<unsigned long long>::max() - fmodValueInUnsignedLongLong) < 2^{64} - 1. |
412 | // 0 < value < 2^{64}. |
413 | value = std::numeric_limits<unsigned long long>::max() - fmodValueInUnsignedLongLong + 1; |
414 | } |
415 | } |
416 | } |
417 | |
418 | namespace WTF { |
419 | |
420 | // From http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 |
421 | inline uint32_t roundUpToPowerOfTwo(uint32_t v) |
422 | { |
423 | v--; |
424 | v |= v >> 1; |
425 | v |= v >> 2; |
426 | v |= v >> 4; |
427 | v |= v >> 8; |
428 | v |= v >> 16; |
429 | v++; |
430 | return v; |
431 | } |
432 | |
433 | inline unsigned fastLog2(unsigned i) |
434 | { |
435 | unsigned log2 = 0; |
436 | if (i & (i - 1)) |
437 | log2 += 1; |
438 | if (i >> 16) |
439 | log2 += 16, i >>= 16; |
440 | if (i >> 8) |
441 | log2 += 8, i >>= 8; |
442 | if (i >> 4) |
443 | log2 += 4, i >>= 4; |
444 | if (i >> 2) |
445 | log2 += 2, i >>= 2; |
446 | if (i >> 1) |
447 | log2 += 1; |
448 | return log2; |
449 | } |
450 | |
451 | } // namespace WTF |
452 | |
453 | #endif // #ifndef WTF_MathExtras_h |
454 | |