[PATCH v2 27/32] mpi: Add the 64-bit SPARC longlong.h from GCC.

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


* mpi/longlong.h [(__sparc__ && __arch64__ || __sparcv9)
&& W_TYPE_SIZE == 64] (add_ssaaaa) [!__VIS__ || __VIS__ < 0x300]:
New.
(sub_ddmmss, UDIV_TIME): New.
(umul_ppmm, UMUL_TIME) [!__clang__]: New.

--

Take the 64-bit SPARC block from GCC, as of commit a011979f3cb
("include: Fix the SPARC64 sub_ddmmss constraints in longlong.h").
Without it, umul_ppmm is a call to __multi3.

Change two things:

- Without VIS3, add_ssaaaa is the same length as the generic version.
  With VIS3, the generic version uses addxc and is half the length,
  so use it for __VIS__ >= 0x300.

- Clang's assembler encodes sethi %hi(0x80000000) as if %hi did not
  shift its operand right by 10, so umul_ppmm gives wrong results
  when the carry constant is needed.  Use the generic umul_ppmm with
  Clang.

With UMUL_TIME and UDIV_TIME defined, mpih-div.c uses the
pre-inverted divisor for dividends of 8 limbs or more with GCC and 2
limbs or more with Clang; before, it never did.

The macros pass a runtime test under qemu-sparc64 with GCC for V9,
UltraSPARC, UltraSPARC III, Niagara 2 and Niagara 3, and with Clang
for V9, UltraSPARC, UltraSPARC III and Niagara 2.

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

diff --git a/mpi/longlong.h b/mpi/longlong.h
index 51bd8af6..3b905a03 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -1339,6 +1339,84 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
 #endif /* udiv_qrnnd */
 #endif /* sparc32 */
 
+#if ((defined (__sparc__) && defined (__arch64__)) || defined (__sparcv9)) \
+    && W_TYPE_SIZE == 64
+/* With VIS3, the generic add_ssaaaa uses addxc.  */
+#if !defined (__VIS__) || __VIS__ < 0x300
+#define add_ssaaaa(sh, sl, ah, al, bh, bl)				\
+  do {									\
+    UDItype __carry = 0;						\
+    __asm__ ("addcc\t%r5,%6,%1\n\t"					\
+	     "add\t%r3,%4,%0\n\t"					\
+	     "movcs\t%%xcc, 1, %2\n\t"					\
+	     "add\t%0, %2, %0"						\
+	     : "=r" __ll_check_output (UDItype, sh),			\
+	       "=&r" __ll_check_output (UDItype, sl),			\
+	       "+r" (__carry)				      		\
+	     : "%rJ" ((UDItype)(ah)),				     	\
+	       "rI" ((UDItype)(bh)),				      	\
+	       "%rJ" ((UDItype)(al)),				     	\
+	       "rI" ((UDItype)(bl))				       	\
+	     __CLOBBER_CC);						\
+  } while (0)
+#endif /* !__VIS__ || __VIS__ < 0x300 */
+
+#define sub_ddmmss(sh, sl, ah, al, bh, bl)				\
+  do {									\
+    UDItype __carry = 0;						\
+    __asm__ ("subcc\t%r5,%6,%1\n\t"					\
+	     "sub\t%r3,%4,%0\n\t"					\
+	     "movcs\t%%xcc, 1, %2\n\t"					\
+	     "sub\t%0, %2, %0"						\
+	     : "=r" __ll_check_output (UDItype, sh),			\
+	       "=&r" __ll_check_output (UDItype, sl),			\
+	       "+r" (__carry)				      		\
+	     : "rJ" ((UDItype)(ah)),				     	\
+	       "rI" ((UDItype)(bh)),				      	\
+	       "rJ" ((UDItype)(al)),				     	\
+	       "rI" ((UDItype)(bl))				       	\
+	     __CLOBBER_CC);						\
+  } while (0)
+
+/* Clang's assembler encodes %hi of a constant without the shift.  */
+#ifndef __clang__
+#define umul_ppmm(wh, wl, u, v)						\
+  do {									\
+	  UDItype tmp1, tmp2, tmp3, tmp4;				\
+	  __asm__ __volatile__ (					\
+		   "srl %7,0,%3\n\t"					\
+		   "mulx %3,%6,%1\n\t"					\
+		   "srlx %6,32,%2\n\t"					\
+		   "mulx %2,%3,%4\n\t"					\
+		   "sllx %4,32,%5\n\t"					\
+		   "srl %6,0,%3\n\t"					\
+		   "sub %1,%5,%5\n\t"					\
+		   "srlx %5,32,%5\n\t"					\
+		   "addcc %4,%5,%4\n\t"					\
+		   "srlx %7,32,%5\n\t"					\
+		   "mulx %3,%5,%3\n\t"					\
+		   "mulx %2,%5,%5\n\t"					\
+		   "sethi %%hi(0x80000000),%2\n\t"			\
+		   "addcc %4,%3,%4\n\t"					\
+		   "srlx %4,32,%4\n\t"					\
+		   "add %2,%2,%2\n\t"					\
+		   "movcc %%xcc,%%g0,%2\n\t"				\
+		   "addcc %5,%4,%5\n\t"					\
+		   "sllx %3,32,%3\n\t"					\
+		   "add %1,%3,%1\n\t"					\
+		   "add %5,%2,%0"					\
+	   : "=r" __ll_check_output (UDItype, wh),			\
+	     "=&r" __ll_check_output (UDItype, wl),			\
+	     "=&r" (tmp1), "=&r" (tmp2), "=&r" (tmp3), "=&r" (tmp4)	\
+	   : "r" ((UDItype)(u)),					\
+	     "r" ((UDItype)(v))						\
+	   __CLOBBER_CC);						\
+  } while (0)
+#define UMUL_TIME 96
+#endif /* !__clang__ */
+#define UDIV_TIME 230
+#endif /* sparc64 */
+
 
 /***************************************
  **************  VAX  ******************

-- 
2.56.1



More information about the Gcrypt-devel mailing list