Skip to content

Commit 8c05027

Browse files
committed
Refactor to avoid copies and improve readability
1 parent b1cdaf1 commit 8c05027

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

include/boost/decimal/decimal32.hpp

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -867,15 +867,16 @@ constexpr auto operator+(decimal32 lhs, decimal32 rhs) noexcept -> decimal32
867867
auto exp_rhs {rhs.biased_exponent()};
868868
detail::normalize(sig_rhs, exp_rhs);
869869

870-
return {detail::add_impl<decimal32>(sig_lhs, exp_lhs, lhs.isneg(),
871-
sig_rhs, exp_rhs, rhs.isneg())};
870+
return detail::add_impl<decimal32>(sig_lhs, exp_lhs, lhs.isneg(),
871+
sig_rhs, exp_rhs, rhs.isneg());
872872
}
873873

874874
template <typename Integer>
875875
constexpr auto operator+(decimal32 lhs, Integer rhs) noexcept
876876
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32)
877877
{
878878
using promoted_significand_type = detail::promote_significand_t<decimal32, Integer>;
879+
using exp_type = decimal32::biased_exponent_type;
879880

880881
#ifndef BOOST_DECIMAL_FAST_MATH
881882
if (isnan(lhs) || isinf(lhs))
@@ -899,7 +900,7 @@ constexpr auto operator+(decimal32 lhs, Integer rhs) noexcept
899900
detail::normalize(sig_lhs, exp_lhs);
900901
auto lhs_components {detail::decimal32_components{sig_lhs, exp_lhs, lhs.isneg()}};
901902

902-
decimal32::biased_exponent_type exp_rhs {0};
903+
exp_type exp_rhs {0};
903904
detail::normalize(sig_rhs, exp_rhs);
904905

905906
// Now that the rhs has been normalized it is guaranteed to fit into the decimal32 significand type
@@ -994,16 +995,17 @@ constexpr auto operator-(decimal32 lhs, decimal32 rhs) noexcept -> decimal32
994995
auto exp_rhs {rhs.biased_exponent()};
995996
detail::normalize(sig_rhs, exp_rhs);
996997

997-
return {detail::sub_impl<decimal32>(sig_lhs, exp_lhs, lhs.isneg(),
998-
sig_rhs, exp_rhs, rhs.isneg(),
999-
abs_lhs_bigger)};
998+
return detail::sub_impl<decimal32>(sig_lhs, exp_lhs, lhs.isneg(),
999+
sig_rhs, exp_rhs, rhs.isneg(),
1000+
abs_lhs_bigger);
10001001
}
10011002

10021003
template <typename Integer>
10031004
constexpr auto operator-(decimal32 lhs, Integer rhs) noexcept
10041005
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32)
10051006
{
10061007
using promoted_significand_type = detail::promote_significand_t<decimal32, Integer>;
1008+
using exp_type = decimal32::biased_exponent_type;
10071009

10081010
#ifndef BOOST_DECIMAL_FAST_MATH
10091011
if (isinf(lhs) || isnan(lhs))
@@ -1024,20 +1026,21 @@ constexpr auto operator-(decimal32 lhs, Integer rhs) noexcept
10241026
auto exp_lhs {lhs.biased_exponent()};
10251027
detail::normalize(sig_lhs, exp_lhs);
10261028

1027-
decimal32::biased_exponent_type exp_rhs {0};
1029+
exp_type exp_rhs {0};
10281030
detail::normalize(sig_rhs, exp_rhs);
10291031
auto final_sig_rhs {static_cast<decimal32::significand_type>(sig_rhs)};
10301032

1031-
return {detail::sub_impl<decimal32>(sig_lhs, exp_lhs, lhs.isneg(),
1032-
final_sig_rhs, exp_rhs, (rhs < 0),
1033-
abs_lhs_bigger)};
1033+
return detail::sub_impl<decimal32>(sig_lhs, exp_lhs, lhs.isneg(),
1034+
final_sig_rhs, exp_rhs, (rhs < 0),
1035+
abs_lhs_bigger);
10341036
}
10351037

10361038
template <typename Integer>
10371039
constexpr auto operator-(Integer lhs, decimal32 rhs) noexcept
10381040
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32)
10391041
{
10401042
using promoted_significand_type = detail::promote_significand_t<decimal32, Integer>;
1043+
using exp_type = decimal32::biased_exponent_type;
10411044

10421045
#ifndef BOOST_DECIMAL_FAST_MATH
10431046
if (isinf(rhs) || isnan(rhs))
@@ -1054,17 +1057,17 @@ constexpr auto operator-(Integer lhs, decimal32 rhs) noexcept
10541057
auto sig_lhs {static_cast<promoted_significand_type>(detail::make_positive_unsigned(lhs))};
10551058
const bool abs_lhs_bigger {sig_lhs > abs(rhs)};
10561059

1057-
decimal32::biased_exponent_type exp_lhs {0};
1060+
exp_type exp_lhs {0};
10581061
detail::normalize(sig_lhs, exp_lhs);
10591062
auto final_sig_lhs {static_cast<decimal32::significand_type>(sig_lhs)};
10601063

10611064
auto sig_rhs {rhs.full_significand()};
10621065
auto exp_rhs {rhs.biased_exponent()};
10631066
detail::normalize(sig_rhs, exp_rhs);
10641067

1065-
return {detail::sub_impl<decimal32>(final_sig_lhs, exp_lhs, (lhs < 0),
1066-
sig_rhs, exp_rhs, rhs.isneg(),
1067-
abs_lhs_bigger)};
1068+
return detail::sub_impl<decimal32>(final_sig_lhs, exp_lhs, (lhs < 0),
1069+
sig_rhs, exp_rhs, rhs.isneg(),
1070+
abs_lhs_bigger);
10681071
}
10691072

10701073
constexpr auto decimal32::operator--() noexcept -> decimal32&
@@ -1661,15 +1664,16 @@ constexpr auto operator*(decimal32 lhs, decimal32 rhs) noexcept -> decimal32
16611664
auto exp_rhs {rhs.biased_exponent()};
16621665
detail::normalize(sig_rhs, exp_rhs);
16631666

1664-
return {detail::mul_impl<decimal32>(sig_lhs, exp_lhs, lhs.isneg(),
1665-
sig_rhs, exp_rhs, rhs.isneg())};
1667+
return detail::mul_impl<decimal32>(sig_lhs, exp_lhs, lhs.isneg(),
1668+
sig_rhs, exp_rhs, rhs.isneg());
16661669
}
16671670

16681671
template <typename Integer>
16691672
constexpr auto operator*(decimal32 lhs, Integer rhs) noexcept
16701673
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32)
16711674
{
16721675
using promoted_significand_type = detail::promote_significand_t<decimal32, Integer>;
1676+
using exp_type = decimal32::biased_exponent_type;
16731677

16741678
#ifndef BOOST_DECIMAL_FAST_MATH
16751679
if (isnan(lhs) || isinf(lhs))
@@ -1683,12 +1687,12 @@ constexpr auto operator*(decimal32 lhs, Integer rhs) noexcept
16831687
detail::normalize(sig_lhs, exp_lhs);
16841688

16851689
auto sig_rhs {static_cast<promoted_significand_type>(detail::make_positive_unsigned(rhs))};
1686-
decimal32::biased_exponent_type exp_rhs {0};
1690+
exp_type exp_rhs {0};
16871691
detail::normalize(sig_rhs, exp_rhs);
16881692
auto final_sig_rhs {static_cast<decimal32::significand_type>(sig_rhs)};
16891693

1690-
return{detail::mul_impl<decimal32>(sig_lhs, exp_lhs, lhs.isneg(),
1691-
final_sig_rhs, exp_rhs, (rhs < 0))};
1694+
return detail::mul_impl<decimal32>(sig_lhs, exp_lhs, lhs.isneg(),
1695+
final_sig_rhs, exp_rhs, (rhs < 0));
16921696
}
16931697

16941698
template <typename Integer>
@@ -1814,6 +1818,8 @@ template <typename Integer>
18141818
constexpr auto operator/(decimal32 lhs, Integer rhs) noexcept
18151819
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32)
18161820
{
1821+
using exp_type = decimal32::biased_exponent_type;
1822+
18171823
#ifndef BOOST_DECIMAL_FAST_MATH
18181824
// Check pre-conditions
18191825
constexpr decimal32 zero {0, 0};
@@ -1847,7 +1853,7 @@ constexpr auto operator/(decimal32 lhs, Integer rhs) noexcept
18471853
detail::normalize(sig_lhs, exp_lhs);
18481854

18491855
detail::decimal32_components lhs_components {sig_lhs, exp_lhs, lhs.isneg()};
1850-
std::int32_t exp_rhs {};
1856+
exp_type exp_rhs {};
18511857
detail::decimal32_components rhs_components {detail::shrink_significand(detail::make_positive_unsigned(rhs), exp_rhs), exp_rhs, rhs < 0};
18521858

18531859
return detail::generic_div_impl<decimal32>(lhs_components, rhs_components);
@@ -1857,6 +1863,8 @@ template <typename Integer>
18571863
constexpr auto operator/(Integer lhs, decimal32 rhs) noexcept
18581864
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32)
18591865
{
1866+
using exp_type = decimal32::biased_exponent_type;
1867+
18601868
#ifndef BOOST_DECIMAL_FAST_MATH
18611869
// Check pre-conditions
18621870
constexpr decimal32 zero {0, 0};
@@ -1887,7 +1895,7 @@ constexpr auto operator/(Integer lhs, decimal32 rhs) noexcept
18871895
auto exp_rhs {rhs.biased_exponent()};
18881896
detail::normalize(sig_rhs, exp_rhs);
18891897

1890-
std::int32_t lhs_exp {};
1898+
exp_type lhs_exp {};
18911899
auto lhs_sig {detail::make_positive_unsigned(detail::shrink_significand(lhs, lhs_exp))};
18921900
detail::decimal32_components lhs_components {lhs_sig, lhs_exp, lhs < 0};
18931901
detail::decimal32_components rhs_components {sig_rhs, exp_rhs, rhs.isneg()};

0 commit comments

Comments
 (0)