Skip to content

Commit 84d31e4

Browse files
authored
Merge pull request #683 from cppalliance/dec128_basics
Refactor decimal128 and decimal128_fast
2 parents 8d6d270 + ba77d62 commit 84d31e4

File tree

11 files changed

+255
-263
lines changed

11 files changed

+255
-263
lines changed

include/boost/decimal/decimal128.hpp

Lines changed: 38 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,17 +1565,16 @@ constexpr auto operator+(decimal128 lhs, decimal128 rhs) noexcept -> decimal128
15651565
<< "\nrhs exp: " << rhs_exp << std::endl;
15661566
#endif
15671567

1568-
const auto result {detail::d128_add_impl<detail::decimal128_components>(
1569-
lhs_sig, lhs_exp, lhs.isneg(),
1570-
rhs_sig, rhs_exp, rhs.isneg())};
1571-
1572-
return {result.sig, result.exp, result.sign};
1568+
return detail::d128_add_impl<decimal128>(lhs_sig, lhs_exp, lhs.isneg(),
1569+
rhs_sig, rhs_exp, rhs.isneg());
15731570
}
15741571

15751572
template <typename Integer>
15761573
constexpr auto operator+(decimal128 lhs, Integer rhs) noexcept
15771574
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal128)
15781575
{
1576+
using exp_type = decimal128::biased_exponent_type;
1577+
15791578
#ifndef BOOST_DECIMAL_FAST_MATH
15801579
if (isnan(lhs) || isinf(lhs))
15811580
{
@@ -1588,18 +1587,17 @@ constexpr auto operator+(decimal128 lhs, Integer rhs) noexcept
15881587
{
15891588
lhs_bigger = !lhs_bigger;
15901589
}
1591-
bool abs_lhs_bigger {abs(lhs) > detail::make_positive_unsigned(rhs)};
1590+
auto sig_rhs {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
1591+
bool abs_lhs_bigger {abs(lhs) > sig_rhs};
15921592

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

1598-
auto sig_rhs {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
1599-
std::int32_t exp_rhs {0};
1598+
exp_type exp_rhs {0};
16001599
detail::normalize<decimal128>(sig_rhs, exp_rhs);
1601-
auto unsigned_sig_rhs = detail::make_positive_unsigned(sig_rhs);
1602-
auto rhs_components {detail::decimal128_components{unsigned_sig_rhs, exp_rhs, (rhs < 0)}};
1600+
auto rhs_components {detail::decimal128_components{sig_rhs, exp_rhs, (rhs < 0)}};
16031601

16041602
if (!lhs_bigger)
16051603
{
@@ -1608,8 +1606,6 @@ constexpr auto operator+(decimal128 lhs, Integer rhs) noexcept
16081606
abs_lhs_bigger = !abs_lhs_bigger;
16091607
}
16101608

1611-
detail::decimal128_components result {};
1612-
16131609
#ifdef BOOST_DECIMAL_DEBUG_ADD
16141610
std::cerr << "Lhs sig: " << lhs_components.sig
16151611
<< "\nLhs exp: " << lhs_components.exp
@@ -1619,19 +1615,17 @@ constexpr auto operator+(decimal128 lhs, Integer rhs) noexcept
16191615

16201616
if (!lhs_components.sign && rhs_components.sign)
16211617
{
1622-
result = detail::d128_sub_impl<detail::decimal128_components>(
1618+
return detail::d128_sub_impl<decimal128>(
16231619
lhs_components.sig, lhs_components.exp, lhs_components.sign,
16241620
rhs_components.sig, rhs_components.exp, rhs_components.sign,
16251621
abs_lhs_bigger);
16261622
}
16271623
else
16281624
{
1629-
result = detail::d128_add_impl<detail::decimal128_components>(
1625+
return detail::d128_add_impl<decimal128>(
16301626
lhs_components.sig, lhs_components.exp, lhs_components.sign,
16311627
rhs_components.sig, rhs_components.exp, rhs_components.sign);
16321628
}
1633-
1634-
return decimal128(result.sig, result.exp, result.sign);
16351629
}
16361630

16371631
template <typename Integer>
@@ -1669,18 +1663,18 @@ constexpr auto operator-(decimal128 lhs, decimal128 rhs) noexcept -> decimal128
16691663
auto exp_rhs {rhs.biased_exponent()};
16701664
detail::normalize<decimal128>(sig_rhs, exp_rhs);
16711665

1672-
const auto result {detail::d128_sub_impl<detail::decimal128_components>(
1666+
return {detail::d128_sub_impl<decimal128>(
16731667
sig_lhs, exp_lhs, lhs.isneg(),
16741668
sig_rhs, exp_rhs, rhs.isneg(),
16751669
abs_lhs_bigger)};
1676-
1677-
return {result.sig, result.exp, result.sign};
16781670
}
16791671

16801672
template <typename Integer>
16811673
constexpr auto operator-(decimal128 lhs, Integer rhs) noexcept
16821674
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal128)
16831675
{
1676+
using exp_type = decimal128::biased_exponent_type;
1677+
16841678
#ifndef BOOST_DECIMAL_FAST_MATH
16851679
if (isinf(lhs) || isnan(lhs))
16861680
{
@@ -1693,31 +1687,28 @@ constexpr auto operator-(decimal128 lhs, Integer rhs) noexcept
16931687
return lhs + detail::make_positive_unsigned(rhs);
16941688
}
16951689

1696-
const bool abs_lhs_bigger {abs(lhs) > detail::make_positive_unsigned(rhs)};
1690+
auto sig_rhs {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
1691+
const bool abs_lhs_bigger {abs(lhs) > sig_rhs};
16971692

16981693
auto sig_lhs {lhs.full_significand()};
16991694
auto exp_lhs {lhs.biased_exponent()};
17001695
detail::normalize<decimal128>(sig_lhs, exp_lhs);
1701-
auto lhs_components {detail::decimal128_components{sig_lhs, exp_lhs, lhs.isneg()}};
17021696

1703-
auto sig_rhs {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
1704-
std::int32_t exp_rhs {0};
1697+
exp_type exp_rhs {0};
17051698
detail::normalize<decimal128>(sig_rhs, exp_rhs);
1706-
auto unsigned_sig_rhs {detail::make_positive_unsigned(sig_rhs)};
1707-
auto rhs_components {detail::decimal128_components{unsigned_sig_rhs, exp_rhs, (rhs < 0)}};
1708-
1709-
const auto result {detail::d128_sub_impl<detail::decimal128_components>(
1710-
lhs_components.sig, lhs_components.exp, lhs_components.sign,
1711-
rhs_components.sig, rhs_components.exp, rhs_components.sign,
1712-
abs_lhs_bigger)};
17131699

1714-
return {result.sig, result.exp, result.sign};
1700+
return detail::d128_sub_impl<decimal128>(
1701+
sig_lhs, exp_lhs, lhs.isneg(),
1702+
sig_rhs, exp_rhs, (rhs < 0),
1703+
abs_lhs_bigger);
17151704
}
17161705

17171706
template <typename Integer>
17181707
constexpr auto operator-(Integer lhs, decimal128 rhs) noexcept
17191708
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal128)
17201709
{
1710+
using exp_type = decimal128::biased_exponent_type;
1711+
17211712
#ifndef BOOST_DECIMAL_FAST_MATH
17221713
if (isinf(rhs) || isnan(rhs))
17231714
{
@@ -1730,25 +1721,20 @@ constexpr auto operator-(Integer lhs, decimal128 rhs) noexcept
17301721
return lhs + (-rhs);
17311722
}
17321723

1733-
const bool abs_lhs_bigger {detail::make_positive_unsigned(lhs) > abs(rhs)};
1734-
17351724
auto sig_lhs {static_cast<detail::uint128>(detail::make_positive_unsigned(lhs))};
1736-
std::int32_t exp_lhs {0};
1725+
const bool abs_lhs_bigger {sig_lhs > abs(rhs)};
1726+
1727+
exp_type exp_lhs {0};
17371728
detail::normalize<decimal128>(sig_lhs, exp_lhs);
1738-
auto unsigned_sig_lhs {detail::make_positive_unsigned(sig_lhs)};
1739-
auto lhs_components {detail::decimal128_components{unsigned_sig_lhs, exp_lhs, (lhs < 0)}};
17401729

17411730
auto sig_rhs {rhs.full_significand()};
17421731
auto exp_rhs {rhs.biased_exponent()};
17431732
detail::normalize<decimal128>(sig_rhs, exp_rhs);
1744-
auto rhs_components {detail::decimal128_components{sig_rhs, exp_rhs, rhs.isneg()}};
1745-
1746-
const auto result {detail::d128_sub_impl<detail::decimal128_components>(
1747-
lhs_components.sig, lhs_components.exp, lhs_components.sign,
1748-
rhs_components.sig, rhs_components.exp, rhs_components.sign,
1749-
abs_lhs_bigger)};
17501733

1751-
return {result.sig, result.exp, result.sign};
1734+
return detail::d128_sub_impl<decimal128>(
1735+
sig_lhs, exp_lhs, (lhs < 0),
1736+
sig_rhs, exp_rhs, rhs.isneg(),
1737+
abs_lhs_bigger);
17521738
}
17531739

17541740
constexpr auto operator*(decimal128 lhs, decimal128 rhs) noexcept -> decimal128
@@ -1775,17 +1761,17 @@ constexpr auto operator*(decimal128 lhs, decimal128 rhs) noexcept -> decimal128
17751761
rhs_sig = rhs_zeros.trimmed_number;
17761762
rhs_exp += static_cast<std::int32_t>(rhs_zeros.number_of_removed_zeros);
17771763

1778-
const auto result {detail::d128_mul_impl<detail::decimal128_components>(
1764+
return detail::d128_mul_impl<decimal128>(
17791765
lhs_sig, lhs_exp, lhs.isneg(),
1780-
rhs_sig, rhs_exp, rhs.isneg())};
1781-
1782-
return {result.sig, result.exp, result.sign};
1766+
rhs_sig, rhs_exp, rhs.isneg());
17831767
}
17841768

17851769
template <typename Integer>
17861770
constexpr auto operator*(decimal128 lhs, Integer rhs) noexcept
17871771
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal128)
17881772
{
1773+
using exp_type = decimal128::biased_exponent_type;
1774+
17891775
#ifndef BOOST_DECIMAL_FAST_MATH
17901776
if (isnan(lhs) || isinf(lhs))
17911777
{
@@ -1798,21 +1784,16 @@ constexpr auto operator*(decimal128 lhs, Integer rhs) noexcept
17981784
const auto lhs_zeros {detail::remove_trailing_zeros(lhs_sig)};
17991785
lhs_sig = lhs_zeros.trimmed_number;
18001786
lhs_exp += static_cast<std::int32_t>(lhs_zeros.number_of_removed_zeros);
1801-
auto lhs_components {detail::decimal128_components{lhs_sig, lhs_exp, lhs.isneg()}};
18021787

18031788
auto rhs_sig {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
1804-
std::int32_t rhs_exp {0};
18051789
const auto rhs_zeros {detail::remove_trailing_zeros(rhs_sig)};
18061790
rhs_sig = rhs_zeros.trimmed_number;
1807-
rhs_exp += static_cast<std::int32_t>(rhs_zeros.number_of_removed_zeros);
1808-
auto unsigned_sig_rhs {detail::make_positive_unsigned(rhs_sig)};
1809-
auto rhs_components {detail::decimal128_components{unsigned_sig_rhs, rhs_exp, (rhs < 0)}};
1791+
const auto rhs_exp = static_cast<exp_type>(rhs_zeros.number_of_removed_zeros);
18101792

1811-
const auto result {detail::d128_mul_impl<detail::decimal128_components>(
1812-
lhs_components.sig, lhs_components.exp, lhs_components.sign,
1813-
rhs_components.sig, rhs_components.exp, rhs_components.sign)};
1793+
return detail::d128_mul_impl<decimal128>(
1794+
lhs_sig, lhs_exp, lhs.isneg(),
1795+
rhs_sig, rhs_exp, (rhs < 0));
18141796

1815-
return {result.sig, result.exp, result.sign};
18161797
}
18171798

18181799
template <typename Integer>
@@ -1869,9 +1850,7 @@ constexpr auto operator/(decimal128 lhs, Integer rhs) noexcept
18691850

18701851
detail::decimal128_components lhs_components {lhs_sig, lhs_exp, lhs.isneg()};
18711852

1872-
auto rhs_sig {detail::make_positive_unsigned(rhs)};
1873-
std::int32_t rhs_exp {};
1874-
detail::decimal128_components rhs_components {rhs_sig, rhs_exp, rhs < 0};
1853+
detail::decimal128_components rhs_components {detail::make_positive_unsigned(rhs), 0, rhs < 0};
18751854
detail::decimal128_components q_components {};
18761855

18771856
detail::d128_generic_div_impl(lhs_components, rhs_components, q_components);

0 commit comments

Comments
 (0)