Skip to content

Commit 7946a42

Browse files
committed
Apply similar logic to 128-bit add/sub path
1 parent 4813757 commit 7946a42

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

include/boost/decimal/detail/add_impl.hpp

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,53 @@ constexpr auto d128_add_impl(T lhs_sig, U lhs_exp, bool lhs_sign,
276276
{
277277
// rounding mode == fe_dec_upward
278278
// Unconditionally round up. Could be 5e+95 + 4e-100 -> 5.000001e+95
279-
return lhs_sig != 0U && (lhs_exp > rhs_exp) ?
280-
ReturnType{lhs_sig + 1U, lhs_exp, lhs_sign} :
281-
ReturnType{rhs_sig + 1U, rhs_exp, rhs_sign};
279+
const bool use_lhs {lhs_sig != 0U && (lhs_exp > rhs_exp)};
280+
281+
if (use_lhs)
282+
{
283+
if (rhs_sig != 0U)
284+
{
285+
if (lhs_sign)
286+
{
287+
if (is_power_of_10(lhs_sig))
288+
{
289+
--lhs_sig;
290+
lhs_sig *= 10U;
291+
lhs_sig += 9U;
292+
--lhs_exp;
293+
}
294+
else
295+
{
296+
--lhs_sig;
297+
}
298+
}
299+
else
300+
{
301+
++lhs_sig;
302+
}
303+
}
304+
305+
return ReturnType{lhs_sig, lhs_exp, lhs_sign} ;
306+
}
307+
else
308+
{
309+
if (lhs_sig != 0U)
310+
{
311+
if (rhs_sign)
312+
{
313+
--rhs_sig;
314+
rhs_sig *= 10U;
315+
rhs_sig += 9U;
316+
--rhs_exp;
317+
}
318+
else
319+
{
320+
++rhs_sig;
321+
}
322+
}
323+
324+
return ReturnType{rhs_sig, rhs_exp, rhs_sign};
325+
}
282326
}
283327
}
284328

0 commit comments

Comments
 (0)