Skip to content

Commit 73669da

Browse files
authored
Merge pull request #691 from cppalliance/add_64
2 parents 860c5e7 + 9ca7ede commit 73669da

File tree

7 files changed

+193
-270
lines changed

7 files changed

+193
-270
lines changed

include/boost/decimal/decimal128.hpp

Lines changed: 5 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,23 +1533,6 @@ constexpr auto operator+(decimal128 lhs, decimal128 rhs) noexcept -> decimal128
15331533
}
15341534
#endif
15351535

1536-
bool lhs_bigger {lhs > rhs};
1537-
if (lhs.isneg() && rhs.isneg())
1538-
{
1539-
lhs_bigger = !lhs_bigger;
1540-
}
1541-
1542-
// Ensure that lhs is always the larger for ease of impl
1543-
if (!lhs_bigger)
1544-
{
1545-
detail::swap(lhs, rhs);
1546-
}
1547-
1548-
if (!lhs.isneg() && rhs.isneg())
1549-
{
1550-
return lhs - abs(rhs);
1551-
}
1552-
15531536
auto lhs_sig {lhs.full_significand()};
15541537
auto lhs_exp {lhs.biased_exponent()};
15551538
detail::normalize<decimal128>(lhs_sig, lhs_exp);
@@ -1558,15 +1541,9 @@ constexpr auto operator+(decimal128 lhs, decimal128 rhs) noexcept -> decimal128
15581541
auto rhs_exp {rhs.biased_exponent()};
15591542
detail::normalize<decimal128>(rhs_sig, rhs_exp);
15601543

1561-
#ifdef BOOST_DECIMAL_DEBUG_ADD_128
1562-
std::cerr << "\nlhs sig: " << static_cast<detail::uint128_t>(lhs_sig)
1563-
<< "\nlhs exp: " << lhs_exp
1564-
<< "\nrhs sig: " << static_cast<detail::uint128_t>(rhs_sig)
1565-
<< "\nrhs exp: " << rhs_exp << std::endl;
1566-
#endif
1567-
15681544
return detail::d128_add_impl<decimal128>(lhs_sig, lhs_exp, lhs.isneg(),
1569-
rhs_sig, rhs_exp, rhs.isneg());
1545+
rhs_sig, rhs_exp, rhs.isneg(),
1546+
abs(lhs) > abs(rhs));
15701547
}
15711548

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

1585-
bool lhs_bigger {lhs > rhs};
1586-
if (lhs.isneg() && (rhs < 0))
1587-
{
1588-
lhs_bigger = !lhs_bigger;
1589-
}
15901562
auto sig_rhs {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
15911563
bool abs_lhs_bigger {abs(lhs) > sig_rhs};
15921564

15931565
auto sig_lhs {lhs.full_significand()};
15941566
auto exp_lhs {lhs.biased_exponent()};
15951567
detail::normalize<decimal128>(sig_lhs, exp_lhs);
1596-
auto lhs_components {detail::decimal128_components{sig_lhs, exp_lhs, lhs.isneg()}};
15971568

15981569
exp_type exp_rhs {0};
15991570
detail::normalize<decimal128>(sig_rhs, exp_rhs);
1600-
auto rhs_components {detail::decimal128_components{sig_rhs, exp_rhs, (rhs < 0)}};
1601-
1602-
if (!lhs_bigger)
1603-
{
1604-
detail::swap(lhs_components, rhs_components);
1605-
lhs_bigger = !lhs_bigger;
1606-
abs_lhs_bigger = !abs_lhs_bigger;
1607-
}
1608-
1609-
#ifdef BOOST_DECIMAL_DEBUG_ADD
1610-
std::cerr << "Lhs sig: " << lhs_components.sig
1611-
<< "\nLhs exp: " << lhs_components.exp
1612-
<< "\nRhs sig: " << rhs_components.sig
1613-
<< "\nRhs exp: " << rhs_components.exp << std::endl;
1614-
#endif
16151571

1616-
if (!lhs_components.sign && rhs_components.sign)
1617-
{
1618-
return detail::d128_sub_impl<decimal128>(
1619-
lhs_components.sig, lhs_components.exp, lhs_components.sign,
1620-
rhs_components.sig, rhs_components.exp, rhs_components.sign,
1621-
abs_lhs_bigger);
1622-
}
1623-
else
1624-
{
1625-
return detail::d128_add_impl<decimal128>(
1626-
lhs_components.sig, lhs_components.exp, lhs_components.sign,
1627-
rhs_components.sig, rhs_components.exp, rhs_components.sign);
1628-
}
1572+
return detail::d128_add_impl<decimal128>(sig_lhs, exp_lhs, lhs.isneg(),
1573+
sig_rhs, exp_rhs, (rhs < 0),
1574+
abs_lhs_bigger);
16291575
}
16301576

16311577
template <typename Integer>

include/boost/decimal/decimal128_fast.hpp

Lines changed: 5 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -777,26 +777,10 @@ constexpr auto operator+(decimal128_fast lhs, decimal128_fast rhs) noexcept -> d
777777
}
778778
#endif
779779

780-
bool lhs_bigger {lhs > rhs};
781-
if (lhs.isneg() && rhs.isneg())
782-
{
783-
lhs_bigger = !lhs_bigger;
784-
}
785-
786-
// Ensure that lhs is always the larger for ease of impl
787-
if (!lhs_bigger)
788-
{
789-
detail::swap(lhs, rhs);
790-
}
791-
792-
if (!lhs.isneg() && rhs.isneg())
793-
{
794-
return lhs - abs(rhs);
795-
}
796-
797780
return detail::d128_add_impl<decimal128_fast>(
798781
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
799-
rhs.significand_, rhs.biased_exponent(), rhs.sign_);
782+
rhs.significand_, rhs.biased_exponent(), rhs.sign_,
783+
(abs(lhs) > abs(rhs)));
800784
};
801785

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

815-
bool lhs_bigger {lhs > rhs};
816-
if (lhs.isneg() && (rhs < 0))
817-
{
818-
lhs_bigger = !lhs_bigger;
819-
}
820-
821799
auto sig_rhs {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
822800
bool abs_lhs_bigger {abs(lhs) > sig_rhs};
823801

824-
auto lhs_components {detail::decimal128_fast_components{lhs.significand_, lhs.biased_exponent(), lhs.isneg()}};
825-
826802
exp_type exp_rhs {0};
827803
detail::normalize<decimal128>(sig_rhs, exp_rhs);
828-
auto rhs_components {detail::decimal128_fast_components{sig_rhs, exp_rhs, (rhs < 0)}};
829804

830-
if (!lhs_bigger)
831-
{
832-
detail::swap(lhs_components, rhs_components);
833-
abs_lhs_bigger = !abs_lhs_bigger;
834-
}
835-
836-
#ifdef BOOST_DECIMAL_DEBUG_ADD
837-
std::cerr << "Lhs sig: " << lhs_components.sig
838-
<< "\nLhs exp: " << lhs_components.exp
839-
<< "\nRhs sig: " << rhs_components.sig
840-
<< "\nRhs exp: " << rhs_components.exp << std::endl;
841-
#endif
842-
843-
if (!lhs_components.sign && rhs_components.sign)
844-
{
845-
return detail::d128_sub_impl<decimal128_fast>(
846-
lhs_components.sig, lhs_components.exp, lhs_components.sign,
847-
rhs_components.sig, rhs_components.exp, rhs_components.sign,
848-
abs_lhs_bigger);
849-
}
850-
else
851-
{
852-
return detail::d128_add_impl<decimal128_fast>(
853-
lhs_components.sig, lhs_components.exp, lhs_components.sign,
854-
rhs_components.sig, rhs_components.exp, rhs_components.sign);
855-
}
805+
return detail::d128_add_impl<decimal128_fast>(lhs.significand_, lhs.biased_exponent(), lhs.sign_,
806+
sig_rhs, exp_rhs, (rhs < 0),
807+
abs_lhs_bigger);
856808
}
857809

858810
template <typename Integer>

include/boost/decimal/decimal64.hpp

Lines changed: 6 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,23 +1194,6 @@ constexpr auto operator+(decimal64 lhs, decimal64 rhs) noexcept -> decimal64
11941194
}
11951195
#endif
11961196

1197-
bool lhs_bigger {lhs > rhs};
1198-
if (lhs.isneg() && rhs.isneg())
1199-
{
1200-
lhs_bigger = !lhs_bigger;
1201-
}
1202-
1203-
// Ensure that lhs is always the larger for ease of impl
1204-
if (!lhs_bigger)
1205-
{
1206-
detail::swap(lhs, rhs);
1207-
}
1208-
1209-
if (!lhs.isneg() && rhs.isneg())
1210-
{
1211-
return lhs - abs(rhs);
1212-
}
1213-
12141197
auto lhs_sig {lhs.full_significand()};
12151198
auto lhs_exp {lhs.biased_exponent()};
12161199
detail::normalize<decimal64>(lhs_sig, lhs_exp);
@@ -1220,7 +1203,8 @@ constexpr auto operator+(decimal64 lhs, decimal64 rhs) noexcept -> decimal64
12201203
detail::normalize<decimal64>(rhs_sig, rhs_exp);
12211204

12221205
return detail::d64_add_impl<decimal64>(lhs_sig, lhs_exp, lhs.isneg(),
1223-
rhs_sig, rhs_exp, rhs.isneg());
1206+
rhs_sig, rhs_exp, rhs.isneg(),
1207+
(abs(lhs) > abs(rhs)));
12241208
}
12251209

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

1240-
bool lhs_bigger {lhs > rhs};
1241-
if (lhs.isneg() && (rhs < 0))
1242-
{
1243-
lhs_bigger = !lhs_bigger;
1244-
}
1245-
12461224
auto sig_rhs {static_cast<promoted_significand_type>(detail::make_positive_unsigned(rhs))};
12471225
bool abs_lhs_bigger {abs(lhs) > sig_rhs};
12481226

12491227
auto sig_lhs {lhs.full_significand()};
12501228
auto exp_lhs {lhs.biased_exponent()};
12511229
detail::normalize<decimal64>(sig_lhs, exp_lhs);
1252-
auto lhs_components {detail::decimal64_components{sig_lhs, exp_lhs, lhs.isneg()}};
12531230

12541231
exp_type exp_rhs {0};
12551232
detail::normalize<decimal64>(sig_rhs, exp_rhs);
1256-
const auto final_sig_rhs {static_cast<detail::decimal64_components::significand_type>(sig_rhs)};
1257-
auto rhs_components {detail::decimal64_components{final_sig_rhs, exp_rhs, (rhs < 0)}};
1258-
1259-
if (!lhs_bigger)
1260-
{
1261-
detail::swap(lhs_components, rhs_components);
1262-
abs_lhs_bigger = !abs_lhs_bigger;
1263-
}
1264-
1265-
#ifdef BOOST_DECIMAL_DEBUG_ADD
1266-
std::cerr << "Lhs sig: " << lhs_components.sig
1267-
<< "\nLhs exp: " << lhs_components.exp
1268-
<< "\nRhs sig: " << rhs_components.sig
1269-
<< "\nRhs exp: " << rhs_components.exp << std::endl;
1270-
#endif
1233+
const auto final_sig_rhs {static_cast<decimal64::significand_type>(sig_rhs)};
12711234

1272-
if (!lhs_components.sign && rhs_components.sign)
1273-
{
1274-
return detail::d64_sub_impl<decimal64>(lhs_components.sig, lhs_components.exp, lhs_components.sign,
1275-
rhs_components.sig, rhs_components.exp, rhs_components.sign,
1276-
abs_lhs_bigger);
1277-
}
1278-
else
1279-
{
1280-
return detail::d64_add_impl<decimal64>(lhs_components.sig, lhs_components.exp, lhs_components.sign,
1281-
rhs_components.sig, rhs_components.exp, rhs_components.sign);
1282-
}
1235+
return detail::d64_add_impl<decimal64>(sig_lhs, exp_lhs, lhs.isneg(),
1236+
final_sig_rhs, exp_rhs, (rhs < 0),
1237+
abs_lhs_bigger);
12831238
}
12841239

12851240
template <typename Integer>

include/boost/decimal/decimal64_fast.hpp

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -888,27 +888,10 @@ constexpr auto operator+(decimal64_fast lhs, decimal64_fast rhs) noexcept -> dec
888888
}
889889
#endif
890890

891-
bool lhs_bigger {lhs > rhs};
892-
if (lhs.isneg() && rhs.isneg())
893-
{
894-
lhs_bigger = !lhs_bigger;
895-
}
896-
897-
// Ensure that lhs is always the larger for ease of impl
898-
if (!lhs_bigger)
899-
{
900-
detail::swap(lhs, rhs);
901-
}
902-
903-
if (!lhs.isneg() && rhs.isneg())
904-
{
905-
return lhs - abs(rhs);
906-
}
907-
908891
return detail::d64_add_impl<decimal64_fast>(
909-
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
910-
rhs.significand_, rhs.biased_exponent(), rhs.sign_
911-
);
892+
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
893+
rhs.significand_, rhs.biased_exponent(), rhs.sign_,
894+
(abs(lhs) > abs(rhs)));
912895
}
913896

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

928-
bool lhs_bigger {lhs > rhs};
929-
if (lhs.isneg() && (rhs < 0))
930-
{
931-
lhs_bigger = !lhs_bigger;
932-
}
933911
auto sig_rhs {static_cast<promoted_significand_type>(detail::make_positive_unsigned(rhs))};
934-
bool abs_lhs_bigger {abs(lhs) > sig_rhs};
935-
936-
auto lhs_components {detail::decimal64_fast_components{lhs.significand_, lhs.biased_exponent(), lhs.isneg()}};
912+
const bool abs_lhs_bigger {abs(lhs) > sig_rhs};
937913

938914
exp_type exp_rhs {0};
939915
detail::normalize<decimal64>(sig_rhs, exp_rhs);
940-
auto unsigned_sig_rhs {static_cast<detail::decimal64_fast_components::significand_type>(sig_rhs)};
941-
auto rhs_components {detail::decimal64_fast_components{unsigned_sig_rhs, exp_rhs, (rhs < 0)}};
942-
943-
if (!lhs_bigger)
944-
{
945-
detail::swap(lhs_components, rhs_components);
946-
abs_lhs_bigger = !abs_lhs_bigger;
947-
}
948-
949-
#ifdef BOOST_DECIMAL_DEBUG_ADD
950-
std::cerr << "Lhs sig: " << lhs_components.sig
951-
<< "\nLhs exp: " << lhs_components.exp
952-
<< "\nRhs sig: " << rhs_components.sig
953-
<< "\nRhs exp: " << rhs_components.exp << std::endl;
954-
#endif
916+
const auto final_sig_rhs {static_cast<decimal64_fast::significand_type>(sig_rhs)};
955917

956-
if (!lhs_components.sign && rhs_components.sign)
957-
{
958-
return detail::d64_sub_impl<decimal64_fast>(lhs_components.sig, lhs_components.exp, lhs_components.sign,
959-
rhs_components.sig, rhs_components.exp, rhs_components.sign,
960-
abs_lhs_bigger);
961-
}
962-
else
963-
{
964-
return detail::d64_add_impl<decimal64_fast>(lhs_components.sig, lhs_components.exp, lhs_components.sign,
965-
rhs_components.sig, rhs_components.exp, rhs_components.sign);
966-
}
918+
return detail::d64_add_impl<decimal64_fast>(lhs.significand_, lhs.biased_exponent(), lhs.sign_,
919+
final_sig_rhs, exp_rhs, (rhs < 0),
920+
abs_lhs_bigger);
967921
}
968922

969923
template <typename Integer>

0 commit comments

Comments
 (0)