d3409d5e |
1 | /* -*-c-*- |
2 | * |
a69a3efd |
3 | * $Id$ |
d3409d5e |
4 | * |
5 | * Basic arithmetic on multiprecision integers |
6 | * |
7 | * (c) 1999 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
d3409d5e |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include "mp.h" |
33 | |
ef5f4810 |
34 | /*----- Macros ------------------------------------------------------------*/ |
35 | |
36 | #define MAX(x, y) ((x) >= (y) ? (x) : (y)) |
37 | |
d3409d5e |
38 | /*----- Main code ---------------------------------------------------------*/ |
39 | |
81578196 |
40 | /* --- @mp_lsl@, @mp_lslc@, @mp_lsr@ --- * |
d3409d5e |
41 | * |
f09e814a |
42 | * Arguments: @mp *d@ = destination |
43 | * @mp *a@ = source |
44 | * @size_t n@ = number of bits to move |
d3409d5e |
45 | * |
f09e814a |
46 | * Returns: Result, @a@ shifted left or right by @n@. |
81578196 |
47 | * |
48 | * Use: Bitwise shift operators. @mp_lslc@ fills the bits introduced |
49 | * on the right with ones instead of zeroes: it's used |
50 | * internally by @mp_lsl2c@, though it may be useful on its |
51 | * own. |
d3409d5e |
52 | */ |
53 | |
f09e814a |
54 | mp *mp_lsl(mp *d, mp *a, size_t n) |
d3409d5e |
55 | { |
f09e814a |
56 | MP_DEST(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS, a->f); |
57 | mpx_lsl(d->v, d->vl, a->v, a->vl, n); |
58 | d->f = a->f & (MP_NEG | MP_BURN); |
59 | MP_SHRINK(d); |
60 | return (d); |
61 | } |
d3409d5e |
62 | |
81578196 |
63 | mp *mp_lslc(mp *d, mp *a, size_t n) |
64 | { |
65 | MP_DEST(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS, a->f); |
66 | mpx_lslc(d->v, d->vl, a->v, a->vl, n); |
67 | d->f = a->f & (MP_NEG | MP_BURN); |
68 | MP_SHRINK(d); |
69 | return (d); |
70 | } |
71 | |
f09e814a |
72 | mp *mp_lsr(mp *d, mp *a, size_t n) |
73 | { |
d34decd2 |
74 | MP_DEST(d, MP_LEN(a), a->f); |
f09e814a |
75 | mpx_lsr(d->v, d->vl, a->v, a->vl, n); |
76 | d->f = a->f & (MP_NEG | MP_BURN); |
d3409d5e |
77 | MP_SHRINK(d); |
78 | return (d); |
79 | } |
80 | |
f09e814a |
81 | /* --- @mp_lsl2c@, @mp_lsr2c@ --- * |
d3409d5e |
82 | * |
83 | * Arguments: @mp *d@ = destination |
84 | * @mp *a@ = source |
f09e814a |
85 | * @size_t n@ = number of bits to move |
d3409d5e |
86 | * |
f09e814a |
87 | * Returns: Result, @a@ shifted left or right by @n@. Handles the |
88 | * pretence of sign-extension for negative numbers. |
d3409d5e |
89 | */ |
90 | |
f09e814a |
91 | mp *mp_lsl2c(mp *d, mp *a, size_t n) |
d3409d5e |
92 | { |
a69a3efd |
93 | if (!MP_NEGP(a)) |
f09e814a |
94 | return (mp_lsl(d, a, n)); |
95 | d = mp_not2c(d, a); |
81578196 |
96 | d = mp_lslc(d, d, n); |
f09e814a |
97 | d = mp_not2c(d, d); |
98 | return (d); |
99 | } |
d3409d5e |
100 | |
f09e814a |
101 | mp *mp_lsr2c(mp *d, mp *a, size_t n) |
102 | { |
a69a3efd |
103 | if (!MP_NEGP(a)) |
f09e814a |
104 | return (mp_lsr(d, a, n)); |
105 | d = mp_not2c(d, a); |
106 | d = mp_lsr(d, d, n); |
107 | d = mp_not2c(d, d); |
108 | return (d); |
d3409d5e |
109 | } |
110 | |
f09e814a |
111 | /* --- @mp_testbit@ --- * |
d3409d5e |
112 | * |
f09e814a |
113 | * Arguments: @mp *x@ = a large integer |
09d00c6b |
114 | * @unsigned long n@ = which bit to test |
d3409d5e |
115 | * |
f09e814a |
116 | * Returns: Nonzero if the bit is set, zero if not. |
d3409d5e |
117 | */ |
118 | |
09d00c6b |
119 | int mp_testbit(mp *x, unsigned long n) |
d3409d5e |
120 | { |
f09e814a |
121 | if (n > MPW_BITS * MP_LEN(x)) |
122 | return (0); |
09d00c6b |
123 | return ((x->v[n/MPW_BITS] >> n%MPW_BITS) & 1u); |
d3409d5e |
124 | } |
125 | |
f09e814a |
126 | /* --- @mp_testbit2c@ --- * |
d3409d5e |
127 | * |
f09e814a |
128 | * Arguments: @mp *x@ = a large integer |
09d00c6b |
129 | * @unsigned long n@ = which bit to test |
d3409d5e |
130 | * |
f09e814a |
131 | * Returns: Nonzero if the bit is set, zero if not. Fakes up two's |
132 | * complement representation. |
d3409d5e |
133 | */ |
134 | |
09d00c6b |
135 | int mp_testbit2c(mp *x, unsigned long n) |
d3409d5e |
136 | { |
f09e814a |
137 | int r; |
a69a3efd |
138 | if (!MP_NEGP(x)) |
f09e814a |
139 | return (mp_testbit(x, n)); |
140 | x = mp_not2c(MP_NEW, x); |
141 | r = !mp_testbit(x, n); |
142 | MP_DROP(x); |
143 | return (r); |
d3409d5e |
144 | } |
145 | |
09d00c6b |
146 | /* --- @mp_setbit@, @mp_clearbit@ --- * |
147 | * |
148 | * Arguments: @mp *d@ = a destination |
149 | * @mp *x@ = a large integer |
150 | * @unsigned long n@ = which bit to modify |
151 | * |
152 | * Returns: The argument @x@, with the appropriate bit set or cleared. |
153 | */ |
154 | |
155 | mp *mp_setbit(mp *d, mp *x, unsigned long n) |
156 | { |
157 | size_t rq; |
158 | |
159 | rq = n + MPW_BITS; rq -= rq % MPW_BITS; |
160 | if (d != x) { |
161 | if (d) MP_DROP(d); |
162 | d = MP_COPY(x); |
163 | } |
164 | MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN)); |
165 | d->v[n/MPW_BITS] |= 1 << n%MPW_BITS; |
166 | return (d); |
167 | } |
168 | |
169 | mp *mp_clearbit(mp *d, mp *x, unsigned long n) |
170 | { |
171 | size_t rq; |
172 | |
173 | rq = n + MPW_BITS; rq -= rq % MPW_BITS; |
174 | if (d != x) { |
175 | if (d) MP_DROP(d); |
176 | d = MP_COPY(x); |
177 | } |
178 | MP_DEST(d, rq, x->f & (MP_NEG | MP_BURN)); |
179 | d->v[n/MPW_BITS] &= ~(1 << n%MPW_BITS); |
180 | return (d); |
181 | } |
182 | |
183 | /* --- @mp_setbit2c@, @mp_clearbit2c@ --- * |
184 | * |
185 | * Arguments: @mp *d@ = a destination |
186 | * @mp *x@ = a large integer |
187 | * @unsigned long n@ = which bit to modify |
188 | * |
189 | * Returns: The argument @x@, with the appropriate bit set or cleared. |
190 | * Fakes up two's complement representation. |
191 | */ |
192 | |
193 | mp *mp_setbit2c(mp *d, mp *x, unsigned long n) |
194 | { |
a69a3efd |
195 | if (!MP_NEGP(x)) |
09d00c6b |
196 | return mp_setbit(d, x, n); |
197 | d = mp_not2c(d, x); |
198 | d = mp_clearbit(d, d, n); |
199 | d = mp_not2c(d, d); |
200 | return (d); |
201 | } |
202 | |
203 | mp *mp_clearbit2c(mp *d, mp *x, unsigned long n) |
204 | { |
a69a3efd |
205 | if (!MP_NEGP(x)) |
09d00c6b |
206 | return mp_clearbit(d, x, n); |
207 | d = mp_not2c(d, x); |
208 | d = mp_setbit(d, d, n); |
209 | d = mp_not2c(d, d); |
210 | return (d); |
211 | } |
212 | |
4b536f42 |
213 | /* --- @mp_eq@ --- * |
214 | * |
215 | * Arguments: @const mp *a, *b@ = two numbers |
216 | * |
217 | * Returns: Nonzero if the numbers are equal. |
218 | */ |
219 | |
220 | int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, b)); } |
221 | |
d3409d5e |
222 | /* --- @mp_cmp@ --- * |
223 | * |
224 | * Arguments: @const mp *a, *b@ = two numbers |
225 | * |
226 | * Returns: Less than, equal to or greater than zero, according to |
227 | * whether @a@ is less than, equal to or greater than @b@. |
228 | */ |
229 | |
230 | int mp_cmp(const mp *a, const mp *b) |
231 | { |
232 | if (!((a->f ^ b->f) & MP_NEG)) |
233 | return (mpx_ucmp(a->v, a->vl, b->v, b->vl)); |
234 | else if (a->f & MP_NEG) |
235 | return (-1); |
236 | else |
237 | return (+1); |
238 | } |
239 | |
397041a9 |
240 | /* --- @mp_neg@ --- * |
241 | * |
242 | * Arguments: @mp *d@ = destination |
243 | * @mp *a@ = argument |
244 | * |
245 | * Returns: The negation of the argument. |
246 | * |
247 | * Use: Negates its argument. |
248 | */ |
249 | |
250 | mp *mp_neg(mp *d, mp *a) |
251 | { |
252 | /* --- Surprising amounts of messing about required --- */ |
253 | |
254 | MP_SHRINK(a); |
255 | MP_COPY(a); |
81578196 |
256 | if (d) |
257 | MP_DROP(d); |
258 | if (a->v == a->vl) |
397041a9 |
259 | return (a); |
397041a9 |
260 | MP_DEST(a, MP_LEN(a), a->f); |
261 | a->f ^= MP_NEG; |
262 | return (a); |
263 | } |
264 | |
f09e814a |
265 | /* --- @mp_bitop@ --- * |
0f32e0f8 |
266 | * |
267 | * Arguments: @mp *d@ = destination |
268 | * @mp *a, *b@ = sources |
269 | * |
f09e814a |
270 | * Returns: The result of the given bitwise operation. These functions |
271 | * don't handle negative numbers at all sensibly. For that, use |
272 | * the @...2c@ variants. The functions are named after the |
273 | * truth tables they generate: |
274 | * |
275 | * a: 0011 |
276 | * b: 0101 |
277 | * @mpx_bitXXXX@ |
0f32e0f8 |
278 | */ |
279 | |
f09e814a |
280 | #define MP_BITBINOP(string) \ |
0f32e0f8 |
281 | \ |
f09e814a |
282 | mp *mp_bit##string(mp *d, mp *a, mp *b) \ |
0f32e0f8 |
283 | { \ |
75263f25 |
284 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & ~MP_NEG); \ |
f09e814a |
285 | mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \ |
0f32e0f8 |
286 | d->f = (a->f | b->f) & MP_BURN; \ |
287 | MP_SHRINK(d); \ |
288 | return (d); \ |
289 | } |
290 | |
f09e814a |
291 | MPX_DOBIN(MP_BITBINOP) |
292 | |
293 | /* --- @mp_not@ --- * |
294 | * |
295 | * Arguments: @mp *d@ = destination |
296 | * @mp *a@ = source |
297 | * |
298 | * Returns: The bitwise complement of the source. |
299 | */ |
0f32e0f8 |
300 | |
301 | mp *mp_not(mp *d, mp *a) |
302 | { |
303 | MP_DEST(d, MP_LEN(a), a->f); |
304 | mpx_not(d->v, d->vl, a->v, a->vl); |
305 | d->f = a->f & MP_BURN; |
306 | MP_SHRINK(d); |
307 | return (d); |
308 | } |
309 | |
f09e814a |
310 | /* --- @mp_bitop2c@ --- * |
311 | * |
312 | * Arguments: @mp *d@ = destination |
313 | * @mp *a, *b@ = sources |
314 | * |
315 | * Returns: The result of the given bitwise operation. Negative numbers |
316 | * are treated as two's complement, sign-extended infinitely to |
317 | * the left. The functions are named after the truth tables |
318 | * they generate: |
319 | * |
320 | * a: 0011 |
321 | * b: 0101 |
322 | * @mpx_bitXXXX@ |
323 | */ |
324 | |
325 | /* --- How this actually works --- * |
326 | * |
327 | * The two arguments are inverted (with a sign-swap) if they're currently |
328 | * negative. This means that we end up using a different function (one which |
329 | * reinverts as we go) for the main operation. Also, if the sign would be |
330 | * negative at the end, we preinvert the output and then invert again with a |
331 | * sign-swap. |
332 | * |
333 | * Start with: wxyz WXYZ |
334 | * If @a@ negative: yzwx or YZWX |
335 | * If @b@ negative: xwzy XWZY |
336 | * If both negative: zyxw ZYXW |
337 | */ |
338 | |
339 | #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \ |
340 | \ |
341 | mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \ |
342 | { \ |
343 | if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \ |
344 | d = mp_bit##base(d, a, b); \ |
345 | p_base \ |
346 | } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \ |
347 | MP_COPY(b); \ |
348 | d = mp_not2c(d, a); \ |
349 | d = mp_bit##an(d, d, b); \ |
350 | MP_DROP(b); \ |
351 | p_an \ |
352 | } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \ |
353 | MP_COPY(a); \ |
354 | d = mp_not2c(d, b); \ |
355 | d = mp_bit##bn(d, a, d); \ |
356 | MP_DROP(a); \ |
357 | p_bn \ |
358 | } else { /* Both negative */ \ |
359 | mp *t = mp_not2c(MP_NEW, a); \ |
360 | mp *d = mp_not2c(d, b); \ |
361 | d = mp_bit##abn(d, t, d); \ |
362 | MP_DROP(t); \ |
363 | p_abn \ |
364 | } \ |
365 | return (d); \ |
366 | } \ |
367 | |
368 | #define NEG d = mp_not2c(d, d); |
369 | #define POS |
370 | MP_BIT2CBINOP(0000, 0000, 0000, 0000, 0000, POS, POS, POS, POS) |
371 | MP_BIT2CBINOP(0001, 0001, 0100, 0010, 0111, POS, POS, POS, NEG) |
372 | MP_BIT2CBINOP(0010, 0010, 0111, 0001, 0100, POS, NEG, POS, POS) |
373 | MP_BIT2CBINOP(0011, 0011, 0011, 0011, 0011, POS, NEG, POS, NEG) |
374 | MP_BIT2CBINOP(0100, 0100, 0001, 0111, 0010, POS, POS, NEG, POS) |
375 | MP_BIT2CBINOP(0101, 0101, 0101, 0101, 0101, POS, POS, NEG, NEG) |
376 | MP_BIT2CBINOP(0110, 0110, 0110, 0110, 0110, POS, NEG, NEG, POS) |
377 | MP_BIT2CBINOP(0111, 0111, 0010, 0100, 0001, POS, NEG, NEG, NEG) |
378 | MP_BIT2CBINOP(1000, 0111, 0010, 0100, 0001, NEG, POS, POS, POS) |
379 | MP_BIT2CBINOP(1001, 0110, 0110, 0110, 0110, NEG, POS, POS, NEG) |
380 | MP_BIT2CBINOP(1010, 0101, 0101, 0101, 0101, NEG, NEG, POS, POS) |
381 | MP_BIT2CBINOP(1011, 0100, 0001, 0111, 0010, NEG, NEG, POS, NEG) |
382 | MP_BIT2CBINOP(1100, 0011, 0011, 0011, 0011, NEG, POS, NEG, POS) |
383 | MP_BIT2CBINOP(1101, 0010, 0111, 0001, 0100, NEG, POS, NEG, NEG) |
384 | MP_BIT2CBINOP(1110, 0001, 0100, 0010, 0111, NEG, NEG, NEG, POS) |
385 | MP_BIT2CBINOP(1111, 0000, 0000, 0000, 0000, NEG, NEG, NEG, NEG) |
386 | #undef NEG |
387 | #undef POS |
388 | |
389 | /* --- @mp_not2c@ --- * |
390 | * |
391 | * Arguments: @mp *d@ = destination |
392 | * @mp *a@ = source |
393 | * |
394 | * Returns: The sign-extended complement of the argument. |
395 | */ |
396 | |
397 | mp *mp_not2c(mp *d, mp *a) |
398 | { |
399 | mpw one = 1; |
400 | |
401 | MP_DEST(d, MP_LEN(a) + 1, a->f); |
402 | if (d == a) { |
a69a3efd |
403 | if (MP_NEGP(a)) |
f09e814a |
404 | MPX_USUBN(d->v, d->vl, 1); |
405 | else |
406 | MPX_UADDN(d->v, d->vl, 1); |
407 | } else { |
a69a3efd |
408 | if (MP_NEGP(a)) |
f09e814a |
409 | mpx_usub(d->v, d->vl, a->v, a->vl, &one, &one + 1); |
410 | else |
411 | mpx_uadd(d->v, d->vl, a->v, a->vl, &one, &one + 1); |
412 | } |
413 | d->f = (a->f & (MP_NEG | MP_BURN)) ^ MP_NEG; |
414 | MP_SHRINK(d); |
415 | return (d); |
416 | } |
417 | |
d3409d5e |
418 | /* --- @mp_add@ --- * |
419 | * |
420 | * Arguments: @mp *d@ = destination |
ef5f4810 |
421 | * @mp *a, *b@ = sources |
d3409d5e |
422 | * |
423 | * Returns: Result, @a@ added to @b@. |
424 | */ |
425 | |
ef5f4810 |
426 | mp *mp_add(mp *d, mp *a, mp *b) |
d3409d5e |
427 | { |
d34decd2 |
428 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f); |
d3409d5e |
429 | if (!((a->f ^ b->f) & MP_NEG)) |
430 | mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
431 | else { |
432 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
ef5f4810 |
433 | mp *t = a; a = b; b = t; |
d3409d5e |
434 | } |
435 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
436 | } |
437 | d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG); |
438 | MP_SHRINK(d); |
439 | return (d); |
440 | } |
441 | |
442 | /* --- @mp_sub@ --- * |
443 | * |
444 | * Arguments: @mp *d@ = destination |
ef5f4810 |
445 | * @mp *a, *b@ = sources |
d3409d5e |
446 | * |
447 | * Returns: Result, @b@ subtracted from @a@. |
448 | */ |
449 | |
ef5f4810 |
450 | mp *mp_sub(mp *d, mp *a, mp *b) |
d3409d5e |
451 | { |
452 | unsigned sgn = 0; |
d34decd2 |
453 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f); |
d3409d5e |
454 | if ((a->f ^ b->f) & MP_NEG) |
455 | mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
456 | else { |
457 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
ef5f4810 |
458 | mp *t = a; a = b; b = t; |
d3409d5e |
459 | sgn = MP_NEG; |
460 | } |
461 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
462 | } |
463 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG); |
464 | MP_SHRINK(d); |
465 | return (d); |
466 | } |
467 | |
468 | /* --- @mp_mul@ --- * |
469 | * |
470 | * Arguments: @mp *d@ = destination |
ef5f4810 |
471 | * @mp *a, *b@ = sources |
d3409d5e |
472 | * |
473 | * Returns: Result, @a@ multiplied by @b@. |
474 | */ |
475 | |
ef5f4810 |
476 | mp *mp_mul(mp *d, mp *a, mp *b) |
d3409d5e |
477 | { |
ef5f4810 |
478 | a = MP_COPY(a); |
479 | b = MP_COPY(b); |
480 | |
52cdaca9 |
481 | if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= MPK_THRESH) { |
d34decd2 |
482 | MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF); |
ef5f4810 |
483 | mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
8017495b |
484 | } else { |
dd22938e |
485 | size_t m = MAX(MP_LEN(a), MP_LEN(b)); |
ef5f4810 |
486 | mpw *s; |
dd22938e |
487 | MP_DEST(d, 3 * m, a->f | b->f | MP_UNDEF); |
488 | s = mpalloc(d->a, 5 * m); |
489 | mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + 5 * m); |
d34decd2 |
490 | mpfree(d->a, s); |
ef5f4810 |
491 | } |
492 | |
d3409d5e |
493 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG); |
494 | MP_SHRINK(d); |
ef5f4810 |
495 | MP_DROP(a); |
496 | MP_DROP(b); |
d3409d5e |
497 | return (d); |
498 | } |
499 | |
500 | /* --- @mp_sqr@ --- * |
501 | * |
502 | * Arguments: @mp *d@ = destination |
ef5f4810 |
503 | * @mp *a@ = source |
d3409d5e |
504 | * |
505 | * Returns: Result, @a@ squared. |
506 | */ |
507 | |
ef5f4810 |
508 | mp *mp_sqr(mp *d, mp *a) |
d3409d5e |
509 | { |
ef5f4810 |
510 | size_t m = MP_LEN(a); |
511 | |
512 | a = MP_COPY(a); |
52cdaca9 |
513 | if (m > MPK_THRESH) { |
ef5f4810 |
514 | mpw *s; |
dd22938e |
515 | MP_DEST(d, 3 * m, a->f | MP_UNDEF); |
516 | s = mpalloc(d->a, 5 * m); |
517 | mpx_ksqr(d->v, d->vl, a->v, a->vl, s, s + 5 * m); |
d34decd2 |
518 | mpfree(d->a, s); |
dd22938e |
519 | } else { |
520 | MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF); |
ef5f4810 |
521 | mpx_usqr(d->v, d->vl, a->v, a->vl); |
dd22938e |
522 | } |
d3409d5e |
523 | d->f = a->f & MP_BURN; |
524 | MP_SHRINK(d); |
ef5f4810 |
525 | MP_DROP(a); |
d3409d5e |
526 | return (d); |
527 | } |
528 | |
529 | /* --- @mp_div@ --- * |
530 | * |
531 | * Arguments: @mp **qq, **rr@ = destination, quotient and remainder |
ef5f4810 |
532 | * @mp *a, *b@ = sources |
d3409d5e |
533 | * |
534 | * Use: Calculates the quotient and remainder when @a@ is divided by |
535 | * @b@. The destinations @*qq@ and @*rr@ must be distinct. |
536 | * Either of @qq@ or @rr@ may be null to indicate that the |
537 | * result is irrelevant. (Discarding both results is silly.) |
538 | * There is a performance advantage if @a == *rr@. |
539 | * |
540 | * The behaviour when @a@ and @b@ have the same sign is |
541 | * straightforward. When the signs differ, this implementation |
542 | * chooses @r@ to have the same sign as @b@, rather than the |
543 | * more normal choice that the remainder has the same sign as |
544 | * the dividend. This makes modular arithmetic a little more |
545 | * straightforward. |
546 | */ |
547 | |
ef5f4810 |
548 | void mp_div(mp **qq, mp **rr, mp *a, mp *b) |
d3409d5e |
549 | { |
550 | mp *r = rr ? *rr : MP_NEW; |
551 | mp *q = qq ? *qq : MP_NEW; |
552 | mpw *sv, *svl; |
553 | |
d3409d5e |
554 | /* --- Set the remainder up right --- * |
555 | * |
556 | * Just in case the divisor is larger, be able to cope with this. It's not |
557 | * important in @mpx_udiv@, but it is here because of the sign correction. |
558 | */ |
559 | |
d34decd2 |
560 | b = MP_COPY(b); |
561 | a = MP_COPY(a); |
562 | if (r) |
563 | MP_DROP(r); |
564 | r = a; |
a9c8f3d6 |
565 | MP_DEST(r, MAX(MP_LEN(a), MP_LEN(b)) + 2, a->f | b->f); |
d3409d5e |
566 | |
567 | /* --- Fix up the quotient too --- */ |
568 | |
d34decd2 |
569 | r = MP_COPY(r); |
570 | MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF); |
571 | MP_DROP(r); |
572 | |
573 | /* --- Set up some temporary workspace --- */ |
574 | |
575 | { |
576 | size_t rq = MP_LEN(b) + 1; |
577 | sv = mpalloc(r->a, rq); |
578 | svl = sv + rq; |
579 | } |
d3409d5e |
580 | |
581 | /* --- Perform the calculation --- */ |
582 | |
583 | mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl); |
584 | |
585 | /* --- Sort out the sign of the results --- * |
586 | * |
587 | * If the signs of the arguments differ, and the remainder is nonzero, I |
588 | * must add one to the absolute value of the quotient and subtract the |
589 | * remainder from @b@. |
590 | */ |
591 | |
d34decd2 |
592 | q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG); |
a69a3efd |
593 | if (MP_NEGP(q)) { |
ef5f4810 |
594 | mpw *v; |
595 | for (v = r->v; v < r->vl; v++) { |
d3409d5e |
596 | if (*v) { |
597 | MPX_UADDN(q->v, q->vl, 1); |
598 | mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl); |
599 | break; |
600 | } |
601 | } |
602 | } |
603 | |
d34decd2 |
604 | r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG); |
d3409d5e |
605 | |
606 | /* --- Store the return values --- */ |
607 | |
d34decd2 |
608 | mpfree(r->a, sv); |
609 | MP_DROP(b); |
610 | |
d3409d5e |
611 | if (!qq) |
612 | MP_DROP(q); |
613 | else { |
614 | MP_SHRINK(q); |
615 | *qq = q; |
616 | } |
617 | |
618 | if (!rr) |
619 | MP_DROP(r); |
620 | else { |
621 | MP_SHRINK(r); |
622 | *rr = r; |
623 | } |
d3409d5e |
624 | } |
625 | |
f1713c63 |
626 | /* --- @mp_odd@ --- * |
627 | * |
628 | * Arguments: @mp *d@ = pointer to destination integer |
629 | * @mp *m@ = pointer to source integer |
630 | * @size_t *s@ = where to store the power of 2 |
631 | * |
632 | * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%. |
633 | * |
634 | * Use: Computes a power of two and an odd integer which, when |
635 | * multiplied, give a specified result. This sort of thing is |
636 | * useful in number theory quite often. |
637 | */ |
638 | |
639 | mp *mp_odd(mp *d, mp *m, size_t *s) |
640 | { |
641 | size_t ss = 0; |
642 | const mpw *v, *vl; |
643 | |
644 | v = m->v; |
645 | vl = m->vl; |
646 | for (; !*v && v < vl; v++) |
647 | ss += MPW_BITS; |
648 | if (v >= vl) |
649 | ss = 0; |
650 | else { |
651 | mpw x = *v; |
652 | mpw mask = MPW_MAX; |
653 | unsigned z = MPW_BITS / 2; |
654 | |
655 | while (z) { |
656 | mask >>= z; |
657 | if (!(x & mask)) { |
658 | x >>= z; |
659 | ss += z; |
660 | } |
661 | z >>= 1; |
662 | } |
663 | } |
664 | |
665 | *s = ss; |
666 | return (mp_lsr(d, m, ss)); |
667 | } |
668 | |
d3409d5e |
669 | /*----- Test rig ----------------------------------------------------------*/ |
670 | |
671 | #ifdef TEST_RIG |
672 | |
673 | static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b) |
674 | { |
4b536f42 |
675 | if (!MP_EQ(expect, result)) { |
d3409d5e |
676 | fprintf(stderr, "\n*** %s failed", op); |
677 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); |
678 | fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10); |
679 | fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 10); |
680 | fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 10); |
681 | fputc('\n', stderr); |
682 | return (0); |
683 | } |
684 | return (1); |
685 | } |
686 | |
687 | #define RIG(name, op) \ |
ef5f4810 |
688 | static int t##name(dstr *v) \ |
d3409d5e |
689 | { \ |
690 | mp *a = *(mp **)v[0].buf; \ |
691 | mpw n = *(int *)v[1].buf; \ |
692 | mp b; \ |
693 | mp *r = *(mp **)v[2].buf; \ |
694 | mp *c = op(MP_NEW, a, n); \ |
695 | int ok; \ |
696 | mp_build(&b, &n, &n + 1); \ |
697 | ok = verify(#name, r, c, a, &b); \ |
698 | mp_drop(a); mp_drop(c); mp_drop(r); \ |
ef5f4810 |
699 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
d3409d5e |
700 | return (ok); \ |
701 | } |
702 | |
703 | RIG(lsl, mp_lsl) |
704 | RIG(lsr, mp_lsr) |
f09e814a |
705 | RIG(lsl2c, mp_lsl2c) |
706 | RIG(lsr2c, mp_lsr2c) |
d3409d5e |
707 | |
708 | #undef RIG |
709 | |
710 | #define RIG(name, op) \ |
ef5f4810 |
711 | static int t##name(dstr *v) \ |
d3409d5e |
712 | { \ |
713 | mp *a = *(mp **)v[0].buf; \ |
714 | mp *b = *(mp **)v[1].buf; \ |
715 | mp *r = *(mp **)v[2].buf; \ |
716 | mp *c = op(MP_NEW, a, b); \ |
717 | int ok = verify(#name, r, c, a, b); \ |
718 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \ |
ef5f4810 |
719 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
d3409d5e |
720 | return (ok); \ |
721 | } |
722 | |
723 | RIG(add, mp_add) |
724 | RIG(sub, mp_sub) |
725 | RIG(mul, mp_mul) |
726 | |
727 | #undef RIG |
728 | |
729 | static int tdiv(dstr *v) |
730 | { |
731 | mp *a = *(mp **)v[0].buf; |
732 | mp *b = *(mp **)v[1].buf; |
733 | mp *q = *(mp **)v[2].buf; |
734 | mp *r = *(mp **)v[3].buf; |
735 | mp *c = MP_NEW, *d = MP_NEW; |
736 | int ok = 1; |
737 | mp_div(&c, &d, a, b); |
738 | ok &= verify("div(quotient)", q, c, a, b); |
739 | ok &= verify("div(remainder)", r, d, a, b); |
740 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q); |
ef5f4810 |
741 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
d3409d5e |
742 | return (ok); |
743 | } |
744 | |
f09e814a |
745 | static int tbin(dstr *v) |
746 | { |
747 | static mp *(*fn[])(mp *, mp *, mp *) = { |
748 | #define DO(string) mp_bit##string##2c, |
749 | MPX_DOBIN(DO) |
750 | #undef DO |
751 | }; |
752 | int ok = 1; |
753 | unsigned op = 0; |
754 | mp *a = *(mp **)v[1].buf; |
755 | mp *b = *(mp **)v[2].buf; |
756 | mp *r = *(mp **)v[3].buf; |
757 | mp *c; |
758 | |
759 | if (strcmp(v[0].buf, "and") == 0) op = 1; |
760 | else if (strcmp(v[0].buf, "or") == 0) op = 7; |
761 | else if (strcmp(v[0].buf, "nand") == 0) op = 14; |
762 | else if (strcmp(v[0].buf, "nor") == 0) op = 8; |
763 | else if (strcmp(v[0].buf, "xor") == 0) op = 6; |
764 | else { |
765 | char *p = v[0].buf; |
766 | while (*p) { |
767 | op <<= 1; |
768 | if (*p++ == '1') |
769 | op |= 1; |
770 | } |
771 | } |
772 | |
773 | c = fn[op](MP_NEW, a, b); |
774 | ok = verify(v[0].buf, r, c, a, b); |
775 | mp_drop(a); mp_drop(b); mp_drop(r); mp_drop(c); |
776 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
777 | return (ok); |
778 | } |
779 | |
09d00c6b |
780 | static int tset(dstr *v) |
781 | { |
782 | mp *a = *(mp **)v[0].buf; |
783 | unsigned long n = *(unsigned long *)v[1].buf; |
784 | mp *r = *(mp **)v[2].buf; |
785 | mp *c; |
786 | int ok = 1; |
787 | |
788 | c = mp_setbit2c(MP_NEW, a, n); |
789 | if (!MP_EQ(c, r)) { |
790 | ok = 0; |
791 | fprintf(stderr, "\n***setbit (set) failed"); |
792 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); |
793 | fprintf(stderr, "\n*** n = %lu", n); |
794 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); |
795 | fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16); |
796 | fputc('\n', stderr); |
797 | } |
798 | if (!mp_testbit2c(r, n)) { |
799 | ok = 0; |
800 | fprintf(stderr, "\n***setbit (test) failed"); |
801 | fprintf(stderr, "\n*** n = %lu", n); |
802 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); |
803 | fputc('\n', stderr); |
804 | } |
805 | mp_drop(a); |
806 | mp_drop(r); |
807 | mp_drop(c); |
808 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
809 | return (ok); |
810 | } |
811 | |
812 | static int tclr(dstr *v) |
813 | { |
814 | mp *a = *(mp **)v[0].buf; |
815 | unsigned long n = *(unsigned long *)v[1].buf; |
816 | mp *r = *(mp **)v[2].buf; |
817 | mp *c; |
818 | int ok = 1; |
819 | |
820 | c = mp_clearbit2c(MP_NEW, a, n); |
821 | if (!MP_EQ(c, r)) { |
822 | ok = 0; |
823 | fprintf(stderr, "\n***clrbit (set) failed"); |
824 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); |
825 | fprintf(stderr, "\n*** n = %lu", n); |
826 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); |
827 | fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16); |
828 | fputc('\n', stderr); |
829 | } |
830 | if (mp_testbit2c(r, n)) { |
831 | ok = 0; |
832 | fprintf(stderr, "\n***clrbit (test) failed"); |
833 | fprintf(stderr, "\n*** n = %lu", n); |
834 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); |
835 | fputc('\n', stderr); |
836 | } |
837 | mp_drop(a); |
838 | mp_drop(c); |
839 | mp_drop(r); |
840 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
841 | return (ok); |
842 | } |
843 | |
397041a9 |
844 | static int tneg(dstr *v) |
845 | { |
846 | mp *a = *(mp **)v[0].buf; |
847 | mp *r = *(mp **)v[1].buf; |
848 | int ok = 1; |
849 | mp *n = mp_neg(MP_NEW, a); |
850 | if (!MP_EQ(r, n)) { |
851 | ok = 0; |
852 | fprintf(stderr, "\n*** neg failed\n"); |
853 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); |
854 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10); |
855 | fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10); |
856 | fputc('\n', stderr); |
857 | } |
858 | mp_drop(n); |
859 | n = mp_neg(a, a); |
860 | if (!MP_EQ(r, n)) { |
861 | ok = 0; |
862 | fprintf(stderr, "\n*** neg failed\n"); |
863 | fputs("\n*** a* = ", stderr); mp_writefile(a, stderr, 10); |
864 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10); |
865 | fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10); |
866 | fputc('\n', stderr); |
867 | } |
868 | mp_drop(a); |
869 | mp_drop(r); |
870 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
871 | return (ok); |
872 | } |
873 | |
f1713c63 |
874 | static int todd(dstr *v) |
875 | { |
876 | mp *a = *(mp **)v[0].buf; |
877 | size_t rs = *(uint32 *)v[1].buf; |
878 | mp *rt = *(mp **)v[2].buf; |
879 | int ok = 1; |
880 | mp *t; |
881 | size_t s; |
882 | t = mp_odd(MP_NEW, a, &s); |
4b536f42 |
883 | if (s != rs || !MP_EQ(t, rt)) { |
f1713c63 |
884 | ok = 0; |
885 | fprintf(stderr, "\n*** odd failed"); |
886 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); |
887 | fprintf(stderr, "\n*** s = %lu", (unsigned long)s); |
888 | fputs("\n*** t = ", stderr); mp_writefile(t, stderr, 10); |
889 | fprintf(stderr, "\n*** rs = %lu", (unsigned long)rs); |
890 | fputs("\n*** rt = ", stderr); mp_writefile(rt, stderr, 10); |
891 | fputc('\n', stderr); |
892 | } |
893 | mp_drop(a); |
894 | mp_drop(rt); |
895 | mp_drop(t); |
09d00c6b |
896 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
f1713c63 |
897 | return (ok); |
898 | } |
899 | |
d3409d5e |
900 | static test_chunk tests[] = { |
f09e814a |
901 | { "lsl", tlsl, { &type_mp, &type_int, &type_mp, 0 } }, |
902 | { "lsr", tlsr, { &type_mp, &type_int, &type_mp, 0 } }, |
903 | { "lsl2c", tlsl2c, { &type_mp, &type_int, &type_mp, 0 } }, |
904 | { "lsr2c", tlsr2c, { &type_mp, &type_int, &type_mp, 0 } }, |
09d00c6b |
905 | { "setbit", tset, { &type_mp, &type_ulong, &type_mp, 0 } }, |
906 | { "clrbit", tclr, { &type_mp, &type_ulong, &type_mp, 0 } }, |
d3409d5e |
907 | { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } }, |
908 | { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } }, |
909 | { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } }, |
910 | { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, |
f09e814a |
911 | { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } }, |
f1713c63 |
912 | { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } }, |
397041a9 |
913 | { "neg", tneg, { &type_mp, &type_mp, 0 } }, |
d3409d5e |
914 | { 0, 0, { 0 } }, |
915 | }; |
916 | |
917 | int main(int argc, char *argv[]) |
918 | { |
919 | sub_init(); |
920 | test_run(argc, argv, tests, SRCDIR "/tests/mp"); |
921 | return (0); |
922 | } |
923 | |
924 | #endif |
925 | |
926 | /*----- That's all, folks -------------------------------------------------*/ |