[ox/std] Fix MaxValue for signed ints

This commit is contained in:
Gary Talent 2022-03-17 03:53:46 -05:00
parent 38cb3fc962
commit b43f5e3b98
2 changed files with 14 additions and 3 deletions

View File

@ -12,3 +12,12 @@ static_assert(ox::onMask<int>(1) == 0b0001);
static_assert(ox::onMask<int>(2) == 0b0011);
static_assert(ox::onMask<int>(3) == 0b0111);
static_assert(ox::onMask<int>(4) == 0b1111);
static_assert(ox::MaxValue<int8_t> == 127);
static_assert(ox::MaxValue<int16_t> == 32767);
static_assert(ox::MaxValue<int32_t> == 2147483647);
static_assert(ox::MaxValue<int64_t> == 9223372036854775807);
static_assert(ox::MaxValue<uint8_t> == 255);
static_assert(ox::MaxValue<uint16_t> == 65535);
static_assert(ox::MaxValue<uint32_t> == 4294967295);
static_assert(ox::MaxValue<uint64_t> == 18446744073709551615u);

View File

@ -38,13 +38,15 @@ constexpr typename enable_if<sizeof(To) == sizeof(From), To>::type cbit_cast(Fro
}
template<typename T>
[[nodiscard]] constexpr T rotl(T i, int shift) noexcept {
[[nodiscard]]
constexpr T rotl(T i, int shift) noexcept {
constexpr auto bits = sizeof(i) * 8;
return (i << static_cast<T>(shift)) | (i >> (bits - static_cast<T>(shift)));
}
template<typename T, typename B = int>
[[nodiscard]] constexpr T onMask(B bits = sizeof(T) << 3 /* *8 */) noexcept {
[[nodiscard]]
constexpr T onMask(B bits = sizeof(T) << 3 /* *8 */) noexcept {
T out = T(0);
for (B i = 0; i < bits; i++) {
out |= static_cast<T>(1) << i;
@ -53,6 +55,6 @@ template<typename T, typename B = int>
}
template<typename T>
constexpr auto MaxValue = onMask<T>();
constexpr auto MaxValue = onMask<T>(is_signed_v<T> ? sizeof(T) * 8 - 1 : sizeof(T) * 8);
}