Skip to content

Commit

Permalink
Refactor name and remove old 128 bit implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mborland committed Jul 2, 2024
1 parent 76dafa5 commit 50393e8
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 79 deletions.
6 changes: 3 additions & 3 deletions include/boost/decimal/decimal128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1602,7 +1602,7 @@ constexpr auto operator-(decimal128 lhs, decimal128 rhs) noexcept -> decimal128
auto exp_rhs {rhs.biased_exponent()};
detail::normalize<decimal128>(sig_rhs, exp_rhs);

return detail::new_d128_sub_impl<decimal128>(
return detail::d128_sub_impl<decimal128>(
sig_lhs, exp_lhs, lhs.isneg(),
sig_rhs, exp_rhs, rhs.isneg(),
abs(lhs) > abs(rhs));
Expand Down Expand Up @@ -1631,7 +1631,7 @@ constexpr auto operator-(decimal128 lhs, Integer rhs) noexcept
exp_type exp_rhs {0};
detail::normalize<decimal128>(sig_rhs, exp_rhs);

return detail::new_d128_sub_impl<decimal128>(
return detail::d128_sub_impl<decimal128>(
sig_lhs, exp_lhs, lhs.isneg(),
sig_rhs, exp_rhs, (rhs < 0),
abs_lhs_bigger);
Expand Down Expand Up @@ -1660,7 +1660,7 @@ constexpr auto operator-(Integer lhs, decimal128 rhs) noexcept
auto exp_rhs {rhs.biased_exponent()};
detail::normalize<decimal128>(sig_rhs, exp_rhs);

return detail::new_d128_sub_impl<decimal128>(
return detail::d128_sub_impl<decimal128>(
sig_lhs, exp_lhs, (lhs < 0),
sig_rhs, exp_rhs, rhs.isneg(),
abs_lhs_bigger);
Expand Down
6 changes: 3 additions & 3 deletions include/boost/decimal/decimal128_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ constexpr auto operator-(decimal128_fast lhs, decimal128_fast rhs) noexcept -> d
}
#endif

return detail::new_d128_sub_impl<decimal128_fast>(
return detail::d128_sub_impl<decimal128_fast>(
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
rhs.significand_, rhs.biased_exponent(), rhs.sign_,
abs(lhs) > abs(rhs));
Expand All @@ -851,7 +851,7 @@ constexpr auto operator-(decimal128_fast lhs, Integer rhs) noexcept
exp_type exp_rhs {0};
detail::normalize<decimal128>(sig_rhs, exp_rhs);

return detail::new_d128_sub_impl<decimal128_fast>(
return detail::d128_sub_impl<decimal128_fast>(
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
sig_rhs, exp_rhs, (rhs < 0),
abs_lhs_bigger);
Expand All @@ -876,7 +876,7 @@ constexpr auto operator-(Integer lhs, decimal128_fast rhs) noexcept
exp_type exp_lhs {0};
detail::normalize<decimal128>(sig_lhs, exp_lhs);

return detail::new_d128_sub_impl<decimal128_fast>(
return detail::d128_sub_impl<decimal128_fast>(
sig_lhs, exp_lhs, (lhs < 0),
rhs.significand_, rhs.biased_exponent(), rhs.sign_,
abs_lhs_bigger);
Expand Down
74 changes: 1 addition & 73 deletions include/boost/decimal/detail/sub_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ constexpr auto d64_sub_impl(T lhs_sig, U lhs_exp, bool lhs_sign,
}

template <typename ReturnType, BOOST_DECIMAL_INTEGRAL T, BOOST_DECIMAL_INTEGRAL U>
constexpr auto new_d128_sub_impl(T lhs_sig, U lhs_exp, bool lhs_sign,
constexpr auto d128_sub_impl(T lhs_sig, U lhs_exp, bool lhs_sign,
T rhs_sig, U rhs_exp, bool rhs_sign,
bool abs_lhs_bigger) noexcept -> ReturnType
{
Expand Down Expand Up @@ -220,78 +220,6 @@ constexpr auto new_d128_sub_impl(T lhs_sig, U lhs_exp, bool lhs_sign,
return {res_sig, new_exp, new_sign};
}

template <typename ReturnType, BOOST_DECIMAL_INTEGRAL T1, BOOST_DECIMAL_INTEGRAL U1,
BOOST_DECIMAL_INTEGRAL T2, BOOST_DECIMAL_INTEGRAL U2>
constexpr auto d128_sub_impl(T1 lhs_sig, U1 lhs_exp, bool lhs_sign,
T2 rhs_sig, U2 rhs_exp, bool rhs_sign,
bool abs_lhs_bigger) noexcept -> ReturnType
{
#if defined(BOOST_DECIMAL_HAS_INT128) && (!defined(__clang_major__) || __clang_major__ > 13)
using sub_type = detail::int128_t;
#else
using sub_type = detail::int128;
#endif

using unsigned_sub_type = detail::make_unsigned_t<sub_type>;

auto delta_exp {lhs_exp > rhs_exp ? lhs_exp - rhs_exp : rhs_exp - lhs_exp};

if (delta_exp > detail::precision_v<decimal128> + 1)
{
// If the difference in exponents is more than the digits of accuracy
// we return the larger of the two
//
// e.g. 1e20 - 1e-20 = 1e20
return abs_lhs_bigger ? ReturnType{detail::shrink_significand<detail::uint128>(lhs_sig, lhs_exp), lhs_exp, false} :
ReturnType{detail::shrink_significand<detail::uint128>(rhs_sig, rhs_exp), rhs_exp, true};
}

// The two numbers can be subtracted together without special handling

auto& sig_bigger {abs_lhs_bigger ? lhs_sig : rhs_sig};
auto& exp_bigger {abs_lhs_bigger ? lhs_exp : rhs_exp};
auto& sig_smaller {abs_lhs_bigger ? rhs_sig : lhs_sig};
auto& smaller_sign {abs_lhs_bigger ? rhs_sign : lhs_sign};

if (delta_exp == 1)
{
sig_bigger *= 10;
--delta_exp;
--exp_bigger;
}
else
{
if (delta_exp >= 2)
{
sig_bigger *= 100;
delta_exp -= 2;
exp_bigger -= 2;
}

if (delta_exp > 1)
{
sig_smaller /= pow10<std::remove_reference_t<decltype(sig_smaller)>>(delta_exp - 1);
delta_exp = 1;
}

if (delta_exp == 1)
{
detail::fenv_round<decimal128>(sig_smaller, smaller_sign);
}
}

const auto signed_sig_lhs {detail::make_signed_value(static_cast<unsigned_sub_type>(lhs_sig), lhs_sign)};
const auto signed_sig_rhs {detail::make_signed_value(static_cast<unsigned_sub_type>(rhs_sig), rhs_sign)};

const auto new_sig {rhs_sign && !lhs_sign ? signed_sig_lhs + signed_sig_rhs : signed_sig_lhs - signed_sig_rhs};

const auto new_exp {abs_lhs_bigger ? lhs_exp : rhs_exp};
const auto new_sign {new_sig < 0};
const auto res_sig {detail::make_positive_unsigned(new_sig)};

return {res_sig, new_exp, new_sign};
}

} // namespace detail
} // namespace decimal
} // namespace boost
Expand Down

0 comments on commit 50393e8

Please sign in to comment.