3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
5 * LibTomMath is a library that provides multiple-precision
6 * integer arithmetic as well as number theoretic functionality.
8 * The library was designed directly after the MPI library by
9 * Michael Fromberger but has been written from scratch with
10 * additional optimizations in place.
12 * The library is free for all purposes without any express
15 * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
18 /* single digit addition */
20 mp_add_d (mp_int * a, mp_digit b, mp_int * c)
23 mp_digit *tmpa, *tmpc, mu;
25 /* grow c as required */
26 if (c->alloc < a->used + 1) {
27 if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {
32 /* if a is negative and |a| >= b, call c = |a| - b */
33 if (a->sign == MP_NEG && (a->used > 1 || a->dp[0] >= b)) {
34 /* temporarily fix sign of a */
38 res = mp_sub_d(a, b, c);
41 a->sign = c->sign = MP_NEG;
46 /* old number of used digits in c */
49 /* sign always positive */
55 /* destination alias */
58 /* if a is positive */
59 if (a->sign == MP_ZPOS) {
60 /* add digit, after this we're propagating
64 mu = *tmpc >> DIGIT_BIT;
67 /* now handle rest of the digits */
68 for (ix = 1; ix < a->used; ix++) {
70 mu = *tmpc >> DIGIT_BIT;
78 c->used = a->used + 1;
80 /* a was negative and |a| < b */
83 /* the result is a single digit */
85 *tmpc++ = b - a->dp[0];
90 /* setup count so the clearing of oldused
91 * can fall through correctly
96 /* now zero to oldused */
97 while (ix++ < oldused) {