Skip to content

Commit 87523d8

Browse files
authored
Merge pull request #677 from cppalliance/better_basics
Improved basic operators for 32 and 64 bit types
2 parents eb7f5db + f6fa637 commit 87523d8

28 files changed

+588
-494
lines changed

include/boost/decimal/decimal128.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ BOOST_DECIMAL_CONSTEXPR_VARIABLE uint128 d128_big_combination_field_mask {UINT64
132132

133133
struct decimal128_components
134134
{
135-
using sig_type = uint128;
135+
using significand_type = uint128;
136+
using biased_exponent_type = std::int32_t;
136137

137-
uint128 sig {};
138-
std::int32_t exp {};
138+
significand_type sig {};
139+
biased_exponent_type exp {};
139140
bool sign {};
140141

141142
constexpr decimal128_components() = default;
@@ -150,6 +151,8 @@ BOOST_DECIMAL_EXPORT class decimal128 final
150151
{
151152
public:
152153
using significand_type = detail::uint128;
154+
using exponent_type = std::uint64_t;
155+
using biased_exponent_type = std::int32_t;
153156

154157
private:
155158
detail::uint128 bits_ {};

include/boost/decimal/decimal128_fast.hpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ BOOST_DECIMAL_CONSTEXPR_VARIABLE auto d128_fast_snan = std::numeric_limits<uint1
3131

3232
struct decimal128_fast_components
3333
{
34-
using sig_type = uint128;
34+
using significand_type = uint128;
35+
using biased_exponent_type = std::int_fast32_t;
3536

36-
uint128 sig;
37-
std::int32_t exp;
37+
significand_type sig;
38+
biased_exponent_type exp;
3839
bool sign;
3940
};
4041

@@ -45,6 +46,7 @@ class decimal128_fast final
4546
public:
4647
using significand_type = detail::uint128;
4748
using exponent_type = std::uint_fast32_t;
49+
using biased_exponent_type = std::int_fast32_t;
4850

4951
private:
5052
// Instead of having to encode and decode at every operation
@@ -69,9 +71,9 @@ class decimal128_fast final
6971
return exponent_;
7072
}
7173

72-
constexpr auto biased_exponent() const noexcept -> std::int32_t
74+
constexpr auto biased_exponent() const noexcept -> biased_exponent_type
7375
{
74-
return static_cast<std::int32_t>(exponent_) - detail::bias_v<decimal128>;
76+
return static_cast<biased_exponent_type>(exponent_) - detail::bias_v<decimal128>;
7577
}
7678

7779
template <typename Decimal, typename TargetType>
@@ -820,7 +822,7 @@ constexpr auto operator+(decimal128_fast lhs, Integer rhs) noexcept
820822
auto lhs_components {detail::decimal128_fast_components{lhs.significand_, lhs.biased_exponent(), lhs.isneg()}};
821823

822824
auto sig_rhs {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
823-
std::int32_t exp_rhs {0};
825+
decimal128_fast::biased_exponent_type exp_rhs {0};
824826
detail::normalize<decimal128>(sig_rhs, exp_rhs);
825827
auto unsigned_sig_rhs = detail::make_positive_unsigned(sig_rhs);
826828
auto rhs_components {detail::decimal128_fast_components{unsigned_sig_rhs, exp_rhs, (rhs < 0)}};
@@ -914,7 +916,7 @@ constexpr auto operator-(decimal128_fast lhs, Integer rhs) noexcept
914916
auto lhs_components {detail::decimal128_fast_components{lhs.significand_, lhs.biased_exponent(), lhs.isneg()}};
915917

916918
auto sig_rhs {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
917-
std::int32_t exp_rhs {0};
919+
decimal128_fast::biased_exponent_type exp_rhs {0};
918920
detail::normalize<decimal128>(sig_rhs, exp_rhs);
919921
auto unsigned_sig_rhs {detail::make_positive_unsigned(sig_rhs)};
920922
auto rhs_components {detail::decimal128_fast_components{unsigned_sig_rhs, exp_rhs, (rhs < 0)}};
@@ -946,7 +948,7 @@ constexpr auto operator-(Integer lhs, decimal128_fast rhs) noexcept
946948
const bool abs_lhs_bigger {detail::make_positive_unsigned(lhs) > abs(rhs)};
947949

948950
auto sig_lhs {static_cast<detail::uint128>(detail::make_positive_unsigned(lhs))};
949-
std::int32_t exp_lhs {0};
951+
decimal128_fast::biased_exponent_type exp_lhs {0};
950952
detail::normalize<decimal128>(sig_lhs, exp_lhs);
951953
auto unsigned_sig_lhs {detail::make_positive_unsigned(sig_lhs)};
952954
auto lhs_components {detail::decimal128_fast_components{unsigned_sig_lhs, exp_lhs, (lhs < 0)}};
@@ -992,7 +994,7 @@ constexpr auto operator*(decimal128_fast lhs, Integer rhs) noexcept
992994
#endif
993995

994996
auto rhs_sig {static_cast<detail::uint128>(detail::make_positive_unsigned(rhs))};
995-
std::int32_t rhs_exp {0};
997+
decimal128_fast::biased_exponent_type rhs_exp {0};
996998
detail::normalize<decimal128_fast>(rhs_sig, rhs_exp);
997999

9981000
const auto result {detail::d128_fast_mul_impl<detail::decimal128_fast_components>(
@@ -1128,7 +1130,7 @@ constexpr auto operator/(decimal128_fast lhs, Integer rhs) noexcept
11281130
detail::decimal128_fast_components lhs_components {lhs.significand_, lhs.biased_exponent(), lhs.isneg()};
11291131

11301132
auto rhs_sig {detail::make_positive_unsigned(rhs)};
1131-
std::int32_t rhs_exp {};
1133+
decimal128_fast::biased_exponent_type rhs_exp {};
11321134
detail::decimal128_fast_components rhs_components {rhs_sig, rhs_exp, rhs < 0};
11331135
detail::decimal128_fast_components q_components {};
11341136

0 commit comments

Comments
 (0)