tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
bit_ceil.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_BIT_BIT_CEIL_HPP
4#define TETL_BIT_BIT_CEIL_HPP
5
9
10namespace etl {
11
24template <etl::builtin_unsigned_integer UInt>
25[[nodiscard]] constexpr auto bit_ceil(UInt x) noexcept -> UInt
26{
27 if (x <= 1U) {
28 return UInt{1};
29 }
30 if constexpr (is_same_v<UInt, decltype(+x)>) {
31 return UInt{1U} << bit_width(UInt{x - 1U});
32 } else {
33 // for types subject to integral promotion
35 return UInt{1U << (bit_width(UInt{x - 1U}) + o) >> o};
36 }
37}
38
39} // namespace etl
40
41#endif // TETL_BIT_BIT_CEIL_HPP
constexpr auto bit_ceil(UInt x) noexcept -> UInt
Calculates the smallest integral power of two that is not smaller than x. If that value is not repres...
Definition bit_ceil.hpp:25
constexpr auto bit_width(UInt x) noexcept -> int
If x is not zero, calculates the number of bits needed to store the value x, that is,...
Definition bit_width.hpp:21
Definition adjacent_find.hpp:8
constexpr bool is_same_v
Definition is_same.hpp:11
static constexpr int digits
Definition numeric_limits.hpp:24