2022-01-27 12:22:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Leon Albrecht <leon2002.la@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2022-01-29 15:51:02 +00:00
|
|
|
#include <AK/BuiltinWrappers.h>
|
2022-01-27 12:22:23 +00:00
|
|
|
#include <AK/Concepts.h>
|
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
#include <AK/Format.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2022-01-29 15:51:02 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-01-27 12:22:23 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|