Changeset 259537 in webkit


Ignore:
Timestamp:
Apr 4, 2020 2:42:23 PM (4 years ago)
Author:
Fujii Hironori
Message:

[Clang 10] Fix -Wimplicit-int-float-conversion compilation warnings in WTF
https://bugs.webkit.org/show_bug.cgi?id=209955

Reviewed by Darin Adler.

Clang 10 reports a compilation warning for int to float
conversions losing the precision. The warning is often reported
for code converting a floating point value to an integer value.
For example:

Optional<int> positive_float_to_int(float f) {

if (f > INT_MAX)

return nullopt;

return static_cast<int>(f);

}

INT_MAX is implicitly converted float, but float can't keep the
precision of such large value. And, C++ spec doesn't specify
whether it would be rounded up or down. Above code should be
rewritten to:

Optional<int> positive_float_to_int(float f) {

if (f >= pow(2, 31))

return nullopt;

return static_cast<int>(f);

}

Instead of using pow, this change added a template variable
maxPlusOne<T>.

  • wtf/MathExtras.h:

(powerOfTwo): Added.
(doubleToInteger): Added.
(maxPlusOne): Added.

  • wtf/MediaTime.cpp:

(WTF::MediaTime::createWithFloat):
(WTF::MediaTime::createWithDouble):

Location:
trunk/Source/WTF
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WTF/ChangeLog

    r259466 r259537  
     12020-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
    1412020-04-03  David Kilzer  <ddkilzer@apple.com>
    242
  • trunk/Source/WTF/wtf/MathExtras.h

    r252525 r259537  
    392392}
    393393
     394template<typename T> constexpr unsigned countOfBits = sizeof(T) * CHAR_BIT;
     395template<typename T> constexpr unsigned countOfMagnitudeBits = countOfBits<T> - std::is_signed_v<T>;
     396
     397constexpr float powerOfTwo(unsigned e)
     398{
     399    float p = 1;
     400    while (e--)
     401        p *= 2;
     402    return p;
     403}
     404
     405template<typename T> constexpr float maxPlusOne = powerOfTwo(countOfMagnitudeBits<T>);
     406
    394407// Calculate d % 2^{64}.
    395408inline void doubleToInteger(double d, unsigned long long& value)
     
    399412    else {
    400413        // -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>);
    402415        if (fmodValue >= 0) {
    403416            // 0 <= fmodValue < 2^{64}.
  • trunk/Source/WTF/wtf/MediaTime.cpp

    r254514 r259537  
    9696    if (std::isinf(floatTime))
    9797        return std::signbit(floatTime) ? negativeInfiniteTime() : positiveInfiniteTime();
    98     if (floatTime > std::numeric_limits<int64_t>::max())
     98    if (floatTime >= maxPlusOne<int64_t>)
    9999        return positiveInfiniteTime();
    100100    if (floatTime < std::numeric_limits<int64_t>::min())
     
    103103        return std::signbit(floatTime) ? negativeInfiniteTime() : positiveInfiniteTime();
    104104
    105     while (floatTime * timeScale > std::numeric_limits<int64_t>::max())
     105    while (floatTime * timeScale >= maxPlusOne<int64_t>)
    106106        timeScale /= 2;
    107107    return MediaTime(static_cast<int64_t>(floatTime * timeScale), timeScale, Valid);
     
    126126    if (std::isinf(doubleTime))
    127127        return std::signbit(doubleTime) ? negativeInfiniteTime() : positiveInfiniteTime();
    128     if (doubleTime > std::numeric_limits<int64_t>::max())
     128    if (doubleTime >= maxPlusOne<int64_t>)
    129129        return positiveInfiniteTime();
    130130    if (doubleTime < std::numeric_limits<int64_t>::min())
     
    133133        return std::signbit(doubleTime) ? negativeInfiniteTime() : positiveInfiniteTime();
    134134
    135     while (doubleTime * timeScale > std::numeric_limits<int64_t>::max())
     135    while (doubleTime * timeScale >= maxPlusOne<int64_t>)
    136136        timeScale /= 2;
    137137    return MediaTime(static_cast<int64_t>(std::round(doubleTime * timeScale)), timeScale, Valid);
Note: See TracChangeset for help on using the changeset viewer.