di 0.1.0
Loading...
Searching...
No Matches
bit_ceil.h
Go to the documentation of this file.
1#pragma once
2
4#include "di/meta/core.h"
5
6namespace di::bit {
7namespace detail {
8 struct BitCeilFunction {
9 template<concepts::UnsignedInteger T>
10 constexpr auto operator()(T value) const -> T {
11 if (value <= 1U) {
12 return T(1);
13 }
14 if constexpr (concepts::SameAs<T, decltype(+value)>) {
15 return T(1) << bit_width(T(value - 1));
16 } else {
17 // Ensure UB if the result cannot fit inside T. This is needed because
18 // T promotes to an int automatically.
19 constexpr int extra_offset = math::NumericLimits<unsigned int>::digits - math::NumericLimits<T>::digits;
20 return T(1U << (bit_width(T(value - 1)) + extra_offset) >> extra_offset);
21 }
22 }
23 };
24}
25
26constexpr inline auto bit_ceil = detail::BitCeilFunction {};
27}
28
29namespace di {
30using bit::bit_ceil;
31}
Definition bit_proxy_reference.h:5
constexpr auto bit_width
Definition bit_width.h:15
constexpr auto bit_ceil
Definition bit_ceil.h:26
constexpr auto value
Definition value.h:34
Definition any_storable.h:9
static constexpr int digits
Definition numeric_limits.h:14