+/* -*-c-*-
+ *
+ * $Id: mp-arith.c,v 1.1 1999/11/17 18:02:16 mdw Exp $
+ *
+ * Basic arithmetic on multiprecision integers
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------*
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------*
+ *
+ * $Log: mp-arith.c,v $
+ * Revision 1.1 1999/11/17 18:02:16 mdw
+ * New multiprecision integer arithmetic suite.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "mp.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @mp_2c@ --- *
+ *
+ * Arguments: @mp *a@ = source
+ *
+ * Returns: Result, @a@ converted to two's complement notation.
+ */
+
+mp *mp_2c(mp *d, mp *a)
+{
+ if (!(a->f & MP_NEG))
+ return (MP_COPY(a));
+
+ MP_MODIFY(d, MP_LEN(a));
+ mpx_2c(d->v, d->vl, a->v, a->vl);
+ d->f = a->f & MP_BURN;
+ MP_SHRINK(d);
+ return (d);
+}
+
+/* --- @mp_sm@ --- *
+ *
+ * Arguments: @mp *d@ = destination
+ * @mp *a@ = source
+ *
+ * Returns: Result, @a@ converted to the native signed-magnitude
+ * notation.
+ */
+
+mp *mp_sm(mp *d, mp *a)
+{
+ if (!MP_LEN(a) || a->vl[-1] < MPW_MAX / 2)
+ return (MP_COPY(a));
+
+ MP_MODIFY(d, MP_LEN(a));
+ mpx_2c(d->v, d->vl, a->v, a->vl);
+ d->f = (a->f & (MP_BURN | MP_NEG)) ^ MP_NEG;
+ MP_SHRINK(d);
+ return (d);
+}
+
+/* --- @mp_lsl@ --- *
+ *
+ * Arguments: @mp *d@ = destination
+ * @const mp *a@ = source
+ * @size_t n@ = number of bits to move
+ *
+ * Returns: Result, @a@ shifted left by @n@.
+ */
+
+mp *mp_lsl(mp *d, const mp *a, size_t n)
+{
+ MP_MODIFY(d, MP_LEN(a) + (n + MPW_BITS - 1) / MPW_BITS);
+ mpx_lsl(d->v, d->vl, a->v, a->vl, n);
+ d->f = a->f & (MP_NEG | MP_BURN);
+ MP_SHRINK(d);
+ return (d);
+}
+
+/* --- @mp_lsr@ --- *
+ *
+ * Arguments: @mp *d@ = destination
+ * @const mp *a@ = source
+ * @size_t n@ = number of bits to move
+ *
+ * Returns: Result, @a@ shifted left by @n@.
+ */
+
+mp *mp_lsr(mp *d, const mp *a, size_t n)
+{
+ MP_MODIFY(d, MP_LEN(a));
+ mpx_lsr(d->v, d->vl, a->v, a->vl, n);
+ d->f = a->f & (MP_NEG | MP_BURN);
+ MP_SHRINK(d);
+ return (d);
+}
+
+/* --- @mp_cmp@ --- *
+ *
+ * Arguments: @const mp *a, *b@ = two numbers
+ *
+ * Returns: Less than, equal to or greater than zero, according to
+ * whether @a@ is less than, equal to or greater than @b@.
+ */
+
+int mp_cmp(const mp *a, const mp *b)
+{
+ if (!((a->f ^ b->f) & MP_NEG))
+ return (mpx_ucmp(a->v, a->vl, b->v, b->vl));
+ else if (a->f & MP_NEG)
+ return (-1);
+ else
+ return (+1);
+}
+
+/* --- @mp_add@ --- *
+ *
+ * Arguments: @mp *d@ = destination
+ * @const mp *a, *b@ = sources
+ *
+ * Returns: Result, @a@ added to @b@.
+ */
+
+mp *mp_add(mp *d, const mp *a, const mp *b)
+{
+ MP_MODIFY(d, (MP_LEN(a) > MP_LEN(b) ? MP_LEN(a) : MP_LEN(b)) + 1);
+ if (!((a->f ^ b->f) & MP_NEG))
+ mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
+ else {
+ if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
+ const mp *t = a; a = b; b = t;
+ }
+ mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
+ }
+ d->f = ((a->f | b->f) & MP_BURN) | (a->f & MP_NEG);
+ MP_SHRINK(d);
+ return (d);
+}
+
+/* --- @mp_sub@ --- *
+ *
+ * Arguments: @mp *d@ = destination
+ * @const mp *a, *b@ = sources
+ *
+ * Returns: Result, @b@ subtracted from @a@.
+ */
+
+mp *mp_sub(mp *d, const mp *a, const mp *b)
+{
+ unsigned sgn = 0;
+ MP_MODIFY(d, (MP_LEN(a) > MP_LEN(b) ? MP_LEN(a) : MP_LEN(b)) + 1);
+ if ((a->f ^ b->f) & MP_NEG)
+ mpx_uadd(d->v, d->vl, a->v, a->vl, b->v, b->vl);
+ else {
+ if (MPX_UCMP(a->v, a->vl, <, b->v, b->vl)) {
+ const mp *t = a; a = b; b = t;
+ sgn = MP_NEG;
+ }
+ mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
+ }
+ d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ sgn) & MP_NEG);
+ MP_SHRINK(d);
+ return (d);
+}
+
+/* --- @mp_mul@ --- *
+ *
+ * Arguments: @mp *d@ = destination
+ * @const mp *a, *b@ = sources
+ *
+ * Returns: Result, @a@ multiplied by @b@.
+ */
+
+mp *mp_mul(mp *d, const mp *a, const mp *b)
+{
+ if (d == a || d == b)
+ d = MP_NEW;
+ MP_MODIFY(d, MP_LEN(a) + MP_LEN(b));
+ mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
+ d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
+ MP_SHRINK(d);
+ return (d);
+}
+
+/* --- @mp_sqr@ --- *
+ *
+ * Arguments: @mp *d@ = destination
+ * @const mp *a@ = source
+ *
+ * Returns: Result, @a@ squared.
+ */
+
+mp *mp_sqr(mp *d, const mp *a)
+{
+ if (d == a)
+ d = MP_NEW;
+ MP_MODIFY(d, 2 * MP_LEN(a));
+ mpx_usqr(d->v, d->vl, a->v, a->vl);
+ d->f = a->f & MP_BURN;
+ MP_SHRINK(d);
+ return (d);
+}
+
+/* --- @mp_div@ --- *
+ *
+ * Arguments: @mp **qq, **rr@ = destination, quotient and remainder
+ * @const mp *a, *b@ = sources
+ *
+ * Use: Calculates the quotient and remainder when @a@ is divided by
+ * @b@. The destinations @*qq@ and @*rr@ must be distinct.
+ * Either of @qq@ or @rr@ may be null to indicate that the
+ * result is irrelevant. (Discarding both results is silly.)
+ * There is a performance advantage if @a == *rr@.
+ *
+ * The behaviour when @a@ and @b@ have the same sign is
+ * straightforward. When the signs differ, this implementation
+ * chooses @r@ to have the same sign as @b@, rather than the
+ * more normal choice that the remainder has the same sign as
+ * the dividend. This makes modular arithmetic a little more
+ * straightforward.
+ */
+
+void mp_div(mp **qq, mp **rr, const mp *a, const mp *b)
+ {
+ mp *r = rr ? *rr : MP_NEW;
+ mp *q = qq ? *qq : MP_NEW;
+ mpw *sv, *svl;
+
+ /* --- Set up some temporary workspace --- */
+
+ {
+ size_t rq = MP_LEN(b) + 1;
+ sv = MP_ALLOC(rq);
+ svl = sv + rq;
+ }
+
+ /* --- Set the remainder up right --- *
+ *
+ * Just in case the divisor is larger, be able to cope with this. It's not
+ * important in @mpx_udiv@, but it is here because of the sign correction.
+ */
+
+ {
+ size_t rq = MP_LEN(a) + 2;
+ if (MP_LEN(b) > rq)
+ rq = MP_LEN(b);
+
+ if (r == a) {
+ MP_SPLIT(r);
+ MP_ENSURE(r, MP_LEN(r) + 2);
+ } else {
+ if (r == b)
+ r = MP_NEW;
+ MP_MODIFY(r, MP_LEN(a) + 2);
+ memcpy(r->v, a->v, MPWS(MP_LEN(a)));
+ memset(r->v + MP_LEN(a), 0, MPWS(2));
+ }
+ }
+
+ /* --- Fix up the quotient too --- */
+
+ if (q == a || q == b)
+ q = MP_NEW;
+ MP_MODIFY(q, MP_LEN(a));
+
+ /* --- Perform the calculation --- */
+
+ mpx_udiv(q->v, q->vl, r->v, r->vl, b->v, b->vl, sv, svl);
+
+ /* --- Sort out the sign of the results --- *
+ *
+ * If the signs of the arguments differ, and the remainder is nonzero, I
+ * must add one to the absolute value of the quotient and subtract the
+ * remainder from @b@.
+ */
+
+ q->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
+ if (q->f & MP_NEG) {
+ mpw *v = r->v;
+ while (v < r->vl) {
+ if (*v) {
+ MPX_UADDN(q->v, q->vl, 1);
+ mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl);
+ break;
+ }
+ }
+ }
+
+ r->f = ((a->f | b->f) & MP_BURN) | (b->f & MP_NEG);
+
+ /* --- Store the return values --- */
+
+ if (!qq)
+ MP_DROP(q);
+ else {
+ MP_SHRINK(q);
+ *qq = q;
+ }
+
+ if (!rr)
+ MP_DROP(r);
+ else {
+ MP_SHRINK(r);
+ *rr = r;
+ }
+
+ MP_FREE(sv);
+}
+
+/*----- Test rig ----------------------------------------------------------*/
+
+#ifdef TEST_RIG
+
+static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
+{
+ if (MP_CMP(expect, !=, result)) {
+ fprintf(stderr, "\n*** %s failed", op);
+ fputs("\n*** a = ", stderr); mp_writefile(a, stderr, 10);
+ fputs("\n*** b = ", stderr); mp_writefile(b, stderr, 10);
+ fputs("\n*** result = ", stderr); mp_writefile(result, stderr, 10);
+ fputs("\n*** expect = ", stderr); mp_writefile(expect, stderr, 10);
+ fputc('\n', stderr);
+ return (0);
+ }
+ return (1);
+}
+
+#define RIG(name, op) \
+ static int t ## name(dstr *v) \
+ { \
+ mp *a = *(mp **)v[0].buf; \
+ mpw n = *(int *)v[1].buf; \
+ mp b; \
+ mp *r = *(mp **)v[2].buf; \
+ mp *c = op(MP_NEW, a, n); \
+ int ok; \
+ mp_build(&b, &n, &n + 1); \
+ ok = verify(#name, r, c, a, &b); \
+ mp_drop(a); mp_drop(c); mp_drop(r); \
+ return (ok); \
+ }
+
+RIG(lsl, mp_lsl)
+RIG(lsr, mp_lsr)
+
+#undef RIG
+
+#define RIG(name, op) \
+ static int t ## name(dstr *v) \
+ { \
+ mp *a = *(mp **)v[0].buf; \
+ mp *b = *(mp **)v[1].buf; \
+ mp *r = *(mp **)v[2].buf; \
+ mp *c = op(MP_NEW, a, b); \
+ int ok = verify(#name, r, c, a, b); \
+ mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(r); \
+ return (ok); \
+ }
+
+RIG(add, mp_add)
+RIG(sub, mp_sub)
+RIG(mul, mp_mul)
+
+#undef RIG
+
+static int tdiv(dstr *v)
+{
+ mp *a = *(mp **)v[0].buf;
+ mp *b = *(mp **)v[1].buf;
+ mp *q = *(mp **)v[2].buf;
+ mp *r = *(mp **)v[3].buf;
+ mp *c = MP_NEW, *d = MP_NEW;
+ int ok = 1;
+ mp_div(&c, &d, a, b);
+ ok &= verify("div(quotient)", q, c, a, b);
+ ok &= verify("div(remainder)", r, d, a, b);
+ mp_drop(a); mp_drop(b); mp_drop(c); mp_drop(d); mp_drop(r); mp_drop(q);
+ return (ok);
+}
+
+static test_chunk tests[] = {
+ { "lsl", tlsl, { &type_mp, &type_mp, &type_mp, 0 } },
+ { "lsr", tlsr, { &type_mp, &type_mp, &type_mp, 0 } },
+ { "add", tadd, { &type_mp, &type_mp, &type_mp, 0 } },
+ { "sub", tsub, { &type_mp, &type_mp, &type_mp, 0 } },
+ { "mul", tmul, { &type_mp, &type_mp, &type_mp, 0 } },
+ { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
+ { 0, 0, { 0 } },
+};
+
+int main(int argc, char *argv[])
+{
+ sub_init();
+ test_run(argc, argv, tests, SRCDIR "/tests/mp");
+ return (0);
+}
+
+#endif
+
+/*----- That's all, folks -------------------------------------------------*/