[PATCH v2 16/32] mpi: Sync the m68k longlong.h with GCC.

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


* mpi/longlong.h [__mc68000__]: Do not test __mc68020__, __NeXT__ or
mc68020.
[!__mc68020__] (umul_ppmm): Use GCC's version.
[__mcoldfire__] (umul_ppmm): New.
[__mcfisaaplus__ || __mcfisac__] (count_leading_zeros): New.
[__mc68020__] (COUNT_LEADING_ZEROS_0): Remove.

--

Take the m68k block from GCC's copy of longlong.h.  GCC defines
__mc68000__ for every m68k and ColdFire CPU, so the other tests are
redundant, and no GCC targets NeXT.

The 68000 umul_ppmm in Libgcrypt is GMP's, which lets the compiler
pick the registers; GCC's always uses d0 to d4.  The code is about the
same size either way.  GCC's ColdFire version, from commit
bab4470d329 ("longlong.h (umul_ppmm): Add ColdFire support."), uses
clr.w instead of eor.w, which ColdFire does not have.  ColdFire ISA
A+ and C have ff1, which GCC uses for __builtin_clz, since commit
7a6525d680a.  Nothing in Libgcrypt uses COUNT_LEADING_ZEROS_0.

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

diff --git a/mpi/longlong.h b/mpi/longlong.h
index a6532eca..4bcf6e17 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -569,91 +569,137 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
 /***************************************
  **************  68000	****************
  ***************************************/
-#if (defined (__mc68000__) || defined (__mc68020__)                     \
-     || defined (__NeXT__) || defined(mc68020)) && W_TYPE_SIZE == 32
-# define add_ssaaaa(sh, sl, ah, al, bh, bl) \
-  __asm__ ("add%.l %5,%1\n"                                             \
-	   "addx%.l %3,%0"                                              \
-	   : "=d" __ll_check_output (USItype, sh),                      \
-	     "=&d" __ll_check_output (USItype, sl)                      \
-	   : "%0" ((USItype)(ah)),                                      \
-	     "d" ((USItype)(bh)),                                       \
-	     "%1" ((USItype)(al)),                                      \
-	     "g" ((USItype)(bl)))
-# define sub_ddmmss(sh, sl, ah, al, bh, bl) \
-  __asm__ ("sub%.l %5,%1\n"                                             \
-	   "subx%.l %3,%0"                                              \
-	   : "=d" __ll_check_output (USItype, sh),                      \
-	     "=&d" __ll_check_output (USItype, sl)                      \
-	   : "0" ((USItype)(ah)),                                       \
-	     "d" ((USItype)(bh)),                                       \
-	     "1" ((USItype)(al)),                                       \
-	     "g" ((USItype)(bl)))
-# if (defined (__mc68020__) || defined (__NeXT__) || defined(mc68020)) \
-     && !defined (__mc68060__)
-#  define umul_ppmm(w1, w0, u, v) \
-  __asm__ ("mulu%.l %3,%1:%0"                                           \
-	   : "=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" __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" __ll_check_output (USItype, q),                       \
-	     "=d" __ll_check_output (USItype, r)                        \
-	   : "0" ((USItype)(n0)),                                       \
-	     "1" ((USItype)(n1)),                                       \
-	     "dmi" ((USItype)(d)))
-# else /* not mc68020 */
-#  define umul_ppmm(xh, xl, a, b) \
-  do { USItype __umul_tmp1, __umul_tmp2;			  \
-	__asm__ ("| Inlined umul_ppmm                         \n" \
- "        move%.l %5,%3                                       \n" \
- "        move%.l %2,%0                                       \n" \
- "        move%.w %3,%1                                       \n" \
- "        swap	%3                                            \n" \
- "        swap	%0                                            \n" \
- "        mulu	%2,%1                                         \n" \
- "        mulu	%3,%0                                         \n" \
- "        mulu	%2,%3                                         \n" \
- "        swap	%2                                            \n" \
- "        mulu	%5,%2                                         \n" \
- "        add%.l	%3,%2                                 \n" \
- "        jcc	1f                                            \n" \
- "        add%.l	%#0x10000,%0                          \n" \
- "1:	move%.l %2,%3                                         \n" \
- "        clr%.w	%2                                    \n" \
- "        swap	%2                                            \n" \
- "        swap	%3                                            \n" \
- "        clr%.w	%3                                    \n" \
- "        add%.l	%3,%1                                 \n" \
- "        addx%.l %2,%0                                       \n" \
- "        | End inlined umul_ppmm"                                \
-	      : "=&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)
-#  define UMUL_TIME 100
-#  define UDIV_TIME 400
-# endif /* not mc68020 */
-# if (defined (__mc68020__) || defined (__NeXT__) || defined(mc68020)) \
-     && !defined (__mcpu32__)
-#  define count_leading_zeros(count, x) \
-  __asm__ ("bfffo %1{%b2:%b2},%0"                                       \
-	   : "=d" __ll_check_output (USItype, count)                    \
-	   : "od" ((USItype)(x)), "n" (0))
-#  define COUNT_LEADING_ZEROS_0 32
-# endif
+#if defined (__mc68000__) && W_TYPE_SIZE == 32
+#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
+  __asm__ ("add%.l %5,%1\n\taddx%.l %3,%0"				\
+	   : "=d" __ll_check_output (USItype, sh),			\
+	     "=&d" __ll_check_output (USItype, sl)			\
+	   : "%0" ((USItype) (ah)),					\
+	     "d" ((USItype) (bh)),					\
+	     "%1" ((USItype) (al)),					\
+	     "g" ((USItype) (bl)))
+#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
+  __asm__ ("sub%.l %5,%1\n\tsubx%.l %3,%0"				\
+	   : "=d" __ll_check_output (USItype, sh),			\
+	     "=&d" __ll_check_output (USItype, sl)			\
+	   : "0" ((USItype) (ah)),					\
+	     "d" ((USItype) (bh)),					\
+	     "1" ((USItype) (al)),					\
+	     "g" ((USItype) (bl)))
+
+/* The '020, '030, '040, '060 and CPU32 have 32x32->64 and 64/32->32q-32r.  */
+#if (defined (__mc68020__) && !defined (__mc68060__))
+#define umul_ppmm(w1, w0, u, v) \
+  __asm__ ("mulu%.l %3,%1:%0"						\
+	   : "=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" __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" __ll_check_output (USItype, q),			\
+	     "=d" __ll_check_output (USItype, r)			\
+	   : "0" ((USItype) (n0)),					\
+	     "1" ((USItype) (n1)),					\
+	     "dmi" ((USItype) (d)))
+
+#elif defined (__mcoldfire__) /* not mc68020 */
+
+#define umul_ppmm(xh, xl, a, b) \
+  __asm__ ("| Inlined umul_ppmm\n"					\
+	   "	move%.l	%2,%/d0\n"					\
+	   "	move%.l	%3,%/d1\n"					\
+	   "	move%.l	%/d0,%/d2\n"					\
+	   "	swap	%/d0\n"						\
+	   "	move%.l	%/d1,%/d3\n"					\
+	   "	swap	%/d1\n"						\
+	   "	move%.w	%/d2,%/d4\n"					\
+	   "	mulu	%/d3,%/d4\n"					\
+	   "	mulu	%/d1,%/d2\n"					\
+	   "	mulu	%/d0,%/d3\n"					\
+	   "	mulu	%/d0,%/d1\n"					\
+	   "	move%.l	%/d4,%/d0\n"					\
+	   "	clr%.w	%/d0\n"						\
+	   "	swap	%/d0\n"						\
+	   "	add%.l	%/d0,%/d2\n"					\
+	   "	add%.l	%/d3,%/d2\n"					\
+	   "	jcc	1f\n"						\
+	   "	add%.l	%#65536,%/d1\n"					\
+	   "1:	swap	%/d2\n"						\
+	   "	moveq	%#0,%/d0\n"					\
+	   "	move%.w	%/d2,%/d0\n"					\
+	   "	move%.w	%/d4,%/d2\n"					\
+	   "	move%.l	%/d2,%1\n"					\
+	   "	add%.l	%/d1,%/d0\n"					\
+	   "	move%.l	%/d0,%0"					\
+	   : "=g" __ll_check_output (USItype, xh),			\
+	     "=g" __ll_check_output (USItype, xl)			\
+	   : "g" ((USItype) (a)),					\
+	     "g" ((USItype) (b))					\
+	   : "d0", "d1", "d2", "d3", "d4")
+#define UMUL_TIME 100
+#define UDIV_TIME 400
+#else /* not ColdFire */
+/* %/ inserts REGISTER_PREFIX, %# inserts IMMEDIATE_PREFIX.  */
+#define umul_ppmm(xh, xl, a, b) \
+  __asm__ ("| Inlined umul_ppmm\n"					\
+	   "	move%.l	%2,%/d0\n"					\
+	   "	move%.l	%3,%/d1\n"					\
+	   "	move%.l	%/d0,%/d2\n"					\
+	   "	swap	%/d0\n"						\
+	   "	move%.l	%/d1,%/d3\n"					\
+	   "	swap	%/d1\n"						\
+	   "	move%.w	%/d2,%/d4\n"					\
+	   "	mulu	%/d3,%/d4\n"					\
+	   "	mulu	%/d1,%/d2\n"					\
+	   "	mulu	%/d0,%/d3\n"					\
+	   "	mulu	%/d0,%/d1\n"					\
+	   "	move%.l	%/d4,%/d0\n"					\
+	   "	eor%.w	%/d0,%/d0\n"					\
+	   "	swap	%/d0\n"						\
+	   "	add%.l	%/d0,%/d2\n"					\
+	   "	add%.l	%/d3,%/d2\n"					\
+	   "	jcc	1f\n"						\
+	   "	add%.l	%#65536,%/d1\n"					\
+	   "1:	swap	%/d2\n"						\
+	   "	moveq	%#0,%/d0\n"					\
+	   "	move%.w	%/d2,%/d0\n"					\
+	   "	move%.w	%/d4,%/d2\n"					\
+	   "	move%.l	%/d2,%1\n"					\
+	   "	add%.l	%/d1,%/d0\n"					\
+	   "	move%.l	%/d0,%0"					\
+	   : "=g" __ll_check_output (USItype, xh),			\
+	     "=g" __ll_check_output (USItype, xl)			\
+	   : "g" ((USItype) (a)),					\
+	     "g" ((USItype) (b))					\
+	   : "d0", "d1", "d2", "d3", "d4")
+#define UMUL_TIME 100
+#define UDIV_TIME 400
+
+#endif /* not mc68020 */
+
+/* The '020, '030, '040 and '060 have bitfield insns.
+   cpu32 disguises as a 68020, but lacks them.  */
+#if defined (__mc68020__) && !defined (__mcpu32__)
+#define count_leading_zeros(count, x) \
+  __asm__ ("bfffo %1{%b2:%b2},%0"					\
+	   : "=d" __ll_check_output (USItype, count)			\
+	   : "od" ((USItype) (x)), "n" (0))
+/* Some ColdFire architectures have a ff1 instruction supported via
+   __builtin_clz. */
+#elif defined (__mcfisaaplus__) || defined (__mcfisac__)
+#define count_leading_zeros(count,x) ((count) = __builtin_clz (x))
+#define COUNT_LEADING_ZEROS_0 32
+#endif
 #endif /* mc68000 */
 
 

-- 
2.56.1



More information about the Gcrypt-devel mailing list