comparison tomsfastmath/src/bit/fp_div_2d.c @ 643:a362b62d38b2 dropbear-tfm

Add tomsfastmath from git rev bfa4582842bc3bab42e4be4aed5703437049502a with Makefile.in renamed
author Matt Johnston <matt@ucc.asn.au>
date Wed, 23 Nov 2011 18:10:20 +0700
parents
children
comparison
equal deleted inserted replaced
642:33fd2f3499d2 643:a362b62d38b2
1 /* TomsFastMath, a fast ISO C bignum library.
2 *
3 * This project is meant to fill in where LibTomMath
4 * falls short. That is speed ;-)
5 *
6 * This project is public domain and free for all purposes.
7 *
8 * Tom St Denis, [email protected]
9 */
10 #include <tfm.h>
11
12 /* c = a / 2**b */
13 void fp_div_2d(fp_int *a, int b, fp_int *c, fp_int *d)
14 {
15 fp_digit D, r, rr;
16 int x;
17 fp_int t;
18
19 /* if the shift count is <= 0 then we do no work */
20 if (b <= 0) {
21 fp_copy (a, c);
22 if (d != NULL) {
23 fp_zero (d);
24 }
25 return;
26 }
27
28 fp_init(&t);
29
30 /* get the remainder */
31 if (d != NULL) {
32 fp_mod_2d (a, b, &t);
33 }
34
35 /* copy */
36 fp_copy(a, c);
37
38 /* shift by as many digits in the bit count */
39 if (b >= (int)DIGIT_BIT) {
40 fp_rshd (c, b / DIGIT_BIT);
41 }
42
43 /* shift any bit count < DIGIT_BIT */
44 D = (fp_digit) (b % DIGIT_BIT);
45 if (D != 0) {
46 register fp_digit *tmpc, mask, shift;
47
48 /* mask */
49 mask = (((fp_digit)1) << D) - 1;
50
51 /* shift for lsb */
52 shift = DIGIT_BIT - D;
53
54 /* alias */
55 tmpc = c->dp + (c->used - 1);
56
57 /* carry */
58 r = 0;
59 for (x = c->used - 1; x >= 0; x--) {
60 /* get the lower bits of this word in a temp */
61 rr = *tmpc & mask;
62
63 /* shift the current word and mix in the carry bits from the previous word */
64 *tmpc = (*tmpc >> D) | (r << shift);
65 --tmpc;
66
67 /* set the carry to the carry bits of the current word found above */
68 r = rr;
69 }
70 }
71 fp_clamp (c);
72 if (d != NULL) {
73 fp_copy (&t, d);
74 }
75 }
76
77 /* $Source$ */
78 /* $Revision$ */
79 /* $Date$ */