1 /* TomsFastMath, a fast ISO C bignum library.
3 * This project is meant to fill in where LibTomMath
4 * falls short. That is speed ;-)
6 * This project is public domain and free for all purposes.
8 * Tom St Denis, tomstdenis@gmail.com
10 #include <tfm_private.h>
12 /* a/b => cb + d == a */
13 int fp_div(fp_int *a, fp_int *b, fp_int *c, fp_int *d)
15 fp_int q, x, y, t1, t2;
16 int n, t, i, norm, neg;
18 /* is divisor zero ? */
19 if (fp_iszero (b) == 1) {
23 /* if a < b then q=0, r = a */
24 if (fp_cmp_mag (a, b) == FP_LT) {
43 neg = (a->sign == b->sign) ? FP_ZPOS : FP_NEG;
44 x.sign = y.sign = FP_ZPOS;
46 /* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */
47 norm = fp_count_bits(&y) % DIGIT_BIT;
48 if (norm < (int)(DIGIT_BIT-1)) {
49 norm = (DIGIT_BIT-1) - norm;
50 fp_mul_2d (&x, norm, &x);
51 fp_mul_2d (&y, norm, &y);
56 /* note hac does 0 based, so if used==5 then its 0,1,2,3,4, e.g. use 4 */
60 /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */
61 fp_lshd (&y, n - t); /* y = y*b**{n-t} */
63 while (fp_cmp (&x, &y) != FP_LT) {
68 /* reset y by shifting it back down */
71 /* step 3. for i from n down to (t + 1) */
72 for (i = n; i >= (t + 1); i--) {
77 /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
78 * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
79 if (x.dp[i] == y.dp[t]) {
80 q.dp[i - t - 1] = ((((fp_word)1) << DIGIT_BIT) - 1);
83 tmp = ((fp_word) x.dp[i]) << ((fp_word) DIGIT_BIT);
84 tmp |= ((fp_word) x.dp[i - 1]);
85 tmp /= ((fp_word) y.dp[t]);
86 q.dp[i - t - 1] = (fp_digit) (tmp);
89 /* while (q{i-t-1} * (yt * b + y{t-1})) >
90 xi * b**2 + xi-1 * b + xi-2
94 q.dp[i - t - 1] = (q.dp[i - t - 1] + 1);
96 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1);
100 t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1];
103 fp_mul_d (&t1, q.dp[i - t - 1], &t1);
105 /* find right hand */
106 t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2];
107 t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1];
110 } while (fp_cmp_mag(&t1, &t2) == FP_GT);
112 /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
113 fp_mul_d (&y, q.dp[i - t - 1], &t1);
114 fp_lshd (&t1, i - t - 1);
115 fp_sub (&x, &t1, &x);
117 /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
118 if (x.sign == FP_NEG) {
120 fp_lshd (&t1, i - t - 1);
121 fp_add (&x, &t1, &x);
122 q.dp[i - t - 1] = q.dp[i - t - 1] - 1;
126 /* now q is the quotient and x is the remainder
127 * [which we have to normalize]
130 /* get sign before writing to c */
131 x.sign = x.used == 0 ? FP_ZPOS : a->sign;
140 fp_div_2d (&x, norm, &x, NULL);
142 /* the following is a kludge, essentially we were seeing the right remainder but
143 with excess digits that should have been zero
145 for (i = b->used; i < x.used; i++) {