1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 08:47:44 +00:00

LibWeb: Perform rounding when dividing CSSPixels

This should allow us to produce results that more closely match old
layouts when divisions were done in floating-point.
This commit is contained in:
Zaggy1024 2023-09-03 19:03:03 -05:00 committed by Alexander Kalenik
parent bd85e1b30b
commit 98926b487c
27 changed files with 250 additions and 233 deletions

View file

@ -284,6 +284,14 @@ constexpr CSSPixels operator*(unsigned long left, CSSPixels right) { return righ
inline float operator*(float left, CSSPixels right) { return right.to_float() * left; }
inline double operator*(double left, CSSPixels right) { return right.to_double() * left; }
template<Integral T>
constexpr static T rounding_divide(T dividend, T divisor)
{
if ((dividend < 0) == (divisor < 0))
return (dividend + (divisor / 2)) / divisor;
return (dividend - (divisor / 2)) / divisor;
}
class CSSPixelFraction {
public:
constexpr CSSPixelFraction(CSSPixels numerator, CSSPixels denominator)
@ -309,7 +317,7 @@ public:
{
i64 wide_value = m_numerator.raw_value();
wide_value <<= CSSPixels::fractional_bits;
wide_value /= m_denominator.raw_value();
wide_value = rounding_divide<i64>(wide_value, m_denominator.raw_value());
return CSSPixels::from_raw(AK::clamp_to_int(wide_value));
}
@ -347,7 +355,7 @@ constexpr CSSPixels CSSPixels::operator*(CSSPixelFraction const& other) const
{
i64 wide_value = raw_value();
wide_value *= other.numerator().raw_value();
wide_value /= other.denominator().raw_value();
wide_value = rounding_divide<i64>(wide_value, other.denominator().raw_value());
return CSSPixels::from_raw(AK::clamp_to_int(wide_value));
}
@ -359,7 +367,7 @@ constexpr CSSPixels CSSPixels::operator/(CSSPixelFraction const& other) const
{
i64 wide_value = raw_value();
wide_value *= other.denominator().raw_value();
wide_value /= other.numerator().raw_value();
wide_value = rounding_divide<i64>(wide_value, other.numerator().raw_value());
return CSSPixels::from_raw(AK::clamp_to_int(wide_value));
}