Moved the Karatsuba macros into a separate file for better sharing.
[u/mdw/catacomb] / karatsuba.h
diff --git a/karatsuba.h b/karatsuba.h
new file mode 100644 (file)
index 0000000..885d907
--- /dev/null
@@ -0,0 +1,128 @@
+/* -*-c-*-
+ *
+ * $Id: karatsuba.h,v 1.1 2000/06/17 11:42:11 mdw Exp $
+ *
+ * Macros for Karatsuba functions
+ *
+ * (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: karatsuba.h,v $
+ * Revision 1.1  2000/06/17 11:42:11  mdw
+ * Moved the Karatsuba macros into a separate file for better sharing.
+ * Fixed some comments.
+ *
+ */
+
+#ifndef CATACOMB_MPX_KMAC_H
+#define CATACOMB_MPX_KMAC_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#ifndef CATACOMB_MPW_H
+#  include "mpw.h"
+#endif
+
+/*----- Macros provided ---------------------------------------------------*/
+
+#define UADD(dv, av, avl) do {                                         \
+  mpw *_dv = (dv);                                                     \
+  const mpw *_av = (av), *_avl = (avl);                                        \
+  mpw _c = 0;                                                          \
+                                                                       \
+  while (_av < _avl) {                                                 \
+    mpw _a, _b;                                                                \
+    mpd _x;                                                            \
+    _a = *_av++;                                                       \
+    _b = *_dv;                                                         \
+    _x = (mpd)_a + (mpd)_b + _c;                                       \
+    *_dv++ = MPW(_x);                                                  \
+    _c = _x >> MPW_BITS;                                               \
+  }                                                                    \
+  while (_c) {                                                         \
+    mpd _x = (mpd)*_dv + (mpd)_c;                                      \
+    *_dv++ = MPW(_x);                                                  \
+    _c = _x >> MPW_BITS;                                               \
+  }                                                                    \
+} while (0)
+
+#define UADD2(dv, dvl, av, avl, bv, bvl) do {                          \
+  mpw *_dv = (dv), *_dvl = (dvl);                                      \
+  const mpw *_av = (av), *_avl = (avl);                                        \
+  const mpw *_bv = (bv), *_bvl = (bvl);                                        \
+  mpw _c = 0;                                                          \
+                                                                       \
+  while (_av < _avl || _bv < _bvl) {                                   \
+    mpw _a, _b;                                                                \
+    mpd _x;                                                            \
+    _a = (_av < _avl) ? *_av++ : 0;                                    \
+    _b = (_bv < _bvl) ? *_bv++ : 0;                                    \
+    _x = (mpd)_a + (mpd)_b + _c;                                       \
+    *_dv++ = MPW(_x);                                                  \
+    _c = _x >> MPW_BITS;                                               \
+  }                                                                    \
+  *_dv++ = _c;                                                         \
+  while (_dv < _dvl)                                                   \
+    *_dv++ = 0;                                                                \
+} while (0)
+
+#define USUB(dv, av, avl) do {                                         \
+  mpw *_dv = (dv);                                                     \
+  const mpw *_av = (av), *_avl = (avl);                                        \
+  mpw _c = 0;                                                          \
+                                                                       \
+  while (_av < _avl) {                                                 \
+    mpw _a, _b;                                                                \
+    mpd _x;                                                            \
+    _a = *_av++;                                                       \
+    _b = *_dv;                                                         \
+    _x = (mpd)_b - (mpd)_a - _c;                                       \
+    *_dv++ = MPW(_x);                                                  \
+    if (_x >> MPW_BITS)                                                        \
+      _c = 1;                                                          \
+    else                                                               \
+      _c = 0;                                                          \
+  }                                                                    \
+  while (_c) {                                                         \
+    mpd _x = (mpd)*_dv - (mpd)_c;                                      \
+    *_dv++ = MPW(_x);                                                  \
+    if (_x >> MPW_BITS)                                                        \
+      _c = 1;                                                          \
+    else                                                               \
+      _c = 0;                                                          \
+  }                                                                    \
+} while (0)
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif