New header-only modules: - Void::Hash — FNV-1a 32/64, CRC-32, DJB2, MurmurHash3-32, UDL literals - Void::Random — Xoshiro256** PRNG, uniform_int/real, coin_flip, shuffle, random_string - Void::Uuid — v4 (random), v5_lite (name-based), parse, RFC 4122 namespaces - Void::Encoding — Base64 (standard + URL-safe), Hex encode/decode - Void::Bits — popcount, bit extract/deposit, rotation, align, byteswap, masks - Void::Math — Vec2/Vec3/Vec4, Mat4 (identity, translation, scale, rotation) Testing module: move Stacktrace from INTERNAL_DEPS to OPTIONAL_DEPS. Added Detail/Color.hpp shim that delegates to Stacktrace::Color when present or provides inline ANSI fallback otherwise. All Stacktrace calls in SignalHandler, ConsoleReporter, and Runner are gated on VOID_HAS_STACKTRACE. Fix Repr.hpp reflect_struct to handle template specializations that lack std::meta::identifier_of (now guarded by has_identifier check). 33/33 tests pass. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
179 lines
5.0 KiB
C++
179 lines
5.0 KiB
C++
#pragma once
|
|
|
|
#include <bit>
|
|
#include <concepts>
|
|
#include <cstdint>
|
|
#include <limits>
|
|
|
|
namespace Void::Bits
|
|
{
|
|
|
|
// ── Queries ───────────────────────────────────────────────────────────────────
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr int popcount(T v) noexcept
|
|
{
|
|
return std::popcount(v);
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr int count_leading_zeros(T v) noexcept
|
|
{
|
|
return std::countl_zero(v);
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr int count_trailing_zeros(T v) noexcept
|
|
{
|
|
return std::countr_zero(v);
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr int count_leading_ones(T v) noexcept
|
|
{
|
|
return std::countl_one(v);
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr int count_trailing_ones(T v) noexcept
|
|
{
|
|
return std::countr_one(v);
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[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 <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr int bit_width(T v) noexcept
|
|
{
|
|
return std::bit_width(v);
|
|
}
|
|
|
|
// ── Rotation ─────────────────────────────────────────────────────────────────
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T rotl(T v, int s) noexcept
|
|
{
|
|
return std::rotl(v, s);
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T rotr(T v, int s) noexcept
|
|
{
|
|
return std::rotr(v, s);
|
|
}
|
|
|
|
// ── Bit manipulation ─────────────────────────────────────────────────────────
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr bool test_bit(T v, int bit) noexcept
|
|
{
|
|
return (v >> bit) & T{1};
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T set_bit(T v, int bit) noexcept
|
|
{
|
|
return v | (T{1} << bit);
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T clear_bit(T v, int bit) noexcept
|
|
{
|
|
return v & ~(T{1} << bit);
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T toggle_bit(T v, int bit) noexcept
|
|
{
|
|
return v ^ (T{1} << bit);
|
|
}
|
|
|
|
/// Extract bits [lo, lo+len) from v.
|
|
template <std::unsigned_integral T>
|
|
[[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 <std::unsigned_integral T>
|
|
[[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 <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T byteswap(T v) noexcept
|
|
{
|
|
return std::byteswap(v);
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T to_little_endian(T v) noexcept
|
|
{
|
|
if constexpr (std::endian::native == std::endian::little) return v;
|
|
return std::byteswap(v);
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[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 <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T ceil_pow2(T v) noexcept
|
|
{
|
|
return std::bit_ceil(v);
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[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 <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T align_up(T v, T align) noexcept
|
|
{
|
|
return (v + align - T{1}) & ~(align - T{1});
|
|
}
|
|
|
|
template <std::unsigned_integral T>
|
|
[[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 <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T low_mask(int n) noexcept
|
|
{
|
|
if (n == 0) return T{0};
|
|
if (n >= std::numeric_limits<T>::digits) return ~T{0};
|
|
return (T{1} << n) - T{1};
|
|
}
|
|
|
|
/// Create a mask with bits [lo, hi] set.
|
|
template <std::unsigned_integral T>
|
|
[[nodiscard]] constexpr T range_mask(int lo, int hi) noexcept
|
|
{
|
|
return low_mask<T>(hi - lo + 1) << lo;
|
|
}
|
|
|
|
} // namespace Void::Bits
|