Skip to content

Commit b0db10d

Browse files
committed
use C++20 functions instead of __builtin_clz for log2
1 parent 08243fc commit b0db10d

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/log2.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55

66
#include "Assert.h"
77

8+
#include <bit>
89
#include <type_traits>
910

1011
namespace Bungee {
1112

1213
template <bool floor = false>
13-
static inline int log2(int x)
14+
static inline int log2(unsigned x)
1415
{
1516
BUNGEE_ASSERT1(x > 0);
1617
BUNGEE_ASSERT1(floor || !(x & (x << 1)));
1718

1819
int y;
1920
if constexpr (floor)
20-
y = __builtin_clz(1) - __builtin_clz(x);
21+
y = std::bit_width(x) - 1;
2122
else
22-
y = __builtin_ctz(x);
23+
y = std::countr_zero(x);
2324

2425
BUNGEE_ASSERT1(floor ? (1 << y <= x && x < 2 << y) : (x == 1 << y));
2526
return y;

0 commit comments

Comments
 (0)