di 0.1.0
Loading...
Searching...
No Matches
countl_zero.h
Go to the documentation of this file.
1#pragma once
2
4#include "di/meta/language.h"
5
6namespace di::bit {
7namespace detail {
8 struct CountlZeroFunction {
9 template<concepts::UnsignedInteger T>
10 constexpr auto operator()(T value) const -> int {
11 if (value == 0) {
13 }
14
15 if constexpr (sizeof(T) <= sizeof(unsigned int)) {
16 constexpr auto extra_width = math::NumericLimits<unsigned int>::digits - math::NumericLimits<T>::digits;
17 return __builtin_clz(value) - extra_width;
18 } else if constexpr (sizeof(T) <= sizeof(unsigned long)) {
19 return __builtin_clzl(value);
20 } else if constexpr (sizeof(T) <= sizeof(unsigned long long)) {
21 return __builtin_clzll(value);
22 } else {
23 static_assert(sizeof(T) == 16);
24 auto high = u64(value >> 64);
25 if (high != 0) {
26 return (*this)(high);
27 }
28 auto low = u64(value & math::NumericLimits<u64>::max);
29 return (*this)(low) + 64;
30 }
31 }
32 };
33}
34
35constexpr inline auto countl_zero = detail::CountlZeroFunction {};
36}
37
38namespace di {
40}
Definition bit_proxy_reference.h:5
constexpr auto countl_zero
Definition countl_zero.h:35
__UINT64_TYPE__ u64
Definition integers.h:12
Definition any_storable.h:9
static constexpr int digits
Definition numeric_limits.h:14