| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * $Id: mp-arith.c,v 1.2 1999/12/10 23:18:39 mdw Exp $ |
| 4 | * |
| 5 | * Basic arithmetic on multiprecision integers |
| 6 | * |
| 7 | * (c) 1999 Straylight/Edgeware |
| 8 | */ |
| 9 | |
| 10 | /*----- Licensing notice --------------------------------------------------* |
| 11 | * |
| 12 | * This file is part of Catacomb. |
| 13 | * |
| 14 | * Catacomb is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU Library General Public License as |
| 16 | * published by the Free Software Foundation; either version 2 of the |
| 17 | * License, or (at your option) any later version. |
| 18 | * |
| 19 | * Catacomb is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU Library General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU Library General Public |
| 25 | * License along with Catacomb; if not, write to the Free |
| 26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 27 | * MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
| 30 | /*----- Revision history --------------------------------------------------* |
| 31 | * |
| 32 | * $Log: mp-arith.c,v $ |
| 33 | * Revision 1.2 1999/12/10 23:18:39 mdw |
| 34 | * Change interface for suggested destinations. |
| 35 | * |
| 36 | * Revision 1.1 1999/11/17 18:02:16 mdw |
| 37 | * New multiprecision integer arithmetic suite. |
| 38 | * |
| 39 | */ |
| 40 | |
| 41 | /*----- Header files ------------------------------------------------------*/ |
| 42 | |
| 43 | #include "mp.h" |
| 44 | |
| 45 | /*----- Macros ------------------------------------------------------------*/ |
| 46 | |
| 47 | #define MAX(x, y) ((x) >= (y) ? (x) : (y)) |
| 48 | |
| 49 | /*----- Main code ---------------------------------------------------------*/ |
| 50 | |
| 51 | /* --- @mp_2c@ --- * |
| 52 | * |
| 53 | * Arguments: @mp *a@ = source |
| 54 | * |
| 55 | * Returns: Result, @a@ converted to two's complement notation. |
| 56 | */ |
| 57 | |
| 58 | mp *mp_2c(mp *d, mp *a) |
| 59 | { |
| 60 | if (!(a->f & MP_NEG)) |
| 61 | return (MP_COPY(a)); |
| 62 | |
| 63 | MP_MODIFY(d, MP_LEN(a)); |
| 64 | mpx_2c(d->v, d->vl, a->v, a->vl); |
| 65 | d->f = a->f & MP_BURN; |
| 66 | MP_SHRINK(d); |
| 67 | return (d); |
| 68 | } |
| 69 | |
| 70 | /* --- @mp_sm@ --- * |
| 71 | * |
| 72 | * Arguments: @mp *d@ = destination |
| 73 | * @mp *a@ = source |
| 74 | * |
| 75 | * Returns: Result, @a@ converted to the native signed-magnitude |
| 76 | * notation. |
| 77 | */ |
| 78 | |
| 79 | mp *mp_sm(mp *d, mp *a) |
| 80 | { |
| 81 | if (!MP_LEN(a) || a->vl[-1] < MPW_MAX / 2) |
| 82 | return (MP_COPY(a)); |
| 83 | |
| 84 | MP_MODIFY(d, MP_LEN(a)); |
| 85 | mpx_2c(d->v, d->vl, a->v, a->vl); |
| 86 | d->f = (a->f & (MP_BURN | MP_NEG)) ^ MP_NEG; |
| 87 | MP_SHRINK(d); |
| 88 | return (d); |
| 89 | } |
| 90 | |
| 91 | /* --- @mp_lsl@ --- * |
| 92 | * |
| 93 | * Arguments: @mp *d@ = destination |
| 94 | * @mp *a@ = source |
| 95 | * @size_t n@ = number of bits to move |
| 96 | * |
| 97 | * Returns: Result, @a@ shifted left by @n@. |
| 98 | */ |
| 99 | |
| 100 | mp *mp_lsl(mp *d, mp *a, size_t n) |
| 101 | { |
| 102 | MP_MODIFY(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS); |
| 103 | mpx_lsl(d->v, d->vl, a->v, a->vl, n); |
| 104 | d->f = a->f & (MP_NEG | MP_BURN); |
| 105 | MP_SHRINK(d); |
| 106 | return (d); |
| 107 | } |
| 108 | |
| 109 | /* --- @mp_lsr@ --- * |
| 110 | * |
| 111 | * Arguments: @mp *d@ = destination |
| 112 | * @mp *a@ = source |
| 113 | * @size_t n@ = number of bits to move |
| 114 | * |
| 115 | * Returns: Result, @a@ shifted left by @n@. |
| 116 | */ |
| 117 | |
| 118 | mp *mp_lsr(mp *d, mp *a, size_t n) |
| 119 | { |
| 120 | MP_MODIFY(d, MP_LEN(a)); |
| 121 | mpx_lsr(d->v, d->vl, a->v, a->vl, n); |
| 122 | d->f = a->f & (MP_NEG | MP_BURN); |
| 123 | MP_SHRINK(d); |
| 124 | return (d); |
| 125 | } |
| 126 | |
| 127 | /* --- @mp_cmp@ --- * |
| 128 | * |
| 129 | * Arguments: @const mp *a, *b@ = two numbers |
| 130 | * |
| 131 | * Returns: Less than, equal to or greater than zero, according to |
| 132 | * whether @a@ is less than, equal to or greater than @b@. |
| 133 | */ |
| 134 | |
| 135 | int mp_cmp(const mp *a, const mp *b) |
| 136 | { |
| 137 | if (!((a->f ^ b->f) & MP_NEG)) |
| 138 | return (mpx_ucmp(a->v, a->vl, b->v, b->vl)); |
| 139 | else if (a->f & MP_NEG) |
| 140 | return (-1); |
| 141 | else |
| 142 | return (+1); |
| 143 | } |
| 144 | |
| 145 | /* --- @mp_add@ --- * |
| 146 | * |
| 147 | * Arguments: @mp *d@ = destination |
| 148 | * @mp *a, *b@ = sources |
| 149 | * |
| 150 | * Returns: Result, @a@ added to @b@. |
| 151 | */ |
| 152 | |
| 153 | mp *mp_add(mp *d, mp *a, mp *b) |
| 154 | { |
| 155 | MP_MODIFY(d, MAX(MP_LEN(a), MP_LEN(b)) + 1); |
| 156 | if (!((a->f ^ b->f) & MP_NEG)) |
| 157 | mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 158 | else { |
| 159 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
| 160 | mp *t = a; a = b; b = t; |
| 161 | } |
| 162 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 163 | } |
| 164 | d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG); |
| 165 | MP_SHRINK(d); |
| 166 | return (d); |
| 167 | } |
| 168 | |
| 169 | /* --- @mp_sub@ --- * |
| 170 | * |
| 171 | * Arguments: @mp *d@ = destination |
| 172 | * @mp *a, *b@ = sources |
| 173 | * |
| 174 | * Returns: Result, @b@ subtracted from @a@. |
| 175 | */ |
| 176 | |
| 177 | mp *mp_sub(mp *d, mp *a, mp *b) |
| 178 | { |
| 179 | unsigned sgn = 0; |
| 180 | MP_MODIFY(d, MAX(MP_LEN(a), MP_LEN(b)) + 1); |
| 181 | if ((a->f ^ b->f) & MP_NEG) |
| 182 | mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 183 | else { |
| 184 | if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) { |
| 185 | mp *t = a; a = b; b = t; |
| 186 | sgn = MP_NEG; |
| 187 | } |
| 188 | mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 189 | } |
| 190 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG); |
| 191 | MP_SHRINK(d); |
| 192 | return (d); |
| 193 | } |
| 194 | |
| 195 | /* --- @mp_mul@ --- * |
| 196 | * |
| 197 | * Arguments: @mp *d@ = destination |
| 198 | * @mp *a, *b@ = sources |
| 199 | * |
| 200 | * Returns: Result, @a@ multiplied by @b@. |
| 201 | */ |
| 202 | |
| 203 | mp *mp_mul(mp *d, mp *a, mp *b) |
| 204 | { |
| 205 | a = MP_COPY(a); |
| 206 | b = MP_COPY(b); |
| 207 | |
| 208 | MP_MODIFY(d, MP_LEN(a) + MP_LEN(b)); |
| 209 | if (MP_LEN(a) <= KARATSUBA_CUTOFF || MP_LEN(b) <= KARATSUBA_CUTOFF) |
| 210 | mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl); |
| 211 | else { |
| 212 | size_t m = MAX(MP_LEN(a), MP_LEN(b)) * 2 + KARATSUBA_SLOP; |
| 213 | mpw *s; |
| 214 | m += 32; |
| 215 | s = MP_ALLOC(m); |
| 216 | mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + m); |
| 217 | MP_FREE(s); |
| 218 | } |
| 219 | |
| 220 | d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG); |
| 221 | MP_SHRINK(d); |
| 222 | MP_DROP(a); |
| 223 | MP_DROP(b); |
| 224 | return (d); |
| 225 | } |
| 226 | |
| 227 | /* --- @mp_sqr@ --- * |
| 228 | * |
| 229 | * Arguments: @mp *d@ = destination |
| 230 | * @mp *a@ = source |
| 231 | * |
| 232 | * Returns: Result, @a@ squared. |
| 233 | */ |
| 234 | |
| 235 | mp *mp_sqr(mp *d, mp *a) |
| 236 | { |
| 237 | size_t m = MP_LEN(a); |
| 238 | |
| 239 | a = MP_COPY(a); |
| 240 | MP_MODIFY(d, 2 * m); |
| 241 | if (m > KARATSUBA_CUTOFF) { |
| 242 | mpw *s; |
| 243 | m = 2 * (m + 1) + 32; |
| 244 | s = MP_ALLOC(m); |
| 245 | mpx_kmul(d->v, d->vl, a->v, a->vl, a->v, a->vl, s, s + m); |
| 246 | MP_FREE(s); |
| 247 | } else |
| 248 | mpx_usqr(d->v, d->vl, a->v, a->vl); |
| 249 | d->f = a->f & MP_BURN; |
| 250 | MP_SHRINK(d); |
| 251 | MP_DROP(a); |
| 252 | return (d); |
| 253 | } |
| 254 | |
| 255 | /* --- @mp_div@ --- * |
| 256 | * |
| 257 | * Arguments: @mp **qq, **rr@ = destination, quotient and remainder |
| 258 | * @mp *a, *b@ = sources |
| 259 | * |
| 260 | * Use: Calculates the quotient and remainder when @a@ is divided by |
| 261 | * @b@. The destinations @*qq@ and @*rr@ must be distinct. |
| 262 | * Either of @qq@ or @rr@ may be null to indicate that the |
| 263 | * result is irrelevant. (Discarding both results is silly.) |
| 264 | * There is a performance advantage if @a == *rr@. |
| 265 | * |
| 266 | * The behaviour when @a@ and @b@ have the same sign is |
| 267 | * straightforward. When the signs differ, this implementation |
| 268 | * chooses @r@ to have the same sign as @b@, rather than the |
| 269 | * more normal choice that the remainder has the same sign as |
| 270 | * the dividend. This makes modular arithmetic a little more |
| 271 | * straightforward. |
| 272 | */ |
| 273 | |
| 274 | void mp_div(mp **qq, mp **rr, mp *a, mp *b) |
| 275 | { |
| 276 | mp *r = rr ? *rr : MP_NEW; |
| 277 | mp *q = qq ? *qq : MP_NEW; |
| 278 | mpw *sv, *svl; |
| 279 | |
| 280 | /* --- Set up some temporary workspace --- */ |
| 281 | |
| 282 | { |
| 283 | size_t rq = MP_LEN(b) + 1; |
| 284 | sv = MP_ALLOC(rq); |
| 285 | svl = sv + rq; |
| 286 | } |
| 287 | |
| 288 | /* --- Set the remainder up right --- * |
| 289 | * |
| 290 | * Just in case the divisor is larger, be able to cope with this. It's not |
| 291 | * important in @mpx_udiv@, but it is here because of the sign correction. |
| 292 | */ |
| 293 | |
| 294 | { |
| 295 | size_t rq = MP_LEN(a) + 2; |
| 296 | if (MP_LEN(b) > rq) |
| 297 | rq = MP_LEN(b); |
| 298 | |
| 299 | b = MP_COPY(b); |
| 300 | if (r == a) { |
| 301 | MP_SPLIT(a); |
| 302 | a = r = MP_COPY(a); |
| 303 | MP_ENSURE(r, MP_LEN(r) + 2); |
| 304 | } else { |
| 305 | a = MP_COPY(a); |
| 306 | MP_MODIFY(r, MP_LEN(a) + 2); |
| 307 | memcpy(r->v, a->v, MPWS(MP_LEN(a))); |
| 308 | memset(r->v + MP_LEN(a), 0, MPWS(2)); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | /* --- Fix up the quotient too --- */ |
| 313 | |
| 314 | MP_MODIFY(q, MP_LEN(a)); |
| 315 | |
| 316 | /* --- Perform the calculation --- */ |
| 317 | |
| 318 | mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl); |
| 319 | |
| 320 | /* --- Sort out the sign of the results --- * |
| 321 | * |
| 322 | * If the signs of the arguments differ, and the remainder is nonzero, I |
| 323 | * must add one to the absolute value of the quotient and subtract the |
| 324 | * remainder from @b@. |
| 325 | */ |
| 326 | |
| 327 | q->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG); |
| 328 | if (q->f & MP_NEG) { |
| 329 | mpw *v; |
| 330 | for (v = r->v; v < r->vl; v++) { |
| 331 | if (*v) { |
| 332 | MPX_UADDN(q->v, q->vl, 1); |
| 333 | mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl); |
| 334 | break; |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | r->f = ((a->f | b->f) & MP_BURN) | (b->f & MP_NEG); |
| 340 | |
| 341 | /* --- Store the return values --- */ |
| 342 | |
| 343 | if (!qq) |
| 344 | MP_DROP(q); |
| 345 | else { |
| 346 | MP_SHRINK(q); |
| 347 | *qq = q; |
| 348 | } |
| 349 | |
| 350 | if (!rr) |
| 351 | MP_DROP(r); |
| 352 | else { |
| 353 | MP_SHRINK(r); |
| 354 | *rr = r; |
| 355 | } |
| 356 | |
| 357 | MP_DROP(a); |
| 358 | MP_DROP(b); |
| 359 | MP_FREE(sv); |
| 360 | } |
| 361 | |
| 362 | /*----- Test rig ----------------------------------------------------------*/ |
| 363 | |
| 364 | #ifdef TEST_RIG |
| 365 | |
| 366 | static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b) |
| 367 | { |
| 368 | if (MP_CMP(expect, !=, result)) { |
| 369 | fprintf(stderr, "\n*** %s failed", op); |
| 370 | fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10); |
| 371 | fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10); |
| 372 | fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 10); |
| 373 | fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 10); |
| 374 | fputc('\n', stderr); |
| 375 | return (0); |
| 376 | } |
| 377 | return (1); |
| 378 | } |
| 379 | |
| 380 | #define RIG(name, op) \ |
| 381 | static int t##name(dstr *v) \ |
| 382 | { \ |
| 383 | mp *a = *(mp **)v[0].buf; \ |
| 384 | mpw n = *(int *)v[1].buf; \ |
| 385 | mp b; \ |
| 386 | mp *r = *(mp **)v[2].buf; \ |
| 387 | mp *c = op(MP_NEW, a, n); \ |
| 388 | int ok; \ |
| 389 | mp_build(&b, &n, &n + 1); \ |
| 390 | ok = verify(#name, r, c, a, &b); \ |
| 391 | mp_drop(a); mp_drop(c); mp_drop(r); \ |
| 392 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
| 393 | return (ok); \ |
| 394 | } |
| 395 | |
| 396 | RIG(lsl, mp_lsl) |
| 397 | RIG(lsr, mp_lsr) |
| 398 | |
| 399 | #undef RIG |
| 400 | |
| 401 | #define RIG(name, op) \ |
| 402 | static int t##name(dstr *v) \ |
| 403 | { \ |
| 404 | mp *a = *(mp **)v[0].buf; \ |
| 405 | mp *b = *(mp **)v[1].buf; \ |
| 406 | mp *r = *(mp **)v[2].buf; \ |
| 407 | mp *c = op(MP_NEW, a, b); \ |
| 408 | int ok = verify(#name, r, c, a, b); \ |
| 409 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \ |
| 410 | assert(mparena_count(MPARENA_GLOBAL) == 0); \ |
| 411 | return (ok); \ |
| 412 | } |
| 413 | |
| 414 | RIG(add, mp_add) |
| 415 | RIG(sub, mp_sub) |
| 416 | RIG(mul, mp_mul) |
| 417 | |
| 418 | #undef RIG |
| 419 | |
| 420 | static int tdiv(dstr *v) |
| 421 | { |
| 422 | mp *a = *(mp **)v[0].buf; |
| 423 | mp *b = *(mp **)v[1].buf; |
| 424 | mp *q = *(mp **)v[2].buf; |
| 425 | mp *r = *(mp **)v[3].buf; |
| 426 | mp *c = MP_NEW, *d = MP_NEW; |
| 427 | int ok = 1; |
| 428 | mp_div(&c, &d, a, b); |
| 429 | ok &= verify("div(quotient)", q, c, a, b); |
| 430 | ok &= verify("div(remainder)", r, d, a, b); |
| 431 | mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q); |
| 432 | assert(mparena_count(MPARENA_GLOBAL) == 0); |
| 433 | return (ok); |
| 434 | } |
| 435 | |
| 436 | static test_chunk tests[] = { |
| 437 | { "lsl", tlsl, { &type_mp, &type_mp, &type_mp, 0 } }, |
| 438 | { "lsr", tlsr, { &type_mp, &type_mp, &type_mp, 0 } }, |
| 439 | { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } }, |
| 440 | { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } }, |
| 441 | { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } }, |
| 442 | { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } }, |
| 443 | { 0, 0, { 0 } }, |
| 444 | }; |
| 445 | |
| 446 | int main(int argc, char *argv[]) |
| 447 | { |
| 448 | sub_init(); |
| 449 | test_run(argc, argv, tests, SRCDIR "/tests/mp"); |
| 450 | return (0); |
| 451 | } |
| 452 | |
| 453 | #endif |
| 454 | |
| 455 | /*----- That's all, folks -------------------------------------------------*/ |