Debian Codegraph @

clang/test/AST/Interp/complex.cpp

raw
1 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -verify=both,expected -Wno-unused-value %s
2 // RUN: %clang_cc1 -verify=both,ref -Wno-unused-value %s
3
4 constexpr _Complex double z1 = {1.0, 2.0};
5 static_assert(__real(z1) == 1.0, "");
6 static_assert(__imag(z1) == 2.0, "");
7
8 static_assert(&__imag z1 == &__real z1 + 1, "");
9 static_assert((*(&__imag z1)) == __imag z1, "");
10 static_assert((*(&__real z1)) == __real z1, "");
11
12
13 static_assert(((1.25 + 0.5j) * (0.25 - 0.75j)) == (0.6875 - 0.8125j), "");
14 static_assert(((1.25 + 0.5j) * 0.25) == (0.3125 + 0.125j), "");
15 static_assert((1.25 * (0.25 - 0.75j)) == (0.3125 - 0.9375j), "");
16 constexpr _Complex float InfC = {1.0, __builtin_inf()};
17 constexpr _Complex float InfInf = __builtin_inf() + InfC;
18 static_assert(__real__(InfInf) == __builtin_inf());
19 static_assert(__imag__(InfInf) == __builtin_inf());
20 static_assert(__builtin_isnan(__real__(InfInf * InfInf)));
21 static_assert(__builtin_isinf_sign(__imag__(InfInf * InfInf)) == 1);
22
23 static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * 1.0)) == 1);
24 static_assert(__builtin_isinf_sign(__imag__((1.0 + InfC) * 1.0)) == 1);
25 static_assert(__builtin_isinf_sign(__real__(1.0 * (__builtin_inf() + 1.0j))) == 1);
26 static_assert(__builtin_isinf_sign(__imag__(1.0 * (1.0 + InfC))) == 1);
27 static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * (1.0 + 1.0j))) == 1);
28 static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) * (__builtin_inf() + 1.0j))) == 1);
29 static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * (__builtin_inf() + 1.0j))) == 1);
30 static_assert(__builtin_isinf_sign(__real__((1.0 + InfC) * (1.0 + 1.0j))) == -1);
31 static_assert(__builtin_isinf_sign(__imag__((1.0 + InfC) * (1.0 + 1.0j))) == 1);
32 static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) * (1.0 + InfC))) == -1);
33 static_assert(__builtin_isinf_sign(__imag__((1.0 + 1.0j) * (1.0 + InfC))) == 1);
34 static_assert(__builtin_isinf_sign(__real__((1.0 + InfC) * (1.0 + InfC))) == -1);
35 static_assert(__builtin_isinf_sign(__real__(InfInf * InfInf)) == 0);
36
37 constexpr _Complex int IIMA = {1,2};
38 constexpr _Complex int IIMB = {10,20};
39 constexpr _Complex int IIMC = IIMA * IIMB;
40 static_assert(__real(IIMC) == -30, "");
41 static_assert(__imag(IIMC) == 40, "");
42
43 static_assert(1.0j / 0.0 == 1); // both-error {{static assertion}} \
44 // both-note {{division by zero}}
45 static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) / (0.0 + 0.0j))) == 1);
46 static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) / 0.0)) == 1); // both-error {{static assertion}} \
47 // both-note {{division by zero}}
48 static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / (0.0 + 0.0j))) == 1);
49 static_assert(__builtin_isinf_sign(__imag__((1.0 + InfC) / (0.0 + 0.0j))) == 1);
50 static_assert(__builtin_isinf_sign(__imag__((InfInf) / (0.0 + 0.0j))) == 1);
51
52 constexpr _Complex int IIDA = {10,20};
53 constexpr _Complex int IIDB = {1,2};
54 constexpr _Complex int IIDC = IIDA / IIDB;
55 static_assert(__real(IIDC) == 10, "");
56 static_assert(__imag(IIDC) == 0, "");
57
58 constexpr _Complex int Comma1 = {1, 2};
59 constexpr _Complex int Comma2 = (0, Comma1);
60 static_assert(Comma1 == Comma1, "");
61
62 constexpr double setter() {
63 _Complex float d = {1.0, 2.0};
64
65 __imag(d) = 4.0;
66 return __imag(d);
67 }
68 static_assert(setter() == 4, "");
69
70 constexpr _Complex double getter() {
71 return {1.0, 3.0};
72 }
73 constexpr _Complex double D = getter();
74 static_assert(__real(D) == 1.0, "");
75 static_assert(__imag(D) == 3.0, "");
76
77
78 constexpr _Complex int I1 = {1, 2};
79 static_assert(__real(I1) == 1, "");
80 static_assert(__imag(I1) == 2, "");
81
82
83 constexpr _Complex double D1 = {};
84 static_assert(__real(D1) == 0, "");
85 static_assert(__imag(D1) == 0, "");
86
87 constexpr _Complex int I2 = {};
88 static_assert(__real(I2) == 0, "");
89 static_assert(__imag(I2) == 0, "");
90
91 static_assert(__real(4.0) == 4.0, "");
92 static_assert(__real(12u) == 12u, "");
93 static_assert(__imag(4.0) == 0.0, "");
94 static_assert(__imag(13) == 0, "");
95
96
97 constexpr _Complex long L1 = D;
98 static_assert(__real(L1) == 1.0, "");
99 static_assert(__imag(L1) == 3.0, "");
100
101 constexpr _Complex short I4 = L1;
102 static_assert(__real(I4) == 1, "");
103 static_assert(__imag(I4) == 3, "");
104
105 constexpr _Complex float D3 = D;
106 static_assert(__real(D3) == 1.0, "");
107 static_assert(__imag(D3) == 3.0, "");
108
109
110 constexpr _Complex int a = 2i;
111 static_assert(__real(a) == 0, "");
112 static_assert(__imag(a) == 2, "");
113
114 constexpr _Complex double b = 4.0i;
115 static_assert(__real(b) == 0, "");
116 static_assert(__imag(b) == 4, "");
117
118 constexpr int ignored() {
119 I2;
120 (int)I2;
121 (float)I2;
122 D1;
123 (int)D1;
124 (double)D1;
125 (_Complex float)I2;
126 (bool)D1;
127 (bool)I2;
128 return 0;
129 }
130 static_assert(ignored() == 0, "");
131 static_assert((int)I1 == 1, "");
132 static_assert((float)D == 1.0f, "");
133
134 static_assert(__real((_Complex unsigned)5) == 5);
135 static_assert(__imag((_Complex unsigned)5) == 0);
136
137 /// Standalone complex expressions.
138 static_assert(__real((_Complex float){1.0, 3.0}) == 1.0, "");
139
140
141 constexpr _Complex double D2 = {12};
142 static_assert(__real(D2) == 12, "");
143 static_assert(__imag(D2) == 0, "");
144
145 constexpr _Complex int I3 = {15};
146 static_assert(__real(I3) == 15, "");
147 static_assert(__imag(I3) == 0, "");
148
149 constexpr _Complex _BitInt(8) A = {4};
150 static_assert(__real(A) == 4, "");
151 static_assert(__imag(A) == 0, "");
152
153
154 constexpr _Complex double Doubles[4] = {{1.0, 2.0}};
155 static_assert(__real(Doubles[0]) == 1.0, "");
156 static_assert(__imag(Doubles[0]) == 2.0, "");
157 static_assert(__real(Doubles[1]) == 0.0, "");
158 static_assert(__imag(Doubles[1]) == 0.0, "");
159 static_assert(__real(Doubles[2]) == 0.0, "");
160 static_assert(__imag(Doubles[2]) == 0.0, "");
161 static_assert(__real(Doubles[3]) == 0.0, "");
162 static_assert(__imag(Doubles[3]) == 0.0, "");
163
164 static_assert(~(0.5 + 1.5j) == (0.5 + -1.5j), "");
165
166 static_assert(__extension__ __imag(A) == 0, "");
167 static_assert(__imag(__extension__ A) == 0, "");
168
169 void func(void) {
170 __complex__ int arr;
171 _Complex int result;
172 int ii = 0;
173 int bb = 0;
174 /// The following line will call into the constant interpreter.
175 result = arr * ii;
176 }
177
178 constexpr _Complex float getComplexFloat() {
179 return {1,2};
180 }
181 static_assert(__real(getComplexFloat()) == 1, "");
182 static_assert(__imag(getComplexFloat()) == 2, "");
183
184 namespace CastToBool {
185 constexpr _Complex int F = {0, 1};
186 static_assert(F, "");
187 constexpr _Complex int F2 = {1, 0};
188 static_assert(F2, "");
189 constexpr _Complex int F3 = {0, 0};
190 static_assert(!F3, "");
191
192 constexpr _Complex unsigned char F4 = {0, 1};
193 static_assert(F4, "");
194 constexpr _Complex unsigned char F5 = {1, 0};
195 static_assert(F5, "");
196 constexpr _Complex unsigned char F6 = {0, 0};
197 static_assert(!F6, "");
198
199 constexpr _Complex float F7 = {0, 1};
200 static_assert(F7, "");
201 constexpr _Complex float F8 = {1, 0};
202 static_assert(F8, "");
203 constexpr _Complex double F9 = {0, 0};
204 static_assert(!F9, "");
205 }
206
207 namespace BinOps {
208 namespace Add {
209 constexpr _Complex float A = { 13.0, 2.0 };
210 constexpr _Complex float B = { 2.0, 1.0 };
211 constexpr _Complex float C = A + B;
212 static_assert(__real(C) == 15.0, "");
213 static_assert(__imag(C) == 3.0, "");
214
215 constexpr _Complex float D = B + A;
216 static_assert(__real(D) == 15.0, "");
217 static_assert(__imag(D) == 3.0, "");
218
219 constexpr _Complex unsigned int I1 = { 5, 10 };
220 constexpr _Complex unsigned int I2 = { 40, 2 };
221 constexpr _Complex unsigned int I3 = I1 + I2;
222 static_assert(__real(I3) == 45, "");
223 static_assert(__imag(I3) == 12, "");
224
225 static_assert(__real(A + 2.0) == 15, "");
226 static_assert(__imag(A + 2.0) == 2, "");
227 static_assert(__real(2.0 + A) == 15, "");
228 static_assert(__imag(2.0 + A) == 2, "");
229
230 static_assert(__real(D + 1) == 16, "");
231 static_assert(__real(D + 1.0) == 16, "");
232 constexpr _Complex double D2 = D + 3.0;
233 static_assert(__real(D2) == 18.0, "");
234 static_assert(__imag(D2) == 3.0, "");
235 constexpr _Complex double D3 = 3.0 + D;
236 static_assert(__real(D3) == 18.0, "");
237 static_assert(__imag(D3) == 3.0, "");
238 }
239
240 namespace Sub {
241 constexpr _Complex float A = { 13.0, 2.0 };
242 constexpr _Complex float B = { 2.0, 1.0 };
243 constexpr _Complex float C = A - B;
244 static_assert(__real(C) == 11.0, "");
245 static_assert(__imag(C) == 1.0, "");
246 static_assert(__real(A - 2.0) == 11, "");
247 static_assert(__real(2.0 - A) == -11, "");
248
249 constexpr _Complex float D = B - A;
250 static_assert(__real(D) == -11.0, "");
251 static_assert(__imag(D) == -1.0, "");
252
253 constexpr _Complex unsigned int I1 = { 5, 10 };
254 constexpr _Complex unsigned int I2 = { 40, 2 };
255 constexpr _Complex unsigned int I3 = I1 - I2;
256 static_assert(__real(I3) == -35, "");
257 static_assert(__imag(I3) == 8, "");
258
259 using Bobble = _Complex float;
260 constexpr _Complex float A_ = { 13.0, 2.0 };
261 constexpr Bobble B_ = { 2.0, 1.0 };
262 constexpr _Complex float D_ = A_ - B_;
263 static_assert(__real(D_) == 11.0, "");
264 static_assert(__imag(D_) == 1.0, "");
265
266 static_assert(__real(D - 1) == -12, "");
267 static_assert(__real(D - 1.0) == -12, "");
268 constexpr _Complex double D2 = D - 3.0;
269 static_assert(__real(D2) == -14.0, "");
270 static_assert(__imag(D2) == -1.0, "");
271 constexpr _Complex double D3 = 3.0 - D;
272 static_assert(__real(D3) == 14.0, "");
273 static_assert(__imag(D3) == 1.0, "");
274 }
275
276 }
277
278 namespace ZeroInit {
279 typedef _Complex float fcomplex;
280 typedef _Complex unsigned icomplex;
281
282 constexpr fcomplex test7 = fcomplex();
283 static_assert(__real(test7) == 0.0f, "");
284 static_assert(__imag(test7) == 0.0f, "");
285
286 constexpr icomplex test8 = icomplex();
287 static_assert(__real(test8) == 0, "");
288 static_assert(__imag(test8) == 0, "");
289
290 constexpr int ignored = (fcomplex(), 0);
291 }
292
293 namespace DeclRefCopy {
294 constexpr _Complex int ComplexInt = 42 + 24i;
295
296 constexpr _Complex int B = ComplexInt;
297 constexpr _Complex int ArrayOfComplexInt[4] = {ComplexInt, ComplexInt, ComplexInt, ComplexInt};
298 static_assert(__real(ArrayOfComplexInt[0]) == 42, "");
299 static_assert(__imag(ArrayOfComplexInt[0]) == 24, "");
300 static_assert(__real(ArrayOfComplexInt[3]) == 42, "");
301 static_assert(__imag(ArrayOfComplexInt[3]) == 24, "");
302
303 constexpr int localComplexArray() {
304 _Complex int A = 42 + 24i;
305 _Complex int ArrayOfComplexInt[4] = {A, A, A, A};
306 return __real(ArrayOfComplexInt[0]) + __imag(ArrayOfComplexInt[3]);
307 }
308 static_assert(localComplexArray() == (24 + 42), "");
309 }
310
311 namespace Builtin {
312 constexpr _Complex float A = __builtin_complex(10.0f, 20.0f);
313 static_assert(__real(A) == 10, "");
314 static_assert(__imag(A) == 20, "");
315
316 constexpr _Complex double B = __builtin_complex(10.0, 20.0);
317 static_assert(__real(B) == 10, "");
318 static_assert(__imag(B) == 20, "");
319
320
321 constexpr _Complex float C = __builtin_complex(10.0f, 20.0); // both-error {{arguments are of different types}}
322 }
323
324 namespace Cmp {
325 static_assert((0.0 + 0.0j) == (0.0 + 0.0j));
326 static_assert((0.0 + 0.0j) != (0.0 + 0.0j)); // both-error {{static assertion}} \
327 // both-note {{evaluates to}}
328
329 static_assert((0.0 + 0.0j) == 0.0);
330 static_assert(0.0 == (0.0 + 0.0j));
331 static_assert(0.0 == 0.0j);
332 static_assert((0.0 + 1.0j) != 0.0);
333 static_assert(1.0 != (0.0 + 0.0j));
334 static_assert(0.0 != 1.0j);
335
336 // Walk around the complex plane stepping between angular differences and
337 // equality.
338 static_assert((1.0 + 0.0j) == (0.0 + 0.0j)); // both-error {{static assertion}} \
339 // both-note {{evaluates to}}
340 static_assert((1.0 + 0.0j) == (1.0 + 0.0j));
341 static_assert((1.0 + 1.0j) == (1.0 + 0.0j)); // both-error {{static assertion}} \
342 // both-note {{evaluates to}}
343 static_assert((1.0 + 1.0j) == (1.0 + 1.0j));
344 static_assert((0.0 + 1.0j) == (1.0 + 1.0j)); // both-error {{static assertion}} \
345 // both-note {{evaluates to}}
346 static_assert((0.0 + 1.0j) == (0.0 + 1.0j));
347 static_assert((-1.0 + 1.0j) == (0.0 + 1.0j)); // both-error {{static assertion}} \
348 // both-note {{evaluates to}}
349 static_assert((-1.0 + 1.0j) == (-1.0 + 1.0j));
350 static_assert((-1.0 + 0.0j) == (-1.0 + 1.0j)); // both-error {{static assertion}} \
351 // both-note {{evaluates to}}
352 static_assert((-1.0 + 0.0j) == (-1.0 + 0.0j));
353 static_assert((-1.0 - 1.0j) == (-1.0 + 0.0j)); // both-error {{static assertion}} \
354 // both-note {{evaluates to}}
355 static_assert((-1.0 - 1.0j) == (-1.0 - 1.0j));
356 static_assert((0.0 - 1.0j) == (-1.0 - 1.0j)); // both-error {{static assertion}} \
357 // both-note {{evaluates to}}
358 static_assert((0.0 - 1.0j) == (0.0 - 1.0j));
359 static_assert((1.0 - 1.0j) == (0.0 - 1.0j)); // both-error {{static assertion}} \
360 // both-note {{evaluates to}}
361 static_assert((1.0 - 1.0j) == (1.0 - 1.0j));
362
363 /// Make sure these are rejected before reaching the constexpr interpreter.
364 static_assert((0.0 + 0.0j) & (0.0 + 0.0j)); // both-error {{invalid operands to binary expression}}
365 static_assert((0.0 + 0.0j) | (0.0 + 0.0j)); // both-error {{invalid operands to binary expression}}
366 static_assert((0.0 + 0.0j) < (0.0 + 0.0j)); // both-error {{invalid operands to binary expression}}
367 static_assert((0.0 + 0.0j) > (0.0 + 0.0j)); // both-error {{invalid operands to binary expression}}
368 static_assert((0.0 + 0.0j) ^ (0.0 + 0.0j)); // both-error {{invalid operands to binary expression}}
369 }
370
371 /// From test/SemaCXX/constant-expression-cxx11.cpp
372 ///
373 /// Some of the diagnostics we emit are different than the one of the
374 /// current interpreter.
375 ///
376 /// FIXME: For the '&test3 + 1' test, we are _not_ creating an explicit pointer variable
377 /// anywhere and so the &test3+1 is the same as __imag(test3) for us.
378 namespace ComplexConstexpr {
379 constexpr _Complex float test1 = {};
380 constexpr _Complex float test2 = {1};
381 constexpr _Complex double test3 = {1,2};
382 constexpr _Complex int test4 = {4};
383 constexpr _Complex int test5 = 4;
384 constexpr _Complex int test6 = {5,6};
385 typedef _Complex float fcomplex;
386 constexpr fcomplex test7 = fcomplex();
387
388 constexpr const double &t2r = __real test3;
389 constexpr const double &t2i = __imag test3;
390 static_assert(&t2r + 1 == &t2i, "");
391 static_assert(t2r == 1.0, "");
392 static_assert(t2i == 2.0, "");
393 constexpr const double *t2p = &t2r;
394 static_assert(t2p[-1] == 0.0, ""); // both-error {{constant expr}} \
395 // both-note {{cannot refer to element -1 of array of 2 elements}}
396 static_assert(t2p[0] == 1.0, "");
397 static_assert(t2p[1] == 2.0, "");
398 static_assert(t2p[2] == 0.0, ""); // both-error {{constant expr}} \
399 // both-note {{one-past-the-end pointer}}
400 static_assert(t2p[3] == 0.0, ""); // both-error {{constant expr}} \
401 // both-note {{cannot refer to element 3 of array of 2 elements}}
402 constexpr _Complex float *p = 0;
403 constexpr float pr = __real *p; // both-error {{constant expr}} \
404 // ref-note {{cannot access real component of null}} \
405 // expected-note {{read of dereferenced null pointer}}
406 constexpr float pi = __imag *p; // both-error {{constant expr}} \
407 // ref-note {{cannot access imaginary component of null}} \
408 // expected-note {{cannot perform pointer arithmetic on null pointer}}
409 constexpr const _Complex double *q = &test3 + 1;
410 constexpr double qr = __real *q; // ref-error {{constant expr}} \
411 // ref-note {{cannot access real component of pointer past the end}}
412 constexpr double qi = __imag *q; // both-error {{constant expr}} \
413 // ref-note {{cannot access imaginary component of pointer past the end}} \
414 // expected-note {{read of dereferenced one-past-the-end pointer}}
415
416 static_assert(__real test6 == 5, "");
417 static_assert(__imag test6 == 6, "");
418 static_assert(&__imag test6 == &__real test6 + 1, "");
419 }