ladybird/AK/IntegralMath.h
Abuneri b5bed37074 AK: Replace FP math in is_power_of with a purely integral algorithm
The previous naive approach was causing test failures because of
rounding issues in some exotic environments. In particular, MSVC
via MSBuild
2024-05-07 16:43:34 -06:00

78 lines
1.4 KiB
C++

/*
* Copyright (c) 2022, Leon Albrecht <leon2002.la@gmail.com>
*
* 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 static_cast<T>(1) << 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 == 1)
return x == 1;
else if constexpr (base == 2)
return is_power_of_two(x);
if (base == 0 && x == 0)
return true;
if (base == 0 || x == 0)
return false;
while (x != 1) {
if (x % base != 0)
return false;
x /= base;
}
return true;
}
}