Skip to content

Commit 6ac4ed9

Browse files
authored
Merge pull request #162 from BoostGSoC21/clarify_exact_arith_struct
Fix #161 via clarify and streamline exact_arithmetic struct
2 parents bd61cea + a906a1c commit 6ac4ed9

File tree

2 files changed

+25
-48
lines changed

2 files changed

+25
-48
lines changed

include/boost/multiprecision/cpp_df_qf/cpp_df_qf_detail.hpp

+16-14
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,30 @@ struct exact_arithmetic
6969

7070
using float_type = FloatingPointType;
7171
using float_pair = std::pair<float_type, float_type>;
72-
using float_tuple = std::tuple<float_type, float_type, float_type, float_type>;
7372

74-
static constexpr auto fast_sum(float_type a, float_type b) -> float_pair
73+
static constexpr auto two_sum(const float_type a, const float_type b) -> float_pair
7574
{
76-
// Exact addition of two floating point numbers, given |a| > |b|
77-
const float_type a_plus_b = a + b;
75+
const float_type hi { a + b };
76+
const float_type a1 { hi - b };
77+
const float_type b1 { hi - a1 };
7878

79-
const float_pair result(a_plus_b, b - (a_plus_b - a));
80-
81-
return result;
79+
return { hi, float_type { (a - a1) + (b - b1) } };
8280
}
8381

84-
static constexpr void sum(float_pair& result, float_type a, float_type b)
82+
static constexpr auto two_diff(const float_type a, const float_type b) -> float_pair
8583
{
86-
// Exact addition of two floating point numbers
87-
const float_type a_plus_b = a + b;
88-
const float_type v = a_plus_b - a;
84+
const float_type hi { a - b };
85+
const float_type a1 { hi + b };
86+
const float_type b1 { hi - a1 };
87+
88+
return { hi, float_type { (a - a1) - (b + b1) } };
89+
}
8990

90-
const float_pair tmp(a_plus_b, (a - (a_plus_b - v)) + (b - v));
91+
static constexpr auto two_hilo_sum(const float_type a, const float_type b) -> float_pair
92+
{
93+
const float_type hi { a + b };
9194

92-
result.first = tmp.first;
93-
result.second = tmp.second;
95+
return { hi, float_type { b - (hi - a) } };
9496
}
9597

9698
static constexpr auto normalize(float_pair& result, float_type a, float_type b) -> void

include/boost/multiprecision/cpp_double_fp.hpp

+9-34
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,13 @@ class cpp_double_fp_backend
478478

479479
const float_type xlo { data.second };
480480

481-
data = two_sum(data.first, v.data.first);
481+
data = arithmetic::two_sum(data.first, v.data.first);
482482

483-
const rep_type thi_tlo { two_sum(xlo, v.data.second) };
483+
const rep_type thi_tlo { arithmetic::two_sum(xlo, v.data.second) };
484484

485-
data = two_hilo_sum(data.first, data.second + thi_tlo.first);
485+
data = arithmetic::two_hilo_sum(data.first, data.second + thi_tlo.first);
486486

487-
data = two_hilo_sum(data.first, thi_tlo.second + data.second);
487+
data = arithmetic::two_hilo_sum(data.first, thi_tlo.second + data.second);
488488

489489
return *this;
490490
}
@@ -539,13 +539,13 @@ class cpp_double_fp_backend
539539

540540
const float_type xlo { data.second };
541541

542-
data = two_diff(data.first, v.data.first);
542+
data = arithmetic::two_diff(data.first, v.data.first);
543543

544-
const rep_type thi_tlo { two_diff(xlo, v.data.second) };
544+
const rep_type thi_tlo { arithmetic::two_diff(xlo, v.data.second) };
545545

546-
data = two_hilo_sum(data.first, data.second + thi_tlo.first);
546+
data = arithmetic::two_hilo_sum(data.first, data.second + thi_tlo.first);
547547

548-
data = two_hilo_sum(data.first, thi_tlo.second + data.second);
548+
data = arithmetic::two_hilo_sum(data.first, thi_tlo.second + data.second);
549549

550550
return *this;
551551
}
@@ -975,7 +975,7 @@ class cpp_double_fp_backend
975975
return
976976
cpp_double_fp_backend
977977
(
978-
arithmetic::fast_sum
978+
arithmetic::two_hilo_sum
979979
(
980980
static_cast<float_type>
981981
(
@@ -1065,31 +1065,6 @@ class cpp_double_fp_backend
10651065

10661066
bool rd_string(const char* pstr);
10671067

1068-
static constexpr rep_type two_sum(const float_type a, const float_type b)
1069-
{
1070-
const float_type hi { a + b };
1071-
const float_type a1 { hi - b };
1072-
const float_type b1 { hi - a1 };
1073-
1074-
return { hi, float_type { (a - a1) + (b - b1) } };
1075-
}
1076-
1077-
static constexpr rep_type two_diff(const float_type a, const float_type b)
1078-
{
1079-
const float_type hi { a - b };
1080-
const float_type a1 { hi + b };
1081-
const float_type b1 { hi - a1 };
1082-
1083-
return { hi, float_type { (a - a1) - (b + b1) } };
1084-
}
1085-
1086-
static constexpr rep_type two_hilo_sum(const float_type a, const float_type b)
1087-
{
1088-
const float_type hi { a + b };
1089-
1090-
return { hi, float_type { b - (hi - a) } };
1091-
}
1092-
10931068
template <typename OtherFloatingPointType,
10941069
typename ::std::enable_if<(cpp_df_qf_detail::is_floating_point_or_float128<OtherFloatingPointType>::value && ((cpp_df_qf_detail::ccmath::numeric_limits<OtherFloatingPointType>::digits10 * 2) < 16))>::type const*>
10951070
friend constexpr void eval_exp(cpp_double_fp_backend<OtherFloatingPointType>& result, const cpp_double_fp_backend<OtherFloatingPointType>& x);

0 commit comments

Comments
 (0)