Change interface for suggested destinations.
[u/mdw/catacomb] / mp-arith.c
index d8381ee..6f00656 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mp-arith.c,v 1.1 1999/11/17 18:02:16 mdw Exp $
+ * $Id: mp-arith.c,v 1.2 1999/12/10 23:18:39 mdw Exp $
  *
  * Basic arithmetic on multiprecision integers
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mp-arith.c,v $
+ * Revision 1.2  1999/12/10 23:18:39  mdw
+ * Change interface for suggested destinations.
+ *
  * Revision 1.1  1999/11/17 18:02:16  mdw
  * New multiprecision integer arithmetic suite.
  *
 
 #include "mp.h"
 
+/*----- Macros ------------------------------------------------------------*/
+
+#define MAX(x, y) ((x) >= (y) ? (x) : (y))
+
 /*----- Main code ---------------------------------------------------------*/
 
 /* --- @mp_2c@ --- *
@@ -84,13 +91,13 @@ mp *mp_sm(mp *d, mp *a)
 /* --- @mp_lsl@ --- *
  *
  * Arguments:  @mp *d@ = destination
- *             @const mp *a@ = source
+ *             @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 *mp_lsl(mp *d, 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);
@@ -102,13 +109,13 @@ mp *mp_lsl(mp *d, const mp *a, size_t n)
 /* --- @mp_lsr@ --- *
  *
  * Arguments:  @mp *d@ = destination
- *             @const mp *a@ = source
+ *             @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 *mp_lsr(mp *d, mp *a, size_t n)
 {
   MP_MODIFY(d, MP_LEN(a));
   mpx_lsr(d->v, d->vl, a->v, a->vl, n);
@@ -138,19 +145,19 @@ int mp_cmp(const mp *a, const mp *b)
 /* --- @mp_add@ --- *
  *
  * Arguments:  @mp *d@ = destination
- *             @const mp *a, *b@ = sources
+ *             @mp *a, *b@ = sources
  *
  * Returns:    Result, @a@ added to @b@.
  */
 
-mp *mp_add(mp *d, const mp *a, const mp *b)
+mp *mp_add(mp *d, mp *a, mp *b)
 {
-  MP_MODIFY(d, (MP_LEN(a) > MP_LEN(b) ? MP_LEN(a) : MP_LEN(b)) + 1);
+  MP_MODIFY(d, MAX(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;
+      mp *t = a; a = b; b = t;
     }
     mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
   }
@@ -162,20 +169,20 @@ mp *mp_add(mp *d, const mp *a, const mp *b)
 /* --- @mp_sub@ --- *
  *
  * Arguments:  @mp *d@ = destination
- *             @const mp *a, *b@ = sources
+ *             @mp *a, *b@ = sources
  *
  * Returns:    Result, @b@ subtracted from @a@.
  */
 
-mp *mp_sub(mp *d, const mp *a, const mp *b)
+mp *mp_sub(mp *d, mp *a, mp *b)
 {
   unsigned sgn = 0;
-  MP_MODIFY(d, (MP_LEN(a) > MP_LEN(b) ? MP_LEN(a) : MP_LEN(b)) + 1);
+  MP_MODIFY(d, MAX(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;
+      mp *t = a; a = b; b = t;
       sgn = MP_NEG;
     }
     mpx_usub(d->v, d->vl, a->v, a->vl, b->v, b->vl);
@@ -188,45 +195,67 @@ mp *mp_sub(mp *d, const mp *a, const mp *b)
 /* --- @mp_mul@ --- *
  *
  * Arguments:  @mp *d@ = destination
- *             @const mp *a, *b@ = sources
+ *             @mp *a, *b@ = sources
  *
  * Returns:    Result, @a@ multiplied by @b@.
  */
 
-mp *mp_mul(mp *d, const mp *a, const mp *b)
+mp *mp_mul(mp *d, mp *a, mp *b)
 {
-  if (d == a || d == b)
-    d = MP_NEW;
+  a = MP_COPY(a);
+  b = MP_COPY(b);
+
   MP_MODIFY(d, MP_LEN(a) + MP_LEN(b));
-  mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
+  if (MP_LEN(a) <= KARATSUBA_CUTOFF || MP_LEN(b) <= KARATSUBA_CUTOFF)
+    mpx_umul(d->v, d->vl, a->v, a->vl, b->v, b->vl);
+  else {
+    size_t m = MAX(MP_LEN(a), MP_LEN(b)) * 2 + KARATSUBA_SLOP;
+    mpw *s;
+    m += 32;
+    s = MP_ALLOC(m);
+    mpx_kmul(d->v, d->vl, a->v, a->vl, b->v, b->vl, s, s + m);
+    MP_FREE(s);
+  }
+
   d->f = ((a->f | b->f) & MP_BURN) | ((a->f ^ b->f) & MP_NEG);
   MP_SHRINK(d);
+  MP_DROP(a);
+  MP_DROP(b);
   return (d);
 }
 
 /* --- @mp_sqr@ --- *
  *
  * Arguments:  @mp *d@ = destination
- *             @const mp *a@ = source
+ *             @mp *a@ = source
  *
  * Returns:    Result, @a@ squared.
  */
 
-mp *mp_sqr(mp *d, const mp *a)
+mp *mp_sqr(mp *d, 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);
+  size_t m = MP_LEN(a);
+
+  a = MP_COPY(a);
+  MP_MODIFY(d, 2 * m);
+  if (m > KARATSUBA_CUTOFF) {
+    mpw *s;
+    m = 2 * (m + 1) + 32;
+    s = MP_ALLOC(m);
+    mpx_kmul(d->v, d->vl, a->v, a->vl, a->v, a->vl, s, s + m);
+    MP_FREE(s);
+  } else 
+    mpx_usqr(d->v, d->vl, a->v, a->vl);
   d->f = a->f & MP_BURN;
   MP_SHRINK(d);
+  MP_DROP(a);
   return (d);
 }
 
 /* --- @mp_div@ --- *
  *
  * Arguments:  @mp **qq, **rr@ = destination, quotient and remainder
- *             @const mp *a, *b@ = sources
+ *             @mp *a, *b@ = sources
  *
  * Use:                Calculates the quotient and remainder when @a@ is divided by
  *             @b@.  The destinations @*qq@ and @*rr@ must be distinct.
@@ -242,7 +271,7 @@ mp *mp_sqr(mp *d, const mp *a)
  *             straightforward.
  */
 
-void mp_div(mp **qq, mp **rr, const mp *a, const mp *b)
+void mp_div(mp **qq, mp **rr, mp *a, mp *b)
  {
   mp *r = rr ? *rr : MP_NEW;
   mp *q = qq ? *qq : MP_NEW;
@@ -267,12 +296,13 @@ void mp_div(mp **qq, mp **rr, const mp *a, const mp *b)
     if (MP_LEN(b) > rq)
       rq = MP_LEN(b);
 
+    b = MP_COPY(b);
     if (r == a) {
-      MP_SPLIT(r);
+      MP_SPLIT(a);
+      a = r = MP_COPY(a);
       MP_ENSURE(r, MP_LEN(r) + 2);
     } else {
-      if (r == b)
-       r = MP_NEW;
+      a = MP_COPY(a);
       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));
@@ -281,8 +311,6 @@ void mp_div(mp **qq, mp **rr, const mp *a, const mp *b)
 
   /* --- Fix up the quotient too --- */
 
-  if (q == a || q == b)
-    q = MP_NEW;
   MP_MODIFY(q, MP_LEN(a));
 
   /* --- Perform the calculation --- */
@@ -298,8 +326,8 @@ void mp_div(mp **qq, mp **rr, const mp *a, const mp *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) {
+    mpw *v;
+    for (v = r->v; v < r->vl; v++) {
       if (*v) {
        MPX_UADDN(q->v, q->vl, 1);
        mpx_usub(r->v, r->vl, b->v, b->vl, r->v, r->vl);
@@ -326,6 +354,8 @@ void mp_div(mp **qq, mp **rr, const mp *a, const mp *b)
     *rr = r;
   }
 
+  MP_DROP(a);
+  MP_DROP(b);
   MP_FREE(sv);
 }
 
@@ -348,7 +378,7 @@ static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
 }
 
 #define RIG(name, op)                                                  \
-  static int t ## name(dstr *v)                                                \
+  static int t##name(dstr *v)                                          \
   {                                                                    \
     mp *a = *(mp **)v[0].buf;                                          \
     mpw n = *(int *)v[1].buf;                                          \
@@ -359,6 +389,7 @@ static int verify(const char *op, mp *expect, mp *result, mp *a, mp *b)
     mp_build(&b, &n, &n + 1);                                          \
     ok = verify(#name, r, c, a, &b);                                   \
     mp_drop(a); mp_drop(c); mp_drop(r);                                        \
+    assert(mparena_count(MPARENA_GLOBAL) == 0);                                \
     return (ok);                                                       \
   }
 
@@ -368,7 +399,7 @@ RIG(lsr, mp_lsr)
 #undef RIG
 
 #define RIG(name, op)                                                  \
-  static int t ## name(dstr *v)                                                \
+  static int t##name(dstr *v)                                          \
   {                                                                    \
     mp *a = *(mp **)v[0].buf;                                          \
     mp *b = *(mp **)v[1].buf;                                          \
@@ -376,6 +407,7 @@ RIG(lsr, mp_lsr)
     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);                    \
+    assert(mparena_count(MPARENA_GLOBAL) == 0);                                \
     return (ok);                                                       \
   }
 
@@ -397,6 +429,7 @@ static int tdiv(dstr *v)
   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);
+  assert(mparena_count(MPARENA_GLOBAL) == 0);
   return (ok);
 }