New interface for computing products of many (small) integers.
[u/mdw/catacomb] / mpmul.c
diff --git a/mpmul.c b/mpmul.c
new file mode 100644 (file)
index 0000000..46f7b72
--- /dev/null
+++ b/mpmul.c
@@ -0,0 +1,149 @@
+/* -*-c-*-
+ *
+ * $Id: mpmul.c,v 1.1 2000/07/01 11:21:39 mdw Exp $
+ *
+ * Multiply many small numbers together
+ *
+ * (c) 2000 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * Catacomb is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Library General Public License for more details.
+ * 
+ * You should have received a copy of the GNU Library General Public
+ * License along with Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: mpmul.c,v $
+ * Revision 1.1  2000/07/01 11:21:39  mdw
+ * New interface for computing products of many (small) integers.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include "mp.h"
+#include "mpint.h"
+#include "mpmul.h"
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @mpmul_init@ --- *
+ *
+ * Arguments:  @mpmul *b@ = pointer to multiplier context to initialize
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes a big multiplier context for use.
+ */
+
+void mpmul_init(mpmul *b)
+{
+  b->i = 0;
+}
+
+/* --- @mpmul_add@ --- *
+ *
+ * Arguments:  @mpmul *b@ = pointer to multiplier context
+ *             @mp *x@ = the next factor to multiply in
+ *
+ * Returns:    ---
+ *
+ * Use:                Contributes another factor to the mix.  It's important that
+ *             the integer lasts at least as long as the multiplication
+ *             context; this sort of rules out @mp_build@ integers.
+ */
+
+void mpmul_add(mpmul *b, mp *x)
+{
+  size_t i = b->i;
+
+  /* --- Now do the reduction step --- */
+
+  x = MP_COPY(x);
+
+  while (i > 0) {
+    if (MP_LEN(b->v[i - 1]) > MP_LEN(x))
+      break;
+    i--;
+    x = mp_mul(x, x, b->v[i]);
+    MP_DROP(b->v[i]);
+  }
+
+  if (i > HWM) {
+    while (i > LWM) {
+      i--;
+      x = mp_mul(x, x, b->v[i]);
+      MP_DROP(b->v[i]);
+    }
+  }
+
+  b->v[i] = x;
+  b->i = i;
+}
+
+/* --- @mpmul_done@ --- *
+ *
+ * Arguments:  @mpmul *b@ = pointer to big multiplication context
+ *
+ * Returns:    The product of all the numbers contributed.
+ *
+ * Use:                Returns a (large) product of numbers.  The context is
+ *             deallocated.
+ */
+
+mp *mpmul_done(mpmul *b)
+{
+  size_t i = b->i;
+  mp *x;
+
+  if (!i)
+    return (MP_ONE);
+  i--;
+  x = b->v[i];
+  while (i > 0) {
+    i--;
+    x = mp_mul(x, x, b->v[i]);
+    MP_DROP(b->v[i]);
+  }
+  return (x);
+}
+
+/* --- @mp_factorial@ --- *
+ *
+ * Arguments:  @unsigned long i@ = number whose factorial should be
+ *                     computed.
+ *
+ * Returns:    The requested factorial.
+ */
+
+mp *mp_factorial(unsigned long i)
+{
+  unsigned long j;
+  mp *x = MP_NEW;
+  mpmul b = MPMUL_INIT;
+
+  for (j = 1; j <= i; j++) {
+    x = mp_fromulong(x, j);
+    mpmul_add(&b, x);
+  }
+  mp_drop(x);
+  return (mpmul_done(&b));
+}
+
+/*----- That's all, folks -------------------------------------------------*/