14
14
*/
15
15
#include <tommath.h>
16
16
17
- /* integer signed division. c*b + d == a [e.g. a/b, c=quotient, d=remainder]
17
+ /* integer signed division.
18
+ * c*b + d == a [e.g. a/b, c=quotient, d=remainder]
18
19
* HAC pp.598 Algorithm 14.20
19
20
*
20
- * Note that the description in HAC is horribly incomplete. For example,
21
- * it doesn't consider the case where digits are removed from 'x' in the inner
22
- * loop. It also doesn't consider the case that y has fewer than three digits, etc..
21
+ * Note that the description in HAC is horribly
22
+ * incomplete. For example, it doesn't consider
23
+ * the case where digits are removed from 'x' in
24
+ * the inner loop. It also doesn't consider the
25
+ * case that y has fewer than three digits, etc..
23
26
*
24
- * The overall algorithm is as described as 14.20 from HAC but fixed to treat these cases.
27
+ * The overall algorithm is as described as
28
+ * 14.20 from HAC but fixed to treat these cases.
25
29
*/
26
30
int
27
31
mp_div (mp_int * a , mp_int * b , mp_int * c , mp_int * d )
28
32
{
29
33
mp_int q , x , y , t1 , t2 ;
30
34
int res , n , t , i , norm , neg ;
31
35
32
-
33
36
/* is divisor zero ? */
34
37
if (mp_iszero (b ) == 1 ) {
35
38
return MP_VAL ;
@@ -73,7 +76,7 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
73
76
neg = (a -> sign == b -> sign ) ? MP_ZPOS : MP_NEG ;
74
77
x .sign = y .sign = MP_ZPOS ;
75
78
76
- /* normalize both x and y, ensure that y >= b/2, [b == 2^ DIGIT_BIT] */
79
+ /* normalize both x and y, ensure that y >= b/2, [b == 2** DIGIT_BIT] */
77
80
norm = mp_count_bits (& y ) % DIGIT_BIT ;
78
81
if (norm < (int )(DIGIT_BIT - 1 )) {
79
82
norm = (DIGIT_BIT - 1 ) - norm ;
@@ -91,8 +94,8 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
91
94
n = x .used - 1 ;
92
95
t = y .used - 1 ;
93
96
94
- /* step 2. while (x >= y*b^ n-t) do { q[n-t] += 1; x -= y*b^ {n-t} } */
95
- if ((res = mp_lshd (& y , n - t )) != MP_OKAY ) { /* y = y*b^ {n-t} */
97
+ /* while (x >= y*b** n-t) do { q[n-t] += 1; x -= y*b** {n-t} } */
98
+ if ((res = mp_lshd (& y , n - t )) != MP_OKAY ) { /* y = y*b** {n-t} */
96
99
goto __Y ;
97
100
}
98
101
@@ -111,7 +114,8 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
111
114
if (i > x .used )
112
115
continue ;
113
116
114
- /* step 3.1 if xi == yt then set q{i-t-1} to b-1, otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
117
+ /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
118
+ * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
115
119
if (x .dp [i ] == y .dp [t ]) {
116
120
q .dp [i - t - 1 ] = ((((mp_digit )1 ) << DIGIT_BIT ) - 1 );
117
121
} else {
@@ -124,7 +128,11 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
124
128
q .dp [i - t - 1 ] = (mp_digit ) (tmp & (mp_word ) (MP_MASK ));
125
129
}
126
130
127
- /* step 3.2 while (q{i-t-1} * (yt * b + y{t-1})) > xi * b^2 + xi-1 * b + xi-2 do q{i-t-1} -= 1; */
131
+ /* while (q{i-t-1} * (yt * b + y{t-1})) >
132
+ xi * b**2 + xi-1 * b + xi-2
133
+
134
+ do q{i-t-1} -= 1;
135
+ */
128
136
q .dp [i - t - 1 ] = (q .dp [i - t - 1 ] + 1 ) & MP_MASK ;
129
137
do {
130
138
q .dp [i - t - 1 ] = (q .dp [i - t - 1 ] - 1 ) & MP_MASK ;
@@ -145,7 +153,7 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
145
153
t2 .used = 3 ;
146
154
} while (mp_cmp_mag (& t1 , & t2 ) == MP_GT );
147
155
148
- /* step 3.3 x = x - q{i-t-1} * y * b^ {i-t-1} */
156
+ /* step 3.3 x = x - q{i-t-1} * y * b** {i-t-1} */
149
157
if ((res = mp_mul_d (& y , q .dp [i - t - 1 ], & t1 )) != MP_OKAY ) {
150
158
goto __Y ;
151
159
}
@@ -158,7 +166,7 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
158
166
goto __Y ;
159
167
}
160
168
161
- /* step 3.4 if x < 0 then { x = x + y*b^ {i-t-1}; q{i-t-1} -= 1; } */
169
+ /* if x < 0 then { x = x + y*b** {i-t-1}; q{i-t-1} -= 1; } */
162
170
if (x .sign == MP_NEG ) {
163
171
if ((res = mp_copy (& y , & t1 )) != MP_OKAY ) {
164
172
goto __Y ;
@@ -174,7 +182,10 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
174
182
}
175
183
}
176
184
177
- /* now q is the quotient and x is the remainder [which we have to normalize] */
185
+ /* now q is the quotient and x is the remainder
186
+ * [which we have to normalize]
187
+ */
188
+
178
189
/* get sign before writing to c */
179
190
x .sign = a -> sign ;
180
191
0 commit comments