ugmisc 0.2-86
Miscellaneous C++ header library
Loading...
Searching...
No Matches
bitops.hpp File Reference

Provides constexpr bit counting functions. More...

#include <cstdint>
#include <type_traits>
#include <utility>
#include "ugmisc/features.hpp"
#include "ugmisc/sfinae_helpers.hpp"

Go to the source code of this file.

Functions

template<BitwiseUint T>
constexpr T ugmisc::clz (T) noexcept
template<BitwiseUint T>
constexpr T ugmisc::cl1 (T) noexcept
template<BitwiseUint T>
constexpr T ugmisc::crz (T) noexcept
template<BitwiseUint T>
constexpr T ugmisc::cr1 (T) noexcept
template<BitwiseUint T>
constexpr T ugmisc::bitwidth (T) noexcept

Detailed Description

Provides constexpr bit counting functions.

If features.hpp claims that we have the std library bit counting functions, we use those.

If not we use a series of bitwise logic ops to home in on the place where the first set bit is, or last set bit or whatever.

TBD: Should the clz function and its relatives be renamed to match the standard functions' names? Probably.

Function Documentation

◆ bitwidth()

template<BitwiseUint T>
T ugmisc::bitwidth ( T )
constexprnoexcept

Counts bits needed to represent the value. Simply forwards to std::bit_width if that is available, or provides its own implementation otherwise.

◆ cl1()

template<BitwiseUint T>
T ugmisc::cl1 ( T )
constexprnoexcept

Counts left one bits. Simply forwards the call to std::countl_one if that is available, or provides its own implementation otherwise.

◆ clz()

template<BitwiseUint T>
T ugmisc::clz ( T )
constexprnoexcept

Counts left zero bits. Simply forwards the call to std::countl_zero if that is available, or provides its own implementation otherwise.

◆ cr1()

template<BitwiseUint T>
T ugmisc::cr1 ( T )
constexprnoexcept

Counts right one-bits. Simply forwards the call to std::countr_one if that is available, or provides its own implementation otherwise.

◆ crz()

template<BitwiseUint T>
T ugmisc::crz ( T )
constexprnoexcept

Counts right zero bits. Simply forwards the call to std::countr_zero if that is available, or provides its own implementation otherwise.