improve bit slice, take care of edge case, move BitSliceType
This commit is contained in:
@@ -20,14 +20,3 @@ using m128 = __m128i;
|
|||||||
#define INT128_MAX ((u128)0x7FFF'FFFF'FFFF'FFFF << 64) | 0xFFFF'FFFF'FFFF'FFFF
|
#define INT128_MAX ((u128)0x7FFF'FFFF'FFFF'FFFF << 64) | 0xFFFF'FFFF'FFFF'FFFF
|
||||||
#define INT128_MIN (-INT128_MAX - 1LL)
|
#define INT128_MIN (-INT128_MAX - 1LL)
|
||||||
|
|
||||||
template <u8 start, u8 end>
|
|
||||||
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>
|
|
||||||
>
|
|
||||||
>
|
|
||||||
>
|
|
||||||
>;
|
|
||||||
|
|||||||
@@ -1,12 +1,32 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <common.hpp>
|
#include <common.hpp>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
namespace natsukashii::util {
|
namespace natsukashii::util {
|
||||||
|
template <u8 start, u8 end>
|
||||||
|
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 <u8 start, u8 end, typename T>
|
template <u8 start, u8 end, typename T>
|
||||||
BitSliceType<start, end> BitSlice(const T& num) {
|
BitSliceType<start, end> BitSlice(const T& num) {
|
||||||
static_assert(end - start > 127);
|
static_assert(end < (sizeof(T) * 8) && start < (sizeof(T) * 8));
|
||||||
static_assert(end - start > sizeof(T));
|
constexpr auto correctedEnd = end == (sizeof(T) * 8) - 1 ? end : end + 1;
|
||||||
return (num >> start) & ((1 << end) - 1);
|
return (num >> start) & ((1 << correctedEnd) - 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user