[PATCH v2 23/32] mpi: Sync the 32-bit SPARC longlong.h with GCC.

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


* mpi/longlong.h [__sparc__ && W_TYPE_SIZE == 32]: Exclude
__arch64__ and __sparcv9.
[__sparc_v9__ && !__clang__] (umul_ppmm, udiv_qrnnd): New.
[__sparc_v8__ || __sparcv8 || __sparc_v9__] (udiv_qrnnd): Compute
the remainder in the asm.
[__sparclite__] (count_leading_zeros): Pass 1 to scan, not 0.
(umul_ppmm): Name the clobbers "g1" and "o5", without the %.

--

Take the 32-bit SPARC block from GCC's copy of longlong.h:

- GCC commit 0de0d5c6062 ("Sparc longlong.h enhancements.") added a
  V9 umul_ppmm that reads the high word of the 64-bit umul result
  with srlx instead of rd %y, and a udiv_qrnnd without the three nops
  after writing %y.  Clang's assembler rejects srlx in
  32-bit mode ("instruction requires a CPU feature not currently
  enabled"), so Clang keeps using the V8 versions on V9.

- GCC's V8 udiv_qrnnd computes the remainder with umul and sub in
  the asm; libgcrypt computed it in C.

- GCC changed the SPARClite scan operand from 0 to 1 in commit
  84530511d25 ("longlong.h (count_leading_zeros): Sparclite scan
  instruction"), and GMP took the same change as "Sparclite scan
  instruction was being invoked incorrectly".

Keep the parts that are libgcrypt's own: __sparcv8, which Clang
defines instead of __sparc_v8__; the SUPERSPARC exception and the
__udiv_qrnnd fallback that mpi/sparc32/udiv.S and
mpi/supersparc/udiv.S provide, in place of GCC's inline V7
udiv_qrnnd; and the UMUL_TIME and UDIV_TIME values.

The object code is unchanged for V7 and for SuperSPARC with
SUPERSPARC defined.  umul_ppmm and udiv_qrnnd pass a freestanding
test under qemu-sparc for V8 with GCC and Clang.  The V9 and
SPARClite code is compile-tested only.

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

diff --git a/mpi/longlong.h b/mpi/longlong.h
index 943baeb2..35bb0034 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -1119,182 +1119,201 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
 /***************************************
  **************  SPARC	****************
  ***************************************/
-#if defined (__sparc__) && W_TYPE_SIZE == 32
-# define add_ssaaaa(sh, sl, ah, al, bh, bl) \
-  __asm__ ("addcc %r4,%5,%1\n" \
-	   "addx %r2,%3,%0"                                             \
-	   : "=r" __ll_check_output (USItype, sh),                      \
-	     "=&r" __ll_check_output (USItype, sl)                      \
-	   : "%rJ" ((USItype)(ah)),                                     \
-	     "rI" ((USItype)(bh)),                                      \
-	     "%rJ" ((USItype)(al)),                                     \
-	     "rI" ((USItype)(bl))                                       \
+#if defined (__sparc__) && !defined (__arch64__) && !defined (__sparcv9) \
+    && W_TYPE_SIZE == 32
+#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
+  __asm__ ("addcc %r4,%5,%1\n\taddx %r2,%3,%0"				\
+	   : "=r" __ll_check_output (USItype, sh),			\
+	     "=&r" __ll_check_output (USItype, sl)			\
+	   : "%rJ" ((USItype) (ah)),					\
+	     "rI" ((USItype) (bh)),					\
+	     "%rJ" ((USItype) (al)),					\
+	     "rI" ((USItype) (bl))					\
 	   __CLOBBER_CC)
-# define sub_ddmmss(sh, sl, ah, al, bh, bl) \
-  __asm__ ("subcc %r4,%5,%1\n" \
-	   "subx %r2,%3,%0"                                             \
-	   : "=r" __ll_check_output (USItype, sh),                      \
-	     "=&r" __ll_check_output (USItype, sl)                      \
-	   : "rJ" ((USItype)(ah)),                                      \
-	     "rI" ((USItype)(bh)),                                      \
-	     "rJ" ((USItype)(al)),                                      \
-	     "rI" ((USItype)(bl))                                       \
+#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
+  __asm__ ("subcc %r4,%5,%1\n\tsubx %r2,%3,%0"				\
+	   : "=r" __ll_check_output (USItype, sh),			\
+	     "=&r" __ll_check_output (USItype, sl)			\
+	   : "rJ" ((USItype) (ah)),					\
+	     "rI" ((USItype) (bh)),					\
+	     "rJ" ((USItype) (al)),					\
+	     "rI" ((USItype) (bl))					\
 	   __CLOBBER_CC)
-# if defined (__sparc_v8__) || defined(__sparcv8) || defined (__sparc_v9__)
-/* Don't match immediate range because, 1) it is not often useful,
-   2) the 'I' flag thinks of the range as a 13 bit signed interval,
-   while we want to match a 13 bit interval, sign extended to 32 bits,
-   but INTERPRETED AS UNSIGNED.  */
-#  define umul_ppmm(w1, w0, u, v) \
-  __asm__ ("umul %2,%3,%1;rd %%y,%0"                                    \
-	   : "=r" __ll_check_output (USItype, w1),                      \
-	     "=r" __ll_check_output (USItype, w0)                       \
-	   : "r" ((USItype)(u)),                                        \
-	     "r" ((USItype)(v)))
-#  define UMUL_TIME 5
-#  ifndef SUPERSPARC	/* SuperSPARC's udiv only handles 53 bit dividends */
-#   define udiv_qrnnd(q, r, n1, n0, d) \
+#if defined (__sparc_v9__) && !defined (__clang__)
+#define umul_ppmm(w1, w0, u, v) \
   do {									\
-    USItype __q;							\
-    __asm__ ("mov %1,%%y;nop;nop;nop;udiv %2,%3,%0"                     \
-	     : "=r" __ll_check_output (USItype, __q)                    \
-	     : "r" ((USItype)(n1)),                                     \
-	       "r" ((USItype)(n0)),                                     \
-	       "r" ((USItype)(d)));                                     \
-    (r) = (n0) - __q * (d);						\
-    (q) = __q;								\
+    register USItype __g1 asm ("g1");					\
+    __asm__ ("umul\t%2,%3,%1\n\t"					\
+	     "srlx\t%1, 32, %0"						\
+	     : "=r" __ll_check_output (USItype, w1),			\
+	       "=r" (__g1)						\
+	     : "r" ((USItype) (u)),					\
+	       "r" ((USItype) (v)));					\
+    (w0) = __g1;							\
   } while (0)
-#   define UDIV_TIME 25
-#  endif /*!SUPERSPARC */
-# else /* ! __sparc_v8__ && ! __sparc_v9__ */
-#  if defined (__sparclite__)
+#define UMUL_TIME 5
+#define udiv_qrnnd(__q, __r, __n1, __n0, __d) \
+  __asm__ ("mov\t%2,%%y\n\t"						\
+	   "udiv\t%3,%4,%0\n\t"						\
+	   "umul\t%0,%4,%1\n\t"						\
+	   "sub\t%3,%1,%1"						\
+	   : "=&r" __ll_check_output (USItype, __q),			\
+	     "=&r" __ll_check_output (USItype, __r)			\
+	   : "r" ((USItype) (__n1)),					\
+	     "r" ((USItype) (__n0)),					\
+	     "r" ((USItype) (__d)))
+#define UDIV_TIME 25
+#else
+#if defined (__sparc_v8__) || defined (__sparcv8) || defined (__sparc_v9__)
+#define umul_ppmm(w1, w0, u, v) \
+  __asm__ ("umul %2,%3,%1;rd %%y,%0"					\
+	   : "=r" __ll_check_output (USItype, w1),			\
+	     "=r" __ll_check_output (USItype, w0)			\
+	   : "r" ((USItype) (u)),					\
+	     "r" ((USItype) (v)))
+#define UMUL_TIME 5
+#ifndef SUPERSPARC	/* SuperSPARC's udiv only handles 53 bit dividends */
+#define udiv_qrnnd(__q, __r, __n1, __n0, __d) \
+  __asm__ ("mov %2,%%y;nop;nop;nop;udiv %3,%4,%0;umul %0,%4,%1;sub %3,%1,%1"\
+	   : "=&r" __ll_check_output (USItype, __q),			\
+	     "=&r" __ll_check_output (USItype, __r)			\
+	   : "r" ((USItype) (__n1)),					\
+	     "r" ((USItype) (__n0)),					\
+	     "r" ((USItype) (__d)))
+#define UDIV_TIME 25
+#endif /* SUPERSPARC */
+#else
+#if defined (__sparclite__)
 /* This has hardware multiply but not divide.  It also has two additional
    instructions scan (ffs from high bit) and divscc.  */
-#   define umul_ppmm(w1, w0, u, v) \
-  __asm__ ("umul %2,%3,%1;rd %%y,%0"                                    \
-	   : "=r" __ll_check_output (USItype, w1),                      \
-	     "=r" __ll_check_output (USItype, w0)                       \
-	   : "r" ((USItype)(u)),                                        \
-	     "r" ((USItype)(v)))
-#   define UMUL_TIME 5
-#   define udiv_qrnnd(q, r, n1, n0, d) \
-  __asm__ ("! Inlined udiv_qrnnd                                     \n" \
- "        wr	%%g0,%2,%%y	! Not a delayed write for sparclite  \n" \
- "        tst	%%g0                                                 \n" \
- "        divscc	%3,%4,%%g1                                   \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%%g1                                 \n" \
- "        divscc	%%g1,%4,%0                                   \n" \
- "        rd	%%y,%1                                               \n" \
- "        bl,a 1f                                                    \n" \
- "        add	%1,%4,%1                                             \n" \
- "1:	! End of inline udiv_qrnnd"                                     \
-	   : "=r" __ll_check_output (USItype, q),                       \
-	     "=r" __ll_check_output (USItype, r)                        \
-	   : "r" ((USItype)(n1)),                                       \
-	     "r" ((USItype)(n0)),                                       \
-	     "rI" ((USItype)(d))                                        \
-	   : "%g1" __AND_CLOBBER_CC)
-#   define UDIV_TIME 37
-#   define count_leading_zeros(count, x) \
-  __asm__ ("scan %1,0,%0"                                               \
-	   : "=r" __ll_check_output (USItype, count)                    \
-	   : "r" ((USItype)(x)))
+#define umul_ppmm(w1, w0, u, v) \
+  __asm__ ("umul %2,%3,%1;rd %%y,%0"					\
+	   : "=r" __ll_check_output (USItype, w1),			\
+	     "=r" __ll_check_output (USItype, w0)			\
+	   : "r" ((USItype) (u)),					\
+	     "r" ((USItype) (v)))
+#define UMUL_TIME 5
+#define udiv_qrnnd(q, r, n1, n0, d) \
+  __asm__ ("! Inlined udiv_qrnnd\n"					\
+"	wr	%%g0,%2,%%y	! Not a delayed write for sparclite\n"	\
+"	tst	%%g0\n"							\
+"	divscc	%3,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%%g1\n"						\
+"	divscc	%%g1,%4,%0\n"						\
+"	rd	%%y,%1\n"						\
+"	bl,a 1f\n"							\
+"	add	%1,%4,%1\n"						\
+"1:	! End of inline udiv_qrnnd"					\
+	   : "=r" __ll_check_output (USItype, q),			\
+	     "=r" __ll_check_output (USItype, r)			\
+	   : "r" ((USItype) (n1)),					\
+	     "r" ((USItype) (n0)),					\
+	     "rI" ((USItype) (d))					\
+	   : "g1" __AND_CLOBBER_CC)
+#define UDIV_TIME 37
+#define count_leading_zeros(count, x) \
+  do {                                                                  \
+  __asm__ ("scan %1,1,%0"                                               \
+	   : "=r" __ll_check_output (USItype, count)			\
+	   : "r" ((USItype) (x)));					\
+  } while (0)
 /* Early sparclites return 63 for an argument of 0, but they warn that future
    implementations might change this.  Therefore, leave COUNT_LEADING_ZEROS_0
    undefined.  */
-#  endif /* !__sparclite__ */
-# endif /* ! __sparc_v8__ && ! __sparc_v9__ */
-/* Default to sparc v7 versions of umul_ppmm and udiv_qrnnd.  */
-# ifndef umul_ppmm
-#  define umul_ppmm(w1, w0, u, v) \
-  __asm__ ("! Inlined umul_ppmm                                        \n" \
- "        wr	%%g0,%2,%%y	! SPARC has 0-3 delay insn after a wr  \n" \
- "        sra	%3,31,%%o5	! Don't move this insn                 \n" \
- "        and	%2,%%o5,%%o5	! Don't move this insn                 \n" \
- "        andcc	%%g0,0,%%g1	! Don't move this insn                 \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,%3,%%g1                                   \n" \
- "        mulscc	%%g1,0,%%g1                                    \n" \
- "        add	%%g1,%%o5,%0                                           \n" \
- "        rd	%%y,%1"                                                 \
-	   : "=r" __ll_check_output (USItype, w1),                      \
-	     "=r" __ll_check_output (USItype, w0)                       \
-	   : "%rI" ((USItype)(u)),                                      \
-	     "r" ((USItype)(v))                                         \
-	   : "%g1", "%o5" __AND_CLOBBER_CC)
-#  define UMUL_TIME 39		/* 39 instructions */
-# endif /* umul_ppmm */
-# ifndef udiv_qrnnd
-#  ifndef LONGLONG_STANDALONE
-#   define udiv_qrnnd(q, r, n1, n0, d) \
+#else
+/* SPARC without integer multiplication and divide instructions.
+   (i.e. at least Sun4/20,40,60,65,75,110,260,280,330,360,380,470,490) */
+#define umul_ppmm(w1, w0, u, v) \
+  __asm__ ("! Inlined umul_ppmm\n"					\
+"	wr	%%g0,%2,%%y	! SPARC has 0-3 delay insn after a wr\n"\
+"	sra	%3,31,%%o5	! Don't move this insn\n"		\
+"	and	%2,%%o5,%%o5	! Don't move this insn\n"		\
+"	andcc	%%g0,0,%%g1	! Don't move this insn\n"		\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,%3,%%g1\n"						\
+"	mulscc	%%g1,0,%%g1\n"						\
+"	add	%%g1,%%o5,%0\n"						\
+"	rd	%%y,%1"							\
+	   : "=r" __ll_check_output (USItype, w1),			\
+	     "=r" __ll_check_output (USItype, w0)			\
+	   : "%rI" ((USItype) (u)),					\
+	     "r" ((USItype) (v))						\
+	   : "g1", "o5" __AND_CLOBBER_CC)
+#define UMUL_TIME 39		/* 39 instructions */
+#endif /* __sparclite__ */
+#endif /* __sparc_v8__ */
+#endif /* __sparc_v9__ */
+#ifndef udiv_qrnnd
+#ifndef LONGLONG_STANDALONE
+#define udiv_qrnnd(q, r, n1, n0, d) \
   do { USItype __r;							\
-    (q) = __udiv_qrnnd (&__r, (n1), (n0), (d)); 			\
+    (q) = __udiv_qrnnd (&__r, (n1), (n0), (d));			\
     (r) = __r;								\
   } while (0)
 extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
-#   define UDIV_TIME 140
-#  endif /* LONGLONG_STANDALONE */
-# endif /* udiv_qrnnd */
-#endif /* __sparc__ */
+#define UDIV_TIME 140
+#endif /* LONGLONG_STANDALONE */
+#endif /* udiv_qrnnd */
+#endif /* sparc32 */
 
 
 /***************************************

-- 
2.56.1



More information about the Gcrypt-devel mailing list