serenity/AK/IntegralMath.h
Steffen Rusitschka 1aa07d7328 AK: Implement FloatExtractor<f128>
This patch adds support for 128-bit floating points in FloatExtractor.

This is required to build SerenityOS on MacOS/aarch64. It might break
building for Raspberry Pi.
2022-12-02 16:22:51 +01:00

57 lines
996 B
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 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;
}
}