New operation to negate numbers.
authormdw <mdw>
Tue, 15 Oct 2002 19:18:31 +0000 (19:18 +0000)
committermdw <mdw>
Tue, 15 Oct 2002 19:18:31 +0000 (19:18 +0000)
mp-arith.c
mp.h
tests/mp

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 } },
 };
 
diff --git a/mp.h b/mp.h
index 6ccf523..f7a703c 100644 (file)
--- a/mp.h
+++ b/mp.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mp.h,v 1.14 2002/10/15 00:19:40 mdw Exp $
+ * $Id: mp.h,v 1.15 2002/10/15 19:18:31 mdw Exp $
  *
  * Simple multiprecision arithmetic
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mp.h,v $
+ * Revision 1.15  2002/10/15 19:18:31  mdw
+ * New operation to negate numbers.
+ *
  * Revision 1.14  2002/10/15 00:19:40  mdw
  * Bit setting and clearing functions.
  *
@@ -656,78 +659,57 @@ extern mp *mp_loadb2c(mp */*d*/, const void */*pv*/, size_t /*sz*/);
 
 extern void mp_storeb2c(const mp */*m*/, void */*pv*/, size_t /*sz*/);
 
-/*----- Simple arithmetic -------------------------------------------------*/
+/*----- Bit operations ----------------------------------------------------*/
 
-/* --- @mp_lsl@, @mp_lsr@ --- *
+/* --- @mp_not@ --- *
  *
  * Arguments:  @mp *d@ = destination
  *             @mp *a@ = source
- *             @size_t n@ = number of bits to move
  *
- * Returns:    Result, @a@ shifted left or right by @n@.
- */
+ * Returns:    The bitwise complement of the source.
+ */ 
 
-extern mp *mp_lsl(mp */*d*/, mp */*a*/, size_t /*n*/);
-extern mp *mp_lsr(mp */*d*/, mp */*a*/, size_t /*n*/);
+extern mp *mp_not(mp */*d*/, mp */*a*/);
 
-/* --- @mp_lsl2c@, @mp_lsr2c@ --- *
+/* --- @mp_bitop@ --- *
  *
  * Arguments:  @mp *d@ = destination
- *             @mp *a@ = source
- *             @size_t n@ = number of bits to move
+ *             @mp *a, *b@ = sources
  *
- * Returns:    Result, @a@ shifted left or right by @n@.  Handles the
- *             pretence of sign-extension for negative numbers.
+ * Returns:    The result of the given bitwise operation.  These functions
+ *             don't handle negative numbers at all sensibly.  For that, use
+ *             the @...2c@ variants.  The functions are named after the
+ *             truth tables they generate:
+ *
+ *                     a:      0011
+ *                     b:      0101
+ *                     @mpx_bitXXXX@
  */
 
-extern mp *mp_lsl2c(mp */*d*/, mp */*a*/, size_t /*n*/);
-extern mp *mp_lsr2c(mp */*d*/, mp */*a*/, size_t /*n*/);
+#define MP_BITDECL(string)                                             \
+  extern mp *mp_bit##string(mp */*d*/, mp */*a*/, mp */*b*/);
+MPX_DOBIN(MP_BITDECL)
 
-/* --- @mp_testbit@ --- *
- *
- * Arguments:  @mp *x@ = a large integer
- *             @unsigned long n@ = which bit to test
+/* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
  *
- * Returns:    Nonzero if the bit is set, zero if not.
+ * Synonyms for the commonly-used functions.
  */
 
-extern int mp_testbit(mp */*x*/, unsigned long /*n*/);
+#define mp_and  mp_bit0001
+#define mp_or   mp_bit0111
+#define mp_nand mp_bit1110
+#define mp_nor  mp_bit1000
+#define mp_xor  mp_bit0110
 
-/* --- @mp_testbit2c@ --- *
+/* --- @mp_testbit@ --- *
  *
  * Arguments:  @mp *x@ = a large integer
  *             @unsigned long n@ = which bit to test
  *
- * Returns:    Nonzero if the bit is set, zero if not.  Fakes up two's
- *             complement representation.
- */
-
-extern int mp_testbit2c(mp */*x*/, unsigned long /*n*/);
-
-/* --- @mp_eq@ --- *
- *
- * Arguments:  @const mp *a, *b@ = two numbers
- *
- * Returns:    Nonzero if the numbers are equal.
- */
-
-extern int mp_eq(const mp */*a*/, const mp */*b*/);
-
-#define MP_EQ(a, b)                                                    \
-  ((((a)->f ^ (b)->f) & MP_NEG) == 0 &&                                        \
-   mpx_ueq((a)->v, (a)->vl, (b)->v, (b)->vl))
-
-/* --- @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@.
+ * Returns:    Nonzero if the bit is set, zero if not.
  */
 
-extern int mp_cmp(const mp */*a*/, const mp */*b*/);
-
-#define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
+extern int mp_testbit(mp */*x*/, unsigned long /*n*/);
 
 /* --- @mp_setbit@, @mp_clearbit@ --- *
  *
@@ -741,58 +723,27 @@ extern int mp_cmp(const mp */*a*/, const mp */*b*/);
 extern mp *mp_setbit(mp */*d*/, mp */*x*/, unsigned long /*n*/);
 extern mp *mp_clearbit(mp */*d*/, mp */*x*/, unsigned long /*n*/);
 
-/* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
- *
- * Arguments:  @mp *d@ = a destination
- *             @mp *x@ = a large integer
- *             @unsigned long n@ = which bit to modify
- *
- * Returns:    The argument @x@, with the appropriate bit set or cleared.
- *             Fakes up two's complement representation.
- */
-
-extern mp *mp_setbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/);
-extern mp *mp_clearbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/);
-
-/* --- @mp_bitop@ --- *
+/* --- @mp_lsl@, @mp_lsr@ --- *
  *
  * Arguments:  @mp *d@ = destination
- *             @mp *a, *b@ = sources
- *
- * Returns:    The result of the given bitwise operation.  These functions
- *             don't handle negative numbers at all sensibly.  For that, use
- *             the @...2c@ variants.  The functions are named after the
- *             truth tables they generate:
- *
- *                     a:      0011
- *                     b:      0101
- *                     @mpx_bitXXXX@
- */
-
-#define MP_BITDECL(string)                                             \
-  extern mp *mp_bit##string(mp */*d*/, mp */*a*/, mp */*b*/);
-MPX_DOBIN(MP_BITDECL)
-
-/* --- @mp_[n]and@, @mp_[n]or@, @mp_[n]xor@, @mp_not@ --- *
+ *             @mp *a@ = source
+ *             @size_t n@ = number of bits to move
  *
- * Synonyms for the commonly-used functions.
+ * Returns:    Result, @a@ shifted left or right by @n@.
  */
 
-#define mp_and  mp_bit0001
-#define mp_or   mp_bit0111
-#define mp_nand mp_bit1110
-#define mp_nor  mp_bit1000
-#define mp_xor  mp_bit0110
+extern mp *mp_lsl(mp */*d*/, mp */*a*/, size_t /*n*/);
+extern mp *mp_lsr(mp */*d*/, mp */*a*/, size_t /*n*/);
 
-/* --- @mp_not@ --- *
+/* --- @mp_not2c@ --- *
  *
  * Arguments:  @mp *d@ = destination
  *             @mp *a@ = source
  *
- * Returns:    The bitwise complement of the source.
- */ 
+ * Returns:    The sign-extended complement of the argument.
+ */
 
-extern mp *mp_not(mp */*d*/, mp */*a*/);
+extern mp *mp_not2c(mp */*d*/, mp */*a*/);
 
 /* --- @mp_bitop2c@ --- *
  *
@@ -824,15 +775,83 @@ MPX_DOBIN(MP_BIT2CDECL)
 #define mp_nor2c  mp_bit10002c
 #define mp_xor2c  mp_bit01102c
 
-/* --- @mp_not2c@ --- *
+/* --- @mp_lsl2c@, @mp_lsr2c@ --- *
  *
  * Arguments:  @mp *d@ = destination
  *             @mp *a@ = source
+ *             @size_t n@ = number of bits to move
  *
- * Returns:    The sign-extended complement of the argument.
+ * Returns:    Result, @a@ shifted left or right by @n@.  Handles the
+ *             pretence of sign-extension for negative numbers.
  */
 
-extern mp *mp_not2c(mp */*d*/, mp */*a*/);
+extern mp *mp_lsl2c(mp */*d*/, mp */*a*/, size_t /*n*/);
+extern mp *mp_lsr2c(mp */*d*/, mp */*a*/, size_t /*n*/);
+
+/* --- @mp_testbit2c@ --- *
+ *
+ * Arguments:  @mp *x@ = a large integer
+ *             @unsigned long n@ = which bit to test
+ *
+ * Returns:    Nonzero if the bit is set, zero if not.  Fakes up two's
+ *             complement representation.
+ */
+
+extern int mp_testbit2c(mp */*x*/, unsigned long /*n*/);
+
+/* --- @mp_setbit2c@, @mp_clearbit2c@ --- *
+ *
+ * Arguments:  @mp *d@ = a destination
+ *             @mp *x@ = a large integer
+ *             @unsigned long n@ = which bit to modify
+ *
+ * Returns:    The argument @x@, with the appropriate bit set or cleared.
+ *             Fakes up two's complement representation.
+ */
+
+extern mp *mp_setbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/);
+extern mp *mp_clearbit2c(mp */*d*/, mp */*x*/, unsigned long /*n*/);
+
+/*----- Comparisons -------------------------------------------------------*/
+
+/* --- @mp_eq@ --- *
+ *
+ * Arguments:  @const mp *a, *b@ = two numbers
+ *
+ * Returns:    Nonzero if the numbers are equal.
+ */
+
+extern int mp_eq(const mp */*a*/, const mp */*b*/);
+
+#define MP_EQ(a, b)                                                    \
+  ((((a)->f ^ (b)->f) & MP_NEG) == 0 &&                                        \
+   mpx_ueq((a)->v, (a)->vl, (b)->v, (b)->vl))
+
+/* --- @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@.
+ */
+
+extern int mp_cmp(const mp */*a*/, const mp */*b*/);
+
+#define MP_CMP(a, op, b) (mp_cmp((a), (b)) op 0)
+
+/*----- Arithmetic operations ---------------------------------------------*/
+
+/* --- @mp_neg@ --- *
+ *
+ * Arguments:  @mp *d@ = destination
+ *             @mp *a@ = argument
+ *
+ * Returns:    The negation of the argument.
+ *
+ * Use:                Negates its argument.
+ */
+
+extern mp *mp_neg(mp */*d*/, mp */*a*/);
 
 /* --- @mp_add@ --- *
  *
index ea51574..6fe61e5 100644 (file)
--- a/tests/mp
+++ b/tests/mp
@@ -1,6 +1,6 @@
 # Test vectors for MP functions
 #
-# $Id: mp,v 1.11 2002/10/15 00:19:40 mdw Exp $
+# $Id: mp,v 1.12 2002/10/15 19:18:31 mdw Exp $
 
 add {
   5 4 9; 5 -4 1; -5 4 -1; -5 -4 -9;
@@ -51,6 +51,12 @@ clrbit {
   -1 1 -3;
 }
 
+neg {
+  0 0;
+  15 -15;
+  -15 15;
+}
+
 odd {
   1 0 1;
   2 1 1;