diff --git a/src/core/common.hpp b/src/core/common.hpp index 6fa5f89f..9ca6b53d 100644 --- a/src/core/common.hpp +++ b/src/core/common.hpp @@ -20,14 +20,3 @@ using m128 = __m128i; #define INT128_MAX ((u128)0x7FFF'FFFF'FFFF'FFFF << 64) | 0xFFFF'FFFF'FFFF'FFFF #define INT128_MIN (-INT128_MAX - 1LL) -template -using BitSliceType = -std::conditional<(end - start) <= 7, u8, - std::conditional<(end - start) <= 15, u16, - std::conditional<(end - start) <= 31, u32, - std::conditional<(end - start) <= 63, u64, - std::conditional<(end - start) <= 127, u128, u128> - > - > - > ->; diff --git a/src/core/util.hpp b/src/core/util.hpp index 27b1902f..cc44dc7f 100644 --- a/src/core/util.hpp +++ b/src/core/util.hpp @@ -1,12 +1,32 @@ #pragma once #include #include +#include namespace natsukashii::util { +template +using BitSliceType = +typename std::conditional<(end - start) <= 7, u8, + typename std::conditional<(end - start) <= 15, u16, + typename std::conditional<(end - start) <= 31, u32, + typename std::conditional<(end - start) <= 63, u64, + typename std::conditional<(end - start) <= 127, u128, void>::type + >::type + >::type + >::type +>::type; + template BitSliceType BitSlice(const T& num) { - static_assert(end - start > 127); - static_assert(end - start > sizeof(T)); - return (num >> start) & ((1 << end) - 1); + static_assert(end < (sizeof(T) * 8) && start < (sizeof(T) * 8)); + constexpr auto correctedEnd = end == (sizeof(T) * 8) - 1 ? end : end + 1; + return (num >> start) & ((1 << correctedEnd) - 1); +} + +template +T BitSlice(const T& num, int start, int end) { + assert(end < (sizeof(T) * 8) && start < (sizeof(T) * 8)); + auto correctedEnd = end == (sizeof(T) * 8) - 1 ? end : end + 1; + return (num >> start) & ((1 << correctedEnd) - 1); } }