Skip to content

Commit 9bcc0d2

Browse files
authored
Merge pull request #812 from cppalliance/802
Fix calculation of resultant digits to keep us in the range of `uint128`
2 parents 30ed86c + 35ada3d commit 9bcc0d2

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

include/boost/decimal/detail/mul_impl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ constexpr auto d128_mul_impl(T1 lhs_sig, U1 lhs_exp, bool lhs_sign,
125125
const auto rhs_dig {detail::num_digits(rhs_sig)};
126126

127127
// If we can avoid it don't do 256 bit multiplication because it is slow
128-
if (lhs_dig * rhs_dig <= std::numeric_limits<uint128>::digits10)
128+
if (lhs_dig + rhs_dig <= std::numeric_limits<uint128>::digits10)
129129
{
130130
auto res_sig {lhs_sig * rhs_sig};
131131
auto res_exp {lhs_exp + rhs_exp};

test/Jamfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ run github_issue_426.cpp ;
5050
run github_issue_448.cpp ;
5151
run-fail github_issue_519.cpp ;
5252
run github_issue_799.cpp ;
53+
run github_issue_802.cpp ;
5354
run link_1.cpp link_2.cpp link_3.cpp ;
5455
run quick.cpp ;
5556
run random_decimal32_comp.cpp ;

test/github_issue_802.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
#include <boost/decimal.hpp>
6+
#include <boost/core/lightweight_test.hpp>
7+
#include <iostream>
8+
9+
void fasting()
10+
{
11+
using namespace boost::decimal;
12+
using Dec = decimal128;
13+
const Dec a{100'000,1}; // 6 dec digits significant
14+
const Dec b{2'000'000,1}; // 7 dec digits significant
15+
constexpr Dec ab{2, 13};
16+
17+
// Here we should be able to use the fast path instead of the slow path
18+
// since 2e13 fits in a 128 bit uint
19+
BOOST_TEST((a * b) == ab);
20+
}
21+
22+
int main()
23+
{
24+
fasting();
25+
26+
return boost::report_errors();
27+
}
28+

0 commit comments

Comments
 (0)