diff options
Diffstat (limited to 'lib/helpers.h')
-rw-r--r-- | lib/helpers.h | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/lib/helpers.h b/lib/helpers.h index f09a206..2c7f4c2 100644 --- a/lib/helpers.h +++ b/lib/helpers.h @@ -1,6 +1,7 @@ #ifndef MYGRATE_HELPERS_H #define MYGRATE_HELPERS_H +#include <boost/numeric/conversion/cast.hpp> #include <concepts> #include <cstdint> #include <cstdlib> @@ -8,8 +9,13 @@ #include <utility> namespace MyGrate { + template<uint8_t size> + using SmallestUInt = std::conditional_t<size <= 8, uint8_t, + std::conditional_t<size <= 16, uint16_t, std::conditional_t<size <= 32, uint32_t, uint64_t>>>; + + template<uint8_t size> constexpr inline auto - bitslice(const std::integral auto i, uint8_t offset, uint8_t size) + bitslice(const std::integral auto i, uint8_t offset) -> SmallestUInt<size> { return (i >> offset) & ((1U << size) - 1U); } @@ -27,7 +33,8 @@ namespace MyGrate { constexpr inline auto mod100_extract(std::integral auto & i) { - const auto r {i % 100}; + using R = std::conditional_t<std::is_signed_v<decltype(i)>, int8_t, uint8_t>; + const auto r {boost::numeric_cast<R>(i % 100)}; i /= 100; return r; } |