[PATCH v2 02/32] mpi: Wrap the casts on asm output operands in a macro.

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


* mpi/longlong.h (__ll_check_output): New macro.
[__alpha, __hppa, __i960__, __mc68000__, __m88000__, __mips__, __mips,
__ibm032__, __sh2__, __sparc__, __vax__]: Use it for the casts on asm
output operands.

--

Using a cast as an lvalue is a GCC extension that clang does not
implement.  Clang rejects the casts on asm output operands with
-Winvalid-gnu-asm-cast, which is an error by default, so these blocks
fail to build with clang.

GCC accepts such a cast only if the conversion is a no-op, which
catches outputs of the wrong type, and that check is worth keeping.
Wrap each output cast in a new macro, __ll_check_output, which expands
to the cast with GCC and to the bare operand with clang:

	: "=r" __ll_check_output (USItype, sh),

This matches GCC's copy of longlong.h, commit b6a6fe2438d ("include:
Wrap the casts on asm output operands in longlong.h in a macro").
Earlier clang fixes for i386, ARM and PowerPC dropped the casts
entirely; the macro keeps GCC's type check instead.

Casts on input operands are left alone: those are ordinary rvalue
conversions.

With GCC the macro expands to the same cast as before, and GCC
generates identical assembly before and after for alpha, hppa, m68k,
sh4, mips and 32-bit sparc.  With clang the 32-bit sparc block now
builds, and the m68k block gets past the front end; the clang used has
no back end for alpha, hppa, m68k or sh.

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

diff --git a/mpi/longlong.h b/mpi/longlong.h
index 712c83e5..04c546c5 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -122,6 +122,16 @@ SPDX-License-Identifier: LGPL-2.1-or-later
 # define __AND_CLOBBER_CC , "cc"
 #endif /* __GNUC__ < 2 */
 
+/* Asm output operand X, written as "=r" __ll_check_output (TYPE, X).
+   GCC accepts a cast on an asm output only if the conversion is a no-op,
+   which catches outputs of the wrong type. Clang rejects such casts, so
+   use X as is. */
+#ifdef __clang__
+# define __ll_check_output(type, x) (x)
+#else
+# define __ll_check_output(type, x) ((type) (x))
+#endif
+
 /***************************************
  ****  Begin CPU Specific Versions  ****
  ***************************************/
@@ -131,7 +141,7 @@ SPDX-License-Identifier: LGPL-2.1-or-later
   do {									\
     UDItype __m0 = (m0), __m1 = (m1);					\
     __asm__ ("umulh %r1,%2,%0"                                          \
-	     : "=r" ((UDItype)(ph))                                     \
+	     : "=r" __ll_check_output (UDItype, ph)                     \
 	     : "%rJ" (__m0),                                            \
 	       "rI" (__m1));                                            \
     (pl) = __m0 * __m1; 						\
@@ -284,8 +294,8 @@ extern UDItype __udiv_qrnnd (UDItype *, UDItype, UDItype, UDItype);
 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
   __asm__ ("add %4,%5,%1\n\t"                                           \
 	   "addc %2,%3,%0"                                              \
-	   : "=r" ((USItype)(sh)),                                      \
-	     "=&r" ((USItype)(sl))                                      \
+	   : "=r" __ll_check_output (USItype, sh),                      \
+	     "=&r" __ll_check_output (USItype, sl)                      \
 	   : "%rM" ((USItype)(ah)),                                     \
 	     "rM" ((USItype)(bh)),                                      \
 	     "%rM" ((USItype)(al)),                                     \
@@ -293,8 +303,8 @@ extern UDItype __udiv_qrnnd (UDItype *, UDItype, UDItype, UDItype);
 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
   __asm__ ("sub %4,%5,%1\n\t"                                           \
 	   "subb %2,%3,%0"                                              \
-	   : "=r" ((USItype)(sh)),                                      \
-	     "=&r" ((USItype)(sl))                                      \
+	   : "=r" __ll_check_output (USItype, sh),                      \
+	     "=&r" __ll_check_output (USItype, sl)                      \
 	   : "rM" ((USItype)(ah)),                                      \
 	     "rM" ((USItype)(bh)),                                      \
 	     "rM" ((USItype)(al)),                                      \
@@ -524,8 +534,8 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
   __asm__ ("cmpo 1,0\n"      \
            "addc %5,%4,%1\n" \
            "addc %3,%2,%0"   \
-	   : "=r" ((USItype)(sh)),                                      \
-	     "=&r" ((USItype)(sl))                                      \
+	   : "=r" __ll_check_output (USItype, sh),                      \
+	     "=&r" __ll_check_output (USItype, sl)                      \
 	   : "%dI" ((USItype)(ah)),                                     \
 	     "dI" ((USItype)(bh)),                                      \
 	     "%dI" ((USItype)(al)),                                     \
@@ -534,8 +544,8 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
   __asm__ ("cmpo 0,0\n"      \
            "subc %5,%4,%1\n" \
            "subc %3,%2,%0"   \
-	   : "=r" ((USItype)(sh)),                                      \
-	     "=&r" ((USItype)(sl))                                      \
+	   : "=r" __ll_check_output (USItype, sh),                      \
+	     "=&r" __ll_check_output (USItype, sl)                      \
 	   : "dI" ((USItype)(ah)),                                      \
 	     "dI" ((USItype)(bh)),                                      \
 	     "dI" ((USItype)(al)),                                      \
@@ -599,8 +609,8 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
   __asm__ ("add%.l %5,%1\n"                                             \
 	   "addx%.l %3,%0"                                              \
-	   : "=d" ((USItype)(sh)),                                      \
-	     "=&d" ((USItype)(sl))                                      \
+	   : "=d" __ll_check_output (USItype, sh),                      \
+	     "=&d" __ll_check_output (USItype, sl)                      \
 	   : "%0" ((USItype)(ah)),                                      \
 	     "d" ((USItype)(bh)),                                       \
 	     "%1" ((USItype)(al)),                                      \
@@ -608,8 +618,8 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
   __asm__ ("sub%.l %5,%1\n"                                             \
 	   "subx%.l %3,%0"                                              \
-	   : "=d" ((USItype)(sh)),                                      \
-	     "=&d" ((USItype)(sl))                                      \
+	   : "=d" __ll_check_output (USItype, sh),                      \
+	     "=&d" __ll_check_output (USItype, sl)                      \
 	   : "0" ((USItype)(ah)),                                       \
 	     "d" ((USItype)(bh)),                                       \
 	     "1" ((USItype)(al)),                                       \
@@ -617,29 +627,29 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
 # if (defined (__mc68020__) || defined (__NeXT__) || defined(mc68020))
 #  define umul_ppmm(w1, w0, u, v) \
   __asm__ ("mulu%.l %3,%1:%0"                                           \
-	   : "=d" ((USItype)(w0)),                                      \
-	     "=d" ((USItype)(w1))                                       \
+	   : "=d" __ll_check_output (USItype, w0),                      \
+	     "=d" __ll_check_output (USItype, w1)                       \
 	   : "%0" ((USItype)(u)),                                       \
 	     "dmi" ((USItype)(v)))
 #  define UMUL_TIME 45
 #  define udiv_qrnnd(q, r, n1, n0, d) \
   __asm__ ("divu%.l %4,%1:%0"                                           \
-	   : "=d" ((USItype)(q)),                                       \
-	     "=d" ((USItype)(r))                                        \
+	   : "=d" __ll_check_output (USItype, q),                       \
+	     "=d" __ll_check_output (USItype, r)                        \
 	   : "0" ((USItype)(n0)),                                       \
 	     "1" ((USItype)(n1)),                                       \
 	     "dmi" ((USItype)(d)))
 #  define UDIV_TIME 90
 #  define sdiv_qrnnd(q, r, n1, n0, d) \
   __asm__ ("divs%.l %4,%1:%0"                                           \
-	   : "=d" ((USItype)(q)),                                       \
-	     "=d" ((USItype)(r))                                        \
+	   : "=d" __ll_check_output (USItype, q),                       \
+	     "=d" __ll_check_output (USItype, r)                        \
 	   : "0" ((USItype)(n0)),                                       \
 	     "1" ((USItype)(n1)),                                       \
 	     "dmi" ((USItype)(d)))
 #  define count_leading_zeros(count, x) \
   __asm__ ("bfffo %1{%b2:%b2},%0"                                       \
-	   : "=d" ((USItype)(count))                                    \
+	   : "=d" __ll_check_output (USItype, count)                    \
 	   : "od" ((USItype)(x)), "n" (0))
 #  define COUNT_LEADING_ZEROS_0 32
 # else /* not mc68020 */
@@ -667,7 +677,8 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
  "        add%.l	%3,%1                                 \n" \
  "        addx%.l %2,%0                                       \n" \
  "        | End inlined umul_ppmm"                                \
-	      : "=&d" ((USItype)(xh)), "=&d" ((USItype)(xl)),     \
+	      : "=&d" __ll_check_output (USItype, xh),            \
+		"=&d" __ll_check_output (USItype, xl),            \
 		"=d" (__umul_tmp1), "=&d" (__umul_tmp2)           \
 	      : "%2" ((USItype)(a)), "d" ((USItype)(b)));         \
   } while (0)
@@ -684,8 +695,8 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
   __asm__ ("addu.co %1,%r4,%r5\n"                                       \
 	   "addu.ci %0,%r2,%r3"                                         \
-	   : "=r" ((USItype)(sh)),                                      \
-	     "=&r" ((USItype)(sl))                                      \
+	   : "=r" __ll_check_output (USItype, sh),                      \
+	     "=&r" __ll_check_output (USItype, sl)                      \
 	   : "%rJ" ((USItype)(ah)),                                     \
 	     "rJ" ((USItype)(bh)),                                      \
 	     "%rJ" ((USItype)(al)),                                     \
@@ -693,8 +704,8 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
   __asm__ ("subu.co %1,%r4,%r5\n"                                       \
 	   "subu.ci %0,%r2,%r3"                                         \
-	   : "=r" ((USItype)(sh)),                                      \
-	     "=&r" ((USItype)(sl))                                      \
+	   : "=r" __ll_check_output (USItype, sh),                      \
+	     "=&r" __ll_check_output (USItype, sl)                      \
 	   : "rJ" ((USItype)(ah)),                                      \
 	     "rJ" ((USItype)(bh)),                                      \
 	     "rJ" ((USItype)(al)),                                      \
@@ -750,8 +761,8 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
 # elif __GNUC__ > 2 || __GNUC_MINOR__ >= 7
 #  define umul_ppmm(w1, w0, u, v) \
   __asm__ ("multu %2,%3"                                                \
-	   : "=l" ((USItype)(w0)),                                      \
-	     "=h" ((USItype)(w1))                                       \
+	   : "=l" __ll_check_output (USItype, w0),                      \
+	     "=h" __ll_check_output (USItype, w1)                       \
 	   : "d" ((USItype)(u)),                                        \
 	     "d" ((USItype)(v)))
 # else
@@ -759,8 +770,8 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
   __asm__ ("multu %2,%3 \n" \
 	   "mflo %0 \n"     \
 	   "mfhi %1"                                                    \
-	   : "=d" ((USItype)(w0)),                                      \
-	     "=d" ((USItype)(w1))                                       \
+	   : "=d" __ll_check_output (USItype, w0),                      \
+	     "=d" __ll_check_output (USItype, w1)                       \
 	   : "d" ((USItype)(u)),                                        \
 	     "d" ((USItype)(v)))
 # endif
@@ -785,8 +796,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
 # elif __GNUC__ > 2 || __GNUC_MINOR__ >= 7
 #  define umul_ppmm(w1, w0, u, v) \
   __asm__ ("dmultu %2,%3"                                               \
-	   : "=l" ((UDItype)(w0)),                                      \
-	     "=h" ((UDItype)(w1))                                       \
+	   : "=l" __ll_check_output (UDItype, w0),                      \
+	     "=h" __ll_check_output (UDItype, w1)                       \
 	   : "d" ((UDItype)(u)),                                        \
 	     "d" ((UDItype)(v)))
 # else
@@ -794,8 +805,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
   __asm__ ("dmultu %2,%3 \n"    \
 	   "mflo %0 \n"         \
 	   "mfhi %1"                                                    \
-	   : "=d" ((UDItype)(w0)),                                      \
-	     "=d" ((UDItype)(w1))                                       \
+	   : "=d" __ll_check_output (UDItype, w0),                      \
+	     "=d" __ll_check_output (UDItype, w1)                       \
 	   : "d" ((UDItype)(u)),                                        \
 	     "d" ((UDItype)(v)))
 # endif
@@ -946,8 +957,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
   __asm__ ("a %1,%5 \n" \
 	   "ae %0,%3"                                                   \
-	   : "=r" ((USItype)(sh)),                                      \
-	     "=&r" ((USItype)(sl))                                      \
+	   : "=r" __ll_check_output (USItype, sh),                      \
+	     "=&r" __ll_check_output (USItype, sl)                      \
 	   : "%0" ((USItype)(ah)),                                      \
 	     "r" ((USItype)(bh)),                                       \
 	     "%1" ((USItype)(al)),                                      \
@@ -955,8 +966,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
   __asm__ ("s %1,%5\n" \
 	   "se %0,%3"                                                   \
-	   : "=r" ((USItype)(sh)),                                      \
-	     "=&r" ((USItype)(sl))                                      \
+	   : "=r" __ll_check_output (USItype, sh),                      \
+	     "=&r" __ll_check_output (USItype, sl)                      \
 	   : "0" ((USItype)(ah)),                                       \
 	     "r" ((USItype)(bh)),                                       \
 	     "1" ((USItype)(al)),                                       \
@@ -985,8 +996,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
        "m	r2,%3    \n" \
        "cas	%0,r2,r0 \n" \
        "mfs	r10,%1"                                                 \
-	     : "=r" ((USItype)(ph)),                                    \
-	       "=r" ((USItype)(pl))                                     \
+	     : "=r" __ll_check_output (USItype, ph),                    \
+	       "=r" __ll_check_output (USItype, pl)                     \
 	     : "%r" (__m0),                                             \
 		"r" (__m1)                                              \
 	     : "r2");                                                   \
@@ -999,12 +1010,12 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
   do {									\
     if ((x) >= 0x10000) 						\
       __asm__ ("clz     %0,%1"                                          \
-	       : "=r" ((USItype)(count))                                \
+	       : "=r" __ll_check_output (USItype, count)                \
 	       : "r" ((USItype)(x) >> 16));                             \
     else								\
       { 								\
 	__asm__ ("clz   %0,%1"                                          \
-		 : "=r" ((USItype)(count))                              \
+		 : "=r" __ll_check_output (USItype, count)              \
 		 : "r" ((USItype)(x)));                                 \
 	(count) += 16;							\
       } 								\
@@ -1022,8 +1033,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
         "dmulu.l %2,%3\n"  \
 	"sts	macl,%1\n" \
 	"sts	mach,%0"                                                \
-	   : "=r" ((USItype)(w1)),                                      \
-	     "=r" ((USItype)(w0))                                       \
+	   : "=r" __ll_check_output (USItype, w1),                      \
+	     "=r" __ll_check_output (USItype, w0)                       \
 	   : "r" ((USItype)(u)),                                        \
 	     "r" ((USItype)(v))                                         \
 	   : "macl", "mach")
@@ -1037,8 +1048,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
   __asm__ ("addcc %r4,%5,%1\n" \
 	   "addx %r2,%3,%0"                                             \
-	   : "=r" ((USItype)(sh)),                                      \
-	     "=&r" ((USItype)(sl))                                      \
+	   : "=r" __ll_check_output (USItype, sh),                      \
+	     "=&r" __ll_check_output (USItype, sl)                      \
 	   : "%rJ" ((USItype)(ah)),                                     \
 	     "rI" ((USItype)(bh)),                                      \
 	     "%rJ" ((USItype)(al)),                                     \
@@ -1047,8 +1058,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
   __asm__ ("subcc %r4,%5,%1\n" \
 	   "subx %r2,%3,%0"                                             \
-	   : "=r" ((USItype)(sh)),                                      \
-	     "=&r" ((USItype)(sl))                                      \
+	   : "=r" __ll_check_output (USItype, sh),                      \
+	     "=&r" __ll_check_output (USItype, sl)                      \
 	   : "rJ" ((USItype)(ah)),                                      \
 	     "rI" ((USItype)(bh)),                                      \
 	     "rJ" ((USItype)(al)),                                      \
@@ -1061,8 +1072,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
    but INTERPRETED AS UNSIGNED.  */
 #  define umul_ppmm(w1, w0, u, v) \
   __asm__ ("umul %2,%3,%1;rd %%y,%0"                                    \
-	   : "=r" ((USItype)(w1)),                                      \
-	     "=r" ((USItype)(w0))                                       \
+	   : "=r" __ll_check_output (USItype, w1),                      \
+	     "=r" __ll_check_output (USItype, w0)                       \
 	   : "r" ((USItype)(u)),                                        \
 	     "r" ((USItype)(v)))
 #  define UMUL_TIME 5
@@ -1071,7 +1082,7 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
   do {									\
     USItype __q;							\
     __asm__ ("mov %1,%%y;nop;nop;nop;udiv %2,%3,%0"                     \
-	     : "=r" ((USItype)(__q))                                    \
+	     : "=r" __ll_check_output (USItype, __q)                    \
 	     : "r" ((USItype)(n1)),                                     \
 	       "r" ((USItype)(n0)),                                     \
 	       "r" ((USItype)(d)));                                     \
@@ -1086,8 +1097,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
    instructions scan (ffs from high bit) and divscc.  */
 #   define umul_ppmm(w1, w0, u, v) \
   __asm__ ("umul %2,%3,%1;rd %%y,%0"                                    \
-	   : "=r" ((USItype)(w1)),                                      \
-	     "=r" ((USItype)(w0))                                       \
+	   : "=r" __ll_check_output (USItype, w1),                      \
+	     "=r" __ll_check_output (USItype, w0)                       \
 	   : "r" ((USItype)(u)),                                        \
 	     "r" ((USItype)(v)))
 #   define UMUL_TIME 5
@@ -1131,8 +1142,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
  "        bl,a 1f                                                    \n" \
  "        add	%1,%4,%1                                             \n" \
  "1:	! End of inline udiv_qrnnd"                                     \
-	   : "=r" ((USItype)(q)),                                       \
-	     "=r" ((USItype)(r))                                        \
+	   : "=r" __ll_check_output (USItype, q),                       \
+	     "=r" __ll_check_output (USItype, r)                        \
 	   : "r" ((USItype)(n1)),                                       \
 	     "r" ((USItype)(n0)),                                       \
 	     "rI" ((USItype)(d))                                        \
@@ -1140,7 +1151,7 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
 #   define UDIV_TIME 37
 #   define count_leading_zeros(count, x) \
   __asm__ ("scan %1,0,%0"                                               \
-	   : "=r" ((USItype)(x))                                        \
+	   : "=r" __ll_check_output (USItype, x)                        \
 	   : "r" ((USItype)(count)))
 /* Early sparclites return 63 for an argument of 0, but they warn that future
    implementations might change this.  Therefore, leave COUNT_LEADING_ZEROS_0
@@ -1190,8 +1201,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
  "        mulscc	%%g1,0,%%g1                                    \n" \
  "        add	%%g1,%%g2,%0                                           \n" \
  "        rd	%%y,%1"                                                 \
-	   : "=r" ((USItype)(w1)),                                      \
-	     "=r" ((USItype)(w0))                                       \
+	   : "=r" __ll_check_output (USItype, w1),                      \
+	     "=r" __ll_check_output (USItype, w0)                       \
 	   : "%rI" ((USItype)(u)),                                      \
 	     "r" ((USItype)(v))                                         \
 	   : "%g1", "%g2" __AND_CLOBBER_CC)
@@ -1218,8 +1229,8 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
 # define add_ssaaaa(sh, sl, ah, al, bh, bl) \
   __asm__ ("addl2 %5,%1\n" \
 	   "adwc %3,%0"                                                 \
-	   : "=g" ((USItype)(sh)),                                      \
-	     "=&g" ((USItype)(sl))                                      \
+	   : "=g" __ll_check_output (USItype, sh),                      \
+	     "=&g" __ll_check_output (USItype, sl)                      \
 	   : "%0" ((USItype)(ah)),                                      \
 	     "g" ((USItype)(bh)),                                       \
 	     "%1" ((USItype)(al)),                                      \
@@ -1227,8 +1238,8 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
 # define sub_ddmmss(sh, sl, ah, al, bh, bl) \
   __asm__ ("subl2 %5,%1\n" \
 	   "sbwc %3,%0"                                                 \
-	   : "=g" ((USItype)(sh)),                                      \
-	     "=&g" ((USItype)(sl))                                      \
+	   : "=g" __ll_check_output (USItype, sh),                      \
+	     "=&g" __ll_check_output (USItype, sl)                      \
 	   : "0" ((USItype)(ah)),                                       \
 	     "g" ((USItype)(bh)),                                       \
 	     "1" ((USItype)(al)),                                       \

-- 
2.56.1



More information about the Gcrypt-devel mailing list