Skip to content

Commit

Permalink
Merge pull request #714 from cppalliance/decimal32_fast_comps_v2
Browse files Browse the repository at this point in the history
Decimal32 fast comps v2
  • Loading branch information
mborland authored Jul 17, 2024
2 parents 2a17f6e + 8d6bedd commit 4bf4e48
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 46 deletions.
101 changes: 55 additions & 46 deletions include/boost/decimal/decimal32_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,7 @@ constexpr auto issignaling(decimal32_fast val) noexcept -> bool

constexpr auto isnormal(decimal32_fast val) noexcept -> bool
{
if (val.exponent_ <= static_cast<std::uint8_t>(detail::precision_v<decimal32> - 1))
{
return false;
}

return (val.significand_ != 0) && isfinite(val);
return (val.significand_ != 0) && isfinite(val) && (val.exponent_ > static_cast<std::uint8_t>(detail::precision_v<decimal32> - 1));
}

constexpr auto isfinite(decimal32_fast val) noexcept -> bool
Expand All @@ -479,21 +474,24 @@ constexpr auto isfinite(decimal32_fast val) noexcept -> bool

constexpr auto operator==(decimal32_fast lhs, decimal32_fast rhs) noexcept -> bool
{
#ifndef BOOST_DECIMAL_FAST_MATH
if (isnan(lhs) || isnan(rhs))
{
return false;
}
#endif

return (lhs.sign_ == rhs.sign_) &
(lhs.exponent_ == rhs.exponent_) &
return
#ifndef BOOST_DECIMAL_FAST_MATH
!isnan(lhs) && !isnan(rhs) &&
#endif
(lhs.sign_ == rhs.sign_) &&
(lhs.exponent_ == rhs.exponent_) &&
(lhs.significand_ == rhs.significand_);
}

constexpr auto operator!=(decimal32_fast lhs, decimal32_fast rhs) noexcept -> bool
{
return !(lhs == rhs);
return
#ifndef BOOST_DECIMAL_FAST_MATH
isnan(lhs) || isnan(rhs) ||
#endif
(lhs.sign_ != rhs.sign_) ||
(lhs.exponent_ != rhs.exponent_) ||
(lhs.significand_ != rhs.significand_);
}

constexpr auto operator<(decimal32_fast lhs, decimal32_fast rhs) noexcept -> bool
Expand Down Expand Up @@ -521,42 +519,37 @@ constexpr auto operator<(decimal32_fast lhs, decimal32_fast rhs) noexcept -> boo
}
#endif

if (lhs.significand_ == 0 || rhs.significand_ == 0)
{
if (lhs.significand_ == 0 && rhs.significand_ == 0)
{
#ifndef BOOST_DECIMAL_FAST_MATH
return lhs.sign_ && !rhs.sign_;
#else
return false;
#endif
}
return lhs.significand_ == 0 ? !rhs.sign_ : lhs.sign_;
}

if (lhs.sign_ != rhs.sign_)
{
return lhs.sign_;
}

if (lhs.exponent_ != rhs.exponent_)
{
return lhs.sign_ ? lhs.exponent_ > rhs.exponent_ : lhs.exponent_ < rhs.exponent_;
}

return lhs.sign_ ? lhs.significand_ > rhs.significand_ : lhs.significand_ < rhs.significand_;
return fast_type_less_parts_impl(lhs.significand_, lhs.biased_exponent(), lhs.sign_,
rhs.significand_, rhs.biased_exponent(), rhs.sign_);
}

constexpr auto operator<=(decimal32_fast lhs, decimal32_fast rhs) noexcept -> bool
{
#ifndef BOOST_DECIMAL_FAST_MATH
if (isnan(lhs) || isnan(rhs))
if (!isfinite(lhs) || !isfinite(rhs))
{
return false;
if (isnan(lhs) || isnan(rhs) ||
(!lhs.isneg() && rhs.isneg()))
{
return false;
}
else if (lhs.isneg() && !rhs.isneg())
{
return true;
}
else if (isfinite(lhs) && isinf(rhs))
{
return !signbit(rhs);
}
else if (isinf(lhs) && isfinite(rhs))
{
return signbit(rhs);
}
}
#endif

return !(rhs < lhs);
return !fast_type_less_parts_impl(rhs.significand_, rhs.biased_exponent(), rhs.sign_,
lhs.significand_, lhs.biased_exponent(), lhs.sign_);
}

constexpr auto operator>(decimal32_fast lhs, decimal32_fast rhs) noexcept -> bool
Expand All @@ -567,13 +560,29 @@ constexpr auto operator>(decimal32_fast lhs, decimal32_fast rhs) noexcept -> boo
constexpr auto operator>=(decimal32_fast lhs, decimal32_fast rhs) noexcept -> bool
{
#ifndef BOOST_DECIMAL_FAST_MATH
if (isnan(lhs) || isnan(rhs))
if (!isfinite(lhs) || !isfinite(rhs))
{
return false;
if (isnan(lhs) || isnan(rhs))
{
return false;
}
else if (lhs.isneg() && !rhs.isneg())
{
return false;
}
else if (isfinite(lhs) && isinf(rhs))
{
return signbit(rhs);
}
else if (isinf(lhs) && isfinite(rhs))
{
return !signbit(lhs);
}
}
#endif

return !(lhs < rhs);
return !fast_type_less_parts_impl(lhs.significand_, lhs.biased_exponent(), lhs.sign_,
rhs.significand_, rhs.biased_exponent(), rhs.sign_);
}

template <typename Integer>
Expand Down
30 changes: 30 additions & 0 deletions include/boost/decimal/detail/comparison.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,36 @@ constexpr auto operator!=(Decimal1 lhs, Decimal2 rhs) noexcept
return !(mixed_decimal_equality_impl(lhs, rhs));
}

template <BOOST_DECIMAL_INTEGRAL T, BOOST_DECIMAL_INTEGRAL U>
BOOST_DECIMAL_FORCE_INLINE constexpr auto fast_type_less_parts_impl(T lhs_sig, U lhs_exp, bool lhs_sign,
T rhs_sig, U rhs_exp, bool rhs_sign) noexcept -> bool
{
if (lhs_sig == 0 || rhs_sig == 0)
{
if (lhs_sig == 0 && rhs_sig == 0)
{
#ifndef BOOST_DECIMAL_FAST_MATH
return lhs_sign && !rhs_sign;
#else
return false;
#endif
}
return lhs_sig == 0 ? !rhs_sign : lhs_sign;
}

if (lhs_sign != rhs_sign)
{
return lhs_sign;
}

if (lhs_exp != rhs_exp)
{
return lhs_sign ? lhs_exp > rhs_exp : lhs_exp < rhs_exp;
}

return lhs_sign ? lhs_sig > rhs_sig : lhs_sig < rhs_sig;
}

template <BOOST_DECIMAL_DECIMAL_FLOATING_TYPE DecimalType = decimal32, BOOST_DECIMAL_INTEGRAL T1,
BOOST_DECIMAL_INTEGRAL U1, BOOST_DECIMAL_INTEGRAL T2, BOOST_DECIMAL_INTEGRAL U2>
constexpr auto less_parts_impl(T1 lhs_sig, U1 lhs_exp, bool lhs_sign,
Expand Down

0 comments on commit 4bf4e48

Please sign in to comment.