xref: /minix3/external/bsd/llvm/dist/clang/test/SemaCXX/complex-folding.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc // RUN: %clang_cc1 %s -std=c++1z -fsyntax-only -verify
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc // Test the constant folding of builtin complex numbers.
4*0a6a1f1dSLionel Sambuc 
5*0a6a1f1dSLionel Sambuc static_assert((0.0 + 0.0j) == (0.0 + 0.0j));
6*0a6a1f1dSLionel Sambuc static_assert((0.0 + 0.0j) != (0.0 + 0.0j)); // expected-error {{static_assert}}
7*0a6a1f1dSLionel Sambuc 
8*0a6a1f1dSLionel Sambuc static_assert((0.0 + 0.0j) == 0.0);
9*0a6a1f1dSLionel Sambuc static_assert(0.0 == (0.0 + 0.0j));
10*0a6a1f1dSLionel Sambuc static_assert(0.0 == 0.0j);
11*0a6a1f1dSLionel Sambuc static_assert((0.0 + 1.0j) != 0.0);
12*0a6a1f1dSLionel Sambuc static_assert(1.0 != (0.0 + 0.0j));
13*0a6a1f1dSLionel Sambuc static_assert(0.0 != 1.0j);
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc // Walk around the complex plane stepping between angular differences and
16*0a6a1f1dSLionel Sambuc // equality.
17*0a6a1f1dSLionel Sambuc static_assert((1.0 + 0.0j) == (0.0 + 0.0j)); // expected-error {{static_assert}}
18*0a6a1f1dSLionel Sambuc static_assert((1.0 + 0.0j) == (1.0 + 0.0j));
19*0a6a1f1dSLionel Sambuc static_assert((1.0 + 1.0j) == (1.0 + 0.0j)); // expected-error {{static_assert}}
20*0a6a1f1dSLionel Sambuc static_assert((1.0 + 1.0j) == (1.0 + 1.0j));
21*0a6a1f1dSLionel Sambuc static_assert((0.0 + 1.0j) == (1.0 + 1.0j)); // expected-error {{static_assert}}
22*0a6a1f1dSLionel Sambuc static_assert((0.0 + 1.0j) == (0.0 + 1.0j));
23*0a6a1f1dSLionel Sambuc static_assert((-1.0 + 1.0j) == (0.0 + 1.0j)); // expected-error {{static_assert}}
24*0a6a1f1dSLionel Sambuc static_assert((-1.0 + 1.0j) == (-1.0 + 1.0j));
25*0a6a1f1dSLionel Sambuc static_assert((-1.0 + 0.0j) == (-1.0 + 1.0j)); // expected-error {{static_assert}}
26*0a6a1f1dSLionel Sambuc static_assert((-1.0 + 0.0j) == (-1.0 + 0.0j));
27*0a6a1f1dSLionel Sambuc static_assert((-1.0 - 1.0j) == (-1.0 + 0.0j)); // expected-error {{static_assert}}
28*0a6a1f1dSLionel Sambuc static_assert((-1.0 - 1.0j) == (-1.0 - 1.0j));
29*0a6a1f1dSLionel Sambuc static_assert((0.0 - 1.0j) == (-1.0 - 1.0j)); // expected-error {{static_assert}}
30*0a6a1f1dSLionel Sambuc static_assert((0.0 - 1.0j) == (0.0 - 1.0j));
31*0a6a1f1dSLionel Sambuc static_assert((1.0 - 1.0j) == (0.0 - 1.0j)); // expected-error {{static_assert}}
32*0a6a1f1dSLionel Sambuc static_assert((1.0 - 1.0j) == (1.0 - 1.0j));
33*0a6a1f1dSLionel Sambuc 
34*0a6a1f1dSLionel Sambuc // Test basic mathematical folding of both complex and real operands.
35*0a6a1f1dSLionel Sambuc static_assert(((1.0 + 0.5j) + (0.25 - 0.75j)) == (1.25 - 0.25j));
36*0a6a1f1dSLionel Sambuc static_assert(((1.0 + 0.5j) + 0.25) == (1.25 + 0.5j));
37*0a6a1f1dSLionel Sambuc static_assert((1.0 + (0.25 - 0.75j)) == (1.25 - 0.75j));
38*0a6a1f1dSLionel Sambuc 
39*0a6a1f1dSLionel Sambuc static_assert(((1.0 + 0.5j) - (0.25 - 0.75j)) == (0.75 + 1.25j));
40*0a6a1f1dSLionel Sambuc static_assert(((1.0 + 0.5j) - 0.25) == (0.75 + 0.5j));
41*0a6a1f1dSLionel Sambuc static_assert((1.0 - (0.25 - 0.75j)) == (0.75 + 0.75j));
42*0a6a1f1dSLionel Sambuc 
43*0a6a1f1dSLionel Sambuc static_assert(((1.25 + 0.5j) * (0.25 - 0.75j)) == (0.6875 - 0.8125j));
44*0a6a1f1dSLionel Sambuc static_assert(((1.25 + 0.5j) * 0.25) == (0.3125 + 0.125j));
45*0a6a1f1dSLionel Sambuc static_assert((1.25 * (0.25 - 0.75j)) == (0.3125 - 0.9375j));
46*0a6a1f1dSLionel Sambuc 
47*0a6a1f1dSLionel Sambuc static_assert(((1.25 + 0.5j) / (0.25 - 0.75j)) == (-0.1 + 1.7j));
48*0a6a1f1dSLionel Sambuc static_assert(((1.25 + 0.5j) / 0.25) == (5.0 + 2.0j));
49*0a6a1f1dSLionel Sambuc static_assert((1.25 / (0.25 - 0.75j)) == (0.5 + 1.5j));
50*0a6a1f1dSLionel Sambuc 
51*0a6a1f1dSLionel Sambuc // Test that infinities are preserved, don't turn into NaNs, and do form zeros
52*0a6a1f1dSLionel Sambuc // when the divisor.
53*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * 1.0)) == 1);
54*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__((1.0 + __builtin_inf() * 1.0j) * 1.0)) == 1);
55*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__(1.0 * (__builtin_inf() + 1.0j))) == 1);
56*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__(1.0 * (1.0 + __builtin_inf() * 1.0j))) == 1);
57*0a6a1f1dSLionel Sambuc 
58*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * (1.0 + 1.0j))) == 1);
59*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) * (__builtin_inf() + 1.0j))) == 1);
60*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * (__builtin_inf() + 1.0j))) == 1);
61*0a6a1f1dSLionel Sambuc 
62*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((1.0 + __builtin_inf() * 1.0j) * (1.0 + 1.0j))) == -1);
63*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__((1.0 + __builtin_inf() * 1.0j) * (1.0 + 1.0j))) == 1);
64*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) * (1.0 + __builtin_inf() * 1.0j))) == -1);
65*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__((1.0 + 1.0j) * (1.0 + __builtin_inf() * 1.0j))) == 1);
66*0a6a1f1dSLionel Sambuc 
67*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((1.0 + __builtin_inf() * 1.0j) * (1.0 + __builtin_inf() * 1.0j))) == -1);
68*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + __builtin_inf() * 1.0j) * (__builtin_inf() + __builtin_inf() * 1.0j))) == -1);
69*0a6a1f1dSLionel Sambuc 
70*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / (1.0 + 1.0j))) == 1);
71*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__(1.0 + (__builtin_inf() * 1.0j) / (1.0 + 1.0j))) == 1);
72*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__((__builtin_inf() + __builtin_inf() * 1.0j) / (1.0 + 1.0j))) == 1);
73*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / 1.0)) == 1);
74*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__(1.0 + (__builtin_inf() * 1.0j) / 1.0)) == 1);
75*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__((__builtin_inf() + __builtin_inf() * 1.0j) / 1.0)) == 1);
76*0a6a1f1dSLionel Sambuc 
77*0a6a1f1dSLionel Sambuc static_assert(((1.0 + 1.0j) / (__builtin_inf() + 1.0j)) == (0.0 + 0.0j));
78*0a6a1f1dSLionel Sambuc static_assert(((1.0 + 1.0j) / (1.0 + __builtin_inf() * 1.0j)) == (0.0 + 0.0j));
79*0a6a1f1dSLionel Sambuc static_assert(((1.0 + 1.0j) / (__builtin_inf() + __builtin_inf() * 1.0j)) == (0.0 + 0.0j));
80*0a6a1f1dSLionel Sambuc static_assert(((1.0 + 1.0j) / __builtin_inf()) == (0.0 + 0.0j));
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) / (0.0 + 0.0j))) == 1);
83*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) / 0.0)) == 1);
84*0a6a1f1dSLionel Sambuc 
85*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / (0.0 + 0.0j))) == 1);
86*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__((1.0 + __builtin_inf() * 1.0j) / (0.0 + 0.0j))) == 1);
87*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__((__builtin_inf() + __builtin_inf() * 1.0j) / (0.0 + 0.0j))) == 1);
88*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / 0.0)) == 1);
89*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__((1.0 + __builtin_inf() * 1.0j) / 0.0)) == 1);
90*0a6a1f1dSLionel Sambuc static_assert(__builtin_isinf_sign(__imag__((__builtin_inf() + __builtin_inf() * 1.0j) / 0.0)) == 1);
91