#pragma once #include #include #include namespace natsukashii::util { template constexpr void panic(const std::string& fmt, Args... args) { fmt::print(fmt, args...); exit(-1); } 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 < (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); } }