More changes. Still embryonic.
[u/mdw/catacomb] / mpx.h
diff --git a/mpx.h b/mpx.h
index 23c0b8d..509da02 100644 (file)
--- a/mpx.h
+++ b/mpx.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mpx.h,v 1.7 1999/12/11 01:51:28 mdw Exp $
+ * $Id: mpx.h,v 1.8 1999/12/11 10:57:43 mdw Exp $
  *
  * Low level multiprecision arithmetic
  *
@@ -30,6 +30,9 @@
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mpx.h,v $
+ * Revision 1.8  1999/12/11 10:57:43  mdw
+ * Karatsuba squaring algorithm.
+ *
  * Revision 1.7  1999/12/11 01:51:28  mdw
  * Change Karatsuba parameters slightly.
  *
@@ -487,10 +490,8 @@ extern void mpx_umuln(mpw */*dv*/, mpw */*dvl*/,
   mpw _cc = 0;                                                         \
   mpd _m = (m);                                                                \
                                                                        \
-  while (_av < _avl) {                                                 \
+  while (_dv < _dvl && _av < _avl) {                                   \
     mpd _x;                                                            \
-    if (_dv >= _dvl)                                                   \
-      break;                                                           \
     _x = (mpd)*_dv + (mpd)_m * (mpd)*_av++ + _cc;                      \
     *_dv++ = MPW(_x);                                                  \
     _cc = _x >> MPW_BITS;                                              \
@@ -515,6 +516,49 @@ extern void mpx_umlan(mpw */*dv*/, mpw */*dvl*/,
 extern void mpx_usqr(mpw */*dv*/, mpw */*dvl*/,
                     const mpw */*av*/, const mpw */*avl*/);
 
+/* --- @mpx_udiv@ --- *
+ *
+ * Arguments:  @mpw *qv, *qvl@ = quotient vector base and limit
+ *             @mpw *rv, *rvl@ = dividend/remainder vector base and limit
+ *             @const mpw *dv, *dvl@ = divisor vector base and limit
+ *             @mpw *sv, *svl@ = scratch workspace 
+ *
+ * Returns:    ---
+ *
+ * Use:                Performs unsigned integer division.  If the result overflows
+ *             the quotient vector, high-order bits are discarded.  (Clearly
+ *             the remainder vector can't overflow.)  The various vectors
+ *             may not overlap in any way.  Yes, I know it's a bit odd
+ *             requiring the dividend to be in the result position but it
+ *             does make some sense really.  The remainder must have
+ *             headroom for at least two extra words.  The scratch space
+ *             must be at least one word larger than the divisor.
+ */
+
+extern void mpx_udiv(mpw */*qv*/, mpw */*qvl*/, mpw */*rv*/, mpw */*rvl*/,
+                    const mpw */*dv*/, const mpw */*dvl*/,
+                    mpw */*sv*/, mpw */*svl*/);
+
+/*----- Karatsuba multiplication algorithms -------------------------------*/
+
+/* --- @KARATSUBA_CUTOFF@ --- *
+ *
+ * This is the limiting length for using Karatsuba algorithms.  It's best to
+ * use the simpler classical multiplication method on numbers smaller than
+ * this.
+ */
+
+#define KARATSUBA_CUTOFF 16
+
+/* --- @KARATSUBA_SLOP@ --- *
+ *
+ * The extra number of words required as scratch space by the Karatsuba
+ * routines.  This is a (generous) guess, since the actual amount of space
+ * required is proportional to the recursion depth.
+ */
+
+#define KARATSUBA_SLOP 32
+
 /* --- @mpx_kmul@ --- *
  *
  * Arguments:  @mpw *dv, *dvl@ = pointer to destination buffer
@@ -530,38 +574,37 @@ extern void mpx_usqr(mpw */*dv*/, mpw */*dvl*/,
  *             more expensive on small ones.
  *
  *             The destination and scratch buffers must be twice as large as
- *             the larger argument.
+ *             the larger argument.  The scratch space must be twice as
+ *             large as the larger argument, plus the magic number
+ *             @KARATSUBA_SLOP@.
  */
 
-#define KARATSUBA_CUTOFF 20
-#define KARATSUBA_SLOP 32
-
 extern void mpx_kmul(mpw */*dv*/, mpw */*dvl*/,
                     const mpw */*av*/, const mpw */*avl*/,
                     const mpw */*bv*/, const mpw */*bvl*/,
                     mpw */*sv*/, mpw */*svl*/);
 
-/* --- @mpx_udiv@ --- *
+/* --- @mpx_ksqr@ --- *
  *
- * Arguments:  @mpw *qv, *qvl@ = quotient vector base and limit
- *             @mpw *rv, *rvl@ = dividend/remainder vector base and limit
- *             @const mpw *dv, *dvl@ = divisor vector base and limit
- *             @mpw *sv, *svl@ = scratch workspace 
+ * Arguments:  @mpw *dv, *dvl@ = pointer to destination buffer
+ *             @const mpw *av, *avl@ = pointer to first argument
+ *             @mpw *sv, *svl@ = pointer to scratch workspace
  *
  * Returns:    ---
  *
- * Use:                Performs unsigned integer division.  If the result overflows
- *             the quotient vector, high-order bits are discarded.  (Clearly
- *             the remainder vector can't overflow.)  The various vectors
- *             may not overlap in any way.  Yes, I know it's a bit odd
- *             requiring the dividend to be in the result position but it
- *             does make some sense really.  The remainder must have
- *             headroom for at least two extra words.  The scratch space
- *             must be at least one word larger than the divisor.
+ * Use:                Squares a multiprecision integers using something similar to
+ *             Karatsuba's multiplication algorithm.  This is rather faster
+ *             than traditional long multiplication (e.g., @mpx_umul@) on
+ *             large numbers, although more expensive on small ones, and
+ *             rather simpler than full-blown Karatsuba multiplication.
+ *
+ *             The destination must be twice as large as the argument.  The
+ *             scratch space must be twice as large as the argument, plus
+ *             the magic number @KARATSUBA_SLOP@.
  */
 
-extern void mpx_udiv(mpw */*qv*/, mpw */*qvl*/, mpw */*rv*/, mpw */*rvl*/,
-                    const mpw */*dv*/, const mpw */*dvl*/,
+extern void mpx_ksqr(mpw */*dv*/, mpw */*dvl*/,
+                    const mpw */*av*/, const mpw */*avl*/,
                     mpw */*sv*/, mpw */*svl*/);
 
 /*----- That's all, folks -------------------------------------------------*/