Skip to content

Commit

Permalink
Merge pull request #691 from cppalliance/add_64
Browse files Browse the repository at this point in the history
  • Loading branch information
mborland authored Jul 2, 2024
2 parents 860c5e7 + 9ca7ede commit 73669da
Show file tree
Hide file tree
Showing 7 changed files with 193 additions and 270 deletions.
64 changes: 5 additions & 59 deletions include/boost/decimal/decimal128.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1533,23 +1533,6 @@ constexpr auto operator+(decimal128 lhs, decimal128 rhs) noexcept -> decimal128
}
#endif

bool lhs_bigger {lhs > rhs};
if (lhs.isneg() && rhs.isneg())
{
lhs_bigger = !lhs_bigger;
}

// Ensure that lhs is always the larger for ease of impl
if (!lhs_bigger)
{
detail::swap(lhs, rhs);
}

if (!lhs.isneg() && rhs.isneg())
{
return lhs - abs(rhs);
}

auto lhs_sig {lhs.full_significand()};
auto lhs_exp {lhs.biased_exponent()};
detail::normalize<decimal128>(lhs_sig, lhs_exp);
Expand All @@ -1558,15 +1541,9 @@ constexpr auto operator+(decimal128 lhs, decimal128 rhs) noexcept -> decimal128
auto rhs_exp {rhs.biased_exponent()};
detail::normalize<decimal128>(rhs_sig, rhs_exp);

#ifdef BOOST_DECIMAL_DEBUG_ADD_128
std::cerr << "\nlhs sig: " << static_cast<detail::uint128_t>(lhs_sig)
<< "\nlhs exp: " << lhs_exp
<< "\nrhs sig: " << static_cast<detail::uint128_t>(rhs_sig)
<< "\nrhs exp: " << rhs_exp << std::endl;
#endif

return detail::d128_add_impl<decimal128>(lhs_sig, lhs_exp, lhs.isneg(),
rhs_sig, rhs_exp, rhs.isneg());
rhs_sig, rhs_exp, rhs.isneg(),
abs(lhs) > abs(rhs));
}

template <typename Integer>
Expand All @@ -1582,50 +1559,19 @@ constexpr auto operator+(decimal128 lhs, Integer rhs) noexcept
}
#endif

bool lhs_bigger {lhs > rhs};
if (lhs.isneg() && (rhs < 0))
{
lhs_bigger = !lhs_bigger;
}
auto sig_rhs {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
bool abs_lhs_bigger {abs(lhs) > sig_rhs};

auto sig_lhs {lhs.full_significand()};
auto exp_lhs {lhs.biased_exponent()};
detail::normalize<decimal128>(sig_lhs, exp_lhs);
auto lhs_components {detail::decimal128_components{sig_lhs, exp_lhs, lhs.isneg()}};

exp_type exp_rhs {0};
detail::normalize<decimal128>(sig_rhs, exp_rhs);
auto rhs_components {detail::decimal128_components{sig_rhs, exp_rhs, (rhs < 0)}};

if (!lhs_bigger)
{
detail::swap(lhs_components, rhs_components);
lhs_bigger = !lhs_bigger;
abs_lhs_bigger = !abs_lhs_bigger;
}

#ifdef BOOST_DECIMAL_DEBUG_ADD
std::cerr << "Lhs sig: " << lhs_components.sig
<< "\nLhs exp: " << lhs_components.exp
<< "\nRhs sig: " << rhs_components.sig
<< "\nRhs exp: " << rhs_components.exp << std::endl;
#endif

if (!lhs_components.sign && rhs_components.sign)
{
return detail::d128_sub_impl<decimal128>(
lhs_components.sig, lhs_components.exp, lhs_components.sign,
rhs_components.sig, rhs_components.exp, rhs_components.sign,
abs_lhs_bigger);
}
else
{
return detail::d128_add_impl<decimal128>(
lhs_components.sig, lhs_components.exp, lhs_components.sign,
rhs_components.sig, rhs_components.exp, rhs_components.sign);
}
return detail::d128_add_impl<decimal128>(sig_lhs, exp_lhs, lhs.isneg(),
sig_rhs, exp_rhs, (rhs < 0),
abs_lhs_bigger);
}

template <typename Integer>
Expand Down
58 changes: 5 additions & 53 deletions include/boost/decimal/decimal128_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,26 +777,10 @@ constexpr auto operator+(decimal128_fast lhs, decimal128_fast rhs) noexcept -> d
}
#endif

bool lhs_bigger {lhs > rhs};
if (lhs.isneg() && rhs.isneg())
{
lhs_bigger = !lhs_bigger;
}

// Ensure that lhs is always the larger for ease of impl
if (!lhs_bigger)
{
detail::swap(lhs, rhs);
}

if (!lhs.isneg() && rhs.isneg())
{
return lhs - abs(rhs);
}

return detail::d128_add_impl<decimal128_fast>(
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
rhs.significand_, rhs.biased_exponent(), rhs.sign_);
rhs.significand_, rhs.biased_exponent(), rhs.sign_,
(abs(lhs) > abs(rhs)));
};

template <typename Integer>
Expand All @@ -812,47 +796,15 @@ constexpr auto operator+(decimal128_fast lhs, Integer rhs) noexcept
}
#endif

bool lhs_bigger {lhs > rhs};
if (lhs.isneg() && (rhs < 0))
{
lhs_bigger = !lhs_bigger;
}

auto sig_rhs {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
bool abs_lhs_bigger {abs(lhs) > sig_rhs};

auto lhs_components {detail::decimal128_fast_components{lhs.significand_, lhs.biased_exponent(), lhs.isneg()}};

exp_type exp_rhs {0};
detail::normalize<decimal128>(sig_rhs, exp_rhs);
auto rhs_components {detail::decimal128_fast_components{sig_rhs, exp_rhs, (rhs < 0)}};

if (!lhs_bigger)
{
detail::swap(lhs_components, rhs_components);
abs_lhs_bigger = !abs_lhs_bigger;
}

#ifdef BOOST_DECIMAL_DEBUG_ADD
std::cerr << "Lhs sig: " << lhs_components.sig
<< "\nLhs exp: " << lhs_components.exp
<< "\nRhs sig: " << rhs_components.sig
<< "\nRhs exp: " << rhs_components.exp << std::endl;
#endif

if (!lhs_components.sign && rhs_components.sign)
{
return detail::d128_sub_impl<decimal128_fast>(
lhs_components.sig, lhs_components.exp, lhs_components.sign,
rhs_components.sig, rhs_components.exp, rhs_components.sign,
abs_lhs_bigger);
}
else
{
return detail::d128_add_impl<decimal128_fast>(
lhs_components.sig, lhs_components.exp, lhs_components.sign,
rhs_components.sig, rhs_components.exp, rhs_components.sign);
}
return detail::d128_add_impl<decimal128_fast>(lhs.significand_, lhs.biased_exponent(), lhs.sign_,
sig_rhs, exp_rhs, (rhs < 0),
abs_lhs_bigger);
}

template <typename Integer>
Expand Down
57 changes: 6 additions & 51 deletions include/boost/decimal/decimal64.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1194,23 +1194,6 @@ constexpr auto operator+(decimal64 lhs, decimal64 rhs) noexcept -> decimal64
}
#endif

bool lhs_bigger {lhs > rhs};
if (lhs.isneg() && rhs.isneg())
{
lhs_bigger = !lhs_bigger;
}

// Ensure that lhs is always the larger for ease of impl
if (!lhs_bigger)
{
detail::swap(lhs, rhs);
}

if (!lhs.isneg() && rhs.isneg())
{
return lhs - abs(rhs);
}

auto lhs_sig {lhs.full_significand()};
auto lhs_exp {lhs.biased_exponent()};
detail::normalize<decimal64>(lhs_sig, lhs_exp);
Expand All @@ -1220,7 +1203,8 @@ constexpr auto operator+(decimal64 lhs, decimal64 rhs) noexcept -> decimal64
detail::normalize<decimal64>(rhs_sig, rhs_exp);

return detail::d64_add_impl<decimal64>(lhs_sig, lhs_exp, lhs.isneg(),
rhs_sig, rhs_exp, rhs.isneg());
rhs_sig, rhs_exp, rhs.isneg(),
(abs(lhs) > abs(rhs)));
}

template <typename Integer>
Expand All @@ -1237,49 +1221,20 @@ constexpr auto operator+(decimal64 lhs, Integer rhs) noexcept
}
#endif

bool lhs_bigger {lhs > rhs};
if (lhs.isneg() && (rhs < 0))
{
lhs_bigger = !lhs_bigger;
}

auto sig_rhs {static_cast<promoted_significand_type>(detail::make_positive_unsigned(rhs))};
bool abs_lhs_bigger {abs(lhs) > sig_rhs};

auto sig_lhs {lhs.full_significand()};
auto exp_lhs {lhs.biased_exponent()};
detail::normalize<decimal64>(sig_lhs, exp_lhs);
auto lhs_components {detail::decimal64_components{sig_lhs, exp_lhs, lhs.isneg()}};

exp_type exp_rhs {0};
detail::normalize<decimal64>(sig_rhs, exp_rhs);
const auto final_sig_rhs {static_cast<detail::decimal64_components::significand_type>(sig_rhs)};
auto rhs_components {detail::decimal64_components{final_sig_rhs, exp_rhs, (rhs < 0)}};

if (!lhs_bigger)
{
detail::swap(lhs_components, rhs_components);
abs_lhs_bigger = !abs_lhs_bigger;
}

#ifdef BOOST_DECIMAL_DEBUG_ADD
std::cerr << "Lhs sig: " << lhs_components.sig
<< "\nLhs exp: " << lhs_components.exp
<< "\nRhs sig: " << rhs_components.sig
<< "\nRhs exp: " << rhs_components.exp << std::endl;
#endif
const auto final_sig_rhs {static_cast<decimal64::significand_type>(sig_rhs)};

if (!lhs_components.sign && rhs_components.sign)
{
return detail::d64_sub_impl<decimal64>(lhs_components.sig, lhs_components.exp, lhs_components.sign,
rhs_components.sig, rhs_components.exp, rhs_components.sign,
abs_lhs_bigger);
}
else
{
return detail::d64_add_impl<decimal64>(lhs_components.sig, lhs_components.exp, lhs_components.sign,
rhs_components.sig, rhs_components.exp, rhs_components.sign);
}
return detail::d64_add_impl<decimal64>(sig_lhs, exp_lhs, lhs.isneg(),
final_sig_rhs, exp_rhs, (rhs < 0),
abs_lhs_bigger);
}

template <typename Integer>
Expand Down
62 changes: 8 additions & 54 deletions include/boost/decimal/decimal64_fast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -888,27 +888,10 @@ constexpr auto operator+(decimal64_fast lhs, decimal64_fast rhs) noexcept -> dec
}
#endif

bool lhs_bigger {lhs > rhs};
if (lhs.isneg() && rhs.isneg())
{
lhs_bigger = !lhs_bigger;
}

// Ensure that lhs is always the larger for ease of impl
if (!lhs_bigger)
{
detail::swap(lhs, rhs);
}

if (!lhs.isneg() && rhs.isneg())
{
return lhs - abs(rhs);
}

return detail::d64_add_impl<decimal64_fast>(
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
rhs.significand_, rhs.biased_exponent(), rhs.sign_
);
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
rhs.significand_, rhs.biased_exponent(), rhs.sign_,
(abs(lhs) > abs(rhs)));
}

template <typename Integer>
Expand All @@ -925,45 +908,16 @@ constexpr auto operator+(decimal64_fast lhs, Integer rhs) noexcept
}
#endif

bool lhs_bigger {lhs > rhs};
if (lhs.isneg() && (rhs < 0))
{
lhs_bigger = !lhs_bigger;
}
auto sig_rhs {static_cast<promoted_significand_type>(detail::make_positive_unsigned(rhs))};
bool abs_lhs_bigger {abs(lhs) > sig_rhs};

auto lhs_components {detail::decimal64_fast_components{lhs.significand_, lhs.biased_exponent(), lhs.isneg()}};
const bool abs_lhs_bigger {abs(lhs) > sig_rhs};

exp_type exp_rhs {0};
detail::normalize<decimal64>(sig_rhs, exp_rhs);
auto unsigned_sig_rhs {static_cast<detail::decimal64_fast_components::significand_type>(sig_rhs)};
auto rhs_components {detail::decimal64_fast_components{unsigned_sig_rhs, exp_rhs, (rhs < 0)}};

if (!lhs_bigger)
{
detail::swap(lhs_components, rhs_components);
abs_lhs_bigger = !abs_lhs_bigger;
}

#ifdef BOOST_DECIMAL_DEBUG_ADD
std::cerr << "Lhs sig: " << lhs_components.sig
<< "\nLhs exp: " << lhs_components.exp
<< "\nRhs sig: " << rhs_components.sig
<< "\nRhs exp: " << rhs_components.exp << std::endl;
#endif
const auto final_sig_rhs {static_cast<decimal64_fast::significand_type>(sig_rhs)};

if (!lhs_components.sign && rhs_components.sign)
{
return detail::d64_sub_impl<decimal64_fast>(lhs_components.sig, lhs_components.exp, lhs_components.sign,
rhs_components.sig, rhs_components.exp, rhs_components.sign,
abs_lhs_bigger);
}
else
{
return detail::d64_add_impl<decimal64_fast>(lhs_components.sig, lhs_components.exp, lhs_components.sign,
rhs_components.sig, rhs_components.exp, rhs_components.sign);
}
return detail::d64_add_impl<decimal64_fast>(lhs.significand_, lhs.biased_exponent(), lhs.sign_,
final_sig_rhs, exp_rhs, (rhs < 0),
abs_lhs_bigger);
}

template <typename Integer>
Expand Down
Loading

0 comments on commit 73669da

Please sign in to comment.