]> pd.if.org Git - zpackage/blobdiff - tomsfastmath/src/bit/fp_cnt_lsb.c
commit files needed for zpm-fetchurl
[zpackage] / tomsfastmath / src / bit / fp_cnt_lsb.c
diff --git a/tomsfastmath/src/bit/fp_cnt_lsb.c b/tomsfastmath/src/bit/fp_cnt_lsb.c
new file mode 100644 (file)
index 0000000..5dfd0f7
--- /dev/null
@@ -0,0 +1,46 @@
+/* TomsFastMath, a fast ISO C bignum library.
+ * 
+ * This project is meant to fill in where LibTomMath
+ * falls short.  That is speed ;-)
+ *
+ * This project is public domain and free for all purposes.
+ * 
+ * Tom St Denis, tomstdenis@gmail.com
+ */
+#include <tfm_private.h>
+
+static const int lnz[16] = {
+   4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
+};
+
+/* Counts the number of lsbs which are zero before the first zero bit */
+int fp_cnt_lsb(fp_int *a)
+{
+   int x;
+   fp_digit q, qq;
+
+   /* easy out */
+   if (fp_iszero(a) == 1) {
+      return 0;
+   }
+
+   /* scan lower digits until non-zero */
+   for (x = 0; x < a->used && a->dp[x] == 0; x++);
+   q = a->dp[x];
+   x *= DIGIT_BIT;
+
+   /* now scan this digit until a 1 is found */
+   if ((q & 1) == 0) {
+      do {
+         qq  = q & 15;
+         x  += lnz[qq];
+         q >>= 4;
+      } while (qq == 0);
+   }
+   return x;
+}
+
+
+/* $Source$ */
+/* $Revision$ */
+/* $Date$ */