From 85267916176cec32db31fd8807513f33f1e51c40 Mon Sep 17 00:00:00 2001 From: Hendiadyoin1 Date: Sun, 23 Jul 2023 19:46:46 +0200 Subject: [PATCH] AK: Use wider type for FixedPoint division This allows us to shift first and then divide, preserving more precision --- AK/FixedPoint.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/AK/FixedPoint.h b/AK/FixedPoint.h index 72d668f488..a0316ff200 100644 --- a/AK/FixedPoint.h +++ b/AK/FixedPoint.h @@ -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; + + DivRes value = raw(); + value <<= precision; + value /= other.raw(); + + return create_raw(value); } template @@ -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; }