[PATCH 1/2] mpi/longlong: make use of compiler provided __builtin_ctz/__builtin_clz

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH 1/2] mpi/longlong: make use of compiler provided __builtin_ctz/__builtin_clz

Jussi Kivilinna-2
* configure.ac (gcry_cv_have_builtin_ctzl, gcry_cv_have_builtin_clz)
(gcry_cv_have_builtin_clzl): New checks.
* mpi/longlong.h (count_leading_zeros, count_trailing_zeros): Use
__buildin_clz[l]/__builtin_ctz[l] if available and bit counting
macros not yet provided by inline assembly.
--

Signed-off-by: Jussi Kivilinna <[hidden email]>
---
 configure.ac   | 45 +++++++++++++++++++++++++++++++++++++++++++++
 mpi/longlong.h | 20 ++++++++++++++++++++
 2 files changed, 65 insertions(+)

diff --git a/configure.ac b/configure.ac
index fda74056..e0d52d8c 100644
--- a/configure.ac
+++ b/configure.ac
@@ -903,6 +903,51 @@ if test "$gcry_cv_have_builtin_ctz" = "yes" ; then
 fi
 
 
+#
+# Check for __builtin_ctzl intrinsic.
+#
+AC_CACHE_CHECK(for __builtin_ctzl,
+       [gcry_cv_have_builtin_ctzl],
+       [gcry_cv_have_builtin_ctzl=no
+        AC_LINK_IFELSE([AC_LANG_PROGRAM([],
+          [unsigned long x = 0; long y = __builtin_ctzl(x); return y;])],
+          [gcry_cv_have_builtin_ctzl=yes])])
+if test "$gcry_cv_have_builtin_ctzl" = "yes" ; then
+   AC_DEFINE(HAVE_BUILTIN_CTZL, 1,
+             [Defined if compiler has '__builtin_ctzl' intrinsic])
+fi
+
+
+#
+# Check for __builtin_clz intrinsic.
+#
+AC_CACHE_CHECK(for __builtin_clz,
+       [gcry_cv_have_builtin_clz],
+       [gcry_cv_have_builtin_clz=no
+        AC_LINK_IFELSE([AC_LANG_PROGRAM([],
+          [unsigned int x = 0; int y = __builtin_clz(x); return y;])],
+          [gcry_cv_have_builtin_clz=yes])])
+if test "$gcry_cv_have_builtin_clz" = "yes" ; then
+   AC_DEFINE(HAVE_BUILTIN_CLZ, 1,
+             [Defined if compiler has '__builtin_clz' intrinsic])
+fi
+
+
+#
+# Check for __builtin_clzl intrinsic.
+#
+AC_CACHE_CHECK(for __builtin_clzl,
+       [gcry_cv_have_builtin_clzl],
+       [gcry_cv_have_builtin_clzl=no
+        AC_LINK_IFELSE([AC_LANG_PROGRAM([],
+          [unsigned long x = 0; long y = __builtin_clzl(x); return y;])],
+          [gcry_cv_have_builtin_clzl=yes])])
+if test "$gcry_cv_have_builtin_clzl" = "yes" ; then
+   AC_DEFINE(HAVE_BUILTIN_CLZL, 1,
+             [Defined if compiler has '__builtin_clzl' intrinsic])
+fi
+
+
 #
 # Check for __sync_synchronize intrinsic.
 #
diff --git a/mpi/longlong.h b/mpi/longlong.h
index 6573c984..6993d6eb 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -1681,6 +1681,26 @@ extern USItype __udiv_qrnnd ();
 #  define udiv_qrnnd __udiv_qrnnd_c
 #endif
 
+#if !defined (count_leading_zeros)
+#  if defined (HAVE_BUILTIN_CLZL) && SIZEOF_UNSIGNED_LONG * 8 == W_TYPE_SIZE
+#    define count_leading_zeros(count, x) (count = __builtin_clzl(x))
+#    undef COUNT_LEADING_ZEROS_0 /* Input X=0 is undefined for the builtin. */
+#  elif defined (HAVE_BUILTIN_CLZ) && SIZEOF_UNSIGNED_INT * 8 == W_TYPE_SIZE
+#    define count_leading_zeros(count, x) (count = __builtin_clz(x))
+#    undef COUNT_LEADING_ZEROS_0 /* Input X=0 is undefined for the builtin. */
+#  endif
+#endif
+
+#if !defined (count_trailing_zeros)
+#  if defined (HAVE_BUILTIN_CTZL) && SIZEOF_UNSIGNED_LONG * 8 == W_TYPE_SIZE
+#    define count_trailing_zeros(count, x) (count = __builtin_ctzl(x))
+#    undef COUNT_LEADING_ZEROS_0 /* Input X=0 is undefined for the builtin. */
+#  elif defined (HAVE_BUILTIN_CTZ) && SIZEOF_UNSIGNED_INT * 8 == W_TYPE_SIZE
+#    define count_trailing_zeros(count, x) (count = __builtin_ctz(x))
+#    undef COUNT_LEADING_ZEROS_0 /* Input X=0 is undefined for the builtin. */
+#  endif
+#endif
+
 #if !defined (count_leading_zeros)
 extern
 #  ifdef __STDC__
--
2.27.0


_______________________________________________
Gcrypt-devel mailing list
[hidden email]
http://lists.gnupg.org/mailman/listinfo/gcrypt-devel
Reply | Threaded
Open this post in threaded view
|

[PATCH 2/2] cipher/bithelp: use __builtin_ctzl when available

Jussi Kivilinna-2
* cipher/bithelp.h (_gcry_ctz64): Use __builtin_ctzl is available.
--

Signed-off-by: Jussi Kivilinna <[hidden email]>
---
 cipher/bithelp.h | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/cipher/bithelp.h b/cipher/bithelp.h
index 26ef7c35..7793ce7c 100644
--- a/cipher/bithelp.h
+++ b/cipher/bithelp.h
@@ -83,7 +83,7 @@ static inline int
 _gcry_ctz (unsigned int x)
 {
 #if defined (HAVE_BUILTIN_CTZ)
-  return x? __builtin_ctz (x) : 8 * sizeof (x);
+  return x ? __builtin_ctz (x) : 8 * sizeof (x);
 #else
   /* See
    * http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightModLookup
@@ -106,9 +106,11 @@ _gcry_ctz (unsigned int x)
 static inline int
 _gcry_ctz64(u64 x)
 {
-#if defined (HAVE_BUILTIN_CTZ) && SIZEOF_UNSIGNED_INT >= 8
+#if defined (HAVE_BUILTIN_CTZL) && SIZEOF_UNSIGNED_LONG >= 8
+  return x ? __builtin_ctzl (x) : 8 * sizeof (x);
+#elif defined (HAVE_BUILTIN_CTZ) && SIZEOF_UNSIGNED_INT >= 8
 #warning hello
-  return x? __builtin_ctz (x) : 8 * sizeof (x);
+  return x ? __builtin_ctz (x) : 8 * sizeof (x);
 #else
   if ((x & 0xffffffff))
     return _gcry_ctz (x);
--
2.27.0


_______________________________________________
Gcrypt-devel mailing list
[hidden email]
http://lists.gnupg.org/mailman/listinfo/gcrypt-devel