[PATCH v2 25/32] mpi: Use builtins for the LoongArch count_leading_zeros.

Matt Turner mattst88 at gmail.com
Wed Nov 25 21:20:02 CET 2026


* mpi/longlong.h [__loongarch__ && W_TYPE_SIZE == 64]
(count_leading_zeros, count_trailing_zeros, COUNT_LEADING_ZEROS_0):
New.

--

Take the LoongArch block from GCC commit 6f653a2c85a ("LoongArch:
implement count_{leading,trailing}_zeros").  GCC and Clang turn the
builtins into clz.d and ctz.d, where the generic count_leading_zeros
looks up _gcry_clz_tab.

The macros pass a runtime test under qemu-loongarch64 with GCC and
Clang.

Signed-off-by: Matt Turner <mattst88 at gmail.com>
---
 mpi/longlong.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mpi/longlong.h b/mpi/longlong.h
index ca4764ab..6e23018e 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -544,6 +544,14 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
     __w; })
 #endif /* __i960__ */
 
+#ifdef __loongarch__
+# 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
+#endif
+
 
 /***************************************
  **************  68000	****************

-- 
2.56.1



More information about the Gcrypt-devel mailing list