Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 69 lines (58 sloc) 1.514 kb
e549ccf added libtommath-0.32
Tom St Denis authored
1 #include <tommath.h>
2 #ifdef BN_MP_DIV_2_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 /* b = a/2 */
455bb4d added libtommath-0.28
Tom St Denis authored
19 int mp_div_2(mp_int * a, mp_int * b)
57354e1 added libtommath-0.12
Tom St Denis authored
20 {
b66471f added libtommath-0.13
Tom St Denis authored
21 int x, res, oldused;
57354e1 added libtommath-0.12
Tom St Denis authored
22
23 /* copy */
b66471f added libtommath-0.13
Tom St Denis authored
24 if (b->alloc < a->used) {
25 if ((res = mp_grow (b, a->used)) != MP_OKAY) {
26 return res;
27 }
57354e1 added libtommath-0.12
Tom St Denis authored
28 }
29
b66471f added libtommath-0.13
Tom St Denis authored
30 oldused = b->used;
31 b->used = a->used;
32 {
33 register mp_digit r, rr, *tmpa, *tmpb;
34
14161e8 added libtommath-0.16
Tom St Denis authored
35 /* source alias */
b66471f added libtommath-0.13
Tom St Denis authored
36 tmpa = a->dp + b->used - 1;
fd181cc added libtommath-0.17
Tom St Denis authored
37
14161e8 added libtommath-0.16
Tom St Denis authored
38 /* dest alias */
b66471f added libtommath-0.13
Tom St Denis authored
39 tmpb = b->dp + b->used - 1;
fd181cc added libtommath-0.17
Tom St Denis authored
40
14161e8 added libtommath-0.16
Tom St Denis authored
41 /* carry */
b66471f added libtommath-0.13
Tom St Denis authored
42 r = 0;
43 for (x = b->used - 1; x >= 0; x--) {
14161e8 added libtommath-0.16
Tom St Denis authored
44 /* get the carry for the next iteration */
b66471f added libtommath-0.13
Tom St Denis authored
45 rr = *tmpa & 1;
fd181cc added libtommath-0.17
Tom St Denis authored
46
14161e8 added libtommath-0.16
Tom St Denis authored
47 /* shift the current digit, add in carry and store */
b66471f added libtommath-0.13
Tom St Denis authored
48 *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
fd181cc added libtommath-0.17
Tom St Denis authored
49
14161e8 added libtommath-0.16
Tom St Denis authored
50 /* forward carry to next iteration */
b66471f added libtommath-0.13
Tom St Denis authored
51 r = rr;
52 }
53
14161e8 added libtommath-0.16
Tom St Denis authored
54 /* zero excess digits */
b66471f added libtommath-0.13
Tom St Denis authored
55 tmpb = b->dp + b->used;
56 for (x = b->used; x < oldused; x++) {
57 *tmpb++ = 0;
58 }
57354e1 added libtommath-0.12
Tom St Denis authored
59 }
82f4858 added libtommath-0.14
Tom St Denis authored
60 b->sign = a->sign;
57354e1 added libtommath-0.12
Tom St Denis authored
61 mp_clamp (b);
62 return MP_OKAY;
63 }
e549ccf added libtommath-0.32
Tom St Denis authored
64 #endif
9294e79 added libtommath-0.36
Tom St Denis authored
65
66 /* $Source$ */
67 /* $Revision$ */
68 /* $Date$ */
Something went wrong with that request. Please try again.