forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntegralMath.h
71 lines (59 loc) · 1.41 KB
/
IntegralMath.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* Copyright (c) 2022, Leon Albrecht <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/BuiltinWrappers.h>
#include <AK/Concepts.h>
#include <AK/Types.h>
namespace AK {
template<Integral T>
constexpr T exp2(T exponent)
{
return 1u << exponent;
}
template<Integral T>
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 & ((((T)1) << (log - 1)) - 1)) != 0;
return log;
}
template<Integral I>
constexpr I pow(I base, I exponent)
{
// https://en.wikipedia.org/wiki/Exponentiation_by_squaring
if (exponent < 0)
return 0;
if (exponent == 0)
return 1;
I res = 1;
while (exponent > 0) {
if (exponent & 1)
res *= base;
base *= base;
exponent /= 2u;
}
return res;
}
template<auto base, Unsigned U = decltype(base)>
constexpr bool is_power_of(U x)
{
if constexpr (base == 2)
return is_power_of_two(x);
// FIXME: I am naive! A log2-based approach (pow<U>(base, (log2(x) / log2(base))) == x) does not work due to rounding errors.
for (U exponent = 0; exponent <= log2(x) / log2(base) + 1; ++exponent) {
if (pow<U>(base, exponent) == x)
return true;
}
return false;
}
}