#pragma once #include #include #include #include namespace Void::Bits { // ── Queries ─────────────────────────────────────────────────────────────────── template [[nodiscard]] constexpr int popcount(T v) noexcept { return std::popcount(v); } template [[nodiscard]] constexpr int count_leading_zeros(T v) noexcept { return std::countl_zero(v); } template [[nodiscard]] constexpr int count_trailing_zeros(T v) noexcept { return std::countr_zero(v); } template [[nodiscard]] constexpr int count_leading_ones(T v) noexcept { return std::countl_one(v); } template [[nodiscard]] constexpr int count_trailing_ones(T v) noexcept { return std::countr_one(v); } template [[nodiscard]] constexpr bool has_single_bit(T v) noexcept { return std::has_single_bit(v); } /// Number of bits needed to represent v (floor(log2(v)) + 1). Zero for v=0. template [[nodiscard]] constexpr int bit_width(T v) noexcept { return std::bit_width(v); } // ── Rotation ───────────────────────────────────────────────────────────────── template [[nodiscard]] constexpr T rotl(T v, int s) noexcept { return std::rotl(v, s); } template [[nodiscard]] constexpr T rotr(T v, int s) noexcept { return std::rotr(v, s); } // ── Bit manipulation ───────────────────────────────────────────────────────── template [[nodiscard]] constexpr bool test_bit(T v, int bit) noexcept { return (v >> bit) & T{1}; } template [[nodiscard]] constexpr T set_bit(T v, int bit) noexcept { return v | (T{1} << bit); } template [[nodiscard]] constexpr T clear_bit(T v, int bit) noexcept { return v & ~(T{1} << bit); } template [[nodiscard]] constexpr T toggle_bit(T v, int bit) noexcept { return v ^ (T{1} << bit); } /// Extract bits [lo, lo+len) from v. template [[nodiscard]] constexpr T extract_bits(T v, int lo, int len) noexcept { return (v >> lo) & ((T{1} << len) - T{1}); } /// Deposit val into bits [lo, lo+len) of dest. template [[nodiscard]] constexpr T deposit_bits(T dest, T val, int lo, int len) noexcept { const T mask = ((T{1} << len) - T{1}) << lo; return (dest & ~mask) | ((val << lo) & mask); } // ── Byte swap / endian ──────────────────────────────────────────────────────── template [[nodiscard]] constexpr T byteswap(T v) noexcept { return std::byteswap(v); } template [[nodiscard]] constexpr T to_little_endian(T v) noexcept { if constexpr (std::endian::native == std::endian::little) return v; return std::byteswap(v); } template [[nodiscard]] constexpr T to_big_endian(T v) noexcept { if constexpr (std::endian::native == std::endian::big) return v; return std::byteswap(v); } // ── Power-of-two helpers ───────────────────────────────────────────────────── template [[nodiscard]] constexpr T ceil_pow2(T v) noexcept { return std::bit_ceil(v); } template [[nodiscard]] constexpr T floor_pow2(T v) noexcept { return std::bit_floor(v); } /// Round v up to the next multiple of align (align must be a power of two). template [[nodiscard]] constexpr T align_up(T v, T align) noexcept { return (v + align - T{1}) & ~(align - T{1}); } template [[nodiscard]] constexpr T align_down(T v, T align) noexcept { return v & ~(align - T{1}); } // ── Masks ──────────────────────────────────────────────────────────────────── /// Create a mask with `n` low bits set. template [[nodiscard]] constexpr T low_mask(int n) noexcept { if (n == 0) return T{0}; if (n >= std::numeric_limits::digits) return ~T{0}; return (T{1} << n) - T{1}; } /// Create a mask with bits [lo, hi] set. template [[nodiscard]] constexpr T range_mask(int lo, int hi) noexcept { return low_mask(hi - lo + 1) << lo; } } // namespace Void::Bits