Changeset 259537 in webkit
- Timestamp:
- Apr 4, 2020 2:42:23 PM (4 years ago)
- Location:
- trunk/Source/WTF
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/WTF/ChangeLog
r259466 r259537 1 2020-04-04 Fujii Hironori <Hironori.Fujii@sony.com> 2 3 [Clang 10] Fix -Wimplicit-int-float-conversion compilation warnings in WTF 4 https://bugs.webkit.org/show_bug.cgi?id=209955 5 6 Reviewed by Darin Adler. 7 8 Clang 10 reports a compilation warning for int to float 9 conversions losing the precision. The warning is often reported 10 for code converting a floating point value to an integer value. 11 For example: 12 13 > Optional<int> positive_float_to_int(float f) { 14 > if (f > INT_MAX) 15 > return nullopt; 16 > return static_cast<int>(f); 17 > } 18 19 INT_MAX is implicitly converted float, but float can't keep the 20 precision of such large value. And, C++ spec doesn't specify 21 whether it would be rounded up or down. Above code should be 22 rewritten to: 23 24 > Optional<int> positive_float_to_int(float f) { 25 > if (f >= pow(2, 31)) 26 > return nullopt; 27 > return static_cast<int>(f); 28 > } 29 30 Instead of using pow, this change added a template variable 31 maxPlusOne<T>. 32 33 * wtf/MathExtras.h: 34 (powerOfTwo): Added. 35 (doubleToInteger): Added. 36 (maxPlusOne): Added. 37 * wtf/MediaTime.cpp: 38 (WTF::MediaTime::createWithFloat): 39 (WTF::MediaTime::createWithDouble): 40 1 41 2020-04-03 David Kilzer <ddkilzer@apple.com> 2 42 -
trunk/Source/WTF/wtf/MathExtras.h
r252525 r259537 392 392 } 393 393 394 template<typename T> constexpr unsigned countOfBits = sizeof(T) * CHAR_BIT; 395 template<typename T> constexpr unsigned countOfMagnitudeBits = countOfBits<T> - std::is_signed_v<T>; 396 397 constexpr float powerOfTwo(unsigned e) 398 { 399 float p = 1; 400 while (e--) 401 p *= 2; 402 return p; 403 } 404 405 template<typename T> constexpr float maxPlusOne = powerOfTwo(countOfMagnitudeBits<T>); 406 394 407 // Calculate d % 2^{64}. 395 408 inline void doubleToInteger(double d, unsigned long long& value) … … 399 412 else { 400 413 // -2^{64} < fmodValue < 2^{64}. 401 double fmodValue = fmod(trunc(d), std::numeric_limits<unsigned long long>::max() + 1.0);414 double fmodValue = fmod(trunc(d), maxPlusOne<unsigned long long>); 402 415 if (fmodValue >= 0) { 403 416 // 0 <= fmodValue < 2^{64}. -
trunk/Source/WTF/wtf/MediaTime.cpp
r254514 r259537 96 96 if (std::isinf(floatTime)) 97 97 return std::signbit(floatTime) ? negativeInfiniteTime() : positiveInfiniteTime(); 98 if (floatTime > std::numeric_limits<int64_t>::max())98 if (floatTime >= maxPlusOne<int64_t>) 99 99 return positiveInfiniteTime(); 100 100 if (floatTime < std::numeric_limits<int64_t>::min()) … … 103 103 return std::signbit(floatTime) ? negativeInfiniteTime() : positiveInfiniteTime(); 104 104 105 while (floatTime * timeScale > std::numeric_limits<int64_t>::max())105 while (floatTime * timeScale >= maxPlusOne<int64_t>) 106 106 timeScale /= 2; 107 107 return MediaTime(static_cast<int64_t>(floatTime * timeScale), timeScale, Valid); … … 126 126 if (std::isinf(doubleTime)) 127 127 return std::signbit(doubleTime) ? negativeInfiniteTime() : positiveInfiniteTime(); 128 if (doubleTime > std::numeric_limits<int64_t>::max())128 if (doubleTime >= maxPlusOne<int64_t>) 129 129 return positiveInfiniteTime(); 130 130 if (doubleTime < std::numeric_limits<int64_t>::min()) … … 133 133 return std::signbit(doubleTime) ? negativeInfiniteTime() : positiveInfiniteTime(); 134 134 135 while (doubleTime * timeScale > std::numeric_limits<int64_t>::max())135 while (doubleTime * timeScale >= maxPlusOne<int64_t>) 136 136 timeScale /= 2; 137 137 return MediaTime(static_cast<int64_t>(std::round(doubleTime * timeScale)), timeScale, Valid);
Note: See TracChangeset
for help on using the changeset viewer.