ugmisc 0.2-76
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 bitwise_uint_only< T > ugmisc::clz (T) noexcept
template<class T>
constexpr bitwise_uint_only< T > ugmisc::cl1 (T) noexcept
template<class T>
constexpr bitwise_uint_only< T > ugmisc::crz (T) noexcept
template<class T>
constexpr bitwise_uint_only< T > ugmisc::cr1 (T) noexcept
template<class T>
constexpr bitwise_uint_only< 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<class T>
bitwise_uint_only< T > ugmisc::bitwidth ( T x)
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>
bitwise_uint_only< T > ugmisc::cl1 ( T v)
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>
bitwise_uint_only< T > ugmisc::clz ( T v)
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>
bitwise_uint_only< T > ugmisc::cr1 ( T v)
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>
bitwise_uint_only< T > ugmisc::crz ( T v)
constexprnoexcept

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