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