Skip to content

Commit

Permalink
Revert "Use #if for the SUS_CHECK_INTEGER_OVERFLOW condition"
Browse files Browse the repository at this point in the history
This reverts commit 389f5ee.

The problem was ODR violations, not the use of if constexpr. =)
  • Loading branch information
danakj committed Nov 29, 2023
1 parent 1b9db0f commit fd0e53f
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 327 deletions.
146 changes: 73 additions & 73 deletions sus/num/__private/signed_integer_methods.inc
Original file line number Diff line number Diff line change
Expand Up @@ -568,12 +568,12 @@ operator<=>(_self l, SignedPrimitiveInteger auto r) noexcept {

/// Satisfies the [`Neg<@doc.self>`]($sus::num::Neg) concept.
sus_pure constexpr inline _self operator-() const& noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
::sus::check(primitive_value != MIN_PRIMITIVE);
return __private::unchecked_neg(primitive_value);
#else
return wrapping_neg();
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
::sus::check(primitive_value != MIN_PRIMITIVE);
return __private::unchecked_neg(primitive_value);
} else {
return wrapping_neg();
}
}
/// Satisfies the [`BitNot<@doc.self>`]($sus::num::BitNot) concept.
sus_pure constexpr inline _self operator~() const& noexcept {
Expand All @@ -595,14 +595,14 @@ sus_pure constexpr inline _self operator~() const& noexcept {
/// #[doc.overloads=signedint.+]
[[nodiscard]] sus_pure_const friend constexpr inline _self operator+(
_self l, _self r) noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::add_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return out.value;
#else
return l.wrapping_add(r);
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::add_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return out.value;
} else {
return l.wrapping_add(r);
}
}
/// #[doc.overloads=signedint.+]
template <std::convertible_to<_self> U>
Expand Down Expand Up @@ -642,14 +642,14 @@ friend constexpr inline _self operator+(U l, _self r) noexcept = delete;
/// #[doc.overloads=signedint.-]
[[nodiscard]] sus_pure_const friend constexpr inline _self operator-(
_self l, _self r) noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::sub_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return out.value;
#else
return l.wrapping_sub(r);
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::sub_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return out.value;
} else {
return l.wrapping_sub(r);
}
}
/// #[doc.overloads=signedint.-]
template <std::convertible_to<_self> U>
Expand Down Expand Up @@ -689,14 +689,14 @@ friend constexpr inline _self operator-(U l, _self r) noexcept = delete;
/// #[doc.overloads=signedint.*]
[[nodiscard]] sus_pure_const friend constexpr inline _self operator*(
_self l, _self r) noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::mul_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return out.value;
#else
return l.wrapping_mul(r);
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::mul_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return out.value;
} else {
return l.wrapping_mul(r);
}
}
/// #[doc.overloads=signedint.*]
template <std::convertible_to<_self> U>
Expand Down Expand Up @@ -920,44 +920,44 @@ friend constexpr inline _self operator^(U l, _self r) noexcept = delete;
/// This operator will panic on overflow when [overflow checks](
/// $sus::num#overflow-behaviour) are enabled.
constexpr inline void operator+=(_self r) & noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::add_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
#else
primitive_value = wrapping_add(r).primitive_value;
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::add_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
} else {
primitive_value = wrapping_add(r).primitive_value;
}
}
/// Satisfies the [`SubAssign<@doc.self>`]($sus::num::SubAssign) concept.
///
/// # Panics
/// This operator will panic on overflow when [overflow checks](
/// $sus::num#overflow-behaviour) are enabled.
constexpr inline void operator-=(_self r) & noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::sub_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
#else
primitive_value = wrapping_sub(r).primitive_value;
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::sub_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
} else {
primitive_value = wrapping_sub(r).primitive_value;
}
}
/// Satisfies the [`MulAssign<@doc.self>`]($sus::num::MulAssign) concept.
///
/// # Panics
/// This operator will panic on overflow when [overflow checks](
/// $sus::num#overflow-behaviour) are enabled.
constexpr inline void operator*=(_self r) & noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::mul_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
#else
primitive_value = wrapping_mul(r).primitive_value;
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::mul_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
} else {
primitive_value = wrapping_mul(r).primitive_value;
}
}
/// Satisfies the [`DivAssign<@doc.self>`]($sus::num::DivAssign) concept.
///
Expand Down Expand Up @@ -999,14 +999,14 @@ constexpr inline void operator^=(_self r) & noexcept {
/// [`@doc.self::BITS`]($sus::num::@doc.self::BITS) and [overflow checks](
/// $sus::num#overflow-behaviour) are enabled.
constexpr inline void operator<<=(u64 r) & noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::shl_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
#else
primitive_value = wrapping_shl(r).primitive_value;
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::shl_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
} else {
primitive_value = wrapping_shl(r).primitive_value;
}
}
/// Satisfies the [`ShrAssign<@doc.self>`]($sus::num::ShrAssign) concept.
///
Expand All @@ -1017,14 +1017,14 @@ constexpr inline void operator<<=(u64 r) & noexcept {
/// [`@doc.self::BITS`]($sus::num::@doc.self::BITS) and [overflow checks](
/// $sus::num#overflow-behaviour) are enabled.
constexpr inline void operator>>=(u64 r) & noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::shr_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
#else
primitive_value = wrapping_shr(r).primitive_value;
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::shr_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
} else {
primitive_value = wrapping_shr(r).primitive_value;
}
}

/// Computes the absolute value of itself.
Expand Down Expand Up @@ -1822,9 +1822,9 @@ sus_pure constexpr _self swap_bytes() const& noexcept {
sus_pure constexpr inline _self pow(u32 rhs) const& noexcept {
const auto out =
__private::pow_with_overflow(primitive_value, rhs.primitive_value);
#if SUS_CHECK_INTEGER_OVERFLOW
::sus::check(!out.overflow);
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
::sus::check(!out.overflow);
}
return out.value;
}

Expand Down
111 changes: 57 additions & 54 deletions sus/num/__private/unsigned_integer_methods.inc
Original file line number Diff line number Diff line change
Expand Up @@ -627,14 +627,15 @@ sus_pure constexpr inline _self operator~() const& noexcept {
/// #[doc.overloads=unsignedint.+]
[[nodiscard]] sus_pure_const friend constexpr inline _self operator+(
_self l, _self r) noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::add_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return _self(out.value);
#else
return _self(__private::unchecked_add(l.primitive_value, r.primitive_value));
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::add_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return _self(out.value);
} else {
return _self(
__private::unchecked_add(l.primitive_value, r.primitive_value));
}
}
#if _pointer
/// #[doc.overloads=unsignedint.+]
Expand Down Expand Up @@ -692,14 +693,15 @@ friend constexpr inline _self operator+(U l, _self r) noexcept = delete;
/// #[doc.overloads=unsignedint.-]
[[nodiscard]] sus_pure_const friend constexpr inline _self operator-(
_self l, _self r) noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::sub_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return _self(out.value);
#else
return _self(__private::unchecked_sub(l.primitive_value, r.primitive_value));
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::sub_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return _self(out.value);
} else {
return _self(
__private::unchecked_sub(l.primitive_value, r.primitive_value));
}
}
#if _pointer
/// #[doc.overloads=unsignedint.-]
Expand Down Expand Up @@ -757,14 +759,15 @@ friend constexpr inline _self operator-(U l, _self r) noexcept = delete;
/// #[doc.overloads=unsignedint.*]
[[nodiscard]] sus_pure_const friend constexpr inline _self operator*(
_self l, _self r) noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::mul_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return _self(out.value);
#else
return _self(__private::unchecked_mul(l.primitive_value, r.primitive_value));
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::mul_with_overflow(l.primitive_value, r.primitive_value);
::sus::check(!out.overflow);
return _self(out.value);
} else {
return _self(
__private::unchecked_mul(l.primitive_value, r.primitive_value));
}
}
#if _pointer
/// #[doc.overloads=unsignedint.*]
Expand Down Expand Up @@ -1108,15 +1111,15 @@ friend constexpr inline _self operator^(U l, _self r) noexcept = delete;

/// Satisfies the [`AddAssign<@doc.self>`]($sus::num::AddAssign) concept.
constexpr inline void operator+=(_self r) & noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::add_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
#else
primitive_value =
__private::unchecked_add(primitive_value, r.primitive_value);
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::add_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
} else {
primitive_value =
__private::unchecked_add(primitive_value, r.primitive_value);
}
}
#if _pointer // uptr can't be created from other safe numerics.
constexpr inline void operator+=(_pointer_sized r) & noexcept {
Expand All @@ -1125,15 +1128,15 @@ constexpr inline void operator+=(_pointer_sized r) & noexcept {
#endif
/// Satisfies the [`SubAssign<@doc.self>`]($sus::num::SubAssign) concept.
constexpr inline void operator-=(_self r) & noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::sub_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
#else
primitive_value =
__private::unchecked_sub(primitive_value, r.primitive_value);
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::sub_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
} else {
primitive_value =
__private::unchecked_sub(primitive_value, r.primitive_value);
}
}
#if _pointer // uptr can't be created from other safe numerics.
constexpr inline void operator-=(_pointer_sized r) & noexcept {
Expand All @@ -1142,15 +1145,15 @@ constexpr inline void operator-=(_pointer_sized r) & noexcept {
#endif
/// Satisfies the [`MulAssign<@doc.self>`]($sus::num::MulAssign) concept.
constexpr inline void operator*=(_self r) & noexcept {
#if SUS_CHECK_INTEGER_OVERFLOW
const auto out =
__private::mul_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
#else
primitive_value =
__private::unchecked_mul(primitive_value, r.primitive_value);
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
const auto out =
__private::mul_with_overflow(primitive_value, r.primitive_value);
::sus::check(!out.overflow);
primitive_value = out.value;
} else {
primitive_value =
__private::unchecked_mul(primitive_value, r.primitive_value);
}
}
#if _pointer // uptr can't be created from other safe numerics.
constexpr inline void operator*=(_pointer_sized r) & noexcept {
Expand Down Expand Up @@ -2024,9 +2027,9 @@ sus_pure constexpr _self swap_bytes() const& noexcept {
sus_pure constexpr _self pow(u32 rhs) const& noexcept {
const auto out =
__private::pow_with_overflow(primitive_value, rhs.primitive_value);
#if SUS_CHECK_INTEGER_OVERFLOW
::sus::check(!out.overflow);
#endif
if constexpr (SUS_CHECK_INTEGER_OVERFLOW) {
::sus::check(!out.overflow);
}
return _self(out.value);
}

Expand Down
Loading

0 comments on commit fd0e53f

Please sign in to comment.