[PATCH v2 03/32] mpi: Fix operands of sparclite count_leading_zeros.
Matt Turner
mattst88 at gmail.com
Wed Nov 25 21:19:40 CET 2026
* mpi/longlong.h [__sparclite__] (count_leading_zeros): Swap the output
and input operands.
--
The macro has the two operands backwards: it declares the value to be
scanned as the asm output and the place to store the result as the asm
input, so it clobbers its argument and never sets the count.
Callers pass count as an lvalue and x as an arbitrary expression, so as
written the macro also fails to compile whenever x is not an lvalue.
With sparc64-unknown-linux-gnu-gcc -m32 -mcpu=sparclite, which defines
__sparclite__, a count_leading_zeros on an lvalue in a function that
does not use x afterwards compiles to no scan at all: GCC sees that the
asm's output is unused and deletes it, and the function returns an
uninitialized count. An rvalue argument fails with "lvalue required
in 'asm' statement". After this change the macro compiles to a single
scan instruction.
GCC fixed the same bug in its copy in 1998 (commit 84530511d25, whose
ChangeLog entry reads "Sparclite scan instruction was being invoked
incorrectly"), and GMP followed. That change also made the second scan
operand 1 instead of 0. This keeps 0: scan XORs rs1 with operand2
shifted arithmetically right by one, so 0 and 1 both give a plain
leading-zero count. That is how the erc32 simulator implements scan,
and the gas testsuite and GCC's former sparclite ffssi2 pattern both
use "scan x, 0, y" to find the first set bit.
Signed-off-by: Matt Turner <mattst88 at gmail.com>
---
mpi/longlong.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/mpi/longlong.h b/mpi/longlong.h
index 04c546c5..c10abddf 100644
--- a/mpi/longlong.h
+++ b/mpi/longlong.h
@@ -1151,8 +1151,8 @@ typedef unsigned int UTItype __attribute__ ((mode (TI)));
# define UDIV_TIME 37
# define count_leading_zeros(count, x) \
__asm__ ("scan %1,0,%0" \
- : "=r" __ll_check_output (USItype, x) \
- : "r" ((USItype)(count)))
+ : "=r" __ll_check_output (USItype, count) \
+ : "r" ((USItype)(x)))
/* 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. */
--
2.56.1
More information about the Gcrypt-devel
mailing list