| 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 | 55 |
| 56 const double piOverFourDouble = M_PI_4; | 56 const double piOverFourDouble = M_PI_4; |
| 57 const float piOverFourFloat = static_cast<float>(M_PI_4); | 57 const float piOverFourFloat = static_cast<float>(M_PI_4); |
| 58 | 58 |
| 59 const double twoPiDouble = piDouble * 2.0; | 59 const double twoPiDouble = piDouble * 2.0; |
| 60 const float twoPiFloat = piFloat * 2.0f; | 60 const float twoPiFloat = piFloat * 2.0f; |
| 61 | 61 |
| 62 #if OS(ANDROID) || COMPILER(MSVC) | 62 #if OS(ANDROID) || COMPILER(MSVC) |
| 63 // ANDROID and MSVC's math.h does not currently supply log2 or log2f. | 63 // ANDROID and MSVC's math.h does not currently supply log2 or log2f. |
| 64 inline double log2(double num) { | 64 inline double log2(double num) { |
| 65 // This constant is roughly M_LN2, which is not provided by default on Windows
and Android. | 65 // This constant is roughly M_LN2, which is not provided by default on Windows |
| 66 // and Android. |
| 66 return log(num) / 0.693147180559945309417232121458176568; | 67 return log(num) / 0.693147180559945309417232121458176568; |
| 67 } | 68 } |
| 68 | 69 |
| 69 inline float log2f(float num) { | 70 inline float log2f(float num) { |
| 70 // This constant is roughly M_LN2, which is not provided by default on Windows
and Android. | 71 // This constant is roughly M_LN2, which is not provided by default on Windows |
| 72 // and Android. |
| 71 return logf(num) / 0.693147180559945309417232121458176568f; | 73 return logf(num) / 0.693147180559945309417232121458176568f; |
| 72 } | 74 } |
| 73 #endif | 75 #endif |
| 74 | 76 |
| 75 #if COMPILER(MSVC) | 77 #if COMPILER(MSVC) |
| 76 | 78 |
| 77 // VS2013 has most of the math functions now, but we still need to work | 79 // VS2013 has most of the math functions now, but we still need to work |
| 78 // around various differences in behavior of Inf. | 80 // around various differences in behavior of Inf. |
| 79 | 81 |
| 80 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN inst
ead of specific values. | 82 // Work around a bug in Win, where atan2(+-infinity, +-infinity) yields NaN |
| 83 // instead of specific values. |
| 81 inline double wtf_atan2(double x, double y) { | 84 inline double wtf_atan2(double x, double y) { |
| 82 double posInf = std::numeric_limits<double>::infinity(); | 85 double posInf = std::numeric_limits<double>::infinity(); |
| 83 double negInf = -std::numeric_limits<double>::infinity(); | 86 double negInf = -std::numeric_limits<double>::infinity(); |
| 84 double nan = std::numeric_limits<double>::quiet_NaN(); | 87 double nan = std::numeric_limits<double>::quiet_NaN(); |
| 85 | 88 |
| 86 double result = nan; | 89 double result = nan; |
| 87 | 90 |
| 88 if (x == posInf && y == posInf) | 91 if (x == posInf && y == posInf) |
| 89 result = piOverFourDouble; | 92 result = piOverFourDouble; |
| 90 else if (x == posInf && y == negInf) | 93 else if (x == posInf && y == negInf) |
| 91 result = 3 * piOverFourDouble; | 94 result = 3 * piOverFourDouble; |
| 92 else if (x == negInf && y == posInf) | 95 else if (x == negInf && y == posInf) |
| 93 result = -piOverFourDouble; | 96 result = -piOverFourDouble; |
| 94 else if (x == negInf && y == negInf) | 97 else if (x == negInf && y == negInf) |
| 95 result = -3 * piOverFourDouble; | 98 result = -3 * piOverFourDouble; |
| 96 else | 99 else |
| 97 result = ::atan2(x, y); | 100 result = ::atan2(x, y); |
| 98 | 101 |
| 99 return result; | 102 return result; |
| 100 } | 103 } |
| 101 | 104 |
| 102 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN
instead of x. | 105 // Work around a bug in the Microsoft CRT, where fmod(x, +-infinity) yields NaN |
| 106 // instead of x. |
| 103 inline double wtf_fmod(double x, double y) { | 107 inline double wtf_fmod(double x, double y) { |
| 104 return (!std::isinf(x) && std::isinf(y)) ? x : fmod(x, y); | 108 return (!std::isinf(x) && std::isinf(y)) ? x : fmod(x, y); |
| 105 } | 109 } |
| 106 | 110 |
| 107 // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead
of 1. | 111 // Work around a bug in the Microsoft CRT, where pow(NaN, 0) yields NaN instead |
| 112 // of 1. |
| 108 inline double wtf_pow(double x, double y) { | 113 inline double wtf_pow(double x, double y) { |
| 109 return y == 0 ? 1 : pow(x, y); | 114 return y == 0 ? 1 : pow(x, y); |
| 110 } | 115 } |
| 111 | 116 |
| 112 #define atan2(x, y) wtf_atan2(x, y) | 117 #define atan2(x, y) wtf_atan2(x, y) |
| 113 #define fmod(x, y) wtf_fmod(x, y) | 118 #define fmod(x, y) wtf_fmod(x, y) |
| 114 #define pow(x, y) wtf_pow(x, y) | 119 #define pow(x, y) wtf_pow(x, y) |
| 115 | 120 |
| 116 #endif // COMPILER(MSVC) | 121 #endif // COMPILER(MSVC) |
| 117 | 122 |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 404 fmod(trunc(d), std::numeric_limits<unsigned long long>::max() + 1.0); | 409 fmod(trunc(d), std::numeric_limits<unsigned long long>::max() + 1.0); |
| 405 if (fmodValue >= 0) { | 410 if (fmodValue >= 0) { |
| 406 // 0 <= fmodValue < 2^{64}. | 411 // 0 <= fmodValue < 2^{64}. |
| 407 // 0 <= value < 2^{64}. This cast causes no loss. | 412 // 0 <= value < 2^{64}. This cast causes no loss. |
| 408 value = static_cast<unsigned long long>(fmodValue); | 413 value = static_cast<unsigned long long>(fmodValue); |
| 409 } else { | 414 } else { |
| 410 // -2^{64} < fmodValue < 0. | 415 // -2^{64} < fmodValue < 0. |
| 411 // 0 < fmodValueInUnsignedLongLong < 2^{64}. This cast causes no loss. | 416 // 0 < fmodValueInUnsignedLongLong < 2^{64}. This cast causes no loss. |
| 412 unsigned long long fmodValueInUnsignedLongLong = | 417 unsigned long long fmodValueInUnsignedLongLong = |
| 413 static_cast<unsigned long long>(-fmodValue); | 418 static_cast<unsigned long long>(-fmodValue); |
| 414 // -1 < (std::numeric_limits<unsigned long long>::max() - fmodValueInUnsig
nedLongLong) < 2^{64} - 1. | 419 // -1 < (std::numeric_limits<unsigned long long>::max() - |
| 420 // fmodValueInUnsignedLongLong) |
| 421 // < 2^{64} - 1. |
| 415 // 0 < value < 2^{64}. | 422 // 0 < value < 2^{64}. |
| 416 value = std::numeric_limits<unsigned long long>::max() - | 423 value = std::numeric_limits<unsigned long long>::max() - |
| 417 fmodValueInUnsignedLongLong + 1; | 424 fmodValueInUnsignedLongLong + 1; |
| 418 } | 425 } |
| 419 } | 426 } |
| 420 } | 427 } |
| 421 | 428 |
| 422 namespace WTF { | 429 namespace WTF { |
| 423 | 430 |
| 424 inline unsigned fastLog2(unsigned i) { | 431 inline unsigned fastLog2(unsigned i) { |
| 425 unsigned log2 = 0; | 432 unsigned log2 = 0; |
| 426 if (i & (i - 1)) | 433 if (i & (i - 1)) |
| 427 log2 += 1; | 434 log2 += 1; |
| 428 if (i >> 16) | 435 if (i >> 16) |
| 429 log2 += 16, i >>= 16; | 436 log2 += 16, i >>= 16; |
| 430 if (i >> 8) | 437 if (i >> 8) |
| 431 log2 += 8, i >>= 8; | 438 log2 += 8, i >>= 8; |
| 432 if (i >> 4) | 439 if (i >> 4) |
| 433 log2 += 4, i >>= 4; | 440 log2 += 4, i >>= 4; |
| 434 if (i >> 2) | 441 if (i >> 2) |
| 435 log2 += 2, i >>= 2; | 442 log2 += 2, i >>= 2; |
| 436 if (i >> 1) | 443 if (i >> 1) |
| 437 log2 += 1; | 444 log2 += 1; |
| 438 return log2; | 445 return log2; |
| 439 } | 446 } |
| 440 | 447 |
| 441 } // namespace WTF | 448 } // namespace WTF |
| 442 | 449 |
| 443 #endif // #ifndef WTF_MathExtras_h | 450 #endif // #ifndef WTF_MathExtras_h |
| OLD | NEW |