1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:47:34 +00:00

AK+Everywhere: Replace __builtin bit functions

In order to reduce our reliance on __builtin_{ffs, clz, ctz, popcount},
this commit removes all calls to these functions and replaces them with
the equivalent functions in AK/BuiltinWrappers.h.
This commit is contained in:
Nick Johnson 2021-12-19 15:46:55 -06:00 committed by Andreas Kling
parent 26bb3e1acf
commit 08e4a1a4dc
20 changed files with 108 additions and 115 deletions

View file

@ -6,6 +6,7 @@
#pragma once
#include <AK/BuiltinWrappers.h>
#include <AK/Concepts.h>
#include <AK/StdLibExtraDetails.h>
#include <AK/Types.h>
@ -45,21 +46,6 @@ constexpr size_t product_odd() { return value * product_odd<value - 2>(); }
return __builtin_##function##f(args); \
}
#define INTEGER_BUILTIN(name) \
template<Integral T> \
constexpr T name(T x) \
{ \
if constexpr (sizeof(T) == sizeof(long long)) \
return __builtin_##name##ll(x); \
if constexpr (sizeof(T) == sizeof(long)) \
return __builtin_##name##l(x); \
return __builtin_##name(x); \
}
INTEGER_BUILTIN(clz);
INTEGER_BUILTIN(ctz);
INTEGER_BUILTIN(popcnt);
namespace Division {
template<FloatingPoint T>
constexpr T fmod(T x, T y)
@ -312,7 +298,7 @@ constexpr T log2(T x)
template<Integral T>
constexpr T log2(T x)
{
return x ? 8 * sizeof(T) - clz(x) : 0;
return x ? 8 * sizeof(T) - count_leading_zeroes(static_cast<MakeUnsigned<T>>(x)) : 0;
}
template<FloatingPoint T>
@ -468,6 +454,5 @@ constexpr T pow(T x, T y)
}
#undef CONSTEXPR_STATE
#undef INTEGER_BUILTIN
}