AK: Use wider type for FixedPoint division

This allows us to shift first and then divide, preserving more precision
This commit is contained in:
Hendiadyoin1 2023-07-23 19:46:46 +02:00 committed by Andrew Kaster
parent e609ac74a3
commit 8526791617
Notes: sideshowbarker 2024-07-17 14:33:07 +09:00

View file

@ -226,8 +226,14 @@ public:
}
constexpr This operator/(This const& other) const
{
// FIXME: Better rounding?
return create_raw((m_value / other.m_value) << (precision));
// FIXME: Figure out a way to use more narrow types and avoid __int128
using DivRes = Conditional<sizeof(Underlying) < sizeof(i64), i64, __int128>;
DivRes value = raw();
value <<= precision;
value /= other.raw();
return create_raw(value);
}
template<Integral I>
@ -278,9 +284,7 @@ public:
}
This& operator/=(This const& other)
{
// FIXME: See above
m_value /= other.raw();
m_value <<= precision;
*this = *this / other;
return *this;
}