summaryrefslogtreecommitdiff
path: root/AK/Math.h
diff options
context:
space:
mode:
authorMacDue <macdue@dueutil.tech>2022-06-29 18:56:39 +0100
committerAndreas Kling <kling@serenityos.org>2022-06-30 11:16:22 +0200
commit072a78b958de7510cadf174b73eaa1b1584f5c00 (patch)
treef64eab29f23bc9aacd4379a9f97c27f01ba2d2e2 /AK/Math.h
parent1e3622432107dc5b47242da88479e0c189e7a488 (diff)
downloadserenity-072a78b958de7510cadf174b73eaa1b1584f5c00.zip
AK: Add AK::ceil(float) and AK::ceil_log2(integer)
Co-authored-by: Leon Albrecht <leon2002.la@gmail.com>
Diffstat (limited to 'AK/Math.h')
-rw-r--r--AK/Math.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/AK/Math.h b/AK/Math.h
index 1dd4e3c6ed..ebf9856f93 100644
--- a/AK/Math.h
+++ b/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
}