1 #include "tommath_private.h"
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
4 /* SPDX-License-Identifier: Unlicense */
6 /* single digit addition */
7 mp_err
mp_add_d(const mp_int
*a
, mp_digit b
, mp_int
*c
)
12 /* fast path for a == c */
16 ((c
->dp
[0] + b
) < MP_DIGIT_MAX
)) {
27 /* grow c as required */
28 if ((err
= mp_grow(c
, a
->used
+ 1)) != MP_OKAY
) {
32 /* if a is negative and |a| >= b, call c = |a| - b */
33 if (mp_isneg(a
) && ((a
->used
> 1) || (a
->dp
[0] >= b
))) {
35 /* temporarily fix sign of a */
39 err
= mp_sub_d(&a_
, b
, c
);
50 /* old number of used digits in c */
53 /* if a is positive */
55 /* add digits, mu is carry */
58 for (i
= 0; i
< a
->used
; i
++) {
59 c
->dp
[i
] = a
->dp
[i
] + mu
;
60 mu
= c
->dp
[i
] >> MP_DIGIT_BIT
;
67 c
->used
= a
->used
+ 1;
69 /* a was negative and |a| < b */
72 /* the result is a single digit */
73 c
->dp
[0] = (a
->used
== 1) ? b
- a
->dp
[0] : b
;
76 /* sign always positive */
79 /* now zero to oldused */
80 s_mp_zero_digs(c
->dp
+ c
->used
, oldused
- c
->used
);