Skip to content

Commit 07a7cdf

Browse files
authored
Merge pull request #1275 from cppalliance/1260
Use the complete range of `int128` when performing 128-bit addition and subtraction
2 parents b5789e1 + 3dfe776 commit 07a7cdf

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

include/boost/decimal/detail/add_impl.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,17 +337,17 @@ constexpr auto d128_add_impl(T lhs_sig, U lhs_exp, bool lhs_sign,
337337
auto& sign_smaller {abs_lhs_bigger ? rhs_sign : lhs_sign};
338338
auto& sign_bigger {abs_lhs_bigger ? lhs_sign : rhs_sign};
339339

340-
if (delta_exp <= 2)
340+
if (delta_exp <= 3)
341341
{
342342
sig_bigger *= pow10(static_cast<boost::int128::uint128_t>(delta_exp));
343343
exp_bigger -= delta_exp;
344344
delta_exp = 0;
345345
}
346346
else
347347
{
348-
sig_bigger *= 100U;
349-
delta_exp -= 2;
350-
exp_bigger -= 2;
348+
sig_bigger *= 1000U;
349+
delta_exp -= 3;
350+
exp_bigger -= 3;
351351

352352
if (delta_exp > 1)
353353
{

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ run github_issue_1107.cpp ;
7676
run github_issue_1110.cpp ;
7777
run github_issue_1112.cpp ;
7878
run github_issue_1174.cpp ;
79+
run github_issue_1260.cpp ;
7980

8081
run link_1.cpp link_2.cpp link_3.cpp ;
8182
run quick.cpp ;

test/github_issue_1260.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2025 Matt Borland
2+
// Distributed under the Boost Software License, Version 1.0.
3+
// https://www.boost.org/LICENSE_1_0.txt
4+
//
5+
// See: https://github.com/cppalliance/decimal/issues/1260
6+
7+
#include <boost/decimal.hpp>
8+
#include <boost/core/lightweight_test.hpp>
9+
10+
template <typename T>
11+
void test()
12+
{
13+
const T lhs {"1E34"};
14+
const T rhs {"-0.51"};
15+
const T res {"9999999999999999999999999999999999"};
16+
17+
const T add_val {lhs + rhs};
18+
BOOST_TEST_EQ(add_val, res);
19+
20+
const T sub_val {lhs - boost::decimal::abs(rhs)};
21+
BOOST_TEST_EQ(sub_val, res);
22+
}
23+
24+
int main()
25+
{
26+
// TODO(mborland): GCC 12+ in 32 bit compilations only with release mode fail
27+
#ifndef __i386__
28+
29+
test<boost::decimal::decimal128_t>();
30+
test<boost::decimal::decimal_fast128_t>();
31+
32+
#endif
33+
34+
return boost::report_errors();
35+
}

0 commit comments

Comments
 (0)