[PATCH v2 22/32] mpi: Sync the SH longlong.h with GCC.

Matt Turner mattst88 at gmail.com
Wed Nov 25 21:19:59 CET 2026


* mpi/longlong.h [__sh__]: Test __sh__ and !__sh1__ instead of
listing SH2, SH3 and SH4.
(umul_ppmm): Allow auto-increment outputs, as GCC does.
(udiv_qrnnd, sub_ddmmss, UDIV_NEEDS_NORMALIZATION, UDIV_TIME): New.

--

Take the SH block from GCC's copy of longlong.h, added in GCC commit
31b6f0aee8f ("re PR target/28014 (space-optimized divide used
inconsistently)").  The old test missed SH2E, SH2A, SH3E and the SH4
variants that define __SH4_SINGLE__, __SH4_SINGLE_ONLY__ or
__SH4_NOFPU__ instead of __SH4__; GCC's sh-c.cc defines __sh1__ only
for SH1, and every SH but SH1 has dmulu.l.

udiv_qrnnd calls __udiv_qrnnd_16, which libgcc has had since that
same commit.  It is hidden, so it links into libgcrypt.so from
libgcc.a as a local symbol.  Without it, udiv_qrnnd divides a UDWtype
and calls both __udivdi3 and __umoddi3; under qemu-sh4, GCC's version
takes 2.2 s for 20 million divisions where the generic code takes
5.2 s.

umul_ppmm, udiv_qrnnd and sub_ddmmss pass the runtime tests on sh4
and sh4eb.

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

diff --git a/mpi/longlong.h b/mpi/longlong.h
index 41f2c0fb..943baeb2 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -1043,21 +1043,79 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
 /***************************************
  **************  SH2  ******************
  ***************************************/
-#if (defined (__sh2__) || defined(__sh3__) || defined(__SH4__) ) \
-    && W_TYPE_SIZE == 32
-# define umul_ppmm(w1, w0, u, v) \
+#if defined(__sh__) && W_TYPE_SIZE == 32
+#ifndef __sh1__
+#define umul_ppmm(w1, w0, u, v) \
   __asm__ (								\
-        "dmulu.l %2,%3\n"  \
-	"sts	macl,%1\n" \
-	"sts	mach,%0"                                                \
-	   : "=r" __ll_check_output (USItype, w1),                      \
-	     "=r" __ll_check_output (USItype, w0)                       \
-	   : "r" ((USItype)(u)),                                        \
-	     "r" ((USItype)(v))                                         \
+       "dmulu.l	%2,%3\n\tsts%M1	macl,%1\n\tsts%M0	mach,%0"	\
+	   : "=r<" __ll_check_output (USItype, w1),			\
+	     "=r<" __ll_check_output (USItype, w0)			\
+	   : "r" ((USItype)(u)),					\
+	     "r" ((USItype)(v))						\
 	   : "macl", "mach")
-# define UMUL_TIME 5
+#define UMUL_TIME 5
 #endif
 
+/* This is the same algorithm as __udiv_qrnnd_c.  */
+#define UDIV_NEEDS_NORMALIZATION 1
+
+#ifdef __FDPIC__
+/* FDPIC needs a special version of the asm fragment to extract the
+   code address from the function descriptor. __udiv_qrnnd_16 is
+   assumed to be local and not to use the GOT, so loading r12 is
+   not needed. */
+#define udiv_qrnnd(q, r, n1, n0, d) \
+  do {									\
+    extern UWtype __udiv_qrnnd_16 (UWtype, UWtype)			\
+			__attribute__ ((visibility ("hidden")));	\
+    /* r0: rn r1: qn */ /* r0: n1 r4: n0 r5: d r6: d1 */ /* r2: __m */	\
+    __asm__ (								\
+	"mov%M4	%4,r5\n"						\
+"	swap.w	%3,r4\n"						\
+"	swap.w	r5,r6\n"						\
+"	mov.l	@%5,r2\n"						\
+"	jsr	@r2\n"							\
+"	shll16	r6\n"							\
+"	swap.w	r4,r4\n"						\
+"	mov.l	@%5,r2\n"						\
+"	jsr	@r2\n"							\
+"	swap.w	r1,%0\n"						\
+"	or	r1,%0"							\
+	: "=r" (q), "=&z" (r)						\
+	: "1" (n1), "r" (n0), "rm" (d), "r" (&__udiv_qrnnd_16)		\
+	: "r1", "r2", "r4", "r5", "r6", "pr", "t");			\
+  } while (0)
+#else
+#define udiv_qrnnd(q, r, n1, n0, d) \
+  do {									\
+    extern UWtype __udiv_qrnnd_16 (UWtype, UWtype)			\
+			__attribute__ ((visibility ("hidden")));	\
+    /* r0: rn r1: qn */ /* r0: n1 r4: n0 r5: d r6: d1 */ /* r2: __m */	\
+    __asm__ (								\
+	"mov%M4 %4,r5\n"						\
+"	swap.w %3,r4\n"							\
+"	swap.w r5,r6\n"							\
+"	jsr @%5\n"							\
+"	shll16 r6\n"							\
+"	swap.w r4,r4\n"							\
+"	jsr @%5\n"							\
+"	swap.w r1,%0\n"							\
+"	or r1,%0"							\
+	: "=r" (q), "=&z" (r)						\
+	: "1" (n1), "r" (n0), "rm" (d), "r" (&__udiv_qrnnd_16)		\
+	: "r1", "r2", "r4", "r5", "r6", "pr", "t");			\
+  } while (0)
+#endif /* __FDPIC__  */
+
+#define UDIV_TIME 80
+
+#define sub_ddmmss(sh, sl, ah, al, bh, bl)				\
+  __asm__ ("clrt;subc %5,%1; subc %4,%0"				\
+	   : "=r" (sh), "=r" (sl)					\
+	   : "0" (ah), "1" (al), "r" (bh), "r" (bl) : "t")
+
+#endif /* __sh__ */
+
 /***************************************
  **************  SPARC	****************
  ***************************************/

-- 
2.56.1



More information about the Gcrypt-devel mailing list