Commit | Line | Data |
---|---|---|
d03ab969 | 1 | /* -*-c-*- |
d03ab969 | 2 | * |
3 | * Low-level multiprecision arithmetic | |
4 | * | |
5 | * (c) 1999 Straylight/Edgeware | |
6 | */ | |
7 | ||
45c0fd36 | 8 | /*----- Licensing notice --------------------------------------------------* |
d03ab969 | 9 | * |
10 | * This file is part of Catacomb. | |
11 | * | |
12 | * Catacomb is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU Library General Public License as | |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
45c0fd36 | 16 | * |
d03ab969 | 17 | * Catacomb is distributed in the hope that it will be useful, |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU Library General Public License for more details. | |
45c0fd36 | 21 | * |
d03ab969 | 22 | * You should have received a copy of the GNU Library General Public |
23 | * License along with Catacomb; if not, write to the Free | |
24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | |
25 | * MA 02111-1307, USA. | |
26 | */ | |
27 | ||
d03ab969 | 28 | /*----- Header files ------------------------------------------------------*/ |
29 | ||
444083ae MW |
30 | #include "config.h" |
31 | ||
c8a2f9ef | 32 | #include <assert.h> |
d03ab969 | 33 | #include <stdio.h> |
34 | #include <stdlib.h> | |
35 | #include <string.h> | |
36 | ||
37 | #include <mLib/bits.h> | |
23bbea75 | 38 | #include <mLib/macros.h> |
d03ab969 | 39 | |
444083ae | 40 | #include "dispatch.h" |
d03ab969 | 41 | #include "mptypes.h" |
42 | #include "mpx.h" | |
75263f25 | 43 | #include "bitops.h" |
d03ab969 | 44 | |
45 | /*----- Loading and storing -----------------------------------------------*/ | |
46 | ||
0c9ebe47 MW |
47 | /* --- These are all variations on a theme --- * |
48 | * | |
49 | * Essentially we want to feed bits into a shift register, @ibits@ bits at a | |
50 | * time, and extract them @obits@ bits at a time whenever there are enough. | |
51 | * Of course, @i@ and @o@ will, in general, be different sizes, and we don't | |
52 | * necessarily know which is larger. | |
53 | * | |
54 | * During an operation, we have a shift register @w@ and a most-recent input | |
55 | * @t@. Together, these hold @bits@ significant bits of input. We arrange | |
56 | * that @bits < ibits + obits <= 2*MPW_BITS@, so we can get away with using | |
57 | * an @mpw@ for both of these quantitities. | |
58 | */ | |
59 | ||
60 | /* --- @MPX_GETBITS@ --- * | |
61 | * | |
62 | * Arguments: @ibits@ = width of input units, in bits | |
63 | * @obits@ = width of output units, in bits | |
64 | * @iavail@ = condition expression: is input data available? | |
65 | * @getbits@ = function or macro: set argument to next input | |
66 | * | |
67 | * Use: Read an input unit into @t@ and update the necessary | |
68 | * variables. | |
69 | * | |
70 | * It is assumed on entry that @bits < obits@. On exit, we have | |
71 | * @bits < ibits + obits@, and @t@ is live. | |
72 | */ | |
73 | ||
74 | #define MPX_GETBITS(ibits, obits, iavail, getbits) do { \ | |
75 | if (!iavail) goto flush; \ | |
76 | if (bits >= ibits) w |= t << (bits - ibits); \ | |
77 | getbits(t); \ | |
78 | bits += ibits; \ | |
79 | } while (0) | |
80 | ||
81 | /* --- @MPX_PUTBITS@ --- * | |
82 | * | |
83 | * Arguments: @ibits@ = width of input units, in bits | |
84 | * @obits@ = width of output units, in bits | |
85 | * @oavail@ = condition expression: is output space available? | |
86 | * @putbits@ = function or macro: write its argument to output | |
87 | * | |
88 | * Use: Emit an output unit, and update the necessary variables. If | |
89 | * the output buffer is full, then force an immediate return. | |
90 | * | |
91 | * We assume that @bits < ibits + obits@, and that @t@ is only | |
92 | * relevant if @bits >= ibits@. (The @MPX_GETBITS@ macro | |
93 | * ensures that this is true.) | |
94 | */ | |
95 | ||
96 | #define SHRW(w, b) ((b) < MPW_BITS ? (w) >> (b) : 0) | |
97 | ||
98 | #define MPX_PUTBITS(ibits, obits, oavail, putbits) do { \ | |
99 | if (!oavail) return; \ | |
100 | if (bits < ibits) { \ | |
101 | putbits(w); \ | |
102 | bits -= obits; \ | |
103 | w = SHRW(w, obits); \ | |
104 | } else { \ | |
105 | putbits(w | (t << (bits - ibits))); \ | |
106 | bits -= obits; \ | |
107 | if (bits >= ibits) w = SHRW(w, obits) | (t << (bits - ibits)); \ | |
108 | else w = SHRW(w, obits) | (t >> (ibits - bits)); \ | |
109 | t = 0; \ | |
110 | } \ | |
111 | } while (0) | |
112 | ||
113 | /* --- @MPX_LOADSTORE@ --- * | |
114 | * | |
115 | * Arguments: @name@ = name of function to create, without @mpx_@ prefix | |
116 | * @wconst@ = qualifiers for @mpw *@ arguments | |
117 | * @oconst@ = qualifiers for octet pointers | |
118 | * @decls@ = additional declarations needed | |
119 | * @ibits@ = width of input units, in bits | |
120 | * @iavail@ = condition expression: is input data available? | |
121 | * @getbits@ = function or macro: set argument to next input | |
122 | * @obits@ = width of output units, in bits | |
123 | * @oavail@ = condition expression: is output space available? | |
124 | * @putbits@ = function or macro: write its argument to output | |
125 | * @clear@ = statements to clear remainder of output | |
126 | * | |
127 | * Use: Generates a function to convert between a sequence of | |
128 | * multiprecision words and a vector of octets. | |
129 | * | |
130 | * The arguments @ibits@, @iavail@ and @getbits@ are passed on | |
131 | * to @MPX_GETBITS@; similarly, @obits@, @oavail@, and @putbits@ | |
132 | * are passed on to @MPX_PUTBITS@. | |
133 | * | |
134 | * The following variables are in scope: @v@ and @vl are the | |
135 | * current base and limit of the word vector; @p@ and @q@ are | |
136 | * the base and limit of the octet vector; @w@ and @t@ form the | |
137 | * shift register used during the conversion (see commentary | |
138 | * above); and @bits@ tracks the number of live bits in the | |
139 | * shift register. | |
140 | */ | |
141 | ||
142 | #define MPX_LOADSTORE(name, wconst, oconst, decls, \ | |
143 | ibits, iavail, getbits, obits, oavail, putbits, \ | |
144 | clear) \ | |
145 | \ | |
146 | void mpx_##name(wconst mpw *v, wconst mpw *vl, \ | |
147 | oconst void *pp, size_t sz) \ | |
148 | { \ | |
149 | mpw t = 0, w = 0; \ | |
150 | oconst octet *p = pp, *q = p + sz; \ | |
151 | int bits = 0; \ | |
152 | decls \ | |
153 | \ | |
154 | for (;;) { \ | |
155 | while (bits < obits) MPX_GETBITS(ibits, obits, iavail, getbits); \ | |
156 | while (bits >= obits) MPX_PUTBITS(ibits, obits, oavail, putbits); \ | |
157 | } \ | |
158 | \ | |
159 | flush: \ | |
160 | while (bits > 0) MPX_PUTBITS(ibits, obits, oavail, putbits); \ | |
161 | clear; \ | |
162 | } | |
163 | ||
164 | #define EMPTY | |
165 | ||
166 | /* --- Macros for @getbits@ and @putbits@ --- */ | |
167 | ||
168 | #define GETMPW(t) do { t = *v++; } while (0) | |
169 | #define PUTMPW(x) do { *v++ = MPW(x); } while (0) | |
170 | ||
171 | #define GETOCTETI(t) do { t = *p++; } while (0) | |
172 | #define PUTOCTETD(x) do { *--q = U8(x); } while (0) | |
173 | ||
174 | #define PUTOCTETI(x) do { *p++ = U8(x); } while (0) | |
175 | #define GETOCTETD(t) do { t = *--q; } while (0) | |
176 | ||
177 | /* --- Machinery for two's complement I/O --- */ | |
178 | ||
179 | #define DECL_2CN \ | |
180 | unsigned c = 1; | |
181 | ||
182 | #define GETMPW_2CN(t) do { \ | |
183 | t = MPW(~*v++ + c); \ | |
184 | c = c && !t; \ | |
185 | } while (0) | |
186 | ||
187 | #define PUTMPW_2CN(t) do { \ | |
188 | mpw _t = MPW(~(t) + c); \ | |
189 | c = c && !_t; \ | |
190 | *v++ = _t; \ | |
191 | } while (0) | |
192 | ||
193 | #define FLUSHW_2CN do { \ | |
194 | if (c) MPX_ONE(v, vl); \ | |
195 | else MPX_ZERO(v, vl); \ | |
196 | } while (0) | |
197 | ||
198 | #define FLUSHO_2CN do { \ | |
199 | memset(p, c ? 0xff : 0, q - p); \ | |
200 | } while (0) | |
201 | ||
d03ab969 | 202 | /* --- @mpx_storel@ --- * |
203 | * | |
204 | * Arguments: @const mpw *v, *vl@ = base and limit of source vector | |
c8a2f9ef | 205 | * @void *pp@ = pointer to octet array |
d03ab969 | 206 | * @size_t sz@ = size of octet array |
207 | * | |
208 | * Returns: --- | |
209 | * | |
210 | * Use: Stores an MP in an octet array, least significant octet | |
211 | * first. High-end octets are silently discarded if there | |
212 | * isn't enough space for them. | |
213 | */ | |
214 | ||
0c9ebe47 MW |
215 | MPX_LOADSTORE(storel, const, EMPTY, EMPTY, |
216 | MPW_BITS, (v < vl), GETMPW, | |
217 | 8, (p < q), PUTOCTETI, | |
218 | { memset(p, 0, q - p); }) | |
d03ab969 | 219 | |
220 | /* --- @mpx_loadl@ --- * | |
221 | * | |
222 | * Arguments: @mpw *v, *vl@ = base and limit of destination vector | |
c8a2f9ef | 223 | * @const void *pp@ = pointer to octet array |
d03ab969 | 224 | * @size_t sz@ = size of octet array |
225 | * | |
226 | * Returns: --- | |
227 | * | |
228 | * Use: Loads an MP in an octet array, least significant octet | |
229 | * first. High-end octets are ignored if there isn't enough | |
230 | * space for them. | |
231 | */ | |
232 | ||
0c9ebe47 MW |
233 | MPX_LOADSTORE(loadl, EMPTY, const, EMPTY, |
234 | 8, (p < q), GETOCTETI, | |
235 | MPW_BITS, (v < vl), PUTMPW, | |
236 | { MPX_ZERO(v, vl); }) | |
237 | ||
d03ab969 | 238 | |
239 | /* --- @mpx_storeb@ --- * | |
240 | * | |
241 | * Arguments: @const mpw *v, *vl@ = base and limit of source vector | |
c8a2f9ef | 242 | * @void *pp@ = pointer to octet array |
d03ab969 | 243 | * @size_t sz@ = size of octet array |
244 | * | |
245 | * Returns: --- | |
246 | * | |
247 | * Use: Stores an MP in an octet array, most significant octet | |
248 | * first. High-end octets are silently discarded if there | |
249 | * isn't enough space for them. | |
250 | */ | |
251 | ||
0c9ebe47 MW |
252 | MPX_LOADSTORE(storeb, const, EMPTY, EMPTY, |
253 | MPW_BITS, (v < vl), GETMPW, | |
254 | 8, (p < q), PUTOCTETD, | |
255 | { memset(p, 0, q - p); }) | |
d03ab969 | 256 | |
257 | /* --- @mpx_loadb@ --- * | |
258 | * | |
259 | * Arguments: @mpw *v, *vl@ = base and limit of destination vector | |
c8a2f9ef | 260 | * @const void *pp@ = pointer to octet array |
d03ab969 | 261 | * @size_t sz@ = size of octet array |
262 | * | |
263 | * Returns: --- | |
264 | * | |
265 | * Use: Loads an MP in an octet array, most significant octet | |
266 | * first. High-end octets are ignored if there isn't enough | |
267 | * space for them. | |
268 | */ | |
269 | ||
0c9ebe47 MW |
270 | MPX_LOADSTORE(loadb, EMPTY, const, EMPTY, |
271 | 8, (p < q), GETOCTETD, | |
272 | MPW_BITS, (v < vl), PUTMPW, | |
273 | { MPX_ZERO(v, vl); }) | |
d03ab969 | 274 | |
f09e814a | 275 | /* --- @mpx_storel2cn@ --- * |
276 | * | |
277 | * Arguments: @const mpw *v, *vl@ = base and limit of source vector | |
278 | * @void *pp@ = pointer to octet array | |
279 | * @size_t sz@ = size of octet array | |
280 | * | |
281 | * Returns: --- | |
282 | * | |
283 | * Use: Stores a negative MP in an octet array, least significant | |
284 | * octet first, as two's complement. High-end octets are | |
285 | * silently discarded if there isn't enough space for them. | |
286 | * This obviously makes the output bad. | |
287 | */ | |
288 | ||
0c9ebe47 MW |
289 | MPX_LOADSTORE(storel2cn, const, EMPTY, DECL_2CN, |
290 | MPW_BITS, (v < vl), GETMPW_2CN, | |
291 | 8, (p < q), PUTOCTETI, | |
292 | { FLUSHO_2CN; }) | |
f09e814a | 293 | |
294 | /* --- @mpx_loadl2cn@ --- * | |
295 | * | |
296 | * Arguments: @mpw *v, *vl@ = base and limit of destination vector | |
297 | * @const void *pp@ = pointer to octet array | |
298 | * @size_t sz@ = size of octet array | |
299 | * | |
300 | * Returns: --- | |
301 | * | |
302 | * Use: Loads a negative MP in an octet array, least significant | |
303 | * octet first, as two's complement. High-end octets are | |
304 | * ignored if there isn't enough space for them. This probably | |
305 | * means you made the wrong choice coming here. | |
306 | */ | |
307 | ||
0c9ebe47 MW |
308 | MPX_LOADSTORE(loadl2cn, EMPTY, const, DECL_2CN, |
309 | 8, (p < q), GETOCTETI, | |
310 | MPW_BITS, (v < vl), PUTMPW_2CN, | |
311 | { FLUSHW_2CN; }) | |
f09e814a | 312 | |
313 | /* --- @mpx_storeb2cn@ --- * | |
314 | * | |
315 | * Arguments: @const mpw *v, *vl@ = base and limit of source vector | |
316 | * @void *pp@ = pointer to octet array | |
317 | * @size_t sz@ = size of octet array | |
318 | * | |
319 | * Returns: --- | |
320 | * | |
321 | * Use: Stores a negative MP in an octet array, most significant | |
322 | * octet first, as two's complement. High-end octets are | |
323 | * silently discarded if there isn't enough space for them, | |
324 | * which probably isn't what you meant. | |
325 | */ | |
326 | ||
0c9ebe47 MW |
327 | MPX_LOADSTORE(storeb2cn, const, EMPTY, DECL_2CN, |
328 | MPW_BITS, (v < vl), GETMPW_2CN, | |
329 | 8, (p < q), PUTOCTETD, | |
330 | { FLUSHO_2CN; }) | |
f09e814a | 331 | |
332 | /* --- @mpx_loadb2cn@ --- * | |
333 | * | |
334 | * Arguments: @mpw *v, *vl@ = base and limit of destination vector | |
335 | * @const void *pp@ = pointer to octet array | |
336 | * @size_t sz@ = size of octet array | |
337 | * | |
338 | * Returns: --- | |
339 | * | |
340 | * Use: Loads a negative MP in an octet array, most significant octet | |
341 | * first as two's complement. High-end octets are ignored if | |
342 | * there isn't enough space for them. This probably means you | |
343 | * chose this function wrongly. | |
344 | */ | |
345 | ||
0c9ebe47 MW |
346 | MPX_LOADSTORE(loadb2cn, EMPTY, const, DECL_2CN, |
347 | 8, (p < q), GETOCTETD, | |
348 | MPW_BITS, (v < vl), PUTMPW_2CN, | |
349 | { FLUSHW_2CN; }) | |
f09e814a | 350 | |
d03ab969 | 351 | /*----- Logical shifting --------------------------------------------------*/ |
352 | ||
5ee480b5 | 353 | /* --- @MPX_SHIFT1@ --- * |
d03ab969 | 354 | * |
5ee480b5 MW |
355 | * Arguments: @init@ = initial accumulator value |
356 | * @out@ = expression to store in each output word | |
357 | * @next@ = expression for next accumulator value | |
d03ab969 | 358 | * |
5ee480b5 MW |
359 | * Use: Performs a single-position shift. The input is scanned |
360 | * right-to-left. In the expressions @out@ and @next@, the | |
361 | * accumulator is available in @w@ and the current input word is | |
362 | * in @t@. | |
d03ab969 | 363 | * |
5ee480b5 MW |
364 | * This macro is intended to be used in the @shift1@ argument of |
365 | * @MPX_SHIFTOP@, and expects variables describing the operation | |
366 | * to be set up accordingly. | |
d03ab969 | 367 | */ |
368 | ||
5ee480b5 MW |
369 | #define MPX_SHIFT1(init, out, next) do { \ |
370 | mpw t, w = (init); \ | |
371 | while (av < avl) { \ | |
372 | if (dv >= dvl) break; \ | |
373 | t = MPW(*av++); \ | |
374 | *dv++ = (out); \ | |
375 | w = (next); \ | |
376 | } \ | |
377 | if (dv < dvl) { *dv++ = MPW(w); MPX_ZERO(dv, dvl); } \ | |
378 | } while (0) | |
379 | ||
380 | /* --- @MPX_SHIFTW@ --- * | |
381 | * | |
382 | * Arguments: @max@ = the maximum shift (in words) which is nontrivial | |
383 | * @clear@ = function (or macro) to clear low-order output words | |
384 | * @copy@ = statement to copy words from input to output | |
385 | * | |
386 | * Use: Performs a shift by a whole number of words. If the shift | |
387 | * amount is @max@ or more words, then the destination is | |
388 | * @clear@ed entirely; otherwise, @copy@ is executed. | |
389 | * | |
390 | * This macro is intended to be used in the @shiftw@ argument of | |
391 | * @MPX_SHIFTOP@, and expects variables describing the operation | |
392 | * to be set up accordingly. | |
393 | */ | |
d03ab969 | 394 | |
5ee480b5 MW |
395 | #define MPX_SHIFTW(max, clear, copy) do { \ |
396 | if (nw >= (max)) clear(dv, dvl); \ | |
397 | else copy \ | |
398 | } while (0) | |
d03ab969 | 399 | |
5ee480b5 MW |
400 | /* --- @MPX_SHIFTOP@ --- * |
401 | * | |
402 | * Arguments: @name@ = name of function to define (without `@mpx_@' prefix) | |
403 | * @shift1@ = statement to shift by a single bit | |
404 | * @shiftw@ = statement to shift by a whole number of words | |
405 | * @shift@ = statement to perform a general shift | |
406 | * | |
407 | * Use: Emits a shift operation. The input is @av@..@avl@; the | |
408 | * output is @dv@..@dvl@; and the shift amount (in bits) is | |
409 | * @n@. In @shiftw@ and @shift@, @nw@ and @nb@ are set up such | |
410 | * that @n = nw*MPW_BITS + nb@ and @nb < MPW_BITS@. | |
411 | */ | |
d03ab969 | 412 | |
5ee480b5 MW |
413 | #define MPX_SHIFTOP(name, shift1, shiftw, shift) \ |
414 | \ | |
415 | void mpx_##name(mpw *dv, mpw *dvl, \ | |
416 | const mpw *av, const mpw *avl, \ | |
417 | size_t n) \ | |
418 | { \ | |
419 | \ | |
420 | if (n == 0) \ | |
421 | MPX_COPY(dv, dvl, av, avl); \ | |
422 | else if (n == 1) \ | |
423 | do shift1 while (0); \ | |
424 | else { \ | |
425 | size_t nw = n/MPW_BITS; \ | |
426 | unsigned nb = n%MPW_BITS; \ | |
427 | if (!nb) do shiftw while (0); \ | |
428 | else do shift while (0); \ | |
429 | } \ | |
430 | } | |
d03ab969 | 431 | |
5ee480b5 MW |
432 | /* --- @MPX_SHIFT_LEFT@ --- * |
433 | * | |
434 | * Arguments: @name@ = name of function to define (without `@mpx_@' prefix) | |
435 | * @init1@ = initializer for single-bit shift accumulator | |
436 | * @clear@ = function (or macro) to clear low-order output words | |
437 | * @flush@ = expression for low-order nontrivial output word | |
438 | * | |
439 | * Use: Emits a left-shift operation. This expands to a call on | |
440 | * @MPX_SHIFTOP@, but implements the complicated @shift@ | |
441 | * statement. | |
442 | * | |
443 | * The @init1@ argument is as for @MPX_SHIFT1@, and @clear@ is | |
444 | * as for @MPX_SHIFTW@ (though is used elsewhere). In a general | |
445 | * shift, @nw@ whole low-order output words are set using | |
446 | * @clear@; high-order words are zeroed; and the remaining words | |
447 | * set with a left-to-right pass across the input; at the end of | |
448 | * the operation, the least significant output word above those | |
449 | * @clear@ed is set using @flush@, which may use the accumulator | |
450 | * @w@ = @av[0] << nb@. | |
451 | */ | |
d03ab969 | 452 | |
5ee480b5 MW |
453 | #define MPX_SHIFT_LEFT(name, init1, clear, flush) \ |
454 | MPX_SHIFTOP(name, { \ | |
455 | MPX_SHIFT1(init1, \ | |
456 | w | (t << 1), \ | |
457 | t >> (MPW_BITS - 1)); \ | |
458 | }, { \ | |
459 | MPX_SHIFTW(dvl - dv, clear, { \ | |
460 | MPX_COPY(dv + nw, dvl, av, avl); \ | |
461 | clear(dv, dv + nw); \ | |
462 | }); \ | |
463 | }, { \ | |
464 | size_t nr = MPW_BITS - nb; \ | |
465 | size_t dvn = dvl - dv; \ | |
466 | size_t avn = avl - av; \ | |
467 | mpw w; \ | |
468 | \ | |
469 | if (dvn <= nw) { \ | |
470 | clear(dv, dvl); \ | |
471 | break; \ | |
472 | } \ | |
473 | \ | |
474 | if (dvn <= avn + nw) { \ | |
475 | avl = av + dvn - nw; \ | |
476 | w = *--avl << nb; \ | |
477 | } else { \ | |
478 | size_t off = avn + nw + 1; \ | |
479 | MPX_ZERO(dv + off, dvl); \ | |
480 | dvl = dv + off; \ | |
481 | w = 0; \ | |
482 | } \ | |
483 | \ | |
484 | while (avl > av) { \ | |
485 | mpw t = *--avl; \ | |
486 | *--dvl = MPW(w | (t >> nr)); \ | |
487 | w = t << nb; \ | |
488 | } \ | |
489 | \ | |
490 | *--dvl = MPW(flush); \ | |
491 | clear(dv, dvl); \ | |
492 | }) | |
c8a2f9ef | 493 | |
5ee480b5 MW |
494 | /* --- @mpx_lsl@ --- * |
495 | * | |
496 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit | |
497 | * @const mpw *av, *avl@ = source vector base and limit | |
498 | * @size_t n@ = number of bit positions to shift by | |
499 | * | |
500 | * Returns: --- | |
501 | * | |
502 | * Use: Performs a logical shift left operation on an integer. | |
503 | */ | |
d03ab969 | 504 | |
5ee480b5 | 505 | MPX_SHIFT_LEFT(lsl, 0, MPX_ZERO, w) |
d03ab969 | 506 | |
81578196 | 507 | /* --- @mpx_lslc@ --- * |
508 | * | |
509 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit | |
510 | * @const mpw *av, *avl@ = source vector base and limit | |
511 | * @size_t n@ = number of bit positions to shift by | |
512 | * | |
513 | * Returns: --- | |
514 | * | |
515 | * Use: Performs a logical shift left operation on an integer, only | |
516 | * it fills in the bits with ones instead of zeroes. | |
517 | */ | |
518 | ||
5ee480b5 | 519 | MPX_SHIFT_LEFT(lslc, 1, MPX_ONE, w | (MPW_MAX >> nr)) |
81578196 | 520 | |
d03ab969 | 521 | /* --- @mpx_lsr@ --- * |
522 | * | |
523 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit | |
524 | * @const mpw *av, *avl@ = source vector base and limit | |
525 | * @size_t n@ = number of bit positions to shift by | |
526 | * | |
527 | * Returns: --- | |
528 | * | |
529 | * Use: Performs a logical shift right operation on an integer. | |
530 | */ | |
531 | ||
5ee480b5 MW |
532 | MPX_SHIFTOP(lsr, { |
533 | MPX_SHIFT1(av < avl ? *av++ >> 1 : 0, | |
534 | w | (t << (MPW_BITS - 1)), | |
535 | t >> 1); | |
536 | }, { | |
537 | MPX_SHIFTW(avl - av, MPX_ZERO, | |
538 | { MPX_COPY(dv, dvl, av + nw, avl); }); | |
539 | }, { | |
540 | size_t nr = MPW_BITS - nb; | |
541 | mpw w; | |
542 | ||
543 | av += nw; | |
544 | w = av < avl ? *av++ : 0; | |
545 | while (av < avl) { | |
546 | mpw t; | |
547 | if (dv >= dvl) goto done; | |
548 | t = *av++; | |
549 | *dv++ = MPW((w >> nb) | (t << nr)); | |
550 | w = t; | |
d03ab969 | 551 | } |
5ee480b5 MW |
552 | if (dv < dvl) { |
553 | *dv++ = MPW(w >> nb); | |
554 | MPX_ZERO(dv, dvl); | |
d03ab969 | 555 | } |
d03ab969 | 556 | done:; |
5ee480b5 | 557 | }) |
d03ab969 | 558 | |
0f32e0f8 | 559 | /*----- Bitwise operations ------------------------------------------------*/ |
560 | ||
f09e814a | 561 | /* --- @mpx_bitop@ --- * |
0f32e0f8 | 562 | * |
563 | * Arguments: @mpw *dv, *dvl@ = destination vector | |
564 | * @const mpw *av, *avl@ = first source vector | |
565 | * @const mpw *bv, *bvl@ = second source vector | |
566 | * | |
567 | * Returns: --- | |
568 | * | |
f09e814a | 569 | * Use; Provides the dyadic boolean functions. |
0f32e0f8 | 570 | */ |
571 | ||
f09e814a | 572 | #define MPX_BITBINOP(string) \ |
0f32e0f8 | 573 | \ |
f09e814a | 574 | void mpx_bit##string(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, \ |
575 | const mpw *bv, const mpw *bvl) \ | |
0f32e0f8 | 576 | { \ |
577 | MPX_SHRINK(av, avl); \ | |
578 | MPX_SHRINK(bv, bvl); \ | |
579 | \ | |
580 | while (dv < dvl) { \ | |
581 | mpw a, b; \ | |
582 | a = (av < avl) ? *av++ : 0; \ | |
583 | b = (bv < bvl) ? *bv++ : 0; \ | |
75263f25 | 584 | *dv++ = B##string(a, b); \ |
23bbea75 | 585 | IGNORE(a); IGNORE(b); \ |
0f32e0f8 | 586 | } \ |
587 | } | |
588 | ||
f09e814a | 589 | MPX_DOBIN(MPX_BITBINOP) |
0f32e0f8 | 590 | |
591 | void mpx_not(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl) | |
592 | { | |
593 | MPX_SHRINK(av, avl); | |
594 | ||
595 | while (dv < dvl) { | |
596 | mpw a; | |
597 | a = (av < avl) ? *av++ : 0; | |
598 | *dv++ = ~a; | |
599 | } | |
600 | } | |
601 | ||
d03ab969 | 602 | /*----- Unsigned arithmetic -----------------------------------------------*/ |
603 | ||
f45a00c6 | 604 | /* --- @mpx_2c@ --- * |
605 | * | |
606 | * Arguments: @mpw *dv, *dvl@ = destination vector | |
607 | * @const mpw *v, *vl@ = source vector | |
608 | * | |
609 | * Returns: --- | |
610 | * | |
611 | * Use: Calculates the two's complement of @v@. | |
612 | */ | |
613 | ||
614 | void mpx_2c(mpw *dv, mpw *dvl, const mpw *v, const mpw *vl) | |
615 | { | |
616 | mpw c = 0; | |
617 | while (dv < dvl && v < vl) | |
618 | *dv++ = c = MPW(~*v++); | |
619 | if (dv < dvl) { | |
620 | if (c > MPW_MAX / 2) | |
621 | c = MPW(~0); | |
622 | while (dv < dvl) | |
623 | *dv++ = c; | |
624 | } | |
625 | MPX_UADDN(dv, dvl, 1); | |
626 | } | |
627 | ||
1a05a8ef | 628 | /* --- @mpx_ueq@ --- * |
629 | * | |
630 | * Arguments: @const mpw *av, *avl@ = first argument vector base and limit | |
631 | * @const mpw *bv, *bvl@ = second argument vector base and limit | |
632 | * | |
633 | * Returns: Nonzero if the two vectors are equal. | |
634 | * | |
635 | * Use: Performs an unsigned integer test for equality. | |
636 | */ | |
637 | ||
638 | int mpx_ueq(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl) | |
639 | { | |
640 | MPX_SHRINK(av, avl); | |
641 | MPX_SHRINK(bv, bvl); | |
642 | if (avl - av != bvl - bv) | |
643 | return (0); | |
644 | while (av < avl) { | |
645 | if (*av++ != *bv++) | |
646 | return (0); | |
647 | } | |
648 | return (1); | |
649 | } | |
650 | ||
d03ab969 | 651 | /* --- @mpx_ucmp@ --- * |
652 | * | |
653 | * Arguments: @const mpw *av, *avl@ = first argument vector base and limit | |
654 | * @const mpw *bv, *bvl@ = second argument vector base and limit | |
655 | * | |
656 | * Returns: Less than, equal to, or greater than zero depending on | |
657 | * whether @a@ is less than, equal to or greater than @b@, | |
658 | * respectively. | |
659 | * | |
660 | * Use: Performs an unsigned integer comparison. | |
661 | */ | |
662 | ||
663 | int mpx_ucmp(const mpw *av, const mpw *avl, const mpw *bv, const mpw *bvl) | |
664 | { | |
665 | MPX_SHRINK(av, avl); | |
666 | MPX_SHRINK(bv, bvl); | |
667 | ||
668 | if (avl - av > bvl - bv) | |
669 | return (+1); | |
670 | else if (avl - av < bvl - bv) | |
671 | return (-1); | |
672 | else while (avl > av) { | |
673 | mpw a = *--avl, b = *--bvl; | |
674 | if (a > b) | |
675 | return (+1); | |
676 | else if (a < b) | |
677 | return (-1); | |
678 | } | |
679 | return (0); | |
680 | } | |
1a05a8ef | 681 | |
d03ab969 | 682 | /* --- @mpx_uadd@ --- * |
683 | * | |
684 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit | |
685 | * @const mpw *av, *avl@ = first addend vector base and limit | |
686 | * @const mpw *bv, *bvl@ = second addend vector base and limit | |
687 | * | |
688 | * Returns: --- | |
689 | * | |
690 | * Use: Performs unsigned integer addition. If the result overflows | |
691 | * the destination vector, high-order bits are discarded. This | |
692 | * means that two's complement addition happens more or less for | |
693 | * free, although that's more a side-effect than anything else. | |
694 | * The result vector may be equal to either or both source | |
695 | * vectors, but may not otherwise overlap them. | |
696 | */ | |
697 | ||
698 | void mpx_uadd(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, | |
699 | const mpw *bv, const mpw *bvl) | |
700 | { | |
701 | mpw c = 0; | |
702 | ||
703 | while (av < avl || bv < bvl) { | |
704 | mpw a, b; | |
705 | mpd x; | |
706 | if (dv >= dvl) | |
707 | return; | |
708 | a = (av < avl) ? *av++ : 0; | |
709 | b = (bv < bvl) ? *bv++ : 0; | |
710 | x = (mpd)a + (mpd)b + c; | |
711 | *dv++ = MPW(x); | |
712 | c = x >> MPW_BITS; | |
713 | } | |
714 | if (dv < dvl) { | |
715 | *dv++ = c; | |
716 | MPX_ZERO(dv, dvl); | |
717 | } | |
718 | } | |
719 | ||
dd517851 | 720 | /* --- @mpx_uaddn@ --- * |
721 | * | |
722 | * Arguments: @mpw *dv, *dvl@ = source and destination base and limit | |
723 | * @mpw n@ = other addend | |
724 | * | |
725 | * Returns: --- | |
726 | * | |
727 | * Use: Adds a small integer to a multiprecision number. | |
728 | */ | |
729 | ||
730 | void mpx_uaddn(mpw *dv, mpw *dvl, mpw n) { MPX_UADDN(dv, dvl, n); } | |
731 | ||
f46efa79 | 732 | /* --- @mpx_uaddnlsl@ --- * |
733 | * | |
734 | * Arguments: @mpw *dv, *dvl@ = destination and first argument vector | |
735 | * @mpw a@ = second argument | |
736 | * @unsigned o@ = offset in bits | |
737 | * | |
738 | * Returns: --- | |
739 | * | |
740 | * Use: Computes %$d + 2^o a$%. If the result overflows then | |
741 | * high-order bits are discarded, as usual. We must have | |
742 | * @0 < o < MPW_BITS@. | |
743 | */ | |
744 | ||
745 | void mpx_uaddnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o) | |
746 | { | |
747 | mpd x = (mpd)a << o; | |
748 | ||
749 | while (x && dv < dvl) { | |
750 | x += *dv; | |
751 | *dv++ = MPW(x); | |
752 | x >>= MPW_BITS; | |
753 | } | |
754 | } | |
755 | ||
d03ab969 | 756 | /* --- @mpx_usub@ --- * |
757 | * | |
758 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit | |
759 | * @const mpw *av, *avl@ = first argument vector base and limit | |
760 | * @const mpw *bv, *bvl@ = second argument vector base and limit | |
761 | * | |
762 | * Returns: --- | |
763 | * | |
764 | * Use: Performs unsigned integer subtraction. If the result | |
765 | * overflows the destination vector, high-order bits are | |
766 | * discarded. This means that two's complement subtraction | |
767 | * happens more or less for free, althuogh that's more a side- | |
768 | * effect than anything else. The result vector may be equal to | |
769 | * either or both source vectors, but may not otherwise overlap | |
770 | * them. | |
771 | */ | |
772 | ||
773 | void mpx_usub(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, | |
774 | const mpw *bv, const mpw *bvl) | |
775 | { | |
776 | mpw c = 0; | |
777 | ||
778 | while (av < avl || bv < bvl) { | |
779 | mpw a, b; | |
780 | mpd x; | |
781 | if (dv >= dvl) | |
782 | return; | |
783 | a = (av < avl) ? *av++ : 0; | |
784 | b = (bv < bvl) ? *bv++ : 0; | |
c8a2f9ef | 785 | x = (mpd)a - (mpd)b - c; |
d03ab969 | 786 | *dv++ = MPW(x); |
c8a2f9ef | 787 | if (x >> MPW_BITS) |
788 | c = 1; | |
789 | else | |
790 | c = 0; | |
d03ab969 | 791 | } |
c8a2f9ef | 792 | if (c) |
793 | c = MPW_MAX; | |
d03ab969 | 794 | while (dv < dvl) |
c8a2f9ef | 795 | *dv++ = c; |
d03ab969 | 796 | } |
797 | ||
dd517851 | 798 | /* --- @mpx_usubn@ --- * |
799 | * | |
800 | * Arguments: @mpw *dv, *dvl@ = source and destination base and limit | |
801 | * @n@ = subtrahend | |
802 | * | |
803 | * Returns: --- | |
804 | * | |
805 | * Use: Subtracts a small integer from a multiprecision number. | |
806 | */ | |
807 | ||
808 | void mpx_usubn(mpw *dv, mpw *dvl, mpw n) { MPX_USUBN(dv, dvl, n); } | |
809 | ||
f46efa79 | 810 | /* --- @mpx_uaddnlsl@ --- * |
811 | * | |
812 | * Arguments: @mpw *dv, *dvl@ = destination and first argument vector | |
813 | * @mpw a@ = second argument | |
814 | * @unsigned o@ = offset in bits | |
815 | * | |
816 | * Returns: --- | |
817 | * | |
818 | * Use: Computes %$d + 2^o a$%. If the result overflows then | |
819 | * high-order bits are discarded, as usual. We must have | |
820 | * @0 < o < MPW_BITS@. | |
821 | */ | |
822 | ||
823 | void mpx_usubnlsl(mpw *dv, mpw *dvl, mpw a, unsigned o) | |
824 | { | |
825 | mpw b = a >> (MPW_BITS - o); | |
826 | a <<= o; | |
827 | ||
828 | if (dv < dvl) { | |
c29970a7 | 829 | mpd x = (mpd)*dv - MPW(a); |
f46efa79 | 830 | *dv++ = MPW(x); |
831 | if (x >> MPW_BITS) | |
832 | b++; | |
833 | MPX_USUBN(dv, dvl, b); | |
834 | } | |
835 | } | |
836 | ||
d03ab969 | 837 | /* --- @mpx_umul@ --- * |
838 | * | |
839 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit | |
840 | * @const mpw *av, *avl@ = multiplicand vector base and limit | |
841 | * @const mpw *bv, *bvl@ = multiplier vector base and limit | |
842 | * | |
843 | * Returns: --- | |
844 | * | |
845 | * Use: Performs unsigned integer multiplication. If the result | |
846 | * overflows the desination vector, high-order bits are | |
847 | * discarded. The result vector may not overlap the argument | |
848 | * vectors in any way. | |
849 | */ | |
850 | ||
444083ae MW |
851 | CPU_DISPATCH(EMPTY, (void), void, mpx_umul, |
852 | (mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, | |
853 | const mpw *bv, const mpw *bvl), | |
854 | (dv, dvl, av, avl, bv, bvl), pick_umul, simple_umul); | |
855 | ||
856 | static void simple_umul(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, | |
857 | const mpw *bv, const mpw *bvl) | |
d03ab969 | 858 | { |
859 | /* --- This is probably worthwhile on a multiply --- */ | |
860 | ||
861 | MPX_SHRINK(av, avl); | |
862 | MPX_SHRINK(bv, bvl); | |
863 | ||
864 | /* --- Deal with a multiply by zero --- */ | |
45c0fd36 | 865 | |
d03ab969 | 866 | if (bv == bvl) { |
c8a2f9ef | 867 | MPX_ZERO(dv, dvl); |
d03ab969 | 868 | return; |
869 | } | |
870 | ||
871 | /* --- Do the initial multiply and initialize the accumulator --- */ | |
872 | ||
873 | MPX_UMULN(dv, dvl, av, avl, *bv++); | |
874 | ||
875 | /* --- Do the remaining multiply/accumulates --- */ | |
876 | ||
c8a2f9ef | 877 | while (dv < dvl && bv < bvl) { |
d03ab969 | 878 | mpw m = *bv++; |
c8a2f9ef | 879 | mpw c = 0; |
d03ab969 | 880 | const mpw *avv = av; |
881 | mpw *dvv = ++dv; | |
882 | ||
883 | while (avv < avl) { | |
884 | mpd x; | |
885 | if (dvv >= dvl) | |
886 | goto next; | |
c8a2f9ef | 887 | x = (mpd)*dvv + (mpd)m * (mpd)*avv++ + c; |
888 | *dvv++ = MPW(x); | |
d03ab969 | 889 | c = x >> MPW_BITS; |
890 | } | |
c8a2f9ef | 891 | MPX_UADDN(dvv, dvl, c); |
d03ab969 | 892 | next:; |
893 | } | |
894 | } | |
895 | ||
444083ae MW |
896 | #define MAYBE_UMUL4(impl) \ |
897 | extern void mpx_umul4_##impl(mpw */*dv*/, \ | |
898 | const mpw */*av*/, const mpw */*avl*/, \ | |
899 | const mpw */*bv*/, const mpw */*bvl*/); \ | |
900 | static void maybe_umul4_##impl(mpw *dv, mpw *dvl, \ | |
901 | const mpw *av, const mpw *avl, \ | |
902 | const mpw *bv, const mpw *bvl) \ | |
903 | { \ | |
904 | size_t an = avl - av, bn = bvl - bv, dn = dvl - dv; \ | |
905 | if (!an || an%4 != 0 || !bn || bn%4 != 0 || dn < an + bn) \ | |
906 | simple_umul(dv, dvl, av, avl, bv, bvl); \ | |
907 | else { \ | |
908 | mpx_umul4_##impl(dv, av, avl, bv, bvl); \ | |
909 | MPX_ZERO(dv + an + bn, dvl); \ | |
910 | } \ | |
911 | } | |
912 | ||
913 | #if CPUFAM_X86 | |
914 | MAYBE_UMUL4(x86_sse2) | |
915 | #endif | |
916 | ||
3119b3ae MW |
917 | #if CPUFAM_AMD64 |
918 | MAYBE_UMUL4(amd64_sse2) | |
919 | #endif | |
920 | ||
444083ae MW |
921 | static mpx_umul__functype *pick_umul(void) |
922 | { | |
923 | #if CPUFAM_X86 | |
924 | DISPATCH_PICK_COND(mpx_umul, maybe_umul4_x86_sse2, | |
925 | cpu_feature_p(CPUFEAT_X86_SSE2)); | |
3119b3ae MW |
926 | #endif |
927 | #if CPUFAM_AMD64 | |
928 | DISPATCH_PICK_COND(mpx_umul, maybe_umul4_amd64_sse2, | |
929 | cpu_feature_p(CPUFEAT_X86_SSE2)); | |
444083ae MW |
930 | #endif |
931 | DISPATCH_PICK_FALLBACK(mpx_umul, simple_umul); | |
932 | } | |
933 | ||
dd517851 | 934 | /* --- @mpx_umuln@ --- * |
935 | * | |
936 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit | |
937 | * @const mpw *av, *avl@ = multiplicand vector base and limit | |
938 | * @mpw m@ = multiplier | |
939 | * | |
940 | * Returns: --- | |
941 | * | |
942 | * Use: Multiplies a multiprecision integer by a single-word value. | |
943 | * The destination and source may be equal. The destination | |
944 | * is completely cleared after use. | |
945 | */ | |
946 | ||
947 | void mpx_umuln(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m) | |
106b481c | 948 | { MPX_UMULN(dv, dvl, av, avl, m); } |
dd517851 | 949 | |
950 | /* --- @mpx_umlan@ --- * | |
951 | * | |
952 | * Arguments: @mpw *dv, *dvl@ = destination/accumulator base and limit | |
953 | * @const mpw *av, *avl@ = multiplicand vector base and limit | |
954 | * @mpw m@ = multiplier | |
955 | * | |
956 | * Returns: --- | |
957 | * | |
958 | * Use: Multiplies a multiprecision integer by a single-word value | |
959 | * and adds the result to an accumulator. | |
960 | */ | |
961 | ||
962 | void mpx_umlan(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl, mpw m) | |
106b481c | 963 | { MPX_UMLAN(dv, dvl, av, avl, m); } |
dd517851 | 964 | |
c8a2f9ef | 965 | /* --- @mpx_usqr@ --- * |
966 | * | |
967 | * Arguments: @mpw *dv, *dvl@ = destination vector base and limit | |
968 | * @const mpw *av, *av@ = source vector base and limit | |
969 | * | |
970 | * Returns: --- | |
971 | * | |
972 | * Use: Performs unsigned integer squaring. The result vector must | |
973 | * not overlap the source vector in any way. | |
974 | */ | |
975 | ||
976 | void mpx_usqr(mpw *dv, mpw *dvl, const mpw *av, const mpw *avl) | |
977 | { | |
978 | MPX_ZERO(dv, dvl); | |
979 | ||
980 | /* --- Main loop --- */ | |
981 | ||
982 | while (av < avl) { | |
983 | const mpw *avv = av; | |
984 | mpw *dvv = dv; | |
985 | mpw a = *av; | |
986 | mpd c; | |
987 | ||
988 | /* --- Stop if I've run out of destination --- */ | |
989 | ||
990 | if (dvv >= dvl) | |
991 | break; | |
992 | ||
993 | /* --- Work out the square at this point in the proceedings --- */ | |
994 | ||
995 | { | |
c8a2f9ef | 996 | mpd x = (mpd)a * (mpd)a + *dvv; |
997 | *dvv++ = MPW(x); | |
998 | c = MPW(x >> MPW_BITS); | |
999 | } | |
1000 | ||
1001 | /* --- Now fix up the rest of the vector upwards --- */ | |
1002 | ||
1003 | avv++; | |
1004 | while (dvv < dvl && avv < avl) { | |
c8a2f9ef | 1005 | mpd x = (mpd)a * (mpd)*avv++; |
1006 | mpd y = ((x << 1) & MPW_MAX) + c + *dvv; | |
1007 | c = (x >> (MPW_BITS - 1)) + (y >> MPW_BITS); | |
1008 | *dvv++ = MPW(y); | |
1009 | } | |
1010 | while (dvv < dvl && c) { | |
1011 | mpd x = c + *dvv; | |
1012 | *dvv++ = MPW(x); | |
1013 | c = x >> MPW_BITS; | |
1014 | } | |
1015 | ||
1016 | /* --- Get ready for the next round --- */ | |
1017 | ||
1018 | av++; | |
1019 | dv += 2; | |
1020 | } | |
1021 | } | |
1022 | ||
d03ab969 | 1023 | /* --- @mpx_udiv@ --- * |
1024 | * | |
1025 | * Arguments: @mpw *qv, *qvl@ = quotient vector base and limit | |
1026 | * @mpw *rv, *rvl@ = dividend/remainder vector base and limit | |
1027 | * @const mpw *dv, *dvl@ = divisor vector base and limit | |
c8a2f9ef | 1028 | * @mpw *sv, *svl@ = scratch workspace |
d03ab969 | 1029 | * |
1030 | * Returns: --- | |
1031 | * | |
1032 | * Use: Performs unsigned integer division. If the result overflows | |
1033 | * the quotient vector, high-order bits are discarded. (Clearly | |
1034 | * the remainder vector can't overflow.) The various vectors | |
1035 | * may not overlap in any way. Yes, I know it's a bit odd | |
1036 | * requiring the dividend to be in the result position but it | |
1037 | * does make some sense really. The remainder must have | |
c8a2f9ef | 1038 | * headroom for at least two extra words. The scratch space |
f45a00c6 | 1039 | * must be at least one word larger than the divisor. |
d03ab969 | 1040 | */ |
1041 | ||
1042 | void mpx_udiv(mpw *qv, mpw *qvl, mpw *rv, mpw *rvl, | |
c8a2f9ef | 1043 | const mpw *dv, const mpw *dvl, |
1044 | mpw *sv, mpw *svl) | |
d03ab969 | 1045 | { |
d03ab969 | 1046 | unsigned norm = 0; |
1047 | size_t scale; | |
1048 | mpw d, dd; | |
1049 | ||
1050 | /* --- Initialize the quotient --- */ | |
1051 | ||
1052 | MPX_ZERO(qv, qvl); | |
1053 | ||
c8a2f9ef | 1054 | /* --- Perform some sanity checks --- */ |
1055 | ||
1056 | MPX_SHRINK(dv, dvl); | |
1057 | assert(((void)"division by zero in mpx_udiv", dv < dvl)); | |
1058 | ||
d03ab969 | 1059 | /* --- Normalize the divisor --- * |
1060 | * | |
1061 | * The algorithm requires that the divisor be at least two digits long. | |
1062 | * This is easy to fix. | |
1063 | */ | |
1064 | ||
c8a2f9ef | 1065 | { |
1066 | unsigned b; | |
d03ab969 | 1067 | |
c8a2f9ef | 1068 | d = dvl[-1]; |
c29970a7 | 1069 | for (b = MPW_P2; b; b >>= 1) { |
34e4f738 | 1070 | if (d <= (MPW_MAX >> b)) { |
c8a2f9ef | 1071 | d <<= b; |
1072 | norm += b; | |
1073 | } | |
1074 | } | |
1075 | if (dv + 1 == dvl) | |
1076 | norm += MPW_BITS; | |
d03ab969 | 1077 | } |
d03ab969 | 1078 | |
1079 | /* --- Normalize the dividend/remainder to match --- */ | |
1080 | ||
c8a2f9ef | 1081 | if (norm) { |
c8a2f9ef | 1082 | mpx_lsl(rv, rvl, rv, rvl, norm); |
f45a00c6 | 1083 | mpx_lsl(sv, svl, dv, dvl, norm); |
c8a2f9ef | 1084 | dv = sv; |
f45a00c6 | 1085 | dvl = svl; |
c8a2f9ef | 1086 | MPX_SHRINK(dv, dvl); |
1087 | } | |
1088 | ||
d03ab969 | 1089 | MPX_SHRINK(rv, rvl); |
c8a2f9ef | 1090 | d = dvl[-1]; |
1091 | dd = dvl[-2]; | |
d03ab969 | 1092 | |
1093 | /* --- Work out the relative scales --- */ | |
1094 | ||
1095 | { | |
1096 | size_t rvn = rvl - rv; | |
c8a2f9ef | 1097 | size_t dvn = dvl - dv; |
d03ab969 | 1098 | |
1099 | /* --- If the divisor is clearly larger, notice this --- */ | |
1100 | ||
1101 | if (dvn > rvn) { | |
1102 | mpx_lsr(rv, rvl, rv, rvl, norm); | |
1103 | return; | |
1104 | } | |
1105 | ||
1106 | scale = rvn - dvn; | |
1107 | } | |
1108 | ||
1109 | /* --- Calculate the most significant quotient digit --- * | |
1110 | * | |
1111 | * Because the divisor has its top bit set, this can only happen once. The | |
1112 | * pointer arithmetic is a little contorted, to make sure that the | |
1113 | * behaviour is defined. | |
1114 | */ | |
1115 | ||
1116 | if (MPX_UCMP(rv + scale, rvl, >=, dv, dvl)) { | |
1117 | mpx_usub(rv + scale, rvl, rv + scale, rvl, dv, dvl); | |
1118 | if (qvl - qv > scale) | |
1119 | qv[scale] = 1; | |
1120 | } | |
1121 | ||
1122 | /* --- Now for the main loop --- */ | |
1123 | ||
1124 | { | |
c8a2f9ef | 1125 | mpw *rvv = rvl - 2; |
d03ab969 | 1126 | |
1127 | while (scale) { | |
c8a2f9ef | 1128 | mpw q; |
1129 | mpd rh; | |
d03ab969 | 1130 | |
1131 | /* --- Get an estimate for the next quotient digit --- */ | |
1132 | ||
c8a2f9ef | 1133 | mpw r = rvv[1]; |
1134 | mpw rr = rvv[0]; | |
1135 | mpw rrr = *--rvv; | |
1136 | ||
1137 | scale--; | |
1138 | rh = ((mpd)r << MPW_BITS) | rr; | |
d03ab969 | 1139 | if (r == d) |
1140 | q = MPW_MAX; | |
c8a2f9ef | 1141 | else |
1142 | q = MPW(rh / d); | |
d03ab969 | 1143 | |
1144 | /* --- Refine the estimate --- */ | |
1145 | ||
1146 | { | |
1147 | mpd yh = (mpd)d * q; | |
ce76ff16 | 1148 | mpd yy = (mpd)dd * q; |
1149 | mpw yl; | |
c8a2f9ef | 1150 | |
ce76ff16 | 1151 | if (yy > MPW_MAX) |
1152 | yh += yy >> MPW_BITS; | |
1153 | yl = MPW(yy); | |
c8a2f9ef | 1154 | |
1155 | while (yh > rh || (yh == rh && yl > rrr)) { | |
1156 | q--; | |
1157 | yh -= d; | |
ce76ff16 | 1158 | if (yl < dd) |
1159 | yh--; | |
99b30c23 | 1160 | yl = MPW(yl - dd); |
c8a2f9ef | 1161 | } |
1162 | } | |
1163 | ||
1164 | /* --- Remove a chunk from the dividend --- */ | |
1165 | ||
1166 | { | |
1167 | mpw *svv; | |
1168 | const mpw *dvv; | |
f45a00c6 | 1169 | mpw mc = 0, sc = 0; |
c8a2f9ef | 1170 | |
f45a00c6 | 1171 | /* --- Calculate the size of the chunk --- * |
1172 | * | |
1173 | * This does the whole job of calculating @r >> scale - qd@. | |
1174 | */ | |
c8a2f9ef | 1175 | |
f45a00c6 | 1176 | for (svv = rv + scale, dvv = dv; |
1177 | dvv < dvl && svv < rvl; | |
1178 | svv++, dvv++) { | |
1179 | mpd x = (mpd)*dvv * (mpd)q + mc; | |
1180 | mc = x >> MPW_BITS; | |
1181 | x = (mpd)*svv - MPW(x) - sc; | |
c8a2f9ef | 1182 | *svv = MPW(x); |
f45a00c6 | 1183 | if (x >> MPW_BITS) |
1184 | sc = 1; | |
1185 | else | |
1186 | sc = 0; | |
1187 | } | |
1188 | ||
1189 | if (svv < rvl) { | |
1190 | mpd x = (mpd)*svv - mc - sc; | |
1191 | *svv++ = MPW(x); | |
1192 | if (x >> MPW_BITS) | |
1193 | sc = MPW_MAX; | |
1194 | else | |
1195 | sc = 0; | |
1196 | while (svv < rvl) | |
1197 | *svv++ = sc; | |
c8a2f9ef | 1198 | } |
c8a2f9ef | 1199 | |
f45a00c6 | 1200 | /* --- Fix if the quotient was too large --- * |
c8a2f9ef | 1201 | * |
f45a00c6 | 1202 | * This doesn't seem to happen very often. |
c8a2f9ef | 1203 | */ |
1204 | ||
c8a2f9ef | 1205 | if (rvl[-1] > MPW_MAX / 2) { |
1206 | mpx_uadd(rv + scale, rvl, rv + scale, rvl, dv, dvl); | |
1207 | q--; | |
1208 | } | |
1209 | } | |
1210 | ||
1211 | /* --- Done for another iteration --- */ | |
1212 | ||
1213 | if (qvl - qv > scale) | |
1214 | qv[scale] = q; | |
1215 | r = rr; | |
1216 | rr = rrr; | |
1217 | } | |
1218 | } | |
1219 | ||
1220 | /* --- Now fiddle with unnormalizing and things --- */ | |
1221 | ||
1222 | mpx_lsr(rv, rvl, rv, rvl, norm); | |
d03ab969 | 1223 | } |
1224 | ||
698bd937 | 1225 | /* --- @mpx_udivn@ --- * |
1226 | * | |
1227 | * Arguments: @mpw *qv, *qvl@ = storage for the quotient (may overlap | |
1228 | * dividend) | |
1229 | * @const mpw *rv, *rvl@ = dividend | |
1230 | * @mpw d@ = single-precision divisor | |
1231 | * | |
1232 | * Returns: Remainder after divison. | |
1233 | * | |
1234 | * Use: Performs a single-precision division operation. | |
1235 | */ | |
1236 | ||
1237 | mpw mpx_udivn(mpw *qv, mpw *qvl, const mpw *rv, const mpw *rvl, mpw d) | |
1238 | { | |
1239 | size_t i; | |
1240 | size_t ql = qvl - qv; | |
1241 | mpd r = 0; | |
1242 | ||
1243 | i = rvl - rv; | |
1244 | while (i > 0) { | |
1245 | i--; | |
1246 | r = (r << MPW_BITS) | rv[i]; | |
1247 | if (i < ql) | |
1248 | qv[i] = r / d; | |
1249 | r %= d; | |
1250 | } | |
1251 | return (MPW(r)); | |
1252 | } | |
1253 | ||
42684bdb | 1254 | /*----- Test rig ----------------------------------------------------------*/ |
1255 | ||
1256 | #ifdef TEST_RIG | |
1257 | ||
1258 | #include <mLib/alloc.h> | |
1259 | #include <mLib/dstr.h> | |
1260 | #include <mLib/quis.h> | |
1261 | #include <mLib/testrig.h> | |
1262 | ||
1263 | #include "mpscan.h" | |
1264 | ||
1265 | #define ALLOC(v, vl, sz) do { \ | |
1266 | size_t _sz = (sz); \ | |
1267 | mpw *_vv = xmalloc(MPWS(_sz)); \ | |
1268 | mpw *_vvl = _vv + _sz; \ | |
444083ae | 1269 | memset(_vv, 0xa5, MPWS(_sz)); \ |
42684bdb | 1270 | (v) = _vv; \ |
1271 | (vl) = _vvl; \ | |
1272 | } while (0) | |
1273 | ||
1274 | #define LOAD(v, vl, d) do { \ | |
1275 | const dstr *_d = (d); \ | |
1276 | mpw *_v, *_vl; \ | |
1277 | ALLOC(_v, _vl, MPW_RQ(_d->len)); \ | |
1278 | mpx_loadb(_v, _vl, _d->buf, _d->len); \ | |
1279 | (v) = _v; \ | |
1280 | (vl) = _vl; \ | |
1281 | } while (0) | |
1282 | ||
1283 | #define MAX(x, y) ((x) > (y) ? (x) : (y)) | |
45c0fd36 | 1284 | |
42684bdb | 1285 | static void dumpbits(const char *msg, const void *pp, size_t sz) |
1286 | { | |
1287 | const octet *p = pp; | |
1288 | fputs(msg, stderr); | |
1289 | for (; sz; sz--) | |
1290 | fprintf(stderr, " %02x", *p++); | |
1291 | fputc('\n', stderr); | |
1292 | } | |
1293 | ||
1294 | static void dumpmp(const char *msg, const mpw *v, const mpw *vl) | |
1295 | { | |
1296 | fputs(msg, stderr); | |
1297 | MPX_SHRINK(v, vl); | |
1298 | while (v < vl) | |
1299 | fprintf(stderr, " %08lx", (unsigned long)*--vl); | |
1300 | fputc('\n', stderr); | |
1301 | } | |
1302 | ||
1303 | static int chkscan(const mpw *v, const mpw *vl, | |
1304 | const void *pp, size_t sz, int step) | |
1305 | { | |
1306 | mpscan mps; | |
1307 | const octet *p = pp; | |
1308 | unsigned bit = 0; | |
1309 | int ok = 1; | |
1310 | ||
1311 | mpscan_initx(&mps, v, vl); | |
1312 | while (sz) { | |
1313 | unsigned x = *p; | |
1314 | int i; | |
1315 | p += step; | |
1316 | for (i = 0; i < 8 && MPSCAN_STEP(&mps); i++) { | |
1317 | if (MPSCAN_BIT(&mps) != (x & 1)) { | |
1318 | fprintf(stderr, | |
1319 | "\n*** error, step %i, bit %u, expected %u, found %u\n", | |
1320 | step, bit, x & 1, MPSCAN_BIT(&mps)); | |
1321 | ok = 0; | |
1322 | } | |
1323 | x >>= 1; | |
1324 | bit++; | |
1325 | } | |
1326 | sz--; | |
1327 | } | |
1328 | ||
1329 | return (ok); | |
1330 | } | |
1331 | ||
1332 | static int loadstore(dstr *v) | |
1333 | { | |
1334 | dstr d = DSTR_INIT; | |
1335 | size_t sz = MPW_RQ(v->len) * 2, diff; | |
1336 | mpw *m, *ml; | |
1337 | int ok = 1; | |
1338 | ||
1339 | dstr_ensure(&d, v->len); | |
1340 | m = xmalloc(MPWS(sz)); | |
1341 | ||
1342 | for (diff = 0; diff < sz; diff += 5) { | |
1343 | size_t oct; | |
1344 | ||
1345 | ml = m + sz - diff; | |
1346 | ||
1347 | mpx_loadl(m, ml, v->buf, v->len); | |
1348 | if (!chkscan(m, ml, v->buf, v->len, +1)) | |
1349 | ok = 0; | |
1350 | MPX_OCTETS(oct, m, ml); | |
1351 | mpx_storel(m, ml, d.buf, d.sz); | |
1352 | if (memcmp(d.buf, v->buf, oct) != 0) { | |
1353 | dumpbits("\n*** storel failed", d.buf, d.sz); | |
1354 | ok = 0; | |
1355 | } | |
1356 | ||
1357 | mpx_loadb(m, ml, v->buf, v->len); | |
1358 | if (!chkscan(m, ml, v->buf + v->len - 1, v->len, -1)) | |
1359 | ok = 0; | |
1360 | MPX_OCTETS(oct, m, ml); | |
1361 | mpx_storeb(m, ml, d.buf, d.sz); | |
1362 | if (memcmp(d.buf + d.sz - oct, v->buf + v->len - oct, oct) != 0) { | |
1363 | dumpbits("\n*** storeb failed", d.buf, d.sz); | |
1364 | ok = 0; | |
1365 | } | |
1366 | } | |
1367 | ||
1368 | if (!ok) | |
1369 | dumpbits("input data", v->buf, v->len); | |
1370 | ||
12ed8a1f | 1371 | xfree(m); |
42684bdb | 1372 | dstr_destroy(&d); |
1373 | return (ok); | |
1374 | } | |
1375 | ||
f09e814a | 1376 | static int twocl(dstr *v) |
1377 | { | |
1378 | dstr d = DSTR_INIT; | |
1379 | mpw *m, *ml; | |
1380 | size_t sz; | |
1381 | int ok = 1; | |
1382 | ||
1383 | sz = v[0].len; if (v[1].len > sz) sz = v[1].len; | |
1384 | dstr_ensure(&d, sz); | |
1385 | ||
1386 | sz = MPW_RQ(sz); | |
1387 | m = xmalloc(MPWS(sz)); | |
1388 | ml = m + sz; | |
1389 | ||
1390 | mpx_loadl(m, ml, v[0].buf, v[0].len); | |
1391 | mpx_storel2cn(m, ml, d.buf, v[1].len); | |
1392 | if (memcmp(d.buf, v[1].buf, v[1].len)) { | |
1393 | dumpbits("\n*** storel2cn failed", d.buf, v[1].len); | |
1394 | ok = 0; | |
1395 | } | |
1396 | ||
1397 | mpx_loadl2cn(m, ml, v[1].buf, v[1].len); | |
1398 | mpx_storel(m, ml, d.buf, v[0].len); | |
1399 | if (memcmp(d.buf, v[0].buf, v[0].len)) { | |
1400 | dumpbits("\n*** loadl2cn failed", d.buf, v[0].len); | |
1401 | ok = 0; | |
1402 | } | |
1403 | ||
1404 | if (!ok) { | |
1405 | dumpbits("pos", v[0].buf, v[0].len); | |
1406 | dumpbits("neg", v[1].buf, v[1].len); | |
1407 | } | |
1408 | ||
12ed8a1f | 1409 | xfree(m); |
f09e814a | 1410 | dstr_destroy(&d); |
1411 | ||
1412 | return (ok); | |
1413 | } | |
1414 | ||
1415 | static int twocb(dstr *v) | |
1416 | { | |
1417 | dstr d = DSTR_INIT; | |
1418 | mpw *m, *ml; | |
1419 | size_t sz; | |
1420 | int ok = 1; | |
1421 | ||
1422 | sz = v[0].len; if (v[1].len > sz) sz = v[1].len; | |
1423 | dstr_ensure(&d, sz); | |
1424 | ||
1425 | sz = MPW_RQ(sz); | |
1426 | m = xmalloc(MPWS(sz)); | |
1427 | ml = m + sz; | |
1428 | ||
1429 | mpx_loadb(m, ml, v[0].buf, v[0].len); | |
1430 | mpx_storeb2cn(m, ml, d.buf, v[1].len); | |
1431 | if (memcmp(d.buf, v[1].buf, v[1].len)) { | |
1432 | dumpbits("\n*** storeb2cn failed", d.buf, v[1].len); | |
1433 | ok = 0; | |
1434 | } | |
1435 | ||
1436 | mpx_loadb2cn(m, ml, v[1].buf, v[1].len); | |
1437 | mpx_storeb(m, ml, d.buf, v[0].len); | |
1438 | if (memcmp(d.buf, v[0].buf, v[0].len)) { | |
1439 | dumpbits("\n*** loadb2cn failed", d.buf, v[0].len); | |
1440 | ok = 0; | |
1441 | } | |
1442 | ||
1443 | if (!ok) { | |
1444 | dumpbits("pos", v[0].buf, v[0].len); | |
1445 | dumpbits("neg", v[1].buf, v[1].len); | |
1446 | } | |
1447 | ||
12ed8a1f | 1448 | xfree(m); |
f09e814a | 1449 | dstr_destroy(&d); |
1450 | ||
1451 | return (ok); | |
1452 | } | |
1453 | ||
42684bdb | 1454 | static int lsl(dstr *v) |
1455 | { | |
1456 | mpw *a, *al; | |
1457 | int n = *(int *)v[1].buf; | |
1458 | mpw *c, *cl; | |
1459 | mpw *d, *dl; | |
1460 | int ok = 1; | |
1461 | ||
1462 | LOAD(a, al, &v[0]); | |
1463 | LOAD(c, cl, &v[2]); | |
1464 | ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS); | |
1465 | ||
1466 | mpx_lsl(d, dl, a, al, n); | |
1a05a8ef | 1467 | if (!mpx_ueq(d, dl, c, cl)) { |
42684bdb | 1468 | fprintf(stderr, "\n*** lsl(%i) failed\n", n); |
45c0fd36 | 1469 | dumpmp(" a", a, al); |
42684bdb | 1470 | dumpmp("expected", c, cl); |
1471 | dumpmp(" result", d, dl); | |
1472 | ok = 0; | |
1473 | } | |
1474 | ||
12ed8a1f | 1475 | xfree(a); xfree(c); xfree(d); |
42684bdb | 1476 | return (ok); |
1477 | } | |
1478 | ||
81578196 | 1479 | static int lslc(dstr *v) |
1480 | { | |
1481 | mpw *a, *al; | |
1482 | int n = *(int *)v[1].buf; | |
1483 | mpw *c, *cl; | |
1484 | mpw *d, *dl; | |
1485 | int ok = 1; | |
1486 | ||
1487 | LOAD(a, al, &v[0]); | |
1488 | LOAD(c, cl, &v[2]); | |
1489 | ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS); | |
1490 | ||
1491 | mpx_lslc(d, dl, a, al, n); | |
1492 | if (!mpx_ueq(d, dl, c, cl)) { | |
1493 | fprintf(stderr, "\n*** lslc(%i) failed\n", n); | |
45c0fd36 | 1494 | dumpmp(" a", a, al); |
81578196 | 1495 | dumpmp("expected", c, cl); |
1496 | dumpmp(" result", d, dl); | |
1497 | ok = 0; | |
1498 | } | |
1499 | ||
12ed8a1f | 1500 | xfree(a); xfree(c); xfree(d); |
81578196 | 1501 | return (ok); |
1502 | } | |
1503 | ||
42684bdb | 1504 | static int lsr(dstr *v) |
1505 | { | |
1506 | mpw *a, *al; | |
1507 | int n = *(int *)v[1].buf; | |
1508 | mpw *c, *cl; | |
1509 | mpw *d, *dl; | |
1510 | int ok = 1; | |
1511 | ||
1512 | LOAD(a, al, &v[0]); | |
1513 | LOAD(c, cl, &v[2]); | |
1514 | ALLOC(d, dl, al - a + (n + MPW_BITS - 1) / MPW_BITS + 1); | |
1515 | ||
1516 | mpx_lsr(d, dl, a, al, n); | |
1a05a8ef | 1517 | if (!mpx_ueq(d, dl, c, cl)) { |
42684bdb | 1518 | fprintf(stderr, "\n*** lsr(%i) failed\n", n); |
45c0fd36 | 1519 | dumpmp(" a", a, al); |
42684bdb | 1520 | dumpmp("expected", c, cl); |
1521 | dumpmp(" result", d, dl); | |
1522 | ok = 0; | |
1523 | } | |
1524 | ||
12ed8a1f | 1525 | xfree(a); xfree(c); xfree(d); |
42684bdb | 1526 | return (ok); |
1527 | } | |
1528 | ||
1529 | static int uadd(dstr *v) | |
1530 | { | |
1531 | mpw *a, *al; | |
1532 | mpw *b, *bl; | |
1533 | mpw *c, *cl; | |
1534 | mpw *d, *dl; | |
1535 | int ok = 1; | |
1536 | ||
1537 | LOAD(a, al, &v[0]); | |
1538 | LOAD(b, bl, &v[1]); | |
1539 | LOAD(c, cl, &v[2]); | |
1540 | ALLOC(d, dl, MAX(al - a, bl - b) + 1); | |
1541 | ||
1542 | mpx_uadd(d, dl, a, al, b, bl); | |
1a05a8ef | 1543 | if (!mpx_ueq(d, dl, c, cl)) { |
42684bdb | 1544 | fprintf(stderr, "\n*** uadd failed\n"); |
45c0fd36 MW |
1545 | dumpmp(" a", a, al); |
1546 | dumpmp(" b", b, bl); | |
42684bdb | 1547 | dumpmp("expected", c, cl); |
1548 | dumpmp(" result", d, dl); | |
1549 | ok = 0; | |
1550 | } | |
1551 | ||
12ed8a1f | 1552 | xfree(a); xfree(b); xfree(c); xfree(d); |
42684bdb | 1553 | return (ok); |
1554 | } | |
1555 | ||
1556 | static int usub(dstr *v) | |
1557 | { | |
1558 | mpw *a, *al; | |
1559 | mpw *b, *bl; | |
1560 | mpw *c, *cl; | |
1561 | mpw *d, *dl; | |
1562 | int ok = 1; | |
1563 | ||
1564 | LOAD(a, al, &v[0]); | |
1565 | LOAD(b, bl, &v[1]); | |
1566 | LOAD(c, cl, &v[2]); | |
1567 | ALLOC(d, dl, al - a); | |
1568 | ||
1569 | mpx_usub(d, dl, a, al, b, bl); | |
1a05a8ef | 1570 | if (!mpx_ueq(d, dl, c, cl)) { |
42684bdb | 1571 | fprintf(stderr, "\n*** usub failed\n"); |
45c0fd36 MW |
1572 | dumpmp(" a", a, al); |
1573 | dumpmp(" b", b, bl); | |
42684bdb | 1574 | dumpmp("expected", c, cl); |
1575 | dumpmp(" result", d, dl); | |
1576 | ok = 0; | |
1577 | } | |
1578 | ||
12ed8a1f | 1579 | xfree(a); xfree(b); xfree(c); xfree(d); |
42684bdb | 1580 | return (ok); |
1581 | } | |
1582 | ||
1583 | static int umul(dstr *v) | |
1584 | { | |
1585 | mpw *a, *al; | |
1586 | mpw *b, *bl; | |
1587 | mpw *c, *cl; | |
1588 | mpw *d, *dl; | |
1589 | int ok = 1; | |
1590 | ||
1591 | LOAD(a, al, &v[0]); | |
1592 | LOAD(b, bl, &v[1]); | |
1593 | LOAD(c, cl, &v[2]); | |
1594 | ALLOC(d, dl, (al - a) + (bl - b)); | |
1595 | ||
1596 | mpx_umul(d, dl, a, al, b, bl); | |
1a05a8ef | 1597 | if (!mpx_ueq(d, dl, c, cl)) { |
42684bdb | 1598 | fprintf(stderr, "\n*** umul failed\n"); |
45c0fd36 MW |
1599 | dumpmp(" a", a, al); |
1600 | dumpmp(" b", b, bl); | |
42684bdb | 1601 | dumpmp("expected", c, cl); |
1602 | dumpmp(" result", d, dl); | |
1603 | ok = 0; | |
1604 | } | |
1605 | ||
12ed8a1f | 1606 | xfree(a); xfree(b); xfree(c); xfree(d); |
42684bdb | 1607 | return (ok); |
1608 | } | |
1609 | ||
1610 | static int usqr(dstr *v) | |
1611 | { | |
1612 | mpw *a, *al; | |
1613 | mpw *c, *cl; | |
1614 | mpw *d, *dl; | |
1615 | int ok = 1; | |
1616 | ||
1617 | LOAD(a, al, &v[0]); | |
1618 | LOAD(c, cl, &v[1]); | |
1619 | ALLOC(d, dl, 2 * (al - a)); | |
1620 | ||
1621 | mpx_usqr(d, dl, a, al); | |
1a05a8ef | 1622 | if (!mpx_ueq(d, dl, c, cl)) { |
42684bdb | 1623 | fprintf(stderr, "\n*** usqr failed\n"); |
45c0fd36 | 1624 | dumpmp(" a", a, al); |
42684bdb | 1625 | dumpmp("expected", c, cl); |
1626 | dumpmp(" result", d, dl); | |
1627 | ok = 0; | |
1628 | } | |
1629 | ||
12ed8a1f | 1630 | xfree(a); xfree(c); xfree(d); |
42684bdb | 1631 | return (ok); |
1632 | } | |
1633 | ||
1634 | static int udiv(dstr *v) | |
1635 | { | |
1636 | mpw *a, *al; | |
1637 | mpw *b, *bl; | |
1638 | mpw *q, *ql; | |
1639 | mpw *r, *rl; | |
1640 | mpw *qq, *qql; | |
1641 | mpw *s, *sl; | |
1642 | int ok = 1; | |
1643 | ||
1644 | ALLOC(a, al, MPW_RQ(v[0].len) + 2); mpx_loadb(a, al, v[0].buf, v[0].len); | |
1645 | LOAD(b, bl, &v[1]); | |
1646 | LOAD(q, ql, &v[2]); | |
1647 | LOAD(r, rl, &v[3]); | |
1648 | ALLOC(qq, qql, al - a); | |
1649 | ALLOC(s, sl, (bl - b) + 1); | |
1650 | ||
1651 | mpx_udiv(qq, qql, a, al, b, bl, s, sl); | |
1a05a8ef | 1652 | if (!mpx_ueq(qq, qql, q, ql) || |
1653 | !mpx_ueq(a, al, r, rl)) { | |
42684bdb | 1654 | fprintf(stderr, "\n*** udiv failed\n"); |
1655 | dumpmp(" divisor", b, bl); | |
1656 | dumpmp("expect r", r, rl); | |
1657 | dumpmp("result r", a, al); | |
1658 | dumpmp("expect q", q, ql); | |
1659 | dumpmp("result q", qq, qql); | |
1660 | ok = 0; | |
1661 | } | |
1662 | ||
12ed8a1f | 1663 | xfree(a); xfree(b); xfree(r); xfree(q); xfree(s); xfree(qq); |
42684bdb | 1664 | return (ok); |
1665 | } | |
1666 | ||
1667 | static test_chunk defs[] = { | |
1668 | { "load-store", loadstore, { &type_hex, 0 } }, | |
f09e814a | 1669 | { "2cl", twocl, { &type_hex, &type_hex, } }, |
1670 | { "2cb", twocb, { &type_hex, &type_hex, } }, | |
42684bdb | 1671 | { "lsl", lsl, { &type_hex, &type_int, &type_hex, 0 } }, |
81578196 | 1672 | { "lslc", lslc, { &type_hex, &type_int, &type_hex, 0 } }, |
42684bdb | 1673 | { "lsr", lsr, { &type_hex, &type_int, &type_hex, 0 } }, |
1674 | { "uadd", uadd, { &type_hex, &type_hex, &type_hex, 0 } }, | |
1675 | { "usub", usub, { &type_hex, &type_hex, &type_hex, 0 } }, | |
1676 | { "umul", umul, { &type_hex, &type_hex, &type_hex, 0 } }, | |
1677 | { "usqr", usqr, { &type_hex, &type_hex, 0 } }, | |
1678 | { "udiv", udiv, { &type_hex, &type_hex, &type_hex, &type_hex, 0 } }, | |
1679 | { 0, 0, { 0 } } | |
1680 | }; | |
1681 | ||
1682 | int main(int argc, char *argv[]) | |
1683 | { | |
0f00dc4c | 1684 | test_run(argc, argv, defs, SRCDIR"/t/mpx"); |
42684bdb | 1685 | return (0); |
1686 | } | |
1687 | ||
42684bdb | 1688 | #endif |
1689 | ||
d03ab969 | 1690 | /*----- That's all, folks -------------------------------------------------*/ |