From 94d2bc6d08caad947a8897a538f2a9b64e16a11a Mon Sep 17 00:00:00 2001 From: Yang Yujie Date: Wed, 6 Aug 2025 12:03:39 +0200 Subject: bitint: Zero-extend the result of a signed->unsigned widening cast A widening cast from a signed _BitInt operand to an unsigned _BitInt type involves filling the extra limb(s) with sign extension. On a target that wants _BitInts extended in memory, if this unsigned type has a partial limb, the unused part of it should be zeroed. e.g. Assuming limb_mode == E_DImode, at the end of void test (unsigned _BitInt(519) *t, _BitInt(512) x) { *t = -x; } the most significant limb of *t should be masked with 0x7f. This patch also fixes gcc.dg/torture/bitint-16.c, which aborts at -O2 when the extension on load is optimized away. * gimple-lower-bitint.cc (bitint_large_huge::lower_mergeable_stmt): Zero-extend the partial limb of any unsigned _BitInt LHS assigned with a widening sign-extension. * gcc.dg/torture/bitint-83.c: New test. --- gcc/testsuite/gcc.dg/torture/bitint-83.c | 48 ++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/torture/bitint-83.c (limited to 'gcc/testsuite') diff --git a/gcc/testsuite/gcc.dg/torture/bitint-83.c b/gcc/testsuite/gcc.dg/torture/bitint-83.c new file mode 100644 index 0000000..8a9df44 --- /dev/null +++ b/gcc/testsuite/gcc.dg/torture/bitint-83.c @@ -0,0 +1,48 @@ +/* Derived from a test in gcc.dg/torture/bitint-16.c */ +/* { dg-do run { target bitint } } */ +/* { dg-options "-std=c23 -pedantic-errors" } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */ +/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */ + +#include "../bitintext.h" + +#define BASIC_TESTS \ + TEST(8) \ + TEST(16) \ + TEST(32) + +#if __BITINT_MAXWIDTH__ >= 519 +#define ALL_TESTS \ + BASIC_TESTS \ + TEST(64) \ + TEST(128) \ + TEST(256) \ + TEST(512) +#else +#define ALL_TESTS BASIC_TESTS +#endif + +#define TEST(N) \ +void \ +test##N (unsigned _BitInt(N + 7) *t, _BitInt(N) x) \ +{ \ + *t = -x; \ +} +ALL_TESTS +#undef TEST + +volatile int y = 0; + +int +main (void) +{ +#define TEST(N) \ + { \ + unsigned _BitInt(N + 7) t; \ + _BitInt(N) x = y + N; \ + test##N (&t, x); \ + BEXTC (t); \ + } + ALL_TESTS +#undef TEST +} -- cgit v1.1