Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 94 lines (80 sloc) 2.074 kB
e549ccf added libtommath-0.32
Tom St Denis authored
1 #include <tommath.h>
2 #ifdef BN_MP_SUB_D_C
57354e1 added libtommath-0.12
Tom St Denis authored
3 /* LibTomMath, multiple-precision integer library -- Tom St Denis
4 *
c1da6aa added libtommath-0.25
Tom St Denis authored
5 * LibTomMath is a library that provides multiple-precision
57354e1 added libtommath-0.12
Tom St Denis authored
6 * integer arithmetic as well as number theoretic functionality.
7 *
c1da6aa added libtommath-0.25
Tom St Denis authored
8 * The library was designed directly after the MPI library by
57354e1 added libtommath-0.12
Tom St Denis authored
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 *
333aebc added libtommath-0.41
Tom St Denis authored
15 * Tom St Denis, tomstdenis@gmail.com, http://libtom.org
57354e1 added libtommath-0.12
Tom St Denis authored
16 */
17
18 /* single digit subtraction */
19 int
20 mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
21 {
03cc01b added libtommath-0.24
Tom St Denis authored
22 mp_digit *tmpa, *tmpc, mu;
23 int res, ix, oldused;
57354e1 added libtommath-0.12
Tom St Denis authored
24
03cc01b added libtommath-0.24
Tom St Denis authored
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 }
57354e1 added libtommath-0.12
Tom St Denis authored
31
03cc01b added libtommath-0.24
Tom St Denis authored
32 /* if a is negative just do an unsigned
33 * addition [with fudged signs]
34 */
35 if (a->sign == MP_NEG) {
36 a->sign = MP_ZPOS;
37 res = mp_add_d(a, b, c);
38 a->sign = c->sign = MP_NEG;
fde740e added libtommath-0.37
Tom St Denis authored
39
40 /* clamp */
41 mp_clamp(c);
42
03cc01b added libtommath-0.24
Tom St Denis authored
43 return res;
57354e1 added libtommath-0.12
Tom St Denis authored
44 }
45
03cc01b added libtommath-0.24
Tom St Denis authored
46 /* setup regs */
47 oldused = c->used;
48 tmpa = a->dp;
49 tmpc = c->dp;
50
51 /* if a <= b simply fix the single digit */
52 if ((a->used == 1 && a->dp[0] <= b) || a->used == 0) {
c1da6aa added libtommath-0.25
Tom St Denis authored
53 if (a->used == 1) {
54 *tmpc++ = b - *tmpa;
55 } else {
56 *tmpc++ = b;
57 }
03cc01b added libtommath-0.24
Tom St Denis authored
58 ix = 1;
59
60 /* negative/1digit */
61 c->sign = MP_NEG;
62 c->used = 1;
63 } else {
64 /* positive/size */
65 c->sign = MP_ZPOS;
66 c->used = a->used;
67
68 /* subtract first digit */
69 *tmpc = *tmpa++ - b;
70 mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1);
71 *tmpc++ &= MP_MASK;
72
73 /* handle rest of the digits */
74 for (ix = 1; ix < a->used; ix++) {
75 *tmpc = *tmpa++ - mu;
76 mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1);
77 *tmpc++ &= MP_MASK;
78 }
79 }
80
c343371 added libtommath-0.27
Tom St Denis authored
81 /* zero excess digits */
82 while (ix++ < oldused) {
03cc01b added libtommath-0.24
Tom St Denis authored
83 *tmpc++ = 0;
84 }
85 mp_clamp(c);
86 return MP_OKAY;
57354e1 added libtommath-0.12
Tom St Denis authored
87 }
03cc01b added libtommath-0.24
Tom St Denis authored
88
e549ccf added libtommath-0.32
Tom St Denis authored
89 #endif
9294e79 added libtommath-0.36
Tom St Denis authored
90
91 /* $Source$ */
92 /* $Revision$ */
93 /* $Date$ */
Something went wrong with that request. Please try again.