Commit | Line | Data |
---|---|---|
d3409d5e | 1 | /* -*-c-*- |
2 | * | |
a69a3efd | 3 | * $Id$ |
d3409d5e | 4 | * |
5 | * Basic arithmetic on multiprecision integers | |
6 | * | |
7 | * (c) 1999 Straylight/Edgeware | |
8 | */ | |
9 | ||
45c0fd36 | 10 | /*----- Licensing notice --------------------------------------------------* |
d3409d5e | 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. | |
45c0fd36 | 18 | * |
d3409d5e | 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. | |
45c0fd36 | 23 | * |
d3409d5e | 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 | { | |
4108c8d2 | 232 | if (!((a->f ^ b->f) & MP_NEG)) { |
233 | if (a->f & MP_NEG) | |
234 | return (-mpx_ucmp(a->v, a->vl, b->v, b->vl)); | |
235 | else | |
236 | return (mpx_ucmp(a->v, a->vl, b->v, b->vl)); | |
237 | } else if (a->f & MP_NEG) | |
d3409d5e | 238 | return (-1); |
239 | else | |
240 | return (+1); | |
241 | } | |
242 | ||
397041a9 | 243 | /* --- @mp_neg@ --- * |
244 | * | |
245 | * Arguments: @mp *d@ = destination | |
246 | * @mp *a@ = argument | |
247 | * | |
248 | * Returns: The negation of the argument. | |
249 | * | |
250 | * Use: Negates its argument. | |
251 | */ | |
252 | ||
253 | mp *mp_neg(mp *d, mp *a) | |
254 | { | |
255 | /* --- Surprising amounts of messing about required --- */ | |
256 | ||
257 | MP_SHRINK(a); | |
258 | MP_COPY(a); | |
81578196 | 259 | if (d) |
260 | MP_DROP(d); | |
261 | if (a->v == a->vl) | |
397041a9 | 262 | return (a); |
397041a9 | 263 | MP_DEST(a, MP_LEN(a), a->f); |
264 | a->f ^= MP_NEG; | |
265 | return (a); | |
266 | } | |
267 | ||
f09e814a | 268 | /* --- @mp_bitop@ --- * |
0f32e0f8 | 269 | * |
270 | * Arguments: @mp *d@ = destination | |
271 | * @mp *a, *b@ = sources | |
272 | * | |
f09e814a | 273 | * Returns: The result of the given bitwise operation. These functions |
274 | * don't handle negative numbers at all sensibly. For that, use | |
275 | * the @...2c@ variants. The functions are named after the | |
276 | * truth tables they generate: | |
277 | * | |
278 | * a: 0011 | |
279 | * b: 0101 | |
280 | * @mpx_bitXXXX@ | |
0f32e0f8 | 281 | */ |
282 | ||
f09e814a | 283 | #define MP_BITBINOP(string) \ |
0f32e0f8 | 284 | \ |
f09e814a | 285 | mp *mp_bit##string(mp *d, mp *a, mp *b) \ |
0f32e0f8 | 286 | { \ |
75263f25 | 287 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)), (a->f | b->f) & ~MP_NEG); \ |
f09e814a | 288 | mpx_bit##string(d->v, d->vl, a->v, a->vl, b->v, b->vl); \ |
0f32e0f8 | 289 | d->f = (a->f | b->f) & MP_BURN; \ |
290 | MP_SHRINK(d); \ | |
291 | return (d); \ | |
292 | } | |
293 | ||
f09e814a | 294 | MPX_DOBIN(MP_BITBINOP) |
295 | ||
296 | /* --- @mp_not@ --- * | |
297 | * | |
298 | * Arguments: @mp *d@ = destination | |
299 | * @mp *a@ = source | |
300 | * | |
301 | * Returns: The bitwise complement of the source. | |
45c0fd36 | 302 | */ |
0f32e0f8 | 303 | |
304 | mp *mp_not(mp *d, mp *a) | |
305 | { | |
306 | MP_DEST(d, MP_LEN(a), a->f); | |
307 | mpx_not(d->v, d->vl, a->v, a->vl); | |
308 | d->f = a->f & MP_BURN; | |
309 | MP_SHRINK(d); | |
310 | return (d); | |
311 | } | |
312 | ||
f09e814a | 313 | /* --- @mp_bitop2c@ --- * |
314 | * | |
315 | * Arguments: @mp *d@ = destination | |
316 | * @mp *a, *b@ = sources | |
317 | * | |
318 | * Returns: The result of the given bitwise operation. Negative numbers | |
319 | * are treated as two's complement, sign-extended infinitely to | |
320 | * the left. The functions are named after the truth tables | |
321 | * they generate: | |
322 | * | |
323 | * a: 0011 | |
324 | * b: 0101 | |
325 | * @mpx_bitXXXX@ | |
326 | */ | |
327 | ||
328 | /* --- How this actually works --- * | |
329 | * | |
330 | * The two arguments are inverted (with a sign-swap) if they're currently | |
331 | * negative. This means that we end up using a different function (one which | |
332 | * reinverts as we go) for the main operation. Also, if the sign would be | |
333 | * negative at the end, we preinvert the output and then invert again with a | |
334 | * sign-swap. | |
335 | * | |
45c0fd36 | 336 | * Start with: wxyz WXYZ |
f09e814a | 337 | * If @a@ negative: yzwx or YZWX |
45c0fd36 MW |
338 | * If @b@ negative: xwzy XWZY |
339 | * If both negative: zyxw ZYXW | |
f09e814a | 340 | */ |
341 | ||
342 | #define MP_BIT2CBINOP(n, base, an, bn, abn, p_base, p_an, p_bn, p_abn) \ | |
343 | \ | |
344 | mp *mp_bit##n##2c(mp *d, mp *a, mp *b) \ | |
345 | { \ | |
346 | if (!((a->f | b->f) & MP_NEG)) { /* Both positive */ \ | |
347 | d = mp_bit##base(d, a, b); \ | |
348 | p_base \ | |
349 | } else if (!(b->f & MP_NEG)) { /* Only @b@ positive */ \ | |
350 | MP_COPY(b); \ | |
351 | d = mp_not2c(d, a); \ | |
352 | d = mp_bit##an(d, d, b); \ | |
353 | MP_DROP(b); \ | |
354 | p_an \ | |
355 | } else if (!(a->f & MP_NEG)) { /* Only @a@ positive */ \ | |
356 | MP_COPY(a); \ | |
357 | d = mp_not2c(d, b); \ | |
358 | d = mp_bit##bn(d, a, d); \ | |
359 | MP_DROP(a); \ | |
360 | p_bn \ | |
361 | } else { /* Both negative */ \ | |
362 | mp *t = mp_not2c(MP_NEW, a); \ | |
15f21155 | 363 | d = mp_not2c(d, b); \ |
f09e814a | 364 | d = mp_bit##abn(d, t, d); \ |
365 | MP_DROP(t); \ | |
366 | p_abn \ | |
367 | } \ | |
368 | return (d); \ | |
369 | } \ | |
370 | ||
371 | #define NEG d = mp_not2c(d, d); | |
372 | #define POS | |
373 | MP_BIT2CBINOP(0000, 0000, 0000, 0000, 0000, POS, POS, POS, POS) | |
374 | MP_BIT2CBINOP(0001, 0001, 0100, 0010, 0111, POS, POS, POS, NEG) | |
375 | MP_BIT2CBINOP(0010, 0010, 0111, 0001, 0100, POS, NEG, POS, POS) | |
376 | MP_BIT2CBINOP(0011, 0011, 0011, 0011, 0011, POS, NEG, POS, NEG) | |
377 | MP_BIT2CBINOP(0100, 0100, 0001, 0111, 0010, POS, POS, NEG, POS) | |
378 | MP_BIT2CBINOP(0101, 0101, 0101, 0101, 0101, POS, POS, NEG, NEG) | |
379 | MP_BIT2CBINOP(0110, 0110, 0110, 0110, 0110, POS, NEG, NEG, POS) | |
380 | MP_BIT2CBINOP(0111, 0111, 0010, 0100, 0001, POS, NEG, NEG, NEG) | |
381 | MP_BIT2CBINOP(1000, 0111, 0010, 0100, 0001, NEG, POS, POS, POS) | |
382 | MP_BIT2CBINOP(1001, 0110, 0110, 0110, 0110, NEG, POS, POS, NEG) | |
383 | MP_BIT2CBINOP(1010, 0101, 0101, 0101, 0101, NEG, NEG, POS, POS) | |
384 | MP_BIT2CBINOP(1011, 0100, 0001, 0111, 0010, NEG, NEG, POS, NEG) | |
385 | MP_BIT2CBINOP(1100, 0011, 0011, 0011, 0011, NEG, POS, NEG, POS) | |
386 | MP_BIT2CBINOP(1101, 0010, 0111, 0001, 0100, NEG, POS, NEG, NEG) | |
387 | MP_BIT2CBINOP(1110, 0001, 0100, 0010, 0111, NEG, NEG, NEG, POS) | |
388 | MP_BIT2CBINOP(1111, 0000, 0000, 0000, 0000, NEG, NEG, NEG, NEG) | |
389 | #undef NEG | |
390 | #undef POS | |
391 | ||
392 | /* --- @mp_not2c@ --- * | |
393 | * | |
394 | * Arguments: @mp *d@ = destination | |
395 | * @mp *a@ = source | |
396 | * | |
397 | * Returns: The sign-extended complement of the argument. | |
398 | */ | |
399 | ||
400 | mp *mp_not2c(mp *d, mp *a) | |
401 | { | |
402 | mpw one = 1; | |
403 | ||
404 | MP_DEST(d, MP_LEN(a) + 1, a->f); | |
405 | if (d == a) { | |
a69a3efd | 406 | if (MP_NEGP(a)) |
f09e814a | 407 | MPX_USUBN(d->v, d->vl, 1); |
408 | else | |
409 | MPX_UADDN(d->v, d->vl, 1); | |
410 | } else { | |
a69a3efd | 411 | if (MP_NEGP(a)) |
f09e814a | 412 | mpx_usub(d->v, d->vl, a->v, a->vl, &one, &one + 1); |
413 | else | |
414 | mpx_uadd(d->v, d->vl, a->v, a->vl, &one, &one + 1); | |
415 | } | |
416 | d->f = (a->f & (MP_NEG | MP_BURN)) ^ MP_NEG; | |
417 | MP_SHRINK(d); | |
418 | return (d); | |
419 | } | |
420 | ||
d3409d5e | 421 | /* --- @mp_add@ --- * |
422 | * | |
423 | * Arguments: @mp *d@ = destination | |
ef5f4810 | 424 | * @mp *a, *b@ = sources |
d3409d5e | 425 | * |
426 | * Returns: Result, @a@ added to @b@. | |
427 | */ | |
428 | ||
ef5f4810 | 429 | mp *mp_add(mp *d, mp *a, mp *b) |
d3409d5e | 430 | { |
d34decd2 | 431 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f); |
d3409d5e | 432 | if (!((a->f ^ b->f) & MP_NEG)) |
433 | mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl); | |
434 | else { | |
435 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { | |
ef5f4810 | 436 | mp *t = a; a = b; b = t; |
d3409d5e | 437 | } |
438 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); | |
439 | } | |
440 | d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG); | |
441 | MP_SHRINK(d); | |
442 | return (d); | |
443 | } | |
444 | ||
445 | /* --- @mp_sub@ --- * | |
446 | * | |
447 | * Arguments: @mp *d@ = destination | |
ef5f4810 | 448 | * @mp *a, *b@ = sources |
d3409d5e | 449 | * |
450 | * Returns: Result, @b@ subtracted from @a@. | |
451 | */ | |
452 | ||
ef5f4810 | 453 | mp *mp_sub(mp *d, mp *a, mp *b) |
d3409d5e | 454 | { |
455 | unsigned sgn = 0; | |
d34decd2 | 456 | MP_DEST(d, MAX(MP_LEN(a), MP_LEN(b)) + 1, a->f | b->f); |
d3409d5e | 457 | if ((a->f ^ b->f) & MP_NEG) |
458 | mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl); | |
459 | else { | |
460 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { | |
ef5f4810 | 461 | mp *t = a; a = b; b = t; |
d3409d5e | 462 | sgn = MP_NEG; |
463 | } | |
464 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); | |
465 | } | |
466 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG); | |
467 | MP_SHRINK(d); | |
468 | return (d); | |
469 | } | |
470 | ||
471 | /* --- @mp_mul@ --- * | |
472 | * | |
473 | * Arguments: @mp *d@ = destination | |
ef5f4810 | 474 | * @mp *a, *b@ = sources |
d3409d5e | 475 | * |
476 | * Returns: Result, @a@ multiplied by @b@. | |
477 | */ | |
478 | ||
ef5f4810 | 479 | mp *mp_mul(mp *d, mp *a, mp *b) |
d3409d5e | 480 | { |
ef5f4810 | 481 | a = MP_COPY(a); |
482 | b = MP_COPY(b); | |
483 | ||
52cdaca9 | 484 | if (MP_LEN(a) <= MPK_THRESH || MP_LEN(b) <= MPK_THRESH) { |
d34decd2 | 485 | MP_DEST(d, MP_LEN(a) + MP_LEN(b), a->f | b->f | MP_UNDEF); |
ef5f4810 | 486 | mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
8017495b | 487 | } else { |
dd22938e | 488 | size_t m = MAX(MP_LEN(a), MP_LEN(b)); |
ef5f4810 | 489 | mpw *s; |
dd22938e | 490 | MP_DEST(d, 3 * m, a->f | b->f | MP_UNDEF); |
491 | s = mpalloc(d->a, 5 * m); | |
492 | mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + 5 * m); | |
d34decd2 | 493 | mpfree(d->a, s); |
ef5f4810 | 494 | } |
495 | ||
d3409d5e | 496 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG); |
497 | MP_SHRINK(d); | |
ef5f4810 | 498 | MP_DROP(a); |
499 | MP_DROP(b); | |
d3409d5e | 500 | return (d); |
501 | } | |
502 | ||
503 | /* --- @mp_sqr@ --- * | |
504 | * | |
505 | * Arguments: @mp *d@ = destination | |
ef5f4810 | 506 | * @mp *a@ = source |
d3409d5e | 507 | * |
508 | * Returns: Result, @a@ squared. | |
509 | */ | |
510 | ||
ef5f4810 | 511 | mp *mp_sqr(mp *d, mp *a) |
d3409d5e | 512 | { |
ef5f4810 | 513 | size_t m = MP_LEN(a); |
514 | ||
515 | a = MP_COPY(a); | |
52cdaca9 | 516 | if (m > MPK_THRESH) { |
ef5f4810 | 517 | mpw *s; |
dd22938e | 518 | MP_DEST(d, 3 * m, a->f | MP_UNDEF); |
519 | s = mpalloc(d->a, 5 * m); | |
520 | mpx_ksqr(d->v, d->vl, a->v, a->vl, s, s + 5 * m); | |
d34decd2 | 521 | mpfree(d->a, s); |
dd22938e | 522 | } else { |
523 | MP_DEST(d, 2 * m + 2, a->f | MP_UNDEF); | |
ef5f4810 | 524 | mpx_usqr(d->v, d->vl, a->v, a->vl); |
dd22938e | 525 | } |
d3409d5e | 526 | d->f = a->f & MP_BURN; |
527 | MP_SHRINK(d); | |
ef5f4810 | 528 | MP_DROP(a); |
d3409d5e | 529 | return (d); |
530 | } | |
531 | ||
532 | /* --- @mp_div@ --- * | |
533 | * | |
534 | * Arguments: @mp **qq, **rr@ = destination, quotient and remainder | |
ef5f4810 | 535 | * @mp *a, *b@ = sources |
d3409d5e | 536 | * |
537 | * Use: Calculates the quotient and remainder when @a@ is divided by | |
538 | * @b@. The destinations @*qq@ and @*rr@ must be distinct. | |
539 | * Either of @qq@ or @rr@ may be null to indicate that the | |
540 | * result is irrelevant. (Discarding both results is silly.) | |
541 | * There is a performance advantage if @a == *rr@. | |
542 | * | |
543 | * The behaviour when @a@ and @b@ have the same sign is | |
544 | * straightforward. When the signs differ, this implementation | |
545 | * chooses @r@ to have the same sign as @b@, rather than the | |
546 | * more normal choice that the remainder has the same sign as | |
547 | * the dividend. This makes modular arithmetic a little more | |
548 | * straightforward. | |
549 | */ | |
550 | ||
ef5f4810 | 551 | void mp_div(mp **qq, mp **rr, mp *a, mp *b) |
d3409d5e | 552 | { |
553 | mp *r = rr ? *rr : MP_NEW; | |
554 | mp *q = qq ? *qq : MP_NEW; | |
555 | mpw *sv, *svl; | |
556 | ||
d3409d5e | 557 | /* --- Set the remainder up right --- * |
558 | * | |
559 | * Just in case the divisor is larger, be able to cope with this. It's not | |
560 | * important in @mpx_udiv@, but it is here because of the sign correction. | |
561 | */ | |
562 | ||
d34decd2 | 563 | b = MP_COPY(b); |
564 | a = MP_COPY(a); | |
565 | if (r) | |
566 | MP_DROP(r); | |
567 | r = a; | |
a9c8f3d6 | 568 | MP_DEST(r, MAX(MP_LEN(a), MP_LEN(b)) + 2, a->f | b->f); |
d3409d5e | 569 | |
570 | /* --- Fix up the quotient too --- */ | |
571 | ||
d34decd2 | 572 | r = MP_COPY(r); |
573 | MP_DEST(q, MP_LEN(r), r->f | MP_UNDEF); | |
574 | MP_DROP(r); | |
575 | ||
576 | /* --- Set up some temporary workspace --- */ | |
577 | ||
578 | { | |
579 | size_t rq = MP_LEN(b) + 1; | |
580 | sv = mpalloc(r->a, rq); | |
581 | svl = sv + rq; | |
582 | } | |
d3409d5e | 583 | |
584 | /* --- Perform the calculation --- */ | |
585 | ||
586 | mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl); | |
587 | ||
588 | /* --- Sort out the sign of the results --- * | |
589 | * | |
590 | * If the signs of the arguments differ, and the remainder is nonzero, I | |
591 | * must add one to the absolute value of the quotient and subtract the | |
592 | * remainder from @b@. | |
593 | */ | |
594 | ||
d34decd2 | 595 | q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG); |
a69a3efd | 596 | if (MP_NEGP(q)) { |
ef5f4810 | 597 | mpw *v; |
598 | for (v = r->v; v < r->vl; v++) { | |
d3409d5e | 599 | if (*v) { |
600 | MPX_UADDN(q->v, q->vl, 1); | |
601 | mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl); | |
602 | break; | |
603 | } | |
604 | } | |
605 | } | |
606 | ||
d34decd2 | 607 | r->f = ((r->f | b->f) & MP_BURN) | (b->f & MP_NEG); |
d3409d5e | 608 | |
609 | /* --- Store the return values --- */ | |
610 | ||
d34decd2 | 611 | mpfree(r->a, sv); |
612 | MP_DROP(b); | |
613 | ||
d3409d5e | 614 | if (!qq) |
615 | MP_DROP(q); | |
616 | else { | |
617 | MP_SHRINK(q); | |
618 | *qq = q; | |
619 | } | |
620 | ||
621 | if (!rr) | |
622 | MP_DROP(r); | |
623 | else { | |
624 | MP_SHRINK(r); | |
625 | *rr = r; | |
626 | } | |
d3409d5e | 627 | } |
628 | ||
f1713c63 | 629 | /* --- @mp_odd@ --- * |
630 | * | |
631 | * Arguments: @mp *d@ = pointer to destination integer | |
632 | * @mp *m@ = pointer to source integer | |
633 | * @size_t *s@ = where to store the power of 2 | |
634 | * | |
635 | * Returns: An odd integer integer %$t$% such that %$m = 2^s t$%. | |
636 | * | |
637 | * Use: Computes a power of two and an odd integer which, when | |
638 | * multiplied, give a specified result. This sort of thing is | |
639 | * useful in number theory quite often. | |
640 | */ | |
641 | ||
642 | mp *mp_odd(mp *d, mp *m, size_t *s) | |
643 | { | |
644 | size_t ss = 0; | |
645 | const mpw *v, *vl; | |
646 | ||
647 | v = m->v; | |
648 | vl = m->vl; | |
649 | for (; !*v && v < vl; v++) | |
650 | ss += MPW_BITS; | |
651 | if (v >= vl) | |
652 | ss = 0; | |
653 | else { | |
654 | mpw x = *v; | |
c29970a7 MW |
655 | unsigned z = MPW_P2; |
656 | mpw mask = ((mpw)1 << z) - 1; | |
f1713c63 | 657 | |
658 | while (z) { | |
f1713c63 | 659 | if (!(x & mask)) { |
660 | x >>= z; | |
661 | ss += z; | |
662 | } | |
663 | z >>= 1; | |
c29970a7 | 664 | mask >>= z; |
f1713c63 | 665 | } |
666 | } | |
667 | ||
668 | *s = ss; | |
669 | return (mp_lsr(d, m, ss)); | |
670 | } | |
671 | ||
d3409d5e | 672 | /*----- Test rig ----------------------------------------------------------*/ |
673 | ||
674 | #ifdef TEST_RIG | |
675 | ||
676 | static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b) | |
677 | { | |
4b536f42 | 678 | if (!MP_EQ(expect, result)) { |
d3409d5e | 679 | fprintf(stderr, "\n*** %s failed", op); |
45c0fd36 MW |
680 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); |
681 | fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10); | |
d3409d5e | 682 | fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 10); |
683 | fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 10); | |
684 | fputc('\n', stderr); | |
685 | return (0); | |
686 | } | |
687 | return (1); | |
688 | } | |
689 | ||
690 | #define RIG(name, op) \ | |
ef5f4810 | 691 | static int t##name(dstr *v) \ |
d3409d5e | 692 | { \ |
693 | mp *a = *(mp **)v[0].buf; \ | |
694 | mpw n = *(int *)v[1].buf; \ | |
695 | mp b; \ | |
696 | mp *r = *(mp **)v[2].buf; \ | |
697 | mp *c = op(MP_NEW, a, n); \ | |
698 | int ok; \ | |
699 | mp_build(&b, &n, &n + 1); \ | |
700 | ok = verify(#name, r, c, a, &b); \ | |
701 | mp_drop(a); mp_drop(c); mp_drop(r); \ | |
ef5f4810 | 702 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
d3409d5e | 703 | return (ok); \ |
704 | } | |
705 | ||
706 | RIG(lsl, mp_lsl) | |
707 | RIG(lsr, mp_lsr) | |
f09e814a | 708 | RIG(lsl2c, mp_lsl2c) |
709 | RIG(lsr2c, mp_lsr2c) | |
d3409d5e | 710 | |
711 | #undef RIG | |
712 | ||
713 | #define RIG(name, op) \ | |
ef5f4810 | 714 | static int t##name(dstr *v) \ |
d3409d5e | 715 | { \ |
716 | mp *a = *(mp **)v[0].buf; \ | |
717 | mp *b = *(mp **)v[1].buf; \ | |
718 | mp *r = *(mp **)v[2].buf; \ | |
719 | mp *c = op(MP_NEW, a, b); \ | |
720 | int ok = verify(#name, r, c, a, b); \ | |
721 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \ | |
ef5f4810 | 722 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
d3409d5e | 723 | return (ok); \ |
724 | } | |
725 | ||
726 | RIG(add, mp_add) | |
727 | RIG(sub, mp_sub) | |
728 | RIG(mul, mp_mul) | |
f4535c64 | 729 | RIG(exp, mp_exp) |
d3409d5e | 730 | |
731 | #undef RIG | |
732 | ||
733 | static int tdiv(dstr *v) | |
734 | { | |
735 | mp *a = *(mp **)v[0].buf; | |
736 | mp *b = *(mp **)v[1].buf; | |
737 | mp *q = *(mp **)v[2].buf; | |
738 | mp *r = *(mp **)v[3].buf; | |
739 | mp *c = MP_NEW, *d = MP_NEW; | |
740 | int ok = 1; | |
741 | mp_div(&c, &d, a, b); | |
742 | ok &= verify("div(quotient)", q, c, a, b); | |
743 | ok &= verify("div(remainder)", r, d, a, b); | |
744 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q); | |
ef5f4810 | 745 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
d3409d5e | 746 | return (ok); |
747 | } | |
748 | ||
f09e814a | 749 | static int tbin(dstr *v) |
750 | { | |
751 | static mp *(*fn[])(mp *, mp *, mp *) = { | |
45c0fd36 | 752 | #define DO(string) mp_bit##string##2c, |
f09e814a | 753 | MPX_DOBIN(DO) |
754 | #undef DO | |
755 | }; | |
756 | int ok = 1; | |
757 | unsigned op = 0; | |
758 | mp *a = *(mp **)v[1].buf; | |
759 | mp *b = *(mp **)v[2].buf; | |
760 | mp *r = *(mp **)v[3].buf; | |
761 | mp *c; | |
45c0fd36 | 762 | |
f09e814a | 763 | if (strcmp(v[0].buf, "and") == 0) op = 1; |
764 | else if (strcmp(v[0].buf, "or") == 0) op = 7; | |
765 | else if (strcmp(v[0].buf, "nand") == 0) op = 14; | |
766 | else if (strcmp(v[0].buf, "nor") == 0) op = 8; | |
767 | else if (strcmp(v[0].buf, "xor") == 0) op = 6; | |
768 | else { | |
769 | char *p = v[0].buf; | |
770 | while (*p) { | |
771 | op <<= 1; | |
772 | if (*p++ == '1') | |
773 | op |= 1; | |
774 | } | |
775 | } | |
776 | ||
777 | c = fn[op](MP_NEW, a, b); | |
778 | ok = verify(v[0].buf, r, c, a, b); | |
779 | mp_drop(a); mp_drop(b); mp_drop(r); mp_drop(c); | |
780 | assert(mparena_count(MPARENA_GLOBAL) == 0); | |
781 | return (ok); | |
782 | } | |
783 | ||
09d00c6b | 784 | static int tset(dstr *v) |
785 | { | |
786 | mp *a = *(mp **)v[0].buf; | |
787 | unsigned long n = *(unsigned long *)v[1].buf; | |
788 | mp *r = *(mp **)v[2].buf; | |
789 | mp *c; | |
790 | int ok = 1; | |
791 | ||
792 | c = mp_setbit2c(MP_NEW, a, n); | |
793 | if (!MP_EQ(c, r)) { | |
794 | ok = 0; | |
795 | fprintf(stderr, "\n***setbit (set) failed"); | |
796 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); | |
797 | fprintf(stderr, "\n*** n = %lu", n); | |
798 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); | |
799 | fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16); | |
800 | fputc('\n', stderr); | |
801 | } | |
802 | if (!mp_testbit2c(r, n)) { | |
803 | ok = 0; | |
804 | fprintf(stderr, "\n***setbit (test) failed"); | |
805 | fprintf(stderr, "\n*** n = %lu", n); | |
806 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); | |
807 | fputc('\n', stderr); | |
808 | } | |
809 | mp_drop(a); | |
810 | mp_drop(r); | |
811 | mp_drop(c); | |
812 | assert(mparena_count(MPARENA_GLOBAL) == 0); | |
813 | return (ok); | |
814 | } | |
815 | ||
816 | static int tclr(dstr *v) | |
817 | { | |
818 | mp *a = *(mp **)v[0].buf; | |
819 | unsigned long n = *(unsigned long *)v[1].buf; | |
820 | mp *r = *(mp **)v[2].buf; | |
821 | mp *c; | |
822 | int ok = 1; | |
823 | ||
824 | c = mp_clearbit2c(MP_NEW, a, n); | |
825 | if (!MP_EQ(c, r)) { | |
826 | ok = 0; | |
827 | fprintf(stderr, "\n***clrbit (set) failed"); | |
828 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 16); | |
829 | fprintf(stderr, "\n*** n = %lu", n); | |
830 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); | |
831 | fputs("\n*** c = ", stderr); mp_writefile(c, stderr, 16); | |
832 | fputc('\n', stderr); | |
833 | } | |
834 | if (mp_testbit2c(r, n)) { | |
835 | ok = 0; | |
836 | fprintf(stderr, "\n***clrbit (test) failed"); | |
837 | fprintf(stderr, "\n*** n = %lu", n); | |
838 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 16); | |
839 | fputc('\n', stderr); | |
840 | } | |
841 | mp_drop(a); | |
842 | mp_drop(c); | |
843 | mp_drop(r); | |
844 | assert(mparena_count(MPARENA_GLOBAL) == 0); | |
845 | return (ok); | |
846 | } | |
847 | ||
397041a9 | 848 | static int tneg(dstr *v) |
849 | { | |
850 | mp *a = *(mp **)v[0].buf; | |
851 | mp *r = *(mp **)v[1].buf; | |
852 | int ok = 1; | |
853 | mp *n = mp_neg(MP_NEW, a); | |
854 | if (!MP_EQ(r, n)) { | |
855 | ok = 0; | |
856 | fprintf(stderr, "\n*** neg failed\n"); | |
857 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); | |
858 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10); | |
859 | fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10); | |
860 | fputc('\n', stderr); | |
861 | } | |
862 | mp_drop(n); | |
863 | n = mp_neg(a, a); | |
864 | if (!MP_EQ(r, n)) { | |
865 | ok = 0; | |
866 | fprintf(stderr, "\n*** neg failed\n"); | |
867 | fputs("\n*** a* = ", stderr); mp_writefile(a, stderr, 10); | |
868 | fputs("\n*** r = ", stderr); mp_writefile(r, stderr, 10); | |
869 | fputs("\n*** n = ", stderr); mp_writefile(n, stderr, 10); | |
870 | fputc('\n', stderr); | |
871 | } | |
872 | mp_drop(a); | |
873 | mp_drop(r); | |
874 | assert(mparena_count(MPARENA_GLOBAL) == 0); | |
45c0fd36 | 875 | return (ok); |
397041a9 | 876 | } |
877 | ||
f1713c63 | 878 | static int todd(dstr *v) |
879 | { | |
880 | mp *a = *(mp **)v[0].buf; | |
881 | size_t rs = *(uint32 *)v[1].buf; | |
882 | mp *rt = *(mp **)v[2].buf; | |
883 | int ok = 1; | |
884 | mp *t; | |
885 | size_t s; | |
886 | t = mp_odd(MP_NEW, a, &s); | |
4b536f42 | 887 | if (s != rs || !MP_EQ(t, rt)) { |
f1713c63 | 888 | ok = 0; |
889 | fprintf(stderr, "\n*** odd failed"); | |
890 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); | |
891 | fprintf(stderr, "\n*** s = %lu", (unsigned long)s); | |
892 | fputs("\n*** t = ", stderr); mp_writefile(t, stderr, 10); | |
893 | fprintf(stderr, "\n*** rs = %lu", (unsigned long)rs); | |
894 | fputs("\n*** rt = ", stderr); mp_writefile(rt, stderr, 10); | |
895 | fputc('\n', stderr); | |
896 | } | |
897 | mp_drop(a); | |
898 | mp_drop(rt); | |
899 | mp_drop(t); | |
09d00c6b | 900 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
f1713c63 | 901 | return (ok); |
902 | } | |
903 | ||
d3409d5e | 904 | static test_chunk tests[] = { |
f09e814a | 905 | { "lsl", tlsl, { &type_mp, &type_int, &type_mp, 0 } }, |
906 | { "lsr", tlsr, { &type_mp, &type_int, &type_mp, 0 } }, | |
907 | { "lsl2c", tlsl2c, { &type_mp, &type_int, &type_mp, 0 } }, | |
908 | { "lsr2c", tlsr2c, { &type_mp, &type_int, &type_mp, 0 } }, | |
09d00c6b | 909 | { "setbit", tset, { &type_mp, &type_ulong, &type_mp, 0 } }, |
910 | { "clrbit", tclr, { &type_mp, &type_ulong, &type_mp, 0 } }, | |
d3409d5e | 911 | { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } }, |
912 | { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } }, | |
913 | { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } }, | |
914 | { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, | |
f4535c64 | 915 | { "exp", texp, { &type_mp, &type_mp, &type_mp, 0 } }, |
f09e814a | 916 | { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } }, |
f1713c63 | 917 | { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } }, |
397041a9 | 918 | { "neg", tneg, { &type_mp, &type_mp, 0 } }, |
d3409d5e | 919 | { 0, 0, { 0 } }, |
920 | }; | |
921 | ||
922 | int main(int argc, char *argv[]) | |
923 | { | |
924 | sub_init(); | |
925 | test_run(argc, argv, tests, SRCDIR "/tests/mp"); | |
926 | return (0); | |
927 | } | |
928 | ||
929 | #endif | |
930 | ||
931 | /*----- That's all, folks -------------------------------------------------*/ |