Hi!
When working on the C++ _BitInt support (just posted), I've noticed that
4 recentish bitint tests are just dg-do compile instead of dg-do run
as I've meant. 3 of those work without any changes, the 4th runs into
some problems. Three of them are problems in the testcase, for
signed _BitInt(1) -1 * -1 and -1 % -1 invoke UB and the check for
return value of large negative bit-precise int times 0, and there is
a question what -- and ++ operators should do on signed _BitInt(1), because
they are defined as E-=1 or E+=1 where 1 is of the appropriate type,
but signed _BitInt(1) doesn't have value 1, so we need E+=-1 and E-=-1
instead.
Given
int
main ()
{
signed _BitInt(3) a;
a = 3wb;
++a;
a = -3wb - 1;
--a;
signed char b;
b = __SCHAR_MAX__;
++b;
b = -__SCHAR_MAX__ - 1;
--b;
}
with -fsanitize=undefined, I think we already implement the
signed _BitInt --/++ as C23/C2Y says so right now, i.e. for
bit-precise integers it uses 1 of the appropriate type, for other
types too, but given E+=1 and E-=1 promotion rules for signed
char/char/signed short or extended signed integer types with precision
smaller than int do promote.
So it is just the weird signed _BitInt(1) case.
For C++ with the patch I've posted it behaves similarly despite the
current wording of the proposal, which I hope will be changed.
Anyway, is this patch ok for trunk?
2026-11-02 Jakub Jelinek <[email protected]>
* gcc.dg/torture/bitint-96.c: Change dg-do compile to dg-do run.
(bar): Avoid signed _BitInt(1) -1 * -1 and -1 % -1 as that is UB.
Don't do predecrement and postincrement for now at runtime.
(main): Fix expected result of -large * 0 multiplication.
* gcc.dg/torture/bitint-97.c: Change dg-do compile to dg-do run.
* gcc.dg/torture/bitint-98.c: Likewise.
* gcc.dg/torture/bitint-99.c: Likewise.
--- a/gcc/testsuite/gcc.dg/torture/bitint-96.c 2026-04-28 08:54:25.379972942
+0200
+++ b/gcc/testsuite/gcc.dg/torture/bitint-96.c 2026-11-02 14:57:32.415742429
+0100
@@ -1,5 +1,5 @@
/* C2Y N3747 - Integer Sets, v5 */
-/* { dg-do compile { target bitint } } */
+/* { dg-do run { target bitint } } */
/* { dg-options "-std=c2y -pedantic-errors" } */
#define expr_has_type(e, t) _Generic (e, default : 0, t : 1)
@@ -39,11 +39,11 @@ bar (_BitInt(1) a[21], signed _BitInt(1)
a[4] = b[4] - c[4];
a[5] = b[5] - c[5];
a[6] = b[6] * c[6];
- a[7] = b[7] * c[8];
+ a[7] = b[7] * c[7];
a[8] = b[8] * c[8];
a[9] = b[9] / c[9];
a[10] = b[10] % c[10];
- a[11] = b[11] % c[11];
+ a[11] = b[10] % c[11];
a[12] = b[12] & c[12];
a[13] = b[13] & c[13];
a[14] = b[14] & c[14];
@@ -87,8 +87,16 @@ main ()
{
if (b != 0 || c != -1)
__builtin_abort ();
- --b;
- ++c;
+ if (0)
+ {
+ --b;
+ ++c;
+ }
+ else
+ {
+ b += (_BitInt(1)) -1wb;
+ c -= (_BitInt(1)) -1wb;
+ }
if (b != -1 || c != 0)
__builtin_abort ();
b = ~b;
@@ -118,7 +126,7 @@ main ()
0, 0, -1,
0, -1, -1,
0, 0, -1
- };
+ };
bar (e, f, g);
_BitInt(1) h[21] = {
0, -1, -1,
@@ -139,8 +147,7 @@ main ()
__builtin_abort ();
if (baz
(24807048826655379640613156875228202584070386120119745959437529938064756141700wb,
0wb) != 0wb)
__builtin_abort ();
- if (baz
(-24807048826655379640613156875228202584070386120119745959437529938064756141700wb,
0wb)
- !=
24807048826655379640613156875228202584070386120119745959437529938064756141700wb)
+ if (baz
(-24807048826655379640613156875228202584070386120119745959437529938064756141700wb,
0wb) != 0wb)
__builtin_abort ();
if (baz (0wb, ~0wb) != 0wb)
__builtin_abort ();
--- a/gcc/testsuite/gcc.dg/torture/bitint-97.c 2026-04-28 08:54:25.380071489
+0200
+++ b/gcc/testsuite/gcc.dg/torture/bitint-97.c 2026-11-02 14:15:53.120759171
+0100
@@ -1,5 +1,5 @@
/* C2Y N3747 - Integer Sets, v5 */
-/* { dg-do compile { target bitint } } */
+/* { dg-do run { target bitint } } */
/* { dg-options "-std=c2y -pedantic-errors -fwrapv" } */
[[gnu::noipa]] void
@@ -24,7 +24,7 @@ main ()
};
signed _BitInt(1) g[8] = {
-1, -1, -1, -1, -1, 0, 0, -1,
- };
+ };
bar (e, f, g);
_BitInt(1) h[8] = {
0, -1, -1, -1, 0, 0, -1, -1
--- a/gcc/testsuite/gcc.dg/torture/bitint-98.c 2026-04-28 08:54:25.380165117
+0200
+++ b/gcc/testsuite/gcc.dg/torture/bitint-98.c 2026-11-02 14:16:08.734552857
+0100
@@ -1,5 +1,5 @@
/* C2Y N3747 - Integer Sets, v5 */
-/* { dg-do compile { target bitint } } */
+/* { dg-do run { target bitint } } */
/* { dg-options "-std=c2y -pedantic-errors" } */
/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */
/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */
@@ -22,7 +22,6 @@ baz (_BitInt(256) x, _BitInt(256) y, _Bi
{
return __builtin_mul_overflow (x, y, z);
}
-
#endif
int
--- a/gcc/testsuite/gcc.dg/torture/bitint-99.c 2026-05-30 09:46:04.466953334
+0200
+++ b/gcc/testsuite/gcc.dg/torture/bitint-99.c 2026-11-02 14:16:14.882471619
+0100
@@ -1,5 +1,5 @@
/* C2Y N3705: bit-precise enum. */
-/* { dg-do compile { target bitint } } */
+/* { dg-do run { target bitint } } */
/* { dg-options "-std=c2y -pedantic-errors" } */
/* { dg-skip-if "" { ! run_expensive_tests } { "*" } { "-O0" "-O2" } } */
/* { dg-skip-if "" { ! run_expensive_tests } { "-flto" } { "" } } */
Jakub