Handy new comparison macros.
[u/mdw/catacomb] / mp-arith.c
index e47f301..a070675 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mp-arith.c,v 1.13 2002/10/15 00:19:40 mdw Exp $
+ * $Id: mp-arith.c,v 1.14 2002/10/15 19:18:31 mdw Exp $
  *
  * Basic arithmetic on multiprecision integers
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mp-arith.c,v $
+ * Revision 1.14  2002/10/15 19:18:31  mdw
+ * New operation to negate numbers.
+ *
  * Revision 1.13  2002/10/15 00:19:40  mdw
  * Bit setting and clearing functions.
  *
@@ -271,6 +274,31 @@ int mp_cmp(const mp *a, const mp *b)
     return (+1);
 }
 
+/* --- @mp_neg@ --- *
+ *
+ * Arguments:  @mp *d@ = destination
+ *             @mp *a@ = argument
+ *
+ * Returns:    The negation of the argument.
+ *
+ * Use:                Negates its argument.
+ */
+
+mp *mp_neg(mp *d, mp *a)
+{
+  /* --- Surprising amounts of messing about required --- */
+
+  MP_SHRINK(a);
+  MP_COPY(a);
+  if (d) MP_DROP(d);
+  if (a->v == a->vl) {
+    return (a);
+  }
+  MP_DEST(a, MP_LEN(a), a->f);
+  a->f ^= MP_NEG;
+  return (a);
+}
+
 /* --- @mp_bitop@ --- *
  *
  * Arguments:  @mp *d@ = destination
@@ -850,6 +878,36 @@ static int tclr(dstr *v)
   return (ok);
 }
 
+static int tneg(dstr *v)
+{
+  mp *a = *(mp **)v[0].buf;
+  mp *r = *(mp **)v[1].buf;
+  int ok = 1;
+  mp *n = mp_neg(MP_NEW, a);
+  if (!MP_EQ(r, n)) {
+    ok = 0;
+    fprintf(stderr, "\n*** neg failed\n");
+    fputs("\n*** a  = ", stderr); mp_writefile(a, stderr, 10);
+    fputs("\n*** r  = ", stderr); mp_writefile(r, stderr, 10);
+    fputs("\n*** n  = ", stderr); mp_writefile(n, stderr, 10);
+    fputc('\n', stderr);
+  }
+  mp_drop(n);
+  n = mp_neg(a, a);
+  if (!MP_EQ(r, n)) {
+    ok = 0;
+    fprintf(stderr, "\n*** neg failed\n");
+    fputs("\n*** a* = ", stderr); mp_writefile(a, stderr, 10);
+    fputs("\n*** r  = ", stderr); mp_writefile(r, stderr, 10);
+    fputs("\n*** n  = ", stderr); mp_writefile(n, stderr, 10);
+    fputc('\n', stderr);
+  }
+  mp_drop(a);
+  mp_drop(r);
+  assert(mparena_count(MPARENA_GLOBAL) == 0);
+  return (ok);  
+}
+
 static int todd(dstr *v)
 {
   mp *a = *(mp **)v[0].buf;
@@ -889,6 +947,7 @@ static test_chunk tests[] = {
   { "div", tdiv, { &type_mp, &type_mp, &type_mp, &type_mp, 0 } },
   { "bin2c", tbin, { &type_string, &type_mp, &type_mp, &type_mp, 0 } },
   { "odd", todd, { &type_mp, &type_uint32, &type_mp, 0 } },
+  { "neg", tneg, { &type_mp, &type_mp, 0 } },
   { 0, 0, { 0 } },
 };