Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / dropbear / libtommath / bn_mp_div_2d.c
blob6c18d806e744f1360285c66f7aa6538d4ac80e67
1 #include <tommath.h>
2 #ifdef BN_MP_DIV_2D_C
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
13 * guarantee it works.
15 * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com
18 /* shift right by a certain bit count (store quotient in c, optional remainder in d) */
19 int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
21 mp_digit D, r, rr;
22 int x, res;
23 mp_int t;
26 /* if the shift count is <= 0 then we do no work */
27 if (b <= 0) {
28 res = mp_copy (a, c);
29 if (d != NULL) {
30 mp_zero (d);
32 return res;
35 if ((res = mp_init (&t)) != MP_OKAY) {
36 return res;
39 /* get the remainder */
40 if (d != NULL) {
41 if ((res = mp_mod_2d (a, b, &t)) != MP_OKAY) {
42 mp_clear (&t);
43 return res;
47 /* copy */
48 if ((res = mp_copy (a, c)) != MP_OKAY) {
49 mp_clear (&t);
50 return res;
53 /* shift by as many digits in the bit count */
54 if (b >= (int)DIGIT_BIT) {
55 mp_rshd (c, b / DIGIT_BIT);
58 /* shift any bit count < DIGIT_BIT */
59 D = (mp_digit) (b % DIGIT_BIT);
60 if (D != 0) {
61 register mp_digit *tmpc, mask, shift;
63 /* mask */
64 mask = (((mp_digit)1) << D) - 1;
66 /* shift for lsb */
67 shift = DIGIT_BIT - D;
69 /* alias */
70 tmpc = c->dp + (c->used - 1);
72 /* carry */
73 r = 0;
74 for (x = c->used - 1; x >= 0; x--) {
75 /* get the lower bits of this word in a temp */
76 rr = *tmpc & mask;
78 /* shift the current word and mix in the carry bits from the previous word */
79 *tmpc = (*tmpc >> D) | (r << shift);
80 --tmpc;
82 /* set the carry to the carry bits of the current word found above */
83 r = rr;
86 mp_clamp (c);
87 if (d != NULL) {
88 mp_exch (&t, d);
90 mp_clear (&t);
91 return MP_OKAY;
93 #endif
95 /* $Source: /cvs/libtom/libtommath/bn_mp_div_2d.c,v $ */
96 /* $Revision: 1.3 $ */
97 /* $Date: 2006/03/31 14:18:44 $ */