1 #pragma once // Source encoding: UTF-8 with BOM (π is a lowercase Greek "pi"). 8 #include <c/limits.hpp> 9 #include <c/stdint.hpp> 11 #include <type_traits> 18 constexpr
inline auto log2_8(
const uint8_t x ) noexcept
21 constexpr
int logs[] =
23 -1, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3
27 return (x & 0xF0? 4 + logs[x >> 4] : logs[x]);
30 constexpr
inline auto log2_16(
const uint16_t x ) noexcept
32 {
return (x & 0xFF00? 8 + log2_8( uint8_t( x >> 8 ) ) : log2_8( uint8_t( x ))); }
34 constexpr
inline auto log2_32(
const uint32_t x ) noexcept
36 {
return (x & 0xFFFF0000? 16 + log2_16( uint16_t( x >> 16 ) ) : log2_16( uint16_t( x ) )); }
38 constexpr
inline auto log2_64(
const uint64_t x ) noexcept
40 {
return (x & 0xFFFFFFFF00000000? 32 + log2_32( uint32_t( x >> 32 ) ) : log2_32( uint32_t( x ))); }
42 constexpr
inline auto log2(
const uint8_t x ) noexcept ->
int {
return log2_8( x ); }
43 constexpr
inline auto log2(
const uint16_t x ) noexcept ->
int {
return log2_16( x ); }
44 constexpr
inline auto log2(
const uint32_t x ) noexcept ->
int {
return log2_32( x ); }
45 constexpr
inline auto log2(
const uint64_t x ) noexcept ->
int {
return log2_64( x ); }
51 template<
class Un
signed >
52 constexpr
inline auto intlog2(
const Unsigned x ) noexcept
55 static_assert( std::is_unsigned_v<Unsigned> );
56 return impl::log2( x );
CPPX_USE_CPPX(bits_per_, magnitude_bits_per_)
constexpr auto intlog2(const Unsigned x) noexcept -> int
The position of the most significant bit in an unsigned value, or -1 for value zero.
Macros for generating more concise and clear using statements, primarily $use_cppx and $use_std,...