Title: [138041] trunk/Source/WebCore
Revision
138041
Author
e...@chromium.org
Date
2012-12-18 10:46:10 -0800 (Tue, 18 Dec 2012)

Log Message

Optimize LayoutUnit::boundedMultiply
https://bugs.webkit.org/show_bug.cgi?id=105216

Reviewed by Levi Weintraub.

LayoutUnit::boundedMultiply is used for multiplication that's
prone to overflow and for all LayoutUnit multiplication if
SATURATED_LAYOUT_ARITHMETIC is enabled. The current approach is
quite inefficient.

Change it to use a more efficient saturated multiplication
implementation. As the implementation needs to use
kFixedPointDenominator it is implemented in LayoutUnit.h instead
of in wtf/SaturatedArithmetic.h.

Test: Covered by TestWebKitAPI/Tests/WebCore/LayoutUnit.cpp

* platform/LayoutUnit.h:
(WebCore::boundedMultiply):

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (138040 => 138041)


--- trunk/Source/WebCore/ChangeLog	2012-12-18 18:43:38 UTC (rev 138040)
+++ trunk/Source/WebCore/ChangeLog	2012-12-18 18:46:10 UTC (rev 138041)
@@ -1,3 +1,25 @@
+2012-12-17  Emil A Eklund  <e...@chromium.org>
+
+        Optimize LayoutUnit::boundedMultiply
+        https://bugs.webkit.org/show_bug.cgi?id=105216
+
+        Reviewed by Levi Weintraub.
+
+        LayoutUnit::boundedMultiply is used for multiplication that's
+        prone to overflow and for all LayoutUnit multiplication if
+        SATURATED_LAYOUT_ARITHMETIC is enabled. The current approach is
+        quite inefficient.
+
+        Change it to use a more efficient saturated multiplication
+        implementation. As the implementation needs to use
+        kFixedPointDenominator it is implemented in LayoutUnit.h instead
+        of in wtf/SaturatedArithmetic.h.
+
+        Test: Covered by TestWebKitAPI/Tests/WebCore/LayoutUnit.cpp
+
+        * platform/LayoutUnit.h:
+        (WebCore::boundedMultiply):
+
 2012-12-18  Bear Travis  <betra...@adobe.com>
 
         [CSS Exclusions] Blocks should not re-use their parent's ExclusionShapeInsideInfo

Modified: trunk/Source/WebCore/platform/LayoutUnit.h (138040 => 138041)


--- trunk/Source/WebCore/platform/LayoutUnit.h	2012-12-18 18:43:38 UTC (rev 138040)
+++ trunk/Source/WebCore/platform/LayoutUnit.h	2012-12-18 18:46:10 UTC (rev 138041)
@@ -483,15 +483,19 @@
 inline LayoutUnit boundedMultiply(const LayoutUnit& a, const LayoutUnit& b)
 {
 #if ENABLE(SUBPIXEL_LAYOUT)
+    int64_t result = static_cast<int64_t>(a.rawValue()) * static_cast<int64_t>(b.rawValue()) / kEffectiveFixedPointDenominator;
+    int32_t high = result >> 32;
+    int32_t low = result;
+    uint32_t saturated = (static_cast<uint32_t>(a.rawValue() ^ b.rawValue()) >> 31) + std::numeric_limits<int>::max();
+    // If the higher 32 bits does not match the lower 32 with sign extension the operation overflowed.
+    if (high != low >> 31)
+        result = saturated;
+
     LayoutUnit returnVal;
-    long long rawVal = static_cast<long long>(a.rawValue()) * b.rawValue() / kEffectiveFixedPointDenominator;
-    if (rawVal > std::numeric_limits<int>::max())
-        return LayoutUnit::max();
-    if (rawVal < std::numeric_limits<int>::min())
-        return LayoutUnit::min();
-    returnVal.setRawValue(rawVal);
+    returnVal.setRawValue(static_cast<int>(result));
     return returnVal;
 #else
+    // FIXME: Should be bounded even in the non-subpixel case.
     return a.rawValue() * b.rawValue();
 #endif
 }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to