factorial: Fix usage message to fit in with conventions.
[u/mdw/catacomb] / mp-arith.c
index aac408f..3af1447 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mp-arith.c,v 1.16.2.1 2003/06/10 13:21:10 mdw Exp $
+ * $Id$
  *
  * Basic arithmetic on multiprecision integers
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: mp-arith.c,v $
- * Revision 1.16.2.1  2003/06/10 13:21:10  mdw
- * Fix bug dividing small things by large ones.
- *
- * Revision 1.16  2003/05/16 09:09:24  mdw
- * Fix @mp_lsl2c@.  Turns out to be surprisingly tricky.
- *
- * Revision 1.15  2002/10/19 17:56:50  mdw
- * Fix bit operations.  Test them (a bit) better.
- *
- * 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.
- *
- * Revision 1.12  2002/10/09 00:36:03  mdw
- * Fix bounds on workspace for Karatsuba operations.
- *
- * Revision 1.11  2002/10/06 22:52:50  mdw
- * Pile of changes for supporting two's complement properly.
- *
- * Revision 1.10  2001/04/03 19:36:05  mdw
- * Add some simple bitwise operations so that Perl can use them.
- *
- * Revision 1.9  2000/10/08 15:48:35  mdw
- * Rename Karatsuba constants now that we have @gfx_kmul@ too.
- *
- * Revision 1.8  2000/10/08 12:02:21  mdw
- * Use @MP_EQ@ instead of @MP_CMP@.
- *
- * Revision 1.7  2000/06/22 19:02:53  mdw
- * New function @mp_odd@ to extract powers of two from an integer.  This is
- * common code from the Rabin-Miller test, RSA key recovery and modular
- * square-root extraction.
- *
- * Revision 1.6  2000/06/17 11:45:09  mdw
- * Major memory management overhaul.  Added arena support.  Use the secure
- * arena for secret integers.  Replace and improve the MP management macros
- * (e.g., replace MP_MODIFY by MP_DEST).
- *
- * Revision 1.5  1999/12/22 15:54:41  mdw
- * Adjust Karatsuba parameters.  Calculate destination size better.
- *
- * Revision 1.4  1999/12/13 15:35:16  mdw
- * Slightly different rules on memory allocation.
- *
- * Revision 1.3  1999/12/11 10:57:43  mdw
- * Karatsuba squaring algorithm.
- *
- * 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.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "mp.h"
@@ -150,7 +90,7 @@ mp *mp_lsr(mp *d, mp *a, size_t n)
 
 mp *mp_lsl2c(mp *d, mp *a, size_t n)
 {
-  if (!(a->f & MP_NEG))
+  if (!MP_NEGP(a))
     return (mp_lsl(d, a, n));
   d = mp_not2c(d, a);
   d = mp_lslc(d, d, n);
@@ -160,7 +100,7 @@ mp *mp_lsl2c(mp *d, mp *a, size_t n)
 
 mp *mp_lsr2c(mp *d, mp *a, size_t n)
 {
-  if (!(a->f & MP_NEG))
+  if (!MP_NEGP(a))
     return (mp_lsr(d, a, n));
   d = mp_not2c(d, a);
   d = mp_lsr(d, d, n);
@@ -195,7 +135,7 @@ int mp_testbit(mp *x, unsigned long n)
 int mp_testbit2c(mp *x, unsigned long n)
 {
   int r;
-  if (!(x->f & MP_NEG))
+  if (!MP_NEGP(x))
     return (mp_testbit(x, n));
   x = mp_not2c(MP_NEW, x);
   r = !mp_testbit(x, n);
@@ -252,7 +192,7 @@ mp *mp_clearbit(mp *d, mp *x, unsigned long n)
 
 mp *mp_setbit2c(mp *d, mp *x, unsigned long n)
 {
-  if (!(x->f & MP_NEG))
+  if (!MP_NEGP(x))
     return mp_setbit(d, x, n);
   d = mp_not2c(d, x);
   d = mp_clearbit(d, d, n);
@@ -262,7 +202,7 @@ mp *mp_setbit2c(mp *d, mp *x, unsigned long n)
 
 mp *mp_clearbit2c(mp *d, mp *x, unsigned long n)
 {
-  if (!(x->f & MP_NEG))
+  if (!MP_NEGP(x))
     return mp_clearbit(d, x, n);
   d = mp_not2c(d, x);
   d = mp_setbit(d, d, n);
@@ -289,9 +229,12 @@ int mp_eq(const mp *a, const mp *b) { return (MP_EQ(a, 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)
+  if (!((a->f ^ b->f) & MP_NEG)) {
+    if (a->f & MP_NEG)
+      return (-mpx_ucmp(a->v, a->vl, b->v, b->vl));
+    else
+      return (mpx_ucmp(a->v, a->vl, b->v, b->vl));
+  } else if (a->f & MP_NEG)
     return (-1);
   else
     return (+1);
@@ -417,7 +360,7 @@ mp *mp_bit##n##2c(mp *d, mp *a, mp *b)                                      \
     p_bn                                                               \
   } else {                             /* Both negative */             \
     mp *t = mp_not2c(MP_NEW, a);                                       \
-    mp *d = mp_not2c(d, b);                                            \
+    d = mp_not2c(d, b);                                                        \
     d = mp_bit##abn(d, t, d);                                          \
     MP_DROP(t);                                                                \
     p_abn                                                              \
@@ -460,12 +403,12 @@ mp *mp_not2c(mp *d, mp *a)
 
   MP_DEST(d, MP_LEN(a) + 1, a->f);
   if (d == a) {
-    if (a->f & MP_NEG)
+    if (MP_NEGP(a))
       MPX_USUBN(d->v, d->vl, 1);
     else
       MPX_UADDN(d->v, d->vl, 1);
   } else {
-    if (a->f & MP_NEG)
+    if (MP_NEGP(a))
       mpx_usub(d->v, d->vl, a->v, a->vl, &one, &one + 1);
     else
       mpx_uadd(d->v, d->vl, a->v, a->vl, &one, &one + 1);
@@ -622,7 +565,7 @@ void mp_div(mp **qq, mp **rr, mp *a, mp *b)
   if (r)
     MP_DROP(r);
   r = a;
-  MP_DEST(r, MP_LEN(b) + 2, a->f | b->f);
+  MP_DEST(r, MAX(MP_LEN(a), MP_LEN(b)) + 2, a->f | b->f);
 
   /* --- Fix up the quotient too --- */
 
@@ -650,7 +593,7 @@ void mp_div(mp **qq, mp **rr, mp *a, mp *b)
    */
 
   q->f = ((r->f | b->f) & MP_BURN) | ((r->f ^ b->f) & MP_NEG);
-  if (q->f & MP_NEG) {
+  if (MP_NEGP(q)) {
     mpw *v;
     for (v = r->v; v < r->vl; v++) {
       if (*v) {
@@ -783,6 +726,7 @@ RIG(lsr2c, mp_lsr2c)
 RIG(add, mp_add)
 RIG(sub, mp_sub)
 RIG(mul, mp_mul)
+RIG(exp, mp_exp)
 
 #undef RIG
 
@@ -968,6 +912,7 @@ static test_chunk tests[] = {
   { "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 } },
+  { "exp", texp, { &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 } },