1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 18:10:56 +09:00

LibWeb: Inline and constexpr most PixelUnits methods

This commit is contained in:
Hendiadyoin1 2023-07-29 16:01:06 +02:00 committed by Alexander Kalenik
parent 1e5ba09d76
commit cc74083f27
Notes: sideshowbarker 2024-07-17 08:27:05 +09:00
2 changed files with 131 additions and 179 deletions

View file

@ -9,44 +9,6 @@
namespace Web {
CSSPixels::CSSPixels(int value)
{
if (value > max_integer_value) [[unlikely]]
m_value = NumericLimits<int>::max();
else if (value < min_integer_value) [[unlikely]]
m_value = NumericLimits<int>::min();
else
m_value = value << fractional_bits;
}
CSSPixels::CSSPixels(unsigned int value)
{
if (value > max_integer_value) [[unlikely]]
m_value = NumericLimits<int>::max();
else
m_value = static_cast<int>(value) << fractional_bits;
}
CSSPixels::CSSPixels(unsigned long value)
{
if (value > max_integer_value) [[unlikely]]
m_value = NumericLimits<int>::max();
else
m_value = static_cast<int>(value) << fractional_bits;
}
CSSPixels::CSSPixels(float value)
{
if (!isnan(value))
m_value = AK::clamp_to_int(value * fixed_point_denominator);
}
CSSPixels::CSSPixels(double value)
{
if (!isnan(value))
m_value = AK::clamp_to_int(value * fixed_point_denominator);
}
float CSSPixels::to_float() const
{
return static_cast<float>(m_value) / fixed_point_denominator;
@ -62,111 +24,4 @@ int CSSPixels::to_int() const
return m_value / fixed_point_denominator;
}
bool CSSPixels::might_be_saturated() const
{
return raw_value() == NumericLimits<i32>::max() || raw_value() == NumericLimits<i32>::min();
}
bool CSSPixels::operator==(CSSPixels const& other) const = default;
CSSPixels& CSSPixels::operator++()
{
m_value = Checked<int>::saturating_add(m_value, fixed_point_denominator);
return *this;
}
CSSPixels& CSSPixels::operator--()
{
m_value = Checked<int>::saturating_sub(m_value, fixed_point_denominator);
return *this;
}
int CSSPixels::operator<=>(CSSPixels const& other) const
{
return raw_value() > other.raw_value() ? 1 : raw_value() < other.raw_value() ? -1
: 0;
}
CSSPixels CSSPixels::operator+() const
{
return from_raw(+raw_value());
}
CSSPixels CSSPixels::operator-() const
{
return from_raw(-raw_value());
}
CSSPixels CSSPixels::operator+(CSSPixels const& other) const
{
return from_raw(Checked<int>::saturating_add(raw_value(), other.raw_value()));
}
CSSPixels CSSPixels::operator-(CSSPixels const& other) const
{
return from_raw(Checked<int>::saturating_sub(raw_value(), other.raw_value()));
}
CSSPixels CSSPixels::operator*(CSSPixels const& other) const
{
i64 value = raw_value();
value *= other.raw_value();
int int_value = AK::clamp_to_int(value >> fractional_bits);
// Rounding:
// If last bit cut off was 1:
if (value & (1u << (fractional_bits - 1))) {
// If the bit after was 1 as well
if (value & (radix_mask >> 2u)) {
// We need to round away from 0
int_value = Checked<int>::saturating_add(int_value, 1);
} else {
// Otherwise we round to the next even value
// Which means we add the least significant bit of the raw integer value
int_value = Checked<int>::saturating_add(int_value, int_value & 1);
}
}
return from_raw(int_value);
}
CSSPixels CSSPixels::operator/(CSSPixels const& other) const
{
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)
{
*this = *this + other;
return *this;
}
CSSPixels& CSSPixels::operator-=(CSSPixels const& other)
{
*this = *this - other;
return *this;
}
CSSPixels& CSSPixels::operator*=(CSSPixels const& other)
{
*this = *this * other;
return *this;
}
CSSPixels& CSSPixels::operator/=(CSSPixels const& other)
{
*this = *this / other;
return *this;
}
CSSPixels CSSPixels::abs() const
{
return from_raw(::abs(m_value));
}
}