mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add constexpr floor and round
This commit is contained in:
parent
6f29ea9976
commit
b38beb106e
Notes:
sideshowbarker
2024-07-16 23:07:21 +09:00
Author: https://github.com/kleinesfilmroellchen Commit: https://github.com/SerenityOS/serenity/commit/b38beb106e Pull-request: https://github.com/SerenityOS/serenity/pull/17512 Reviewed-by: https://github.com/ADKaster ✅ Reviewed-by: https://github.com/davidot ✅ Reviewed-by: https://github.com/timschumi
1 changed files with 30 additions and 0 deletions
30
AK/Math.h
30
AK/Math.h
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue