openMSX
power_of_two.hh
Go to the documentation of this file.
1#ifndef POWER_OF_TWO_HH
2#define POWER_OF_TWO_HH
3
4#include "narrow.hh"
5#include <bit>
6#include <cassert>
7#include <concepts>
8#include <cstdint>
9
10namespace openmsx {
11
17template<std::unsigned_integral T>
19 constexpr power_of_two(T size)
20 : exponent(narrow_cast<uint8_t>(std::bit_width(size - 1)))
21 {
22 assert(std::has_single_bit(size));
23 };
24
25 [[nodiscard]] constexpr operator T() const
26 {
27 return T{1} << exponent;
28 }
29
30 uint8_t exponent = 0;
31};
32
33} // namespace openmsx
34
35#endif
This file implemented 3 utility functions:
Definition Autofire.cc:11
STL namespace.
constexpr To narrow_cast(From &&from) noexcept
Definition narrow.hh:21
A constexpr power_of_two struct for power of two numbers, with compact storage and built-in value cor...
constexpr power_of_two(T size)