mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-12-30 22:54:35 +03:00
AK: Change standard casting method of FixedPoint to truncation
This matches what floats do. Also fix typo `trunk`->`trunc`
This commit is contained in:
parent
8526791617
commit
05c959e40b
Notes:
sideshowbarker
2024-07-18 05:37:06 +09:00
Author: https://github.com/Hendiadyoin1 Commit: https://github.com/SerenityOS/serenity/commit/05c959e40b Pull-request: https://github.com/SerenityOS/serenity/pull/20163 Reviewed-by: https://github.com/DanShaders
@ -66,19 +66,7 @@ public:
|
|||||||
template<Integral I>
|
template<Integral I>
|
||||||
explicit constexpr operator I() const
|
explicit constexpr operator I() const
|
||||||
{
|
{
|
||||||
I value = m_value >> precision;
|
return trunc().raw() >> precision;
|
||||||
// fract(m_value) >= .5?
|
|
||||||
if (m_value & (1u << (precision - 1))) {
|
|
||||||
// fract(m_value) > .5?
|
|
||||||
if (m_value & (radix_mask >> 2u)) {
|
|
||||||
// yes: round "up";
|
|
||||||
value += (m_value > 0 ? 1 : -1);
|
|
||||||
} else {
|
|
||||||
// no: round to even;
|
|
||||||
value += value & 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static constexpr This create_raw(Underlying value)
|
static constexpr This create_raw(Underlying value)
|
||||||
@ -111,9 +99,23 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Note/FIXME: This uses round to nearest break-tie to even
|
||||||
|
// Not break-tie away from 0 as the C99's round does
|
||||||
constexpr This round() const
|
constexpr This round() const
|
||||||
{
|
{
|
||||||
return This { static_cast<Underlying>(*this) };
|
Underlying value = m_value >> precision;
|
||||||
|
// fract(m_value) >= .5?
|
||||||
|
if (m_value & (1u << (precision - 1))) {
|
||||||
|
// fract(m_value) > .5?
|
||||||
|
if (m_value & (radix_mask >> 2u)) {
|
||||||
|
// yes: round "up";
|
||||||
|
value += (m_value > 0 ? 1 : -1);
|
||||||
|
} else {
|
||||||
|
// no: round to even;
|
||||||
|
value += value & 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
constexpr This floor() const
|
constexpr This floor() const
|
||||||
{
|
{
|
||||||
@ -124,7 +126,7 @@ public:
|
|||||||
return create_raw((m_value & ~radix_mask)
|
return create_raw((m_value & ~radix_mask)
|
||||||
+ (m_value & radix_mask ? 1 << precision : 0));
|
+ (m_value & radix_mask ? 1 << precision : 0));
|
||||||
}
|
}
|
||||||
constexpr This trunk() const
|
constexpr This trunc() const
|
||||||
{
|
{
|
||||||
return create_raw((m_value & ~radix_mask)
|
return create_raw((m_value & ~radix_mask)
|
||||||
+ ((m_value & radix_mask)
|
+ ((m_value & radix_mask)
|
||||||
@ -132,14 +134,14 @@ public:
|
|||||||
: 0));
|
: 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr Underlying lround() const { return static_cast<Underlying>(*this); }
|
constexpr Underlying lround() const { return round().raw() >> precision; }
|
||||||
constexpr Underlying lfloor() const { return m_value >> precision; }
|
constexpr Underlying lfloor() const { return m_value >> precision; }
|
||||||
constexpr Underlying lceil() const
|
constexpr Underlying lceil() const
|
||||||
{
|
{
|
||||||
return (m_value >> precision)
|
return (m_value >> precision)
|
||||||
+ (m_value & radix_mask ? 1 : 0);
|
+ (m_value & radix_mask ? 1 : 0);
|
||||||
}
|
}
|
||||||
constexpr Underlying ltrunk() const
|
constexpr Underlying ltrunc() const
|
||||||
{
|
{
|
||||||
return (m_value >> precision)
|
return (m_value >> precision)
|
||||||
+ ((m_value & radix_mask)
|
+ ((m_value & radix_mask)
|
||||||
@ -437,7 +439,7 @@ struct Formatter<FixedPoint<precision, Underlying>> : StandardFormatter {
|
|||||||
if constexpr (IsSigned<Underlying>)
|
if constexpr (IsSigned<Underlying>)
|
||||||
is_negative = value < 0;
|
is_negative = value < 0;
|
||||||
|
|
||||||
i64 integer = value.ltrunk();
|
i64 integer = value.ltrunc();
|
||||||
constexpr u64 one = static_cast<Underlying>(1) << precision;
|
constexpr u64 one = static_cast<Underlying>(1) << precision;
|
||||||
u64 fraction_raw = value.raw() & (one - 1);
|
u64 fraction_raw = value.raw() & (one - 1);
|
||||||
return builder.put_fixed_point(is_negative, integer, fraction_raw, one, base, upper_case, m_zero_pad, m_use_separator, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode, real_number_display_mode);
|
return builder.put_fixed_point(is_negative, integer, fraction_raw, one, base, upper_case, m_zero_pad, m_use_separator, m_align, m_width.value(), m_precision.value(), m_fill, m_sign_mode, real_number_display_mode);
|
||||||
|
@ -41,42 +41,42 @@ TEST_CASE(rounding)
|
|||||||
EXPECT_EQ(Type(0.5).round(), Type(0));
|
EXPECT_EQ(Type(0.5).round(), Type(0));
|
||||||
EXPECT_EQ(Type(0.5).floor(), Type(0));
|
EXPECT_EQ(Type(0.5).floor(), Type(0));
|
||||||
EXPECT_EQ(Type(0.5).ceil(), Type(1));
|
EXPECT_EQ(Type(0.5).ceil(), Type(1));
|
||||||
EXPECT_EQ(Type(0.75).trunk(), Type(0));
|
EXPECT_EQ(Type(0.75).trunc(), Type(0));
|
||||||
|
|
||||||
EXPECT_EQ(Type(1.5).round(), Type(2));
|
EXPECT_EQ(Type(1.5).round(), Type(2));
|
||||||
EXPECT_EQ(Type(1.5).floor(), Type(1));
|
EXPECT_EQ(Type(1.5).floor(), Type(1));
|
||||||
EXPECT_EQ(Type(1.5).ceil(), Type(2));
|
EXPECT_EQ(Type(1.5).ceil(), Type(2));
|
||||||
EXPECT_EQ(Type(1.25).trunk(), Type(1));
|
EXPECT_EQ(Type(1.25).trunc(), Type(1));
|
||||||
|
|
||||||
EXPECT_EQ(Type(-0.5).round(), Type(0));
|
EXPECT_EQ(Type(-0.5).round(), Type(0));
|
||||||
EXPECT_EQ(Type(-0.5).floor(), Type(-1));
|
EXPECT_EQ(Type(-0.5).floor(), Type(-1));
|
||||||
EXPECT_EQ(Type(-0.5).ceil(), Type(0));
|
EXPECT_EQ(Type(-0.5).ceil(), Type(0));
|
||||||
EXPECT_EQ(Type(-0.75).trunk(), Type(0));
|
EXPECT_EQ(Type(-0.75).trunc(), Type(0));
|
||||||
|
|
||||||
EXPECT_EQ(Type(-1.5).round(), Type(-2));
|
EXPECT_EQ(Type(-1.5).round(), Type(-2));
|
||||||
EXPECT_EQ(Type(-1.5).floor(), Type(-2));
|
EXPECT_EQ(Type(-1.5).floor(), Type(-2));
|
||||||
EXPECT_EQ(Type(-1.5).ceil(), Type(-1));
|
EXPECT_EQ(Type(-1.5).ceil(), Type(-1));
|
||||||
EXPECT_EQ(Type(-1.25).trunk(), Type(-1));
|
EXPECT_EQ(Type(-1.25).trunc(), Type(-1));
|
||||||
|
|
||||||
EXPECT_EQ(Type(0.5).lround(), 0);
|
EXPECT_EQ(Type(0.5).lround(), 0);
|
||||||
EXPECT_EQ(Type(0.5).lfloor(), 0);
|
EXPECT_EQ(Type(0.5).lfloor(), 0);
|
||||||
EXPECT_EQ(Type(0.5).lceil(), 1);
|
EXPECT_EQ(Type(0.5).lceil(), 1);
|
||||||
EXPECT_EQ(Type(0.5).ltrunk(), 0);
|
EXPECT_EQ(Type(0.5).ltrunc(), 0);
|
||||||
|
|
||||||
EXPECT_EQ(Type(1.5).lround(), 2);
|
EXPECT_EQ(Type(1.5).lround(), 2);
|
||||||
EXPECT_EQ(Type(1.5).lfloor(), 1);
|
EXPECT_EQ(Type(1.5).lfloor(), 1);
|
||||||
EXPECT_EQ(Type(1.5).lceil(), 2);
|
EXPECT_EQ(Type(1.5).lceil(), 2);
|
||||||
EXPECT_EQ(Type(1.5).ltrunk(), 1);
|
EXPECT_EQ(Type(1.5).ltrunc(), 1);
|
||||||
|
|
||||||
EXPECT_EQ(Type(-0.5).lround(), 0);
|
EXPECT_EQ(Type(-0.5).lround(), 0);
|
||||||
EXPECT_EQ(Type(-0.5).lfloor(), -1);
|
EXPECT_EQ(Type(-0.5).lfloor(), -1);
|
||||||
EXPECT_EQ(Type(-0.5).lceil(), 0);
|
EXPECT_EQ(Type(-0.5).lceil(), 0);
|
||||||
EXPECT_EQ(Type(-0.5).ltrunk(), 0);
|
EXPECT_EQ(Type(-0.5).ltrunc(), 0);
|
||||||
|
|
||||||
EXPECT_EQ(Type(-1.5).lround(), -2);
|
EXPECT_EQ(Type(-1.5).lround(), -2);
|
||||||
EXPECT_EQ(Type(-1.5).lfloor(), -2);
|
EXPECT_EQ(Type(-1.5).lfloor(), -2);
|
||||||
EXPECT_EQ(Type(-1.5).lceil(), -1);
|
EXPECT_EQ(Type(-1.5).lceil(), -1);
|
||||||
EXPECT_EQ(Type(-1.5).ltrunk(), -1);
|
EXPECT_EQ(Type(-1.5).ltrunc(), -1);
|
||||||
|
|
||||||
// Check that sRGB TRC curve parameters match the s15fixed16 values stored in Gimp's built-in profile.
|
// Check that sRGB TRC curve parameters match the s15fixed16 values stored in Gimp's built-in profile.
|
||||||
// (This only requires that the FixedPoint<> constructor rounds before truncating to the fixed-point value,
|
// (This only requires that the FixedPoint<> constructor rounds before truncating to the fixed-point value,
|
||||||
|
@ -116,7 +116,7 @@ FixedPoint<16, u32> DMT::MonitorTiming::vertical_frequency_hz() const
|
|||||||
|
|
||||||
u32 DMT::MonitorTiming::refresh_rate_hz() const
|
u32 DMT::MonitorTiming::refresh_rate_hz() const
|
||||||
{
|
{
|
||||||
return vertical_frequency_hz().ltrunk();
|
return vertical_frequency_hz().ltrunc();
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef KERNEL
|
#ifndef KERNEL
|
||||||
|
Loading…
Reference in New Issue
Block a user