Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
bit: Bit Manipulation Facilities

The bit subpackage provides backports of the C++20 <bit> header, along with other useful bit manipulation utilities.

The public header is

The Bazel target is

//:bit

bit_cast

arene::base::bit_cast<To>(from) returns a value of type To whose object representation is bit-identical to that of from.

The specification mirrors std::bit_cast, and the call participates in overload resolution only when:

/// @brief Reinterpret the object representation of a @c float as the matching
/// IEEE 754 unsigned integer bit pattern.
auto float_to_bits(float const value) noexcept -> std::uint32_t { return arene::base::bit_cast<std::uint32_t>(value); }

constexpr Availability

bit_cast is usable in a constant expression only when the compiler provides __builtin_bit_cast (Clang 9+, GCC 11+). On toolchains without it (e.g. GCC 8), the implementation falls back to std::memcpy and therefore cannot appear in a constexpr context. Query this at compile time via the ARENE_HAS_CONSTEXPR_BIT_CAST platform support macro:

#if ARENE_IS_ON(ARENE_HAS_CONSTEXPR_BIT_CAST)
/// @brief When @c __builtin_bit_cast is available, @c bit_cast can be used
/// inside a @c constexpr context.
constexpr auto one_f_bits = arene::base::bit_cast<std::uint32_t>(1.0F);
// NOLINTNEXTLINE(readability-magic-numbers) IEEE 754 single-precision bit pattern for 1.0F
static_assert(one_f_bits == 0x3F800000U, "IEEE 754 single-precision bit pattern for 1.0F");
#endif