UgMisc 0.2-128
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<class T>
constexpr auto ugmisc::clz (T) noexcept -> UGMISC_BITWISE_UINT_RETURN(T, int)
template<class T>
constexpr auto ugmisc::cl1 (T) noexcept -> UGMISC_BITWISE_UINT_RETURN(T, int)
template<class T>
constexpr auto ugmisc::crz (T) noexcept -> UGMISC_BITWISE_UINT_RETURN(T, int)
template<class T>
constexpr auto ugmisc::cr1 (T) noexcept -> UGMISC_BITWISE_UINT_RETURN(T, int)
template<class T>
constexpr auto ugmisc::bitwidth (T) noexcept -> UGMISC_BITWISE_UINT_RETURN(T, int)

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.

Even if the standard bit op functions are available, UgMisc will fall back to its own implementation if the standard functions don't accept an otherwise suitable type. UgMisc determines a type is suitable depending on the type's specialisation of std::numeric_limits and whether certain expressions are valid.

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

Function Documentation

◆ bitwidth()

template<class T>
auto ugmisc::bitwidth ( T x) -> UGMISC_BITWISE_UINT_RETURN(T, int)
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<class T>
auto ugmisc::cl1 ( T x) -> UGMISC_BITWISE_UINT_RETURN(T, int)
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<class T>
auto ugmisc::clz ( T x) -> UGMISC_BITWISE_UINT_RETURN(T, int)
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<class T>
auto ugmisc::cr1 ( T x) -> UGMISC_BITWISE_UINT_RETURN(T, int)
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<class T>
auto ugmisc::crz ( T x) -> UGMISC_BITWISE_UINT_RETURN(T, int)
constexprnoexcept

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