diff options
author | kleines Filmröllchen <filmroellchen@serenityos.org> | 2023-02-17 18:56:33 +0100 |
---|---|---|
committer | Andrew Kaster <andrewdkaster@gmail.com> | 2023-03-10 04:07:14 -0700 |
commit | b38beb106e010e327d5956f3a0e825b0d2632dec (patch) | |
tree | 82eb0e5f5c5dfe7b26e037df7b803fe41ea2e42c /AK/Math.h | |
parent | 6f29ea997650b44507c4e713cff2f5828b35b3ac (diff) | |
download | serenity-b38beb106e010e327d5956f3a0e825b0d2632dec.zip |
AK: Add constexpr floor and round
Diffstat (limited to 'AK/Math.h')
-rw-r--r-- | AK/Math.h | 30 |
1 files changed, 30 insertions, 0 deletions
@@ -757,6 +757,36 @@ constexpr T ceil(T num) #endif } +template<FloatingPoint T> +constexpr T floor(T num) +{ + if (is_constant_evaluated()) { + if (num < NumericLimits<i64>::min() || num > NumericLimits<i64>::max()) + return num; + return (static_cast<T>(static_cast<i64>(num)) == num) + ? static_cast<i64>(num) + : static_cast<i64>(num) - ((num > 0) ? 0 : 1); + } +#if ARCH(AARCH64) + AARCH64_INSTRUCTION(frintm, num); +#else + return __builtin_floor(num); +#endif +} + +template<FloatingPoint T> +constexpr T round(T x) +{ + CONSTEXPR_STATE(round, x); + // Note: This is break-tie-away-from-zero, so not the hw's understanding of + // "nearest", which would be towards even. + if (x == 0.) + return x; + if (x > 0.) + return floor(x + .5); + return ceil(x - .5); +} + #undef CONSTEXPR_STATE #undef AARCH64_INSTRUCTION } |