From 65f57efb5bcd4f85f6bbdfea682aa9266da15383 Mon Sep 17 00:00:00 2001 From: Hendiadyoin1 Date: Wed, 13 Apr 2022 21:51:27 +0200 Subject: [PATCH] LibGfx: Specialize Rect::to_rounded a bit more We were always calling llround[fd], even for floating point targets. Also for rounding to integer, we don't need to have C99's rounding rules and can just cast, assuming the standard rounding mode. --- Userland/Libraries/LibGfx/Rect.h | 46 +++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibGfx/Rect.h b/Userland/Libraries/LibGfx/Rect.h index 82534cae6f..9c6f63afaf 100644 --- a/Userland/Libraries/LibGfx/Rect.h +++ b/Userland/Libraries/LibGfx/Rect.h @@ -697,24 +697,46 @@ public: return Rect(*this); } - template + template [[nodiscard]] ALWAYS_INLINE Rect to_rounded() const { + // FIXME: We may get away with `rint[lf]?()` here. + // This would even give us some more control of these internals, + // while the break-tie algorithm does not really matter if constexpr (IsSame) { return { - static_cast(llroundf(x())), - static_cast(llroundf(y())), - static_cast(llroundf(width())), - static_cast(llroundf(height())), - }; - } else { - return { - static_cast(llroundd(x())), - static_cast(llroundd(y())), - static_cast(llroundd(width())), - static_cast(llroundd(height())), + static_cast(roundf(x())), + static_cast(roundf(y())), + static_cast(roundf(width())), + static_cast(roundf(height())), }; } + if constexpr (IsSame) { + return { + static_cast(round(x())), + static_cast(round(y())), + static_cast(round(width())), + static_cast(round(height())), + }; + } + + return { + static_cast(roundl(x())), + static_cast(roundl(y())), + static_cast(roundl(width())), + static_cast(roundl(height())), + }; + } + + template + ALWAYS_INLINE Rect to_rounded() const + { + return { + round_to(x()), + round_to(y()), + round_to(width()), + round_to(height()), + }; } [[nodiscard]] String to_string() const;