[PATCH v2 11/32] mpi: Use builtins for the AArch64 count_leading_zeros.
Matt Turner
mattst88 at gmail.com
Wed Nov 25 21:19:48 CET 2026
* mpi/longlong.h [__aarch64__] (count_leading_zeros): Use
__builtin_clzll or __builtin_clz instead of asm. Move before the
Alpha block.
(count_trailing_zeros, COUNT_LEADING_ZEROS_0): New.
--
Take the AArch64 count_leading_zeros and count_trailing_zeros from
GCC's copy of longlong.h, which got them in commit 130a78598c6
("Merge longlong.h from glibc tree."). Both compile to a single clz,
or rbit and clz, with GCC and Clang, and the compiler can see through
them. The asm add_ssaaaa, sub_ddmmss and umul_ppmm, which GCC does
not have, stay.
Signed-off-by: Matt Turner <mattst88 at gmail.com>
---
mpi/longlong.h | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/mpi/longlong.h b/mpi/longlong.h
index 9375338d..a27ee71b 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -148,6 +148,22 @@ extern const UQItype __clz_tab[256] attribute_hidden;
**** Begin CPU Specific Versions ****
***************************************/
+#if defined (__aarch64__)
+
+#if W_TYPE_SIZE == 32
+#define count_leading_zeros(COUNT, X) ((COUNT) = __builtin_clz (X))
+#define count_trailing_zeros(COUNT, X) ((COUNT) = __builtin_ctz (X))
+#define COUNT_LEADING_ZEROS_0 32
+#endif /* W_TYPE_SIZE == 32 */
+
+#if W_TYPE_SIZE == 64
+#define count_leading_zeros(COUNT, X) ((COUNT) = __builtin_clzll (X))
+#define count_trailing_zeros(COUNT, X) ((COUNT) = __builtin_ctzll (X))
+#define COUNT_LEADING_ZEROS_0 64
+#endif /* W_TYPE_SIZE == 64 */
+
+#endif /* __aarch64__ */
+
#if defined (__alpha) && W_TYPE_SIZE == 64
/* There is a bug in g++ before version 5 that
errors on __builtin_alpha_umulh. */
@@ -321,14 +337,6 @@ extern UDItype __udiv_qrnnd (UDItype *, UDItype, UDItype, UDItype);
"r" (__m1)); \
(ph) = __ph; \
} while (0)
-# define count_leading_zeros(count, x) \
- do { \
- UDItype __co; \
- __asm__ ("clz %0, %1\n" \
- : "=r" (__co) \
- : "r" ((UDItype)(x))); \
- (count) = __co; \
- } while (0)
#endif /* __aarch64__ */
/***************************************
--
2.56.1
More information about the Gcrypt-devel
mailing list