mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 23:20:20 +00:00
AK: Add AK::ceil(float) and AK::ceil_log2(integer)
Co-authored-by: Leon Albrecht <leon2002.la@gmail.com>
This commit is contained in:
parent
1e36224321
commit
072a78b958
Notes:
sideshowbarker
2024-07-17 09:52:05 +09:00
Author: https://github.com/MacDue Commit: https://github.com/SerenityOS/serenity/commit/072a78b958 Pull-request: https://github.com/SerenityOS/serenity/pull/14413 Reviewed-by: https://github.com/Hendiadyoin1 Reviewed-by: https://github.com/alimpfard
2 changed files with 25 additions and 0 deletions
|
@ -24,6 +24,17 @@ constexpr T log2(T x)
|
|||
return x ? (8 * sizeof(T) - 1) - count_leading_zeroes(static_cast<MakeUnsigned<T>>(x)) : 0;
|
||||
}
|
||||
|
||||
template<Integral T>
|
||||
constexpr T ceil_log2(T x)
|
||||
{
|
||||
if (!x)
|
||||
return 0;
|
||||
|
||||
T log = AK::log2(x);
|
||||
log += (x & ((1 << (log - 1)) - 1)) != 0;
|
||||
return log;
|
||||
}
|
||||
|
||||
template<Integral I>
|
||||
constexpr I pow(I base, I exponent)
|
||||
{
|
||||
|
|
14
AK/Math.h
14
AK/Math.h
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include <AK/BuiltinWrappers.h>
|
||||
#include <AK/Concepts.h>
|
||||
#include <AK/NumericLimits.h>
|
||||
#include <AK/StdLibExtraDetails.h>
|
||||
#include <AK/Types.h>
|
||||
|
||||
|
@ -665,6 +666,19 @@ constexpr T pow(T x, T y)
|
|||
return exp2<T>(y * log2<T>(x));
|
||||
}
|
||||
|
||||
template<FloatingPoint T>
|
||||
constexpr T ceil(T num)
|
||||
{
|
||||
if (is_constant_evaluated()) {
|
||||
if (num < NumericLimits<i64>::min() || num > NumericLimits<i64>::max())
|
||||
return num;
|
||||
return (static_cast<double>(static_cast<i64>(num)) == num)
|
||||
? static_cast<i64>(num)
|
||||
: static_cast<i64>(num) + ((num > 0) ? 1 : 0);
|
||||
}
|
||||
return __builtin_ceil(num);
|
||||
}
|
||||
|
||||
#undef CONSTEXPR_STATE
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue