diff options
author | Hendiadyoin1 <leon.a@serenityos.org> | 2022-04-13 21:51:27 +0200 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-05-07 20:25:39 +0200 |
commit | 65f57efb5bcd4f85f6bbdfea682aa9266da15383 (patch) | |
tree | 00155a0cd7b028847e0af32fdb3ac1ed7cd2c4c2 /Userland | |
parent | 37ff2b9bd2399004df46246294c102509b0f0db9 (diff) | |
download | serenity-65f57efb5bcd4f85f6bbdfea682aa9266da15383.zip |
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.
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibGfx/Rect.h | 42 |
1 files changed, 32 insertions, 10 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<U>(*this); } - template<typename U> + template<FloatingPoint U> [[nodiscard]] ALWAYS_INLINE Rect<U> 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<T, float>) { return { - static_cast<U>(llroundf(x())), - static_cast<U>(llroundf(y())), - static_cast<U>(llroundf(width())), - static_cast<U>(llroundf(height())), + static_cast<U>(roundf(x())), + static_cast<U>(roundf(y())), + static_cast<U>(roundf(width())), + static_cast<U>(roundf(height())), }; - } else { + } + if constexpr (IsSame<T, double>) { return { - static_cast<U>(llroundd(x())), - static_cast<U>(llroundd(y())), - static_cast<U>(llroundd(width())), - static_cast<U>(llroundd(height())), + static_cast<U>(round(x())), + static_cast<U>(round(y())), + static_cast<U>(round(width())), + static_cast<U>(round(height())), }; } + + return { + static_cast<U>(roundl(x())), + static_cast<U>(roundl(y())), + static_cast<U>(roundl(width())), + static_cast<U>(roundl(height())), + }; + } + + template<Integral I> + ALWAYS_INLINE Rect<I> to_rounded() const + { + return { + round_to<I>(x()), + round_to<I>(y()), + round_to<I>(width()), + round_to<I>(height()), + }; } [[nodiscard]] String to_string() const; |