[PATCH v2 14/32] mpi: Sync the i386 and x86-64 longlong.h with GCC.
Matt Turner
mattst88 at gmail.com
Wed Nov 25 21:19:51 CET 2026
* mpi/longlong.h [__i386__ || __i486__] (add_ssaaaa, sub_ddmmss)
(umul_ppmm, udiv_qrnnd): Support Intel asm syntax. Do not clobber
"cc". Use __ll_check_output.
(count_leading_zeros, count_trailing_zeros): Use builtins instead of
asm.
(UMUL_TIME, UDIV_TIME): Define unconditionally.
[__x86_64]: Test __x86_64__ instead.
(add_ssaaaa, sub_ddmmss): Use the "rme" constraint instead of "g".
(add_ssaaaa, sub_ddmmss, umul_ppmm, udiv_qrnnd): Support Intel asm
syntax. Do not clobber "cc". Use __ll_check_output.
(count_leading_zeros, count_trailing_zeros): Use builtins instead of
asm.
(UMUL_TIME, UDIV_TIME): Define unconditionally.
--
Take the i386 and x86-64 blocks from GCC's copy of longlong.h. "g"
allows any 64-bit immediate, which add and sub cannot encode; "rme"
allows only those that fit in a sign-extended 32 bits. GCC fixed
that and used builtins for count_leading_zeros and
count_trailing_zeros in commit e7e0aaeca8a ("longlong.h (__x86_64__):
Add definitions for add_ssaaaa..."), added the Intel syntax in commit
9fe2319edef, and switched x86-64 to the long long builtins in commit
10937b0c3a7. The "cc" clobber does nothing on x86, where asm always
clobbers the flags.
With the builtins, the compiler sees through the bsr and does not need
the operand in memory, as Clang put it before. The asm for add, sub,
mul and div is otherwise the same with GCC and Clang.
Signed-off-by: Matt Turner <mattst88 at gmail.com>
---
mpi/longlong.h | 178 ++++++++++++++++++++++-----------------------------------
1 file changed, 68 insertions(+), 110 deletions(-)
diff --git a/mpi/longlong.h b/mpi/longlong.h
index 50fa5e98..2bcd967d 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -468,121 +468,79 @@ extern USItype __udiv_qrnnd (USItype *, USItype, USItype, USItype);
************** I386 *****************
***************************************/
#if (defined (__i386__) || defined (__i486__)) && W_TYPE_SIZE == 32
-# define add_ssaaaa(sh, sl, ah, al, bh, bl) \
- __asm__ ("addl %5,%1\n" \
- "adcl %3,%0" \
- : "=r" ((sh)), \
- "=&r" ((sl)) \
- : "%0" ((USItype)(ah)), \
- "g" ((USItype)(bh)), \
- "%1" ((USItype)(al)), \
- "g" ((USItype)(bl)) \
- __CLOBBER_CC)
-# define sub_ddmmss(sh, sl, ah, al, bh, bl) \
- __asm__ ("subl %5,%1\n" \
- "sbbl %3,%0" \
- : "=r" ((sh)), \
- "=&r" ((sl)) \
- : "0" ((USItype)(ah)), \
- "g" ((USItype)(bh)), \
- "1" ((USItype)(al)), \
- "g" ((USItype)(bl)) \
- __CLOBBER_CC)
-# define umul_ppmm(w1, w0, u, v) \
- __asm__ ("mull %3" \
- : "=a" ((w0)), \
- "=d" ((w1)) \
- : "%0" ((USItype)(u)), \
- "rm" ((USItype)(v)) \
- __CLOBBER_CC)
-# define udiv_qrnnd(q, r, n1, n0, d) \
- __asm__ ("divl %4" \
- : "=a" ((q)), \
- "=d" ((r)) \
- : "0" ((USItype)(n0)), \
- "1" ((USItype)(n1)), \
- "rm" ((USItype)(d)) \
- __CLOBBER_CC)
-# define count_leading_zeros(count, x) \
- do { \
- USItype __cbtmp; \
- __asm__ ("bsrl %1,%0" \
- : "=r" (__cbtmp) : "rm" ((USItype)(x)) \
- __CLOBBER_CC); \
- (count) = __cbtmp ^ 31; \
- } while (0)
-# define count_trailing_zeros(count, x) \
- __asm__ ("rep;bsfl %1,%0" : "=r" (count) : "rm" ((USItype)(x)) __CLOBBER_CC)
-# ifndef UMUL_TIME
-# define UMUL_TIME 40
-# endif
-# ifndef UDIV_TIME
-# define UDIV_TIME 40
-# endif
+#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
+ __asm__ ("add{l} {%5,%1|%1,%5}\n\tadc{l} {%3,%0|%0,%3}" \
+ : "=r" __ll_check_output (USItype, sh), \
+ "=&r" __ll_check_output (USItype, sl) \
+ : "%0" ((USItype) (ah)), \
+ "g" ((USItype) (bh)), \
+ "%1" ((USItype) (al)), \
+ "g" ((USItype) (bl)))
+#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
+ __asm__ ("sub{l} {%5,%1|%1,%5}\n\tsbb{l} {%3,%0|%0,%3}" \
+ : "=r" __ll_check_output (USItype, sh), \
+ "=&r" __ll_check_output (USItype, sl) \
+ : "0" ((USItype) (ah)), \
+ "g" ((USItype) (bh)), \
+ "1" ((USItype) (al)), \
+ "g" ((USItype) (bl)))
+#define umul_ppmm(w1, w0, u, v) \
+ __asm__ ("mul{l} %3" \
+ : "=a" __ll_check_output (USItype, w0), \
+ "=d" __ll_check_output (USItype, w1) \
+ : "%0" ((USItype) (u)), \
+ "rm" ((USItype) (v)))
+#define udiv_qrnnd(q, r, n1, n0, dv) \
+ __asm__ ("div{l} %4" \
+ : "=a" __ll_check_output (USItype, q), \
+ "=d" __ll_check_output (USItype, r) \
+ : "0" ((USItype) (n0)), \
+ "1" ((USItype) (n1)), \
+ "rm" ((USItype) (dv)))
+#define count_leading_zeros(count, x) ((count) = __builtin_clz (x))
+#define count_trailing_zeros(count, x) ((count) = __builtin_ctz (x))
+#define UMUL_TIME 40
+#define UDIV_TIME 40
#endif /* 80x86 */
/***************************************
*********** AMD64 / x86-64 ************
***************************************/
-#if defined(__x86_64) && W_TYPE_SIZE == 64
-# define add_ssaaaa(sh, sl, ah, al, bh, bl) \
- __asm__ ("addq %5,%1\n" \
- "adcq %3,%0" \
- : "=r" ((sh)), \
- "=&r" ((sl)) \
- : "0" ((UDItype)(ah)), \
- "g" ((UDItype)(bh)), \
- "1" ((UDItype)(al)), \
- "g" ((UDItype)(bl)) \
- __CLOBBER_CC)
-# define sub_ddmmss(sh, sl, ah, al, bh, bl) \
- __asm__ ("subq %5,%1\n" \
- "sbbq %3,%0" \
- : "=r" ((sh)), \
- "=&r" ((sl)) \
- : "0" ((UDItype)(ah)), \
- "g" ((UDItype)(bh)), \
- "1" ((UDItype)(al)), \
- "g" ((UDItype)(bl)) \
- __CLOBBER_CC)
-# define umul_ppmm(w1, w0, u, v) \
- __asm__ ("mulq %3" \
- : "=a" ((w0)), \
- "=d" ((w1)) \
- : "0" ((UDItype)(u)), \
- "rm" ((UDItype)(v)) \
- __CLOBBER_CC)
-# define udiv_qrnnd(q, r, n1, n0, d) \
- __asm__ ("divq %4" \
- : "=a" ((q)), \
- "=d" ((r)) \
- : "0" ((UDItype)(n0)), \
- "1" ((UDItype)(n1)), \
- "rm" ((UDItype)(d)) \
- __CLOBBER_CC)
-# define count_leading_zeros(count, x) \
- do { \
- UDItype __cbtmp; \
- __asm__ ("bsrq %1,%0" \
- : "=r" (__cbtmp) : "rm" ((UDItype)(x)) \
- __CLOBBER_CC); \
- (count) = __cbtmp ^ 63; \
- } while (0)
-# define count_trailing_zeros(count, x) \
- do { \
- UDItype __cbtmp; \
- __asm__ ("rep;bsfq %1,%0" \
- : "=r" (__cbtmp) : "rm" ((UDItype)(x)) \
- __CLOBBER_CC); \
- (count) = __cbtmp; \
- } while (0)
-# ifndef UMUL_TIME
-# define UMUL_TIME 40
-# endif
-# ifndef UDIV_TIME
-# define UDIV_TIME 40
-# endif
-#endif /* __x86_64 */
+#if defined (__x86_64__) && W_TYPE_SIZE == 64
+#define add_ssaaaa(sh, sl, ah, al, bh, bl) \
+ __asm__ ("add{q} {%5,%1|%1,%5}\n\tadc{q} {%3,%0|%0,%3}" \
+ : "=r" __ll_check_output (UDItype, sh), \
+ "=&r" __ll_check_output (UDItype, sl) \
+ : "%0" ((UDItype) (ah)), \
+ "rme" ((UDItype) (bh)), \
+ "%1" ((UDItype) (al)), \
+ "rme" ((UDItype) (bl)))
+#define sub_ddmmss(sh, sl, ah, al, bh, bl) \
+ __asm__ ("sub{q} {%5,%1|%1,%5}\n\tsbb{q} {%3,%0|%0,%3}" \
+ : "=r" __ll_check_output (UDItype, sh), \
+ "=&r" __ll_check_output (UDItype, sl) \
+ : "0" ((UDItype) (ah)), \
+ "rme" ((UDItype) (bh)), \
+ "1" ((UDItype) (al)), \
+ "rme" ((UDItype) (bl)))
+#define umul_ppmm(w1, w0, u, v) \
+ __asm__ ("mul{q} %3" \
+ : "=a" __ll_check_output (UDItype, w0), \
+ "=d" __ll_check_output (UDItype, w1) \
+ : "%0" ((UDItype) (u)), \
+ "rm" ((UDItype) (v)))
+#define udiv_qrnnd(q, r, n1, n0, dv) \
+ __asm__ ("div{q} %4" \
+ : "=a" __ll_check_output (UDItype, q), \
+ "=d" __ll_check_output (UDItype, r) \
+ : "0" ((UDItype) (n0)), \
+ "1" ((UDItype) (n1)), \
+ "rm" ((UDItype) (dv)))
+#define count_leading_zeros(count, x) ((count) = __builtin_clzll (x))
+#define count_trailing_zeros(count, x) ((count) = __builtin_ctzll (x))
+#define UMUL_TIME 40
+#define UDIV_TIME 40
+#endif /* x86_64 */
/***************************************
--
2.56.1
More information about the Gcrypt-devel
mailing list