3 * $Id: mp-arith.c,v 1.1 1999/11/17 18:02:16 mdw Exp $
5 * Basic arithmetic on multiprecision integers
7 * (c) 1999 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
32 * $Log: mp-arith.c,v $
33 * Revision 1.1 1999/11/17 18:02:16 mdw
34 * New multiprecision integer arithmetic suite.
38 /*----- Header files ------------------------------------------------------*/
42 /*----- Main code ---------------------------------------------------------*/
46 * Arguments: @mp *a@ = source
48 * Returns: Result, @a@ converted to two's complement notation.
51 mp *mp_2c(mp *d, mp *a)
56 MP_MODIFY(d, MP_LEN(a));
57 mpx_2c(d->v, d->vl, a->v, a->vl);
58 d->f = a->f & MP_BURN;
65 * Arguments: @mp *d@ = destination
68 * Returns: Result, @a@ converted to the native signed-magnitude
72 mp *mp_sm(mp *d, mp *a)
74 if (!MP_LEN(a) || a->vl[-1] < MPW_MAX / 2)
77 MP_MODIFY(d, MP_LEN(a));
78 mpx_2c(d->v, d->vl, a->v, a->vl);
79 d->f = (a->f & (MP_BURN | MP_NEG)) ^ MP_NEG;
86 * Arguments: @mp *d@ = destination
87 * @const mp *a@ = source
88 * @size_t n@ = number of bits to move
90 * Returns: Result, @a@ shifted left by @n@.
93 mp *mp_lsl(mp *d, const mp *a, size_t n)
95 MP_MODIFY(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS);
96 mpx_lsl(d->v, d->vl, a->v, a->vl, n);
97 d->f = a->f & (MP_NEG | MP_BURN);
102 /* --- @mp_lsr@ --- *
104 * Arguments: @mp *d@ = destination
105 * @const mp *a@ = source
106 * @size_t n@ = number of bits to move
108 * Returns: Result, @a@ shifted left by @n@.
111 mp *mp_lsr(mp *d, const mp *a, size_t n)
113 MP_MODIFY(d, MP_LEN(a));
114 mpx_lsr(d->v, d->vl, a->v, a->vl, n);
115 d->f = a->f & (MP_NEG | MP_BURN);
120 /* --- @mp_cmp@ --- *
122 * Arguments: @const mp *a, *b@ = two numbers
124 * Returns: Less than, equal to or greater than zero, according to
125 * whether @a@ is less than, equal to or greater than @b@.
128 int mp_cmp(const mp *a, const mp *b)
130 if (!((a->f ^ b->f) & MP_NEG))
131 return (mpx_ucmp(a->v, a->vl, b->v, b->vl));
132 else if (a->f & MP_NEG)
138 /* --- @mp_add@ --- *
140 * Arguments: @mp *d@ = destination
141 * @const mp *a, *b@ = sources
143 * Returns: Result, @a@ added to @b@.
146 mp *mp_add(mp *d, const mp *a, const mp *b)
148 MP_MODIFY(d, (MP_LEN(a) > MP_LEN(b) ? MP_LEN(a) : MP_LEN(b)) + 1);
149 if (!((a->f ^ b->f) & MP_NEG))
150 mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
152 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
153 const mp *t = a; a = b; b = t;
155 mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
157 d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG);
162 /* --- @mp_sub@ --- *
164 * Arguments: @mp *d@ = destination
165 * @const mp *a, *b@ = sources
167 * Returns: Result, @b@ subtracted from @a@.
170 mp *mp_sub(mp *d, const mp *a, const mp *b)
173 MP_MODIFY(d, (MP_LEN(a) > MP_LEN(b) ? MP_LEN(a) : MP_LEN(b)) + 1);
174 if ((a->f ^ b->f) & MP_NEG)
175 mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
177 if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
178 const mp *t = a; a = b; b = t;
181 mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
183 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG);
188 /* --- @mp_mul@ --- *
190 * Arguments: @mp *d@ = destination
191 * @const mp *a, *b@ = sources
193 * Returns: Result, @a@ multiplied by @b@.
196 mp *mp_mul(mp *d, const mp *a, const mp *b)
198 if (d == a || d == b)
200 MP_MODIFY(d, MP_LEN(a) + MP_LEN(b));
201 mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
202 d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
207 /* --- @mp_sqr@ --- *
209 * Arguments: @mp *d@ = destination
210 * @const mp *a@ = source
212 * Returns: Result, @a@ squared.
215 mp *mp_sqr(mp *d, const mp *a)
219 MP_MODIFY(d, 2 * MP_LEN(a));
220 mpx_usqr(d->v, d->vl, a->v, a->vl);
221 d->f = a->f & MP_BURN;
226 /* --- @mp_div@ --- *
228 * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
229 * @const mp *a, *b@ = sources
231 * Use: Calculates the quotient and remainder when @a@ is divided by
232 * @b@. The destinations @*qq@ and @*rr@ must be distinct.
233 * Either of @qq@ or @rr@ may be null to indicate that the
234 * result is irrelevant. (Discarding both results is silly.)
235 * There is a performance advantage if @a == *rr@.
237 * The behaviour when @a@ and @b@ have the same sign is
238 * straightforward. When the signs differ, this implementation
239 * chooses @r@ to have the same sign as @b@, rather than the
240 * more normal choice that the remainder has the same sign as
241 * the dividend. This makes modular arithmetic a little more
245 void mp_div(mp **qq, mp **rr, const mp *a, const mp *b)
247 mp *r = rr ? *rr : MP_NEW;
248 mp *q = qq ? *qq : MP_NEW;
251 /* --- Set up some temporary workspace --- */
254 size_t rq = MP_LEN(b) + 1;
259 /* --- Set the remainder up right --- *
261 * Just in case the divisor is larger, be able to cope with this. It's not
262 * important in @mpx_udiv@, but it is here because of the sign correction.
266 size_t rq = MP_LEN(a) + 2;
272 MP_ENSURE(r, MP_LEN(r) + 2);
276 MP_MODIFY(r, MP_LEN(a) + 2);
277 memcpy(r->v, a->v, MPWS(MP_LEN(a)));
278 memset(r->v + MP_LEN(a), 0, MPWS(2));
282 /* --- Fix up the quotient too --- */
284 if (q == a || q == b)
286 MP_MODIFY(q, MP_LEN(a));
288 /* --- Perform the calculation --- */
290 mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl);
292 /* --- Sort out the sign of the results --- *
294 * If the signs of the arguments differ, and the remainder is nonzero, I
295 * must add one to the absolute value of the quotient and subtract the
296 * remainder from @b@.
299 q->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
304 MPX_UADDN(q->v, q->vl, 1);
305 mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl);
311 r->f = ((a->f | b->f) & MP_BURN) | (b->f & MP_NEG);
313 /* --- Store the return values --- */
332 /*----- Test rig ----------------------------------------------------------*/
336 static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
338 if (MP_CMP(expect, !=, result)) {
339 fprintf(stderr, "\n*** %s failed", op);
340 fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
341 fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10);
342 fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 10);
343 fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 10);
350 #define RIG(name, op) \
351 static int t ## name(dstr *v) \
353 mp *a = *(mp **)v[0].buf; \
354 mpw n = *(int *)v[1].buf; \
356 mp *r = *(mp **)v[2].buf; \
357 mp *c = op(MP_NEW, a, n); \
359 mp_build(&b, &n, &n + 1); \
360 ok = verify(#name, r, c, a, &b); \
361 mp_drop(a); mp_drop(c); mp_drop(r); \
370 #define RIG(name, op) \
371 static int t ## name(dstr *v) \
373 mp *a = *(mp **)v[0].buf; \
374 mp *b = *(mp **)v[1].buf; \
375 mp *r = *(mp **)v[2].buf; \
376 mp *c = op(MP_NEW, a, b); \
377 int ok = verify(#name, r, c, a, b); \
378 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
388 static int tdiv(dstr *v)
390 mp *a = *(mp **)v[0].buf;
391 mp *b = *(mp **)v[1].buf;
392 mp *q = *(mp **)v[2].buf;
393 mp *r = *(mp **)v[3].buf;
394 mp *c = MP_NEW, *d = MP_NEW;
396 mp_div(&c, &d, a, b);
397 ok &= verify("div(quotient)", q, c, a, b);
398 ok &= verify("div(remainder)", r, d, a, b);
399 mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q);
403 static test_chunk tests[] = {
404 { "lsl", tlsl, { &type_mp, &type_mp, &type_mp, 0 } },
405 { "lsr", tlsr, { &type_mp, &type_mp, &type_mp, 0 } },
406 { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } },
407 { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } },
408 { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } },
409 { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
413 int main(int argc, char *argv[])
416 test_run(argc, argv, tests, SRCDIR "/tests/mp");
422 /*----- That's all, folks -------------------------------------------------*/