[PATCH v2 24/32] mpi: Sync the ARM longlong.h with GCC.

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


* mpi/longlong.h [__arm__] (__ARM_ARCH): Do not define.
(umul_ppmm) [__ARM_ARCH_2__ || __ARM_ARCH_2A__ || __ARM_ARCH_3__]:
Use temporaries instead of clobbering r0, r1 and r2.
(umul_ppmm): Otherwise use a 64-bit C multiply.
(UMUL_TIME): Set to 3 when umull is available.
(count_leading_zeros, count_trailing_zeros, COUNT_LEADING_ZEROS_0):
Use the builtins for all ARM, including Thumb-1.

--

Take the ARM block from GCC's copy of longlong.h.  GCC commit
8993fde4324 ("arm: Enable umull and ctz in longlong.h") replaced the
umull asm with a 64-bit multiply that the compiler turns into umull,
and added count_trailing_zeros; commit ef0a4b67381 defined
count_leading_zeros with __builtin_clz.  With only the ARMv2 and ARMv3
macros to test, the __ARM_ARCH derivation for compilers older than GCC
4.8 is no longer needed.

The object code for a test using every macro is the same for ARMv6
Thumb-1 with GCC and ARMv5TE with Clang, and differs only in
instruction scheduling for ARMv5TE with GCC.  The macros pass a
runtime test under qemu-arm with GCC for ARMv4, ARMv4T, ARMv5TE,
ARMv6, ARMv6 Thumb-1 and ARMv7-A Thumb-2, and with Clang for ARMv4T,
ARMv6 Thumb-1 and ARMv7-A Thumb-2.  Neither compiler supports ARMv2
or ARMv3 any more, so the umul_ppmm asm for them is untested.

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

diff --git a/mpi/longlong.h b/mpi/longlong.h
index 35bb0034..ca4764ab 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -221,90 +221,69 @@ extern UDItype __udiv_qrnnd (UDItype *, UDItype, UDItype, UDItype);
 /***************************************
  **************  ARM  ******************
  ***************************************/
-#if defined (__arm__) && W_TYPE_SIZE == 32 && \
-    (!defined (__thumb__) || defined (__thumb2__))
-/* The __ARM_ARCH define is provided by gcc 4.8.  Construct it otherwise.  */
-# ifndef __ARM_ARCH
-#  ifdef __ARM_ARCH_2__
-#   define __ARM_ARCH 2
-#  elif defined (__ARM_ARCH_3__) || defined (__ARM_ARCH_3M__)
-#   define __ARM_ARCH 3
-#  elif defined (__ARM_ARCH_4__) || defined (__ARM_ARCH_4T__)
-#   define __ARM_ARCH 4
-#  elif defined (__ARM_ARCH_5__) || defined (__ARM_ARCH_5E__) \
-        || defined(__ARM_ARCH_5T__) || defined(__ARM_ARCH_5TE__) \
-        || defined(__ARM_ARCH_5TEJ__)
-#   define __ARM_ARCH 5
-#  elif defined (__ARM_ARCH_6__) || defined(__ARM_ARCH_6J__) \
-        || defined (__ARM_ARCH_6Z__) || defined(__ARM_ARCH_6ZK__) \
-        || defined (__ARM_ARCH_6K__) || defined(__ARM_ARCH_6T2__)
-#   define __ARM_ARCH 6
-#  elif defined (__ARM_ARCH_7__) || defined(__ARM_ARCH_7A__) \
-        || defined(__ARM_ARCH_7R__) || defined(__ARM_ARCH_7M__) \
-        || defined(__ARM_ARCH_7EM__)
-#   define __ARM_ARCH 7
-#  else
-   /* could not detect? */
-#  endif
-# endif /* !__ARM_ARCH */
-
-# define add_ssaaaa(sh, sl, ah, al, bh, bl) \
-  __asm__ ("adds %1, %4, %5\n"                                          \
-	   "adc  %0, %2, %3"                                            \
-	   : "=r" ((sh)),                                               \
-	     "=&r" ((sl))                                               \
-	   : "%r" ((USItype)(ah)),                                      \
-	     "rI" ((USItype)(bh)),                                      \
-	     "%r" ((USItype)(al)),                                      \
-	     "rI" ((USItype)(bl)) __CLOBBER_CC)
-# define sub_ddmmss(sh, sl, ah, al, bh, bl) \
-  __asm__ ("subs %1, %4, %5\n"                                          \
-	   "sbc  %0, %2, %3"                                            \
-	   : "=r" ((sh)),                                               \
-	     "=&r" ((sl))                                               \
-	   : "r" ((USItype)(ah)),                                       \
-	     "rI" ((USItype)(bh)),                                      \
-	     "r" ((USItype)(al)),                                       \
-	     "rI" ((USItype)(bl)) __CLOBBER_CC)
-# if (defined __ARM_ARCH && __ARM_ARCH <= 3)
-#  define umul_ppmm(xh, xl, a, b) \
-  __asm__ ("@ Inlined umul_ppmm\n"                                      \
-	"mov	%|r0, %2, lsr #16		@ AAAA\n"               \
-	"mov	%|r2, %3, lsr #16		@ BBBB\n"               \
-	"bic	%|r1, %2, %|r0, lsl #16		@ aaaa\n"               \
-	"bic	%0, %3, %|r2, lsl #16		@ bbbb\n"               \
-	"mul	%1, %|r1, %|r2			@ aaaa * BBBB\n"        \
-	"mul	%|r2, %|r0, %|r2		@ AAAA * BBBB\n"        \
-	"mul	%|r1, %0, %|r1			@ aaaa * bbbb\n"        \
-	"mul	%0, %|r0, %0			@ AAAA * bbbb\n"        \
-	"adds	%|r0, %1, %0			@ central sum\n"        \
-	"addcs	%|r2, %|r2, #65536\n"                                   \
-	"adds	%1, %|r1, %|r0, lsl #16\n"                              \
-	"adc	%0, %|r2, %|r0, lsr #16"                                \
-	   : "=&r" ((xh)),                                              \
-	     "=r" ((xl))                                                \
-	   : "r" ((USItype)(a)),                                        \
-	     "r" ((USItype)(b))                                         \
-	   : "r0", "r1", "r2" __AND_CLOBBER_CC)
-# else /* __ARM_ARCH >= 4 */
-#  define umul_ppmm(xh, xl, a, b)                                         \
-  __asm__ ("@ Inlined umul_ppmm\n"                                      \
-	   "umull %1, %0, %2, %3"                                       \
-		   : "=&r" ((xh)),                                      \
-		     "=r" ((xl))                                        \
-		   : "r" ((USItype)(a)),                                \
-		     "r" ((USItype)(b)))
-# endif /* __ARM_ARCH >= 4 */
-# define UMUL_TIME 20
+#if defined (__arm__) && (defined (__thumb2__) || !defined (__thumb__)) \
+ && W_TYPE_SIZE == 32
+#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
+  __asm__ ("adds	%1, %4, %5\n\tadc	%0, %2, %3"		\
+	   : "=r" __ll_check_output (USItype, sh),			\
+	     "=&r" __ll_check_output (USItype, sl)			\
+	   : "%r" ((USItype) (ah)),					\
+	     "rI" ((USItype) (bh)),					\
+	     "%r" ((USItype) (al)),					\
+	     "rI" ((USItype) (bl)) __CLOBBER_CC)
+#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
+  __asm__ ("subs	%1, %4, %5\n\tsbc	%0, %2, %3"		\
+	   : "=r" __ll_check_output (USItype, sh),			\
+	     "=&r" __ll_check_output (USItype, sl)			\
+	   : "r" ((USItype) (ah)),					\
+	     "rI" ((USItype) (bh)),					\
+	     "r" ((USItype) (al)),					\
+	     "rI" ((USItype) (bl)) __CLOBBER_CC)
+# if defined(__ARM_ARCH_2__) || defined(__ARM_ARCH_2A__) \
+     || defined(__ARM_ARCH_3__)
+#  define umul_ppmm(xh, xl, a, b)					\
+  do {									\
+    register USItype __t0, __t1, __t2;					\
+    __asm__ ("%@ Inlined umul_ppmm\n"					\
+	   "	mov	%2, %5, lsr #16\n"				\
+	   "	mov	%0, %6, lsr #16\n"				\
+	   "	bic	%3, %5, %2, lsl #16\n"				\
+	   "	bic	%4, %6, %0, lsl #16\n"				\
+	   "	mul	%1, %3, %4\n"					\
+	   "	mul	%4, %2, %4\n"					\
+	   "	mul	%3, %0, %3\n"					\
+	   "	mul	%0, %2, %0\n"					\
+	   "	adds	%3, %4, %3\n"					\
+	   "	addcs	%0, %0, #65536\n"				\
+	   "	adds	%1, %1, %3, lsl #16\n"				\
+	   "	adc	%0, %0, %3, lsr #16"				\
+	   : "=&r" __ll_check_output (USItype, xh),			\
+	     "=r" __ll_check_output (USItype, xl),			\
+	     "=&r" (__t0), "=&r" (__t1), "=r" (__t2)			\
+	   : "r" ((USItype) (a)),					\
+	     "r" ((USItype) (b)) __CLOBBER_CC );			\
+  } while (0)
+#  define UMUL_TIME 20
+# else
+#  define umul_ppmm(xh, xl, a, b)					\
+  do {									\
+    /* Generate umull, under compiler control.  */			\
+    register UDItype __t0 = (UDItype)(USItype)(a) * (USItype)(b);	\
+    (xl) = (USItype)__t0;						\
+    (xh) = (USItype)(__t0 >> 32);					\
+  } while (0)
+#  define UMUL_TIME 3
+# endif
 # define UDIV_TIME 100
-# if (defined __ARM_ARCH && __ARM_ARCH >= 5)
-#  define count_leading_zeros(count, x) \
-  __asm__ ("clz %0, %1"                                                 \
-		   : "=r" ((count))                                     \
-		   : "r" ((USItype)(x)))
-# endif /* __ARM_ARCH >= 5 */
 #endif /* __arm__ */
 
+#if defined(__arm__)
+/* Let gcc decide how best to implement count_leading_zeros.  */
+#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
+
 /***************************************
  **********  ARM64 / Aarch64  **********
  ***************************************/

-- 
2.56.1



More information about the Gcrypt-devel mailing list