Adam Macdonald a8d8b9b9ab
All checks were successful
Build (Arch Linux) / build (push) Successful in 3m10s
initial commit
2025-04-16 01:58:29 +01:00

38 lines
762 B
C++

#pragma once
#include <cstdint>
namespace kuiper
{
// https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
inline std::uint64_t next_pow2(std::uint64_t value) {
value--;
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
value |= value >> 32;
value++;
value += (value == 0); // Case for if value == 0 (returns 0 as the next power of 2, which 0 is not)
return value;
}
inline std::uint32_t next_pow2(std::uint32_t value) {
value--;
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
value++;
value += (value == 0);
return value;
}
} // namespace kuiper