Skip to content

Commit ced54d4

Browse files
committed
Avoid copies and improve readability
1 parent 8c05027 commit ced54d4

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

include/boost/decimal/decimal32_fast.hpp

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -786,17 +786,18 @@ constexpr auto operator+(decimal32_fast lhs, decimal32_fast rhs) noexcept -> dec
786786
return lhs - abs(rhs);
787787
}
788788

789-
return {detail::add_impl<decimal32_fast>(
789+
return detail::add_impl<decimal32_fast>(
790790
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
791791
rhs.significand_, rhs.biased_exponent(), rhs.sign_
792-
)};
792+
);
793793
}
794794

795795
template <typename Integer>
796796
constexpr auto operator+(decimal32_fast lhs, Integer rhs) noexcept
797797
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32_fast)
798798
{
799799
using promoted_significand_type = detail::promote_significand_t<decimal32_fast, Integer>;
800+
using exp_type = decimal32_fast::biased_exponent_type;
800801

801802
#ifndef BOOST_DECIMAL_FAST_MATH
802803
if (isnan(lhs) || isinf(lhs))
@@ -816,7 +817,7 @@ constexpr auto operator+(decimal32_fast lhs, Integer rhs) noexcept
816817

817818
auto lhs_components {detail::decimal32_fast_components{lhs.significand_, lhs.biased_exponent(), lhs.isneg()}};
818819

819-
decimal32_fast::biased_exponent_type exp_rhs {0};
820+
exp_type exp_rhs {0};
820821
detail::normalize(sig_rhs, exp_rhs);
821822
auto unsigned_sig_rhs {static_cast<detail::decimal32_fast_components::significand_type>(detail::make_positive_unsigned(sig_rhs))};
822823
auto rhs_components {detail::decimal32_fast_components{unsigned_sig_rhs, exp_rhs, (rhs < 0)}};
@@ -866,18 +867,19 @@ constexpr auto operator-(decimal32_fast lhs, decimal32_fast rhs) noexcept -> dec
866867

867868
const bool abs_lhs_bigger {abs(lhs) > abs(rhs)};
868869

869-
return {detail::sub_impl<decimal32_fast>(
870+
return detail::sub_impl<decimal32_fast>(
870871
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
871872
rhs.significand_, rhs.biased_exponent(), rhs.sign_,
872873
abs_lhs_bigger
873-
)};
874+
);
874875
}
875876

876877
template <typename Integer>
877878
constexpr auto operator-(decimal32_fast lhs, Integer rhs) noexcept
878879
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32_fast)
879880
{
880881
using promoted_significand_type = detail::promote_significand_t<decimal32_fast, Integer>;
882+
using exp_type = decimal32_fast::biased_exponent_type;
881883

882884
#ifndef BOOST_DECIMAL_FAST_MATH
883885
if (isinf(lhs) || isnan(lhs))
@@ -895,21 +897,22 @@ constexpr auto operator-(decimal32_fast lhs, Integer rhs) noexcept
895897

896898
const bool abs_lhs_bigger {abs(lhs) > sig_rhs};
897899

898-
decimal32_fast::biased_exponent_type exp_rhs {0};
900+
exp_type exp_rhs {0};
899901
detail::normalize(sig_rhs, exp_rhs);
900902
auto final_sig_rhs {static_cast<decimal32_fast::significand_type>(detail::make_positive_unsigned(sig_rhs))};
901903

902-
return {detail::sub_impl<decimal32_fast>(
904+
return detail::sub_impl<decimal32_fast>(
903905
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
904906
final_sig_rhs, exp_rhs, (rhs < 0),
905-
abs_lhs_bigger)};
907+
abs_lhs_bigger);
906908
}
907909

908910
template <typename Integer>
909911
constexpr auto operator-(Integer lhs, decimal32_fast rhs) noexcept
910912
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32_fast)
911913
{
912914
using promoted_significand_type = detail::promote_significand_t<decimal32_fast, Integer>;
915+
using exp_type = decimal32_fast::biased_exponent_type;
913916

914917
#ifndef BOOST_DECIMAL_FAST_MATH
915918
if (isinf(rhs) || isnan(rhs))
@@ -926,15 +929,15 @@ constexpr auto operator-(Integer lhs, decimal32_fast rhs) noexcept
926929
auto sig_lhs {static_cast<promoted_significand_type>(detail::make_positive_unsigned(lhs))};
927930
const bool abs_lhs_bigger {sig_lhs > abs(rhs)};
928931

929-
decimal32_fast::biased_exponent_type exp_lhs {0};
932+
exp_type exp_lhs {0};
930933
detail::normalize(sig_lhs, exp_lhs);
931934
auto final_sig_lhs {static_cast<decimal32_fast::significand_type>(detail::make_positive_unsigned(sig_lhs))};
932935

933-
return {detail::sub_impl<decimal32_fast>(
936+
return detail::sub_impl<decimal32_fast>(
934937
final_sig_lhs, exp_lhs, (lhs < 0),
935938
rhs.significand_, rhs.biased_exponent(), rhs.sign_,
936939
abs_lhs_bigger
937-
)};
940+
);
938941
}
939942

940943
constexpr auto operator*(decimal32_fast lhs, decimal32_fast rhs) noexcept -> decimal32_fast
@@ -949,17 +952,18 @@ constexpr auto operator*(decimal32_fast lhs, decimal32_fast rhs) noexcept -> dec
949952
}
950953
#endif
951954

952-
return {detail::mul_impl<decimal32_fast>(
955+
return detail::mul_impl<decimal32_fast>(
953956
lhs.significand_, lhs.biased_exponent(), lhs.sign_,
954957
rhs.significand_, rhs.biased_exponent(), rhs.sign_
955-
)};
958+
);
956959
}
957960

958961
template <typename Integer>
959962
constexpr auto operator*(decimal32_fast lhs, Integer rhs) noexcept
960963
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32_fast)
961964
{
962965
using promoted_significand_type = detail::promote_significand_t<decimal32_fast, Integer>;
966+
using exp_type = decimal32_fast::biased_exponent_type;
963967

964968
#ifndef BOOST_DECIMAL_FAST_MATH
965969
if (isnan(lhs) || isinf(lhs))
@@ -969,12 +973,15 @@ constexpr auto operator*(decimal32_fast lhs, Integer rhs) noexcept
969973
#endif
970974

971975
auto sig_rhs {static_cast<promoted_significand_type>(detail::make_positive_unsigned(rhs))};
972-
decimal32_fast::biased_exponent_type exp_rhs {0};
976+
exp_type exp_rhs {0};
973977
detail::normalize(sig_rhs, exp_rhs);
978+
979+
// We don't know if the original value of rhs fits into the decimal32_fast significand type
980+
// but once it's normalized it's guaranteed to fit
974981
const auto final_sig_rhs {static_cast<decimal32_fast::significand_type>(sig_rhs)};
975982

976-
return {detail::mul_impl<decimal32_fast>(lhs.significand_, lhs.biased_exponent(), lhs.sign_,
977-
final_sig_rhs, exp_rhs, (rhs < 0))};
983+
return detail::mul_impl<decimal32_fast>(lhs.significand_, lhs.biased_exponent(), lhs.sign_,
984+
final_sig_rhs, exp_rhs, (rhs < 0));
978985
}
979986

980987
template <typename Integer>
@@ -1072,6 +1079,8 @@ template <typename Integer>
10721079
constexpr auto operator/(decimal32_fast lhs, Integer rhs) noexcept
10731080
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32_fast)
10741081
{
1082+
using exp_type = decimal32_fast::biased_exponent_type;
1083+
10751084
#ifndef BOOST_DECIMAL_FAST_MATH
10761085
// Check pre-conditions
10771086
constexpr decimal32_fast zero {0, 0};
@@ -1101,7 +1110,7 @@ constexpr auto operator/(decimal32_fast lhs, Integer rhs) noexcept
11011110
#endif
11021111

11031112
const detail::decimal32_fast_components lhs_components {lhs.significand_, lhs.biased_exponent(), lhs.sign_};
1104-
std::int32_t exp_rhs {};
1113+
exp_type exp_rhs {};
11051114
const detail::decimal32_fast_components rhs_components {detail::shrink_significand<decimal32_fast::significand_type>(detail::make_positive_unsigned(rhs), exp_rhs), exp_rhs, rhs < 0};
11061115

11071116
return detail::generic_div_impl<decimal32_fast>(lhs_components, rhs_components);
@@ -1111,6 +1120,8 @@ template <typename Integer>
11111120
constexpr auto operator/(Integer lhs, decimal32_fast rhs) noexcept
11121121
BOOST_DECIMAL_REQUIRES_RETURN(detail::is_integral_v, Integer, decimal32_fast)
11131122
{
1123+
using exp_type = decimal32_fast::biased_exponent_type;
1124+
11141125
#ifndef BOOST_DECIMAL_FAST_MATH
11151126
// Check pre-conditions
11161127
constexpr decimal32_fast zero {0, 0};
@@ -1137,7 +1148,7 @@ constexpr auto operator/(Integer lhs, decimal32_fast rhs) noexcept
11371148
}
11381149
#endif
11391150

1140-
std::int32_t lhs_exp {};
1151+
exp_type lhs_exp {};
11411152
const auto lhs_sig {detail::make_positive_unsigned(detail::shrink_significand<decimal32_fast::significand_type>(lhs, lhs_exp))};
11421153
const detail::decimal32_fast_components lhs_components {lhs_sig, lhs_exp, lhs < 0};
11431154
const detail::decimal32_fast_components rhs_components {rhs.significand_, rhs.biased_exponent(), rhs.isneg()};

0 commit comments

Comments
 (0)