summaryrefslogtreecommitdiffstats
path: root/pbn_mul.c
diff options
context:
space:
mode:
Diffstat (limited to 'pbn_mul.c')
-rw-r--r--pbn_mul.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/pbn_mul.c b/pbn_mul.c
new file mode 100644
index 0000000..c7b13c9
--- /dev/null
+++ b/pbn_mul.c
@@ -0,0 +1,56 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2007 H. Peter Anvin - All Rights Reserved
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation, Inc.,
+ * 59 Temple Place Ste 330, Boston MA 02111-1307, USA; version 2.1,
+ * incorporated herein by reference.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * pbn_mul.c
+ *
+ * Multiply two numbers. This could be made significantly faster for
+ * very large numbers by using Karatsuba or FFT-based multiplication.
+ */
+
+#include "pbnint.h"
+
+struct pbn *pbn_mul(struct pbn *s1, struct pbn *s2)
+{
+ size_t bits, len;
+ struct pbn *d;
+ int i, j;
+ pbn_atom_t x1;
+ pbn_2atom_t c;
+
+ bits = s1->bits + s2->bits;
+ len = (bits+PBN_ATOM_BITS-1)/PBN_ATOM_BITS;
+
+ d = pbn_new(len);
+ d->bits = bits;
+
+ for (i = 0; i < s1->len; i++) {
+ x1 = s1->num[i];
+ c = 0;
+ for (j = 0; j < s2->len; j++) {
+ c += (pbn_2atom_t)x1 * s2->num[j];
+ d->num[i+j] += (pbn_atom_t)c;
+ c >>= PBN_ATOM_BITS;
+ }
+ if (c)
+ d->num[i+j] += (pbn_atom_t)c;
+ }
+ d->minus = s1->minus ^ s2->minus;
+
+ pbn_free(s1);
+ pbn_free(s2);
+
+ _pbn_adjust_bits(d);
+
+ return d;
+}
+