From 54e34d6a903a34d19a0e383d99593b03e7ee3673 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Thu, 17 Mar 2022 03:53:46 -0500 Subject: [PATCH] [ox/std] Fix MaxValue for signed ints (synced from b43f5e3b98499331f159962d1b697767b4a42f09) --- src/ox/std/bit.cpp | 9 +++++++++ src/ox/std/bit.hpp | 8 +++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/ox/std/bit.cpp b/src/ox/std/bit.cpp index 6f4a60a87..5af893f1b 100644 --- a/src/ox/std/bit.cpp +++ b/src/ox/std/bit.cpp @@ -12,3 +12,12 @@ static_assert(ox::onMask(1) == 0b0001); static_assert(ox::onMask(2) == 0b0011); static_assert(ox::onMask(3) == 0b0111); static_assert(ox::onMask(4) == 0b1111); + +static_assert(ox::MaxValue == 127); +static_assert(ox::MaxValue == 32767); +static_assert(ox::MaxValue == 2147483647); +static_assert(ox::MaxValue == 9223372036854775807); +static_assert(ox::MaxValue == 255); +static_assert(ox::MaxValue == 65535); +static_assert(ox::MaxValue == 4294967295); +static_assert(ox::MaxValue == 18446744073709551615u); diff --git a/src/ox/std/bit.hpp b/src/ox/std/bit.hpp index 61915217b..23cb75a8f 100644 --- a/src/ox/std/bit.hpp +++ b/src/ox/std/bit.hpp @@ -38,13 +38,15 @@ constexpr typename enable_if::type cbit_cast(Fro } template -[[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(shift)) | (i >> (bits - static_cast(shift))); } template -[[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(1) << i; @@ -53,6 +55,6 @@ template } template -constexpr auto MaxValue = onMask(); +constexpr auto MaxValue = onMask(is_signed_v ? sizeof(T) * 8 - 1 : sizeof(T) * 8); }