Changeset 138952 in webkit


Ignore:
Timestamp:
Jan 7, 2013 9:25:37 AM (11 years ago)
Author:
jchaffraix@webkit.org
Message:

Support size_t multiplication and division operators on LayoutUnit
https://bugs.webkit.org/show_bug.cgi?id=83848

Reviewed by Emil A Eklund.

Source/WebCore:

Per Darin's suggestion, adding a version of the operator for most unsigned types (excluding
only unsigned char). This should automatically cover size_t as it should be one of these.

Test: TestWebKitAPI/Tests/WebCore/LayoutUnit.cpp

  • platform/LayoutUnit.h:

(WebCore::LayoutUnit::LayoutUnit):
(WebCore::operator*):
(WebCore::operator/):
Added the operators and (possibly saturating) constructors for unsigned short, unsigned long,
unsigned long long.

Tools:

  • TestWebKitAPI/Tests/WebCore/LayoutUnit.cpp:

Added some simple tests for the new operators using size_t.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r138948 r138952  
     12013-01-07  Julien Chaffraix  <jchaffraix@webkit.org>
     2
     3        Support size_t multiplication and division operators on LayoutUnit
     4        https://bugs.webkit.org/show_bug.cgi?id=83848
     5
     6        Reviewed by Emil A Eklund.
     7
     8        Per Darin's suggestion, adding a version of the operator for most unsigned types (excluding
     9        only unsigned char). This should automatically cover size_t as it should be one of these.
     10
     11        Test: TestWebKitAPI/Tests/WebCore/LayoutUnit.cpp
     12
     13        * platform/LayoutUnit.h:
     14        (WebCore::LayoutUnit::LayoutUnit):
     15        (WebCore::operator*):
     16        (WebCore::operator/):
     17        Added the operators and (possibly saturating) constructors for unsigned short, unsigned long,
     18        unsigned long long.
     19
    1202013-01-07  Sheriff Bot  <webkit.review.bot@gmail.com>
    221
  • trunk/Source/WebCore/platform/LayoutUnit.h

    r138736 r138952  
    6767class LayoutUnit {
    6868public:
    69     // FIXME: Ideally we would have size_t versions of the constructor and operators.
    70     // However due to compiler and platform differences adding those are non-trivial.
    71     // See https://bugs.webkit.org/show_bug.cgi?id=83848 for details.
    72    
    7369    LayoutUnit() : m_value(0) { }
    7470#if ENABLE(SUBPIXEL_LAYOUT)
     
    7672    LayoutUnit(unsigned short value) { setValue(value); }
    7773    LayoutUnit(unsigned value) { setValue(value); }
     74    LayoutUnit(unsigned long value)
     75    {
     76#if ENABLE(SATURATED_LAYOUT_ARITHMETIC)
     77        m_value = clampTo<int>(value * kEffectiveFixedPointDenominator);
     78#else
     79        REPORT_OVERFLOW(isInBounds(static_cast<unsigned>(value)));
     80        m_value = value * kEffectiveFixedPointDenominator;
     81#endif
     82    }
     83    LayoutUnit(unsigned long long value)
     84    {
     85#if ENABLE(SATURATED_LAYOUT_ARITHMETIC)
     86        m_value = clampTo<int>(value * kEffectiveFixedPointDenominator);
     87#else
     88        REPORT_OVERFLOW(isInBounds(static_cast<unsigned>(value)));
     89        m_value = value * kEffectiveFixedPointDenominator;
     90#endif
     91    }
    7892    LayoutUnit(float value)
    7993    {
     
    98112    LayoutUnit(unsigned short value) { REPORT_OVERFLOW(isInBounds(value)); m_value = value; }
    99113    LayoutUnit(unsigned value) { REPORT_OVERFLOW(isInBounds(value)); m_value = value; }
     114    LayoutUnit(unsigned long long value) { REPORT_OVERFLOW(isInBounds(static_cast<unsigned>(value))); m_value = value; }
     115    LayoutUnit(unsigned long value) { REPORT_OVERFLOW(isInBounds(static_cast<unsigned>(value))); m_value = value; }
    100116    LayoutUnit(float value) { REPORT_OVERFLOW(isInBounds(value)); m_value = value; }
    101117    LayoutUnit(double value) { REPORT_OVERFLOW(isInBounds(value)); m_value = value; }
     
    538554}
    539555
     556inline LayoutUnit operator*(const LayoutUnit& a, unsigned short b)
     557{
     558    return a * LayoutUnit(b);
     559}
     560
    540561inline LayoutUnit operator*(const LayoutUnit& a, unsigned b)
    541562{
     
    543564}
    544565
     566inline LayoutUnit operator*(const LayoutUnit& a, unsigned long b)
     567{
     568    return a * LayoutUnit(b);
     569}
     570
     571inline LayoutUnit operator*(const LayoutUnit& a, unsigned long long b)
     572{
     573    return a * LayoutUnit(b);
     574}
     575
     576inline LayoutUnit operator*(unsigned short a, const LayoutUnit& b)
     577{
     578    return LayoutUnit(a) * b;
     579}
     580
    545581inline LayoutUnit operator*(unsigned a, const LayoutUnit& b)
     582{
     583    return LayoutUnit(a) * b;
     584}
     585
     586inline LayoutUnit operator*(unsigned long a, const LayoutUnit& b)
     587{
     588    return LayoutUnit(a) * b;
     589}
     590
     591inline LayoutUnit operator*(unsigned long long a, const LayoutUnit& b)
    546592{
    547593    return LayoutUnit(a) * b;
     
    594640}
    595641
     642inline LayoutUnit operator/(const LayoutUnit& a, unsigned short b)
     643{
     644    return a / LayoutUnit(b);
     645}
     646
    596647inline LayoutUnit operator/(const LayoutUnit& a, unsigned b)
    597648{
     
    599650}
    600651
     652inline LayoutUnit operator/(const LayoutUnit& a, unsigned long b)
     653{
     654    return a / LayoutUnit(b);
     655}
     656
     657inline LayoutUnit operator/(const LayoutUnit& a, unsigned long long b)
     658{
     659    return a / LayoutUnit(b);
     660}
     661
    601662inline float operator/(const float a, const LayoutUnit& b)
    602663{
     
    614675}
    615676
     677inline LayoutUnit operator/(unsigned short a, const LayoutUnit& b)
     678{
     679    return LayoutUnit(a) / b;
     680}
     681
    616682inline LayoutUnit operator/(unsigned a, const LayoutUnit& b)
     683{
     684    return LayoutUnit(a) / b;
     685}
     686
     687inline LayoutUnit operator/(unsigned long a, const LayoutUnit& b)
     688{
     689    return LayoutUnit(a) / b;
     690}
     691
     692inline LayoutUnit operator/(unsigned long long a, const LayoutUnit& b)
    617693{
    618694    return LayoutUnit(a) / b;
  • trunk/Tools/ChangeLog

    r138951 r138952  
     12013-01-07  Julien Chaffraix  <jchaffraix@webkit.org>
     2
     3        Support size_t multiplication and division operators on LayoutUnit
     4        https://bugs.webkit.org/show_bug.cgi?id=83848
     5
     6        Reviewed by Emil A Eklund.
     7
     8        * TestWebKitAPI/Tests/WebCore/LayoutUnit.cpp:
     9        Added some simple tests for the new operators using size_t.
     10
    1112013-01-07  Csaba Osztrogonác  <ossy@webkit.org>
    212
  • trunk/Tools/TestWebKitAPI/Tests/WebCore/LayoutUnit.cpp

    r138736 r138952  
    147147    ASSERT_EQ((LayoutUnit(-100) * LayoutUnit(3.33)).round(), -333);
    148148    ASSERT_EQ((LayoutUnit(-100) * LayoutUnit(-3.33)).round(), 333);
    149    
     149
     150    size_t aHundredSizeT = 100;
     151    ASSERT_EQ((LayoutUnit(aHundredSizeT) * LayoutUnit(1)).toInt(), 100);
     152    ASSERT_EQ((aHundredSizeT * LayoutUnit(4)).toInt(), 400);
     153    ASSERT_EQ((LayoutUnit(4) * aHundredSizeT).toInt(), 400);
     154
    150155    int quarterMax = intMaxForLayoutUnit / 4;
    151156    ASSERT_EQ((LayoutUnit(quarterMax) * LayoutUnit(2)).toInt(), quarterMax * 2);
     
    153158    ASSERT_EQ((LayoutUnit(quarterMax) * LayoutUnit(4)).toInt(), quarterMax * 4);
    154159    ASSERT_EQ((LayoutUnit(quarterMax) * LayoutUnit(5)).toInt(), intMaxForLayoutUnit);
     160
     161    size_t overflowIntSizeT = intMaxForLayoutUnit * 4;
     162    ASSERT_EQ((LayoutUnit(overflowIntSizeT) * LayoutUnit(2)).toInt(), intMaxForLayoutUnit);
     163    ASSERT_EQ((overflowIntSizeT * LayoutUnit(4)).toInt(), intMaxForLayoutUnit);
     164    ASSERT_EQ((LayoutUnit(4) * overflowIntSizeT).toInt(), intMaxForLayoutUnit);
    155165}
    156166
     
    184194    ASSERT_FLOAT_EQ((LayoutUnit(-0.5) / LayoutUnit(-2)).toFloat(), 0.25f);
    185195   
     196    size_t aHundredSizeT = 100;
     197    ASSERT_EQ((LayoutUnit(aHundredSizeT) / LayoutUnit(2)).toInt(), 50);
     198    ASSERT_EQ((aHundredSizeT / LayoutUnit(4)).toInt(), 25);
     199    ASSERT_EQ((LayoutUnit(400) / aHundredSizeT).toInt(), 4);
     200
    186201    ASSERT_EQ((LayoutUnit(intMaxForLayoutUnit) / LayoutUnit(2)).toInt(), intMaxForLayoutUnit / 2);
    187202    ASSERT_EQ((LayoutUnit(intMaxForLayoutUnit) / LayoutUnit(0.5)).toInt(), intMaxForLayoutUnit);
Note: See TracChangeset for help on using the changeset viewer.