Line data Source code
1 : #include <tommath.h>
2 : #ifdef BN_MP_DIV_2D_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, tomstdenis@gmail.com, http://libtom.org
16 : */
17 :
18 : /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
19 260688 : int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
20 : {
21 : mp_digit D, r, rr;
22 : int x, res;
23 : mp_int t;
24 :
25 :
26 : /* if the shift count is <= 0 then we do no work */
27 260688 : if (b <= 0) {
28 0 : res = mp_copy (a, c);
29 0 : if (d != NULL) {
30 0 : mp_zero (d);
31 : }
32 0 : return res;
33 : }
34 :
35 260688 : if ((res = mp_init (&t)) != MP_OKAY) {
36 0 : return res;
37 : }
38 :
39 : /* get the remainder */
40 260688 : if (d != NULL) {
41 0 : if ((res = mp_mod_2d (a, b, &t)) != MP_OKAY) {
42 0 : mp_clear (&t);
43 0 : return res;
44 : }
45 : }
46 :
47 : /* copy */
48 260688 : if ((res = mp_copy (a, c)) != MP_OKAY) {
49 0 : mp_clear (&t);
50 0 : return res;
51 : }
52 :
53 : /* shift by as many digits in the bit count */
54 260688 : if (b >= (int)DIGIT_BIT) {
55 0 : mp_rshd (c, b / DIGIT_BIT);
56 : }
57 :
58 : /* shift any bit count < DIGIT_BIT */
59 260688 : D = (mp_digit) (b % DIGIT_BIT);
60 260688 : if (D != 0) {
61 : register mp_digit *tmpc, mask, shift;
62 :
63 : /* mask */
64 260688 : mask = (((mp_digit)1) << D) - 1;
65 :
66 : /* shift for lsb */
67 260688 : shift = DIGIT_BIT - D;
68 :
69 : /* alias */
70 260688 : tmpc = c->dp + (c->used - 1);
71 :
72 : /* carry */
73 260688 : r = 0;
74 12422099 : for (x = c->used - 1; x >= 0; x--) {
75 : /* get the lower bits of this word in a temp */
76 12161411 : rr = *tmpc & mask;
77 :
78 : /* shift the current word and mix in the carry bits from the previous word */
79 12161411 : *tmpc = (*tmpc >> D) | (r << shift);
80 12161411 : --tmpc;
81 :
82 : /* set the carry to the carry bits of the current word found above */
83 12161411 : r = rr;
84 : }
85 : }
86 260688 : mp_clamp (c);
87 260688 : if (d != NULL) {
88 0 : mp_exch (&t, d);
89 : }
90 260688 : mp_clear (&t);
91 260688 : return MP_OKAY;
92 : }
93 : #endif
94 :
95 : /* $Source: /cvs/libtom/libtommath/bn_mp_div_2d.c,v $ */
96 : /* $Revision: 1.4 $ */
97 : /* $Date: 2006/12/28 01:25:13 $ */
|