Skip to content

Commit

Permalink
Merge pull request #162 from BoostGSoC21/clarify_exact_arith_struct
Browse files Browse the repository at this point in the history
Fix #161 via clarify and streamline exact_arithmetic struct
  • Loading branch information
ckormanyos authored Dec 30, 2024
2 parents bd61cea + a906a1c commit 6ac4ed9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 48 deletions.
30 changes: 16 additions & 14 deletions include/boost/multiprecision/cpp_df_qf/cpp_df_qf_detail.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,30 @@ struct exact_arithmetic

using float_type = FloatingPointType;
using float_pair = std::pair<float_type, float_type>;
using float_tuple = std::tuple<float_type, float_type, float_type, float_type>;

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

const float_pair result(a_plus_b, b - (a_plus_b - a));

return result;
return { hi, float_type { (a - a1) + (b - b1) } };
}

static constexpr void sum(float_pair& result, float_type a, float_type b)
static constexpr auto two_diff(const float_type a, const float_type b) -> float_pair
{
// Exact addition of two floating point numbers
const float_type a_plus_b = a + b;
const float_type v = a_plus_b - a;
const float_type hi { a - b };
const float_type a1 { hi + b };
const float_type b1 { hi - a1 };

return { hi, float_type { (a - a1) - (b + b1) } };
}

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

result.first = tmp.first;
result.second = tmp.second;
return { hi, float_type { b - (hi - a) } };
}

static constexpr auto normalize(float_pair& result, float_type a, float_type b) -> void
Expand Down
43 changes: 9 additions & 34 deletions include/boost/multiprecision/cpp_double_fp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -478,13 +478,13 @@ class cpp_double_fp_backend

const float_type xlo { data.second };

data = two_sum(data.first, v.data.first);
data = arithmetic::two_sum(data.first, v.data.first);

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

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

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

return *this;
}
Expand Down Expand Up @@ -539,13 +539,13 @@ class cpp_double_fp_backend

const float_type xlo { data.second };

data = two_diff(data.first, v.data.first);
data = arithmetic::two_diff(data.first, v.data.first);

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

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

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

return *this;
}
Expand Down Expand Up @@ -975,7 +975,7 @@ class cpp_double_fp_backend
return
cpp_double_fp_backend
(
arithmetic::fast_sum
arithmetic::two_hilo_sum
(
static_cast<float_type>
(
Expand Down Expand Up @@ -1065,31 +1065,6 @@ class cpp_double_fp_backend

bool rd_string(const char* pstr);

static constexpr rep_type two_sum(const float_type a, const float_type b)
{
const float_type hi { a + b };
const float_type a1 { hi - b };
const float_type b1 { hi - a1 };

return { hi, float_type { (a - a1) + (b - b1) } };
}

static constexpr rep_type two_diff(const float_type a, const float_type b)
{
const float_type hi { a - b };
const float_type a1 { hi + b };
const float_type b1 { hi - a1 };

return { hi, float_type { (a - a1) - (b + b1) } };
}

static constexpr rep_type two_hilo_sum(const float_type a, const float_type b)
{
const float_type hi { a + b };

return { hi, float_type { b - (hi - a) } };
}

template <typename OtherFloatingPointType,
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*>
friend constexpr void eval_exp(cpp_double_fp_backend<OtherFloatingPointType>& result, const cpp_double_fp_backend<OtherFloatingPointType>& x);
Expand Down

0 comments on commit 6ac4ed9

Please sign in to comment.