From 71f56d8697ba20d18ff19e797fc148630e65c128 Mon Sep 17 00:00:00 2001 From: Hendiadyoin1 Date: Wed, 26 Jul 2023 15:03:51 +0200 Subject: [PATCH] LibWeb: Saturate result in PixelUnits::operator/ --- Tests/LibWeb/TestCSSPixels.cpp | 5 +++++ Userland/Libraries/LibWeb/PixelUnits.cpp | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/Tests/LibWeb/TestCSSPixels.cpp b/Tests/LibWeb/TestCSSPixels.cpp index 2948ef7231d..f403e109bb0 100644 --- a/Tests/LibWeb/TestCSSPixels.cpp +++ b/Tests/LibWeb/TestCSSPixels.cpp @@ -31,6 +31,11 @@ TEST_CASE(division1) CSSPixels b(5); CSSPixels c = a / b; EXPECT_EQ(c, CSSPixels(2)); + + a = CSSPixels::from_raw(0x3FFF'FFFF); // int_max / 2 + b = 0.25; + EXPECT(!a.might_be_saturated()); + EXPECT((a / b).might_be_saturated()); } TEST_CASE(multiplication1) diff --git a/Userland/Libraries/LibWeb/PixelUnits.cpp b/Userland/Libraries/LibWeb/PixelUnits.cpp index 5b1d6c0cb88..b373d9935eb 100644 --- a/Userland/Libraries/LibWeb/PixelUnits.cpp +++ b/Userland/Libraries/LibWeb/PixelUnits.cpp @@ -124,9 +124,12 @@ CSSPixels CSSPixels::operator*(CSSPixels const& other) const CSSPixels CSSPixels::operator/(CSSPixels const& other) const { - CSSPixels result; - result.set_raw_value(static_cast(fixed_point_denominator) * raw_value() / other.raw_value()); - return result; + i64 mult = raw_value(); + mult <<= fractional_bits; + mult /= other.raw_value(); + + int int_value = AK::clamp_to_int(mult); + return from_raw(int_value); } CSSPixels& CSSPixels::operator+=(CSSPixels const& other)