13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
-
+
+
-
+
-
+
-
+
|
* guarantee it works.
*
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
/* single digit addition */
int
mp_add_d (mp_int * a, mp_digit b, mp_int * c)
mp_add_d (const mp_int * a, mp_digit b, mp_int * c)
{
int res, ix, oldused;
mp_digit *tmpa, *tmpc, mu;
/* grow c as required */
if (c->alloc < (a->used + 1)) {
if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {
return res;
}
}
/* if a is negative and |a| >= b, call c = |a| - b */
if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) {
mp_int a_ = *a;
/* temporarily fix sign of a */
a->sign = MP_ZPOS;
a_.sign = MP_ZPOS;
/* c = |a| - b */
res = mp_sub_d(a, b, c);
res = mp_sub_d(&a_, b, c);
/* fix sign */
a->sign = c->sign = MP_NEG;
c->sign = MP_NEG;
/* clamp */
mp_clamp(c);
return res;
}
|