On Tue, Sep 05, 2023 at 10:40:26PM +0000, Joseph Myers wrote:
> Additional tests I think should be added (for things I expect should
> already work):
>
> * Tests for BITINT_MAXWIDTH in <limits.h>. Test that it's defined for
> C2x, but not defined for C11/C17 (the latter independent of whether the
> target has _BitInt support). Test the value as well: _BitInt
> (BITINT_MAXWIDTH) should be OK (both signed and unsigned) but _BitInt
> (BITINT_MAXWIDTH + 1) should not be OK. Also test that BITINT_MAXWIDTH >=
> ULLONG_MAX.
>
> * Test _BitInt (N) where N is a constexpr variable or enum constant (I
> expect these should work - the required call to convert_lvalue_to_rvalue
> for constexpr to work is present - but I don't see such tests in the
> testsuite).
>
> * Test that -funsigned-bitfields does not affect the signedness of _BitInt
> (N) bit-fields (the standard wording isn't entirely clear, but that's
> what's implemented in the patches).
>
> * Test the errors for _Sat used with _BitInt (though such a test might not
> actually run at present because no target supports both features).
Here is a patch which adds that test coverage.
> I looked at places in c-family/ and c/ that refer to INTEGER_TYPE to
> consider whether they should handle BITINT_TYPE and whether that matches
> what the patch does there. I think the following places that don't handle
> it probably should (with appropriate testcases added for the relevant
> functionality, e.g. warnings) unless there is some specific reason in each
> case why that's unnecessary or incorrect. c-pretty-print.cc:
> c_pretty_printer::direct_declarator and c_pretty_printer::declarator.
> c-warn.cc throughout. Maybe c-ada-spec.cc, though it might be best to ask
> the Ada maintainers there. c-common.cc: unsafe_conversion_p,
> c_common_truthvalue_conversion warnings, c_common_get_alias_set,
> check_builtin_function_arguments BUILT_IN_ASSUME_ALIGNED case.
> c-aux-info.cc (might need other support for generating appropriate names
> in output). c-parser.cc:c_parser_omp_clause_schedule. c-fold.cc
> throughout. c-typeck.c: the build_conditional_expr case where one operand
> is EXCESS_PRECISION_EXPR; build_c_cast; convert_for_assignment checks for
> integer/pointer conversions.
As well as what I've managed to come up for the above changes.
2023-09-06 Jakub Jelinek <jakub@redhat.com>
PR c/102989
* gcc.dg/bitint-2.c (foo): Add tests for constexpr var or enumerator
arguments of _BitInt.
* gcc.dg/bitint-31.c: Remove forgotten 0 &&.
* gcc.dg/bitint-32.c: New test.
* gcc.dg/bitint-33.c: New test.
* gcc.dg/bitint-34.c: New test.
* gcc.dg/bitint-35.c: New test.
* gcc.dg/bitint-36.c: New test.
* gcc.dg/fixed-point/bitint-1.c: New test.
Jakub
@@ -14,6 +14,10 @@ foo (void)
_BitInt(5) unsigned d = (unsigned _BitInt(5)) 4;
_BitInt(32) e = (_BitInt(32)) 5;
_BitInt(32) unsigned f = (unsigned _BitInt(32)) 6;
+ constexpr int g = 43;
+ enum E { F = 44 };
+ _BitInt(g) h;
+ unsigned _BitInt(F) i;
static_assert (expr_has_type (a, signed _BitInt(42)), "");
static_assert (expr_has_type (a, _BitInt(42)), "");
static_assert (!expr_has_type (a, unsigned _BitInt(42)), "");
@@ -66,6 +70,8 @@ foo (void)
static_assert (expr_has_type (-1UWB, unsigned _BitInt(1)), "");
static_assert (expr_has_type (1uWB, unsigned _BitInt(1)), "");
static_assert (expr_has_type (2Uwb, unsigned _BitInt(2)), "");
+ static_assert (expr_has_type (h, signed _BitInt(43)), "");
+ static_assert (expr_has_type (i, unsigned _BitInt(44)), "");
static_assert (0wb == 0, "");
static_assert (-1wb == -1, "");
static_assert (0xffffffffwb == 4294967295wb, "");
@@ -255,7 +255,7 @@ main ()
check_round (testfltu_192 (340282356779733661637539395458142568448uwb), __builtin_inff (), 0xffffffp+104f, __builtin_inff (), 0xffffffp+104f);
check_round (testfltu_192 (6277101735386680763835789423207666416102355444464034512895uwb), __builtin_inff (), 0xffffffp+104f, __builtin_inff (), 0xffffffp+104f);
#endif
-#if 0 && __BITINT_MAXWIDTH__ >= 575
+#if __BITINT_MAXWIDTH__ >= 575
check_round (testflt_575 (10633823015541376812058405359715352575wb), 0xfffffep+99f, 0xfffffep+99f, 0xffffffp+99f, 0xfffffep+99f);
check_round (testflt_575 (10633823015541376812058405359715352576wb), 0xfffffep+99f, 0xfffffep+99f, 0xffffffp+99f, 0xfffffep+99f);
check_round (testflt_575 (10633823015541376812058405359715352577wb), 0xffffffp+99f, 0xfffffep+99f, 0xffffffp+99f, 0xfffffep+99f);
@@ -0,0 +1,14 @@
+/* PR c/102989 */
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-std=c2x" } */
+
+#include <limits.h>
+
+#ifndef BITINT_MAXWIDTH
+#error BITINT_MAXWIDTH not defined
+#elif BITINT_MAXWIDTH < ULLONG_WIDTH
+#error BITINT_MAXWIDTH smaller than ULLONG_WIDTH
+#endif
+
+_BitInt(BITINT_MAXWIDTH) a;
+_BitInt(BITINT_MAXWIDTH + 1) b; /* { dg-error "'_BitInt' argument '\[0-9]+' is larger than 'BITINT_MAXWIDTH' '\[0-9]+'" } */
@@ -0,0 +1,9 @@
+/* PR c/102989 */
+/* { dg-do compile } */
+/* { dg-options "-std=c17" } */
+
+#include <limits.h>
+
+#ifdef BITINT_MAXWIDTH
+#error BITINT_MAXWIDTH defined in C11
+#endif
@@ -0,0 +1,16 @@
+/* PR c/102989 */
+/* Test that -funsigned-bitfields doesn't affect _BitInt bit-fields which are always signed. */
+/* { dg-do run { target bitint } } */
+/* { dg-options "-std=c2x -funsigned-bitfields" } */
+
+struct S { _BitInt(22) a : 7; signed _BitInt(22) b : 7; unsigned _BitInt(22) c : 7; } s;
+
+int
+main ()
+{
+ s.a = -64;
+ s.b = -64;
+ s.c = -64;
+ if (s.a != -64 || s.b != -64 || s.c != 64)
+ __builtin_abort ();
+}
@@ -0,0 +1,37 @@
+/* PR c/102989 */
+/* { dg-do compile { target { bitint && { float32 && int32 } } } } */
+/* { dg-options "-std=c2x -Wconversion -Wfloat-conversion" } */
+/* { dg-add-options float32 } */
+
+void
+foo (_Float32 x)
+{
+ _BitInt(57) a = 1.5F32; /* { dg-warning "conversion from '_Float32' to '_BitInt\\\(57\\\)' changes value from '1.5e\\\+0f32' to '1'" } */
+ _BitInt(27) b = 76117358uwb; /* { dg-warning "signed conversion from 'unsigned _BitInt\\\(27\\\)' to '_BitInt\\\(27\\\)' changes value from '76117358' to '-58100370'" } */
+ unsigned _BitInt(27) c = -15wb; /* { dg-warning "unsigned conversion from '_BitInt\\\(5\\\)' to 'unsigned _BitInt\\\(27\\\)' changes value from '-15' to '134217713'" } */
+ _BitInt(27) d = -390288573wb; /* { dg-warning "overflow in conversion from '_BitInt\\\(30\\\)' to '_BitInt\\\(27\\\)' changes value from '-390288573' to '12364611'" } */
+ unsigned _BitInt(27) e = 309641337uwb; /* { dg-warning "conversion from 'unsigned _BitInt\\\(29\\\)' to 'unsigned _BitInt\\\(27\\\)' changes value from '309641337' to '41205881'" } */
+ _BitInt(27) f = 76117358U; /* { dg-warning "signed conversion from 'unsigned int' to '_BitInt\\\(27\\\)' changes value from '76117358' to '-58100370'" } */
+ unsigned _BitInt(27) g = -15; /* { dg-warning "unsigned conversion from 'int' to 'unsigned _BitInt\\\(27\\\)' changes value from '-15' to '134217713'" } */
+ _BitInt(27) h = -390288573; /* { dg-warning "overflow in conversion from 'int' to '_BitInt\\\(27\\\)' changes value from '-390288573' to '12364611'" } */
+ unsigned _BitInt(27) i = 309641337U; /* { dg-warning "conversion from 'unsigned int' to 'unsigned _BitInt\\\(27\\\)' changes value from '309641337' to '41205881'" } */
+ int j = 2936216298uwb; /* { dg-warning "signed conversion from 'unsigned _BitInt\\\(32\\\)' to 'int' changes value from '2936216298' to '-1358750998'" } */
+ unsigned int k = -15wb; /* { dg-warning "unsigned conversion from '_BitInt\\\(5\\\)' to 'unsigned int' changes value from '-15' to '4294967281'" } */
+ int l = -8087431137529383656wb; /* { dg-warning "overflow in conversion from '_BitInt\\\(64\\\)' to 'int' changes value from '-8087431137529383656' to '-1105152744'" } */
+ unsigned int m = 1664073919553255778uwb; /* { dg-warning "conversion from 'unsigned _BitInt\\\(61\\\)' to 'unsigned int' changes value from '1664073919553255778' to '3338058082'" } */
+#if __BITINT_MAXWIDTH__ >= 575
+ _Float32 n = 51441631083309184313435496923626431699697406185384986811300218556561965470218425783308778801748592322915101142266821623326688106425864884688172114173397118407357447763009120wb; /* { dg-warning "conversion from '_BitInt\\\(575\\\)' to '_Float32' changes value from '0x353eab28b46b03ea99b84f9736cd8dbe5e986915a0383c3cb381c0da41e31b3621c75fd53262bfcb1b0e6251dbf00f3988784e29b08b65640c263e4d0959832a20e2ff5245be1e60' to '\\\+Inff32'" "" { target bitint575 } } */
+#endif
+ _BitInt(57) o = x; /* { dg-warning "conversion from '_Float32' to '_BitInt\\\(57\\\)' may change value" } */
+ unsigned _BitInt(15) p = 32767uwb;
+ unsigned _BitInt(15) q = (_BitInt(42)) p;
+ _BitInt(17) r = 0;
+ _BitInt(17) s = ((_BitInt(42)) r) & 32767wb;
+#if __BITINT_MAXWIDTH__ >= 575
+ _BitInt(575) t = 0;
+ _Float32 u = t; /* { dg-warning "conversion from '_BitInt\\\(575\\\)' to '_Float32' may change value" "" { target bitint575 } } */
+#endif
+ _BitInt(42) v = 0;
+ unsigned _BitInt(17) w = v; /* { dg-warning "conversion from '_BitInt\\\(42\\\)' to 'unsigned _BitInt\\\(17\\\)' may change value" } */
+ _BitInt(17) y = v; /* { dg-warning "conversion from '_BitInt\\\(42\\\)' to '_BitInt\\\(17\\\)' may change value" } */
+}
@@ -0,0 +1,39 @@
+/* PR c/102989 */
+/* { dg-do compile { target { bitint } } } */
+/* { dg-options "-std=c2x -Wint-in-bool-context -Waddress -Wpointer-to-int-cast -Wint-to-pointer-cast -Wint-conversion -Wshift-negative-value -Wshift-count-overflow -Wdiv-by-zero" } */
+
+extern char *ax[];
+
+void bar (int);
+
+void
+foo (_BitInt(61) x, long long *y)
+{
+ if (x << 15) /* { dg-warning "'<<' in boolean context, did you mean '<'" } */
+ ;
+ bar ((_BitInt(sizeof (void *) * __CHAR_BIT__)) (ax + 0) == 0);
+ bar ((unsigned _BitInt(sizeof (void *) * __CHAR_BIT__)) (ax + 1) == 0);
+ char *z = __builtin_assume_aligned (y, 32wb, 8wb);
+ *z = 42;
+ _BitInt(sizeof (void *) * __CHAR_BIT__ + 1) a = (_BitInt(sizeof (void *) * __CHAR_BIT__ + 1)) y; /* { dg-warning "cast from pointer to integer of different size" } */
+ unsigned _BitInt(sizeof (void *) * __CHAR_BIT__ + 1) b = (unsigned _BitInt(sizeof (void *) * __CHAR_BIT__ + 1)) y; /* { dg-warning "cast from pointer to integer of different size" } */
+ _BitInt(sizeof (void *) * __CHAR_BIT__ - 1) c = (_BitInt(sizeof (void *) * __CHAR_BIT__ - 1)) y; /* { dg-warning "cast from pointer to integer of different size" } */
+ unsigned _BitInt(sizeof (void *) * __CHAR_BIT__ - 1) d = (unsigned _BitInt(sizeof (void *) * __CHAR_BIT__ - 1)) y; /* { dg-warning "cast from pointer to integer of different size" } */
+ void *e = (void *) a; /* { dg-warning "cast to pointer from integer of different size" } */
+ void *f = (void *) b; /* { dg-warning "cast to pointer from integer of different size" } */
+ void *g = (void *) c; /* { dg-warning "cast to pointer from integer of different size" } */
+ void *h = (void *) d; /* { dg-warning "cast to pointer from integer of different size" } */
+ a = y; /* { dg-warning "assignment to '_BitInt\\\(\[0-9]+\\\)' from 'long long int \\\*' makes integer from pointer without a cast" } */
+ b = y; /* { dg-warning "assignment to 'unsigned _BitInt\\\(\[0-9]+\\\)' from 'long long int \\\*' makes integer from pointer without a cast" } */
+ c = y; /* { dg-warning "assignment to '_BitInt\\\(\[0-9]+\\\)' from 'long long int \\\*' makes integer from pointer without a cast" } */
+ d = y; /* { dg-warning "assignment to 'unsigned _BitInt\\\(\[0-9]+\\\)' from 'long long int \\\*' makes integer from pointer without a cast" } */
+ e = a; /* { dg-warning "assignment to 'void \\\*' from '_BitInt\\\(\[0-9]+\\\)' makes pointer from integer without a cast" } */
+ f = b; /* { dg-warning "assignment to 'void \\\*' from 'unsigned _BitInt\\\(\[0-9]+\\\)' makes pointer from integer without a cast" } */
+ g = c; /* { dg-warning "assignment to 'void \\\*' from '_BitInt\\\(\[0-9]+\\\)' makes pointer from integer without a cast" } */
+ h = d; /* { dg-warning "assignment to 'void \\\*' from 'unsigned _BitInt\\\(\[0-9]+\\\)' makes pointer from integer without a cast" } */
+ _BitInt(61) i = (1wb - 1152921504606846975wb) << 1; /* { dg-warning "left shift of negative value" } */
+ /* { dg-warning "result of '-1152921504606846974 << 1' requires 62 bits to represent, but '_BitInt\\\(61\\\)' only has 61 bits" "" { target *-*-* } .-1 } */
+ _BitInt(61) j = i << (60 + 1); /* { dg-warning "left shift count >= width of type" } */
+ _BitInt(61) k = i >> (60 + 1); /* { dg-warning "right shift count >= width of type" } */
+ _BitInt(61) l = i / (51wb - 51wb); /* { dg-warning "division by zero" } */
+}
@@ -0,0 +1,10 @@
+/* PR c/102989 */
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-std=c2x" } */
+
+void
+foo (void)
+{
+ _Sat _BitInt (42) a; /* { dg-error "both '_Sat' and '_BitInt' in declaration specifiers" } */
+ _BitInt (42) _Sat b; /* { dg-error "both '_Sat' and '_BitInt' in declaration specifiers" } */
+}