Files
Library/include/Void/Core/Bits.hpp
faumarayandClaude Fable 5 28b94883fd Restructure into umbrella modules (~55 -> 16 targets)
Headers move, CMake targets merge, C++ namespaces stay unchanged:
- Core: Scope, Lazy, Counted, Flags, Hash, Bits, Uuid, TypeList, TypeMap, Variant
- Functional: Pipeline, Either, Result, Retry
- Containers: Ring, Pool, PriorityQueue, LRU, Trie, Graph, DisjointSet, Bitset, Interval
- Math: +Matrix, Numeric, Random, Histogram
- Text: Str, Format, Tokenizer, Diff, Encoding, Semver
- Data: Json, Csv, Ini
- Async: Coro, Concurrency, EventBus, Observable
- Time: +Bench
- Log: Log facade + Logging framework (facade now header-only, breaking
  the Stacktrace<->Log link cycle the merge would have created)
- System: FileSystem + Process
- Configuration: +Argparse
- Unchanged: Stacktrace, Plugin, StateMachine, Testing, TestingMock, Detail

Module system fixes:
- OPTIONAL_DEPS resolution deferred to void_finalize_modules() so it no
  longer depends on the alphabetical discovery order of src/ subdirs
  (Testing -> Text would have been silently dropped).
- Every header-only module now has a tracked module.cmake; previously the
  empty src/<Name> marker dirs were untracked, so a fresh clone would have
  silently skipped those modules.
- VOID_HAS_LOGGING/CONCURRENCY/STR consumers renamed to LOG/ASYNC/TEXT.

Tests move to tests/standalone/<Umbrella>/<Feature>.cpp (Result suite files
prefixed: ResultBasic etc.). Test count verified identical (831) before and
after. README/CLAUDE.md module tables updated.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 18:50:02 +03:00

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