Mercurial > hg > Members > anatofuz > MoarVM
comparison 3rdparty/libtommath/bn_mp_add_d.c @ 0:2cf249471370
convert mercurial for git
author | Takahiro SHIMIZU <anatofuz@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 08 May 2018 16:09:12 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:2cf249471370 |
---|---|
1 #include <tommath_private.h> | |
2 #ifdef BN_MP_ADD_D_C | |
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis | |
4 * | |
5 * LibTomMath is a library that provides multiple-precision | |
6 * integer arithmetic as well as number theoretic functionality. | |
7 * | |
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. | |
11 * | |
12 * The library is free for all purposes without any express | |
13 * guarantee it works. | |
14 * | |
15 * Tom St Denis, tstdenis82@gmail.com, http://libtom.org | |
16 */ | |
17 | |
18 /* single digit addition */ | |
19 int | |
20 mp_add_d (mp_int * a, mp_digit b, mp_int * c) | |
21 { | |
22 int res, ix, oldused; | |
23 mp_digit *tmpa, *tmpc, mu; | |
24 | |
25 /* grow c as required */ | |
26 if (c->alloc < (a->used + 1)) { | |
27 if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { | |
28 return res; | |
29 } | |
30 } | |
31 | |
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 */ | |
35 a->sign = MP_ZPOS; | |
36 | |
37 /* c = |a| - b */ | |
38 res = mp_sub_d(a, b, c); | |
39 | |
40 /* fix sign */ | |
41 a->sign = c->sign = MP_NEG; | |
42 | |
43 /* clamp */ | |
44 mp_clamp(c); | |
45 | |
46 return res; | |
47 } | |
48 | |
49 /* old number of used digits in c */ | |
50 oldused = c->used; | |
51 | |
52 /* source alias */ | |
53 tmpa = a->dp; | |
54 | |
55 /* destination alias */ | |
56 tmpc = c->dp; | |
57 | |
58 /* if a is positive */ | |
59 if (a->sign == MP_ZPOS) { | |
60 /* add digit, after this we're propagating | |
61 * the carry. | |
62 */ | |
63 *tmpc = *tmpa++ + b; | |
64 mu = *tmpc >> DIGIT_BIT; | |
65 *tmpc++ &= MP_MASK; | |
66 | |
67 /* now handle rest of the digits */ | |
68 for (ix = 1; ix < a->used; ix++) { | |
69 *tmpc = *tmpa++ + mu; | |
70 mu = *tmpc >> DIGIT_BIT; | |
71 *tmpc++ &= MP_MASK; | |
72 } | |
73 /* set final carry */ | |
74 ix++; | |
75 *tmpc++ = mu; | |
76 | |
77 /* setup size */ | |
78 c->used = a->used + 1; | |
79 } else { | |
80 /* a was negative and |a| < b */ | |
81 c->used = 1; | |
82 | |
83 /* the result is a single digit */ | |
84 if (a->used == 1) { | |
85 *tmpc++ = b - a->dp[0]; | |
86 } else { | |
87 *tmpc++ = b; | |
88 } | |
89 | |
90 /* setup count so the clearing of oldused | |
91 * can fall through correctly | |
92 */ | |
93 ix = 1; | |
94 } | |
95 | |
96 /* sign always positive */ | |
97 c->sign = MP_ZPOS; | |
98 | |
99 /* now zero to oldused */ | |
100 while (ix++ < oldused) { | |
101 *tmpc++ = 0; | |
102 } | |
103 mp_clamp(c); | |
104 | |
105 return MP_OKAY; | |
106 } | |
107 | |
108 #endif | |
109 | |
110 /* $Source$ */ | |
111 /* $Revision$ */ | |
112 /* $Date$ */ |