1 /* PR sanitizer/69099 */
2 /* { dg-do compile } */
3 /* { dg-options "-std=c11 -pedantic-errors -fsanitize=float-cast-overflow" } */
4 /* This is gcc.dg/atomic/c11-atomic-exec-2.c.  */
5 
6 extern void abort (void);
7 extern void exit (int);
8 
9 #define CMPLX(X, Y) __builtin_complex ((X), (Y))
10 
11 #define TEST_COMPOUND(TYPE, LHSVAL, RHSVAL, OP)				\
12   do									\
13     {									\
14       static volatile _Atomic (TYPE) a = (TYPE) (LHSVAL);		\
15       if ((a OP##= (RHSVAL)) != (TYPE) ((TYPE) (LHSVAL) OP (RHSVAL)))	\
16 	abort ();							\
17       if (a != (TYPE) ((TYPE) (LHSVAL) OP (RHSVAL)))			\
18 	abort ();							\
19     }									\
20   while (0)
21 
22 #define TEST_COMPOUND_ARITH(LHSVAL, RHSVAL, OP)				\
23   do									\
24     {									\
25       TEST_COMPOUND (_Bool, (LHSVAL), (RHSVAL), OP);			\
26       TEST_COMPOUND (char, (LHSVAL), (RHSVAL), OP);			\
27       TEST_COMPOUND (signed char, (LHSVAL), (RHSVAL), OP);		\
28       TEST_COMPOUND (unsigned char, (LHSVAL), (RHSVAL), OP);		\
29       TEST_COMPOUND (signed short, (LHSVAL), (RHSVAL), OP);		\
30       TEST_COMPOUND (unsigned short, (LHSVAL), (RHSVAL), OP);		\
31       TEST_COMPOUND (signed int, (LHSVAL), (RHSVAL), OP);		\
32       TEST_COMPOUND (unsigned int, (LHSVAL), (RHSVAL), OP);		\
33       TEST_COMPOUND (signed long, (LHSVAL), (RHSVAL), OP);		\
34       TEST_COMPOUND (unsigned long, (LHSVAL), (RHSVAL), OP);		\
35       TEST_COMPOUND (signed long long, (LHSVAL), (RHSVAL), OP);		\
36       TEST_COMPOUND (unsigned long long, (LHSVAL), (RHSVAL), OP);	\
37       TEST_COMPOUND (float, (LHSVAL), (RHSVAL), OP);			\
38       TEST_COMPOUND (double, (LHSVAL), (RHSVAL), OP);			\
39       TEST_COMPOUND (long double, (LHSVAL), (RHSVAL), OP);		\
40       TEST_COMPOUND (_Complex float, (LHSVAL), (RHSVAL), OP);		\
41       TEST_COMPOUND (_Complex double, (LHSVAL), (RHSVAL), OP);		\
42       TEST_COMPOUND (_Complex long double, (LHSVAL), (RHSVAL), OP);	\
43     }									\
44   while (0)
45 
46 #define TEST_COMPOUND_INT(LHSVAL, RHSVAL, OP)				\
47   do									\
48     {									\
49       TEST_COMPOUND (_Bool, (LHSVAL), (RHSVAL), OP);			\
50       TEST_COMPOUND (char, (LHSVAL), (RHSVAL), OP);			\
51       TEST_COMPOUND (signed char, (LHSVAL), (RHSVAL), OP);		\
52       TEST_COMPOUND (unsigned char, (LHSVAL), (RHSVAL), OP);		\
53       TEST_COMPOUND (signed short, (LHSVAL), (RHSVAL), OP);		\
54       TEST_COMPOUND (unsigned short, (LHSVAL), (RHSVAL), OP);		\
55       TEST_COMPOUND (signed int, (LHSVAL), (RHSVAL), OP);		\
56       TEST_COMPOUND (unsigned int, (LHSVAL), (RHSVAL), OP);		\
57       TEST_COMPOUND (signed long, (LHSVAL), (RHSVAL), OP);		\
58       TEST_COMPOUND (unsigned long, (LHSVAL), (RHSVAL), OP);		\
59       TEST_COMPOUND (signed long long, (LHSVAL), (RHSVAL), OP);		\
60       TEST_COMPOUND (unsigned long long, (LHSVAL), (RHSVAL), OP);	\
61     }									\
62   while (0)
63 
64 static void
test_mult(void)65 test_mult (void)
66 {
67   TEST_COMPOUND_ARITH (1, 2, *);
68   TEST_COMPOUND_ARITH (-3, 5, *);
69   TEST_COMPOUND_ARITH (-7, -20, *);
70   TEST_COMPOUND_ARITH (1.25, 3.5, *);
71   TEST_COMPOUND_ARITH (CMPLX (1.5, 2.5), CMPLX (3.5, 4.5), *);
72   TEST_COMPOUND_ARITH (CMPLX (1.5, 2.5), 2, *);
73 }
74 
75 static void
test_div(void)76 test_div (void)
77 {
78   TEST_COMPOUND_ARITH (1, 2, /);
79   TEST_COMPOUND_ARITH (-6, 3, /);
80   TEST_COMPOUND_ARITH (-70, -10, /);
81   TEST_COMPOUND_ARITH (1.25, 2.5, /);
82   TEST_COMPOUND_ARITH (CMPLX (1.0, 1.0), CMPLX (0.5, 0.5), /);
83   TEST_COMPOUND_ARITH (CMPLX (1.5, 2.5), 2, /);
84 }
85 
86 static void
test_mod(void)87 test_mod (void)
88 {
89   TEST_COMPOUND_INT (1, 2, %);
90   TEST_COMPOUND_INT (-3, 5, %);
91   TEST_COMPOUND_INT (-7, -2, %);
92 }
93 
94 static void
test_plus(void)95 test_plus (void)
96 {
97   TEST_COMPOUND_ARITH (1, 2, +);
98   TEST_COMPOUND_ARITH (-3, 5, +);
99   TEST_COMPOUND_ARITH (-7, -20, +);
100   TEST_COMPOUND_ARITH (1.25, 3.5, +);
101   TEST_COMPOUND_ARITH (CMPLX (1.5, 2.5), CMPLX (3.5, 4.5), +);
102   TEST_COMPOUND_ARITH (CMPLX (1.5, 2.5), 2, +);
103   static int ia[2];
104   TEST_COMPOUND (int *, &ia[1], 1, +);
105   TEST_COMPOUND (int *, &ia[1], -1, +);
106 }
107 
108 static void
test_minus(void)109 test_minus (void)
110 {
111   TEST_COMPOUND_ARITH (1, 2, -);
112   TEST_COMPOUND_ARITH (-3, 5, -);
113   TEST_COMPOUND_ARITH (-7, -20, -);
114   TEST_COMPOUND_ARITH (3.5, 1.25, -);
115   TEST_COMPOUND_ARITH (CMPLX (3.5, 4.5), CMPLX (1.5, 2.5), -);
116   TEST_COMPOUND_ARITH (CMPLX (3.5, 2.5), 2, -);
117   static int ia[2];
118   TEST_COMPOUND (int *, &ia[1], 1, -);
119   TEST_COMPOUND (int *, &ia[1], -1, -);
120 }
121 
122 static void
test_lshift(void)123 test_lshift (void)
124 {
125   TEST_COMPOUND_INT (1, 7, <<);
126   TEST_COMPOUND_INT (15, 3, <<);
127 }
128 
129 static void
test_rshift(void)130 test_rshift (void)
131 {
132   TEST_COMPOUND_INT (1, 1, >>);
133   TEST_COMPOUND_INT (127, 4, >>);
134 }
135 
136 static void
test_and(void)137 test_and (void)
138 {
139   TEST_COMPOUND_INT (0x1234, 0x7856, &);
140   TEST_COMPOUND_INT (-1, 0x12345678, &);
141 }
142 
143 static void
test_xor(void)144 test_xor (void)
145 {
146   TEST_COMPOUND_INT (0x1234, 0x7856, ^);
147   TEST_COMPOUND_INT (-1, 0x12345678, ^);
148 }
149 
150 static void
test_or(void)151 test_or (void)
152 {
153   TEST_COMPOUND_INT (0x1234, 0x7856, |);
154   TEST_COMPOUND_INT (-12345, 0x12345678, |);
155 }
156 
157 int
main(void)158 main (void)
159 {
160   test_mult ();
161   test_div ();
162   test_mod ();
163   test_plus ();
164   test_minus ();
165   test_lshift ();
166   test_rshift ();
167   test_and ();
168   test_xor ();
169   test_or ();
170   exit (0);
171 }
172