| 1 |
// RUN: %clang_cc1 %s -std=c++1z -fsyntax-only -verify |
| 2 |
// RUN: %clang_cc1 %s -std=c++1z -fsyntax-only -verify -fexperimental-new-constant-interpreter |
| 3 |
// |
| 4 |
// Test the constant folding of builtin complex numbers. |
| 5 |
|
| 6 |
static_assert((0.0 + 0.0j) == (0.0 + 0.0j)); |
| 7 |
static_assert((0.0 + 0.0j) != (0.0 + 0.0j)); // expected-error {{static assertion}} \ |
| 8 |
// expected-note {{evaluates to}} |
| 9 |
|
| 10 |
static_assert((0.0 + 0.0j) == 0.0); |
| 11 |
static_assert(0.0 == (0.0 + 0.0j)); |
| 12 |
static_assert(0.0 == 0.0j); |
| 13 |
static_assert((0.0 + 1.0j) != 0.0); |
| 14 |
static_assert(1.0 != (0.0 + 0.0j)); |
| 15 |
static_assert(0.0 != 1.0j); |
| 16 |
|
| 17 |
// Walk around the complex plane stepping between angular differences and |
| 18 |
// equality. |
| 19 |
static_assert((1.0 + 0.0j) == (0.0 + 0.0j)); // expected-error {{static assertion}} \ |
| 20 |
// expected-note {{evaluates to}} |
| 21 |
static_assert((1.0 + 0.0j) == (1.0 + 0.0j)); |
| 22 |
static_assert((1.0 + 1.0j) == (1.0 + 0.0j)); // expected-error {{static assertion}} \ |
| 23 |
// expected-note {{evaluates to}} |
| 24 |
static_assert((1.0 + 1.0j) == (1.0 + 1.0j)); |
| 25 |
static_assert((0.0 + 1.0j) == (1.0 + 1.0j)); // expected-error {{static assertion}} \ |
| 26 |
// expected-note {{evaluates to}} |
| 27 |
static_assert((0.0 + 1.0j) == (0.0 + 1.0j)); |
| 28 |
static_assert((-1.0 + 1.0j) == (0.0 + 1.0j)); // expected-error {{static assertion}} \ |
| 29 |
// expected-note {{evaluates to}} |
| 30 |
static_assert((-1.0 + 1.0j) == (-1.0 + 1.0j)); |
| 31 |
static_assert((-1.0 + 0.0j) == (-1.0 + 1.0j)); // expected-error {{static assertion}} \ |
| 32 |
// expected-note {{evaluates to}} |
| 33 |
static_assert((-1.0 + 0.0j) == (-1.0 + 0.0j)); |
| 34 |
static_assert((-1.0 - 1.0j) == (-1.0 + 0.0j)); // expected-error {{static assertion}} \ |
| 35 |
// expected-note {{evaluates to}} |
| 36 |
static_assert((-1.0 - 1.0j) == (-1.0 - 1.0j)); |
| 37 |
static_assert((0.0 - 1.0j) == (-1.0 - 1.0j)); // expected-error {{static assertion}} \ |
| 38 |
// expected-note {{evaluates to}} |
| 39 |
static_assert((0.0 - 1.0j) == (0.0 - 1.0j)); |
| 40 |
static_assert((1.0 - 1.0j) == (0.0 - 1.0j)); // expected-error {{static assertion}} \ |
| 41 |
// expected-note {{evaluates to}} |
| 42 |
static_assert((1.0 - 1.0j) == (1.0 - 1.0j)); |
| 43 |
|
| 44 |
// Test basic mathematical folding of both complex and real operands. |
| 45 |
static_assert(((1.0 + 0.5j) + (0.25 - 0.75j)) == (1.25 - 0.25j)); |
| 46 |
static_assert(((1.0 + 0.5j) + 0.25) == (1.25 + 0.5j)); |
| 47 |
static_assert((1.0 + (0.25 - 0.75j)) == (1.25 - 0.75j)); |
| 48 |
|
| 49 |
static_assert(((1.0 + 0.5j) - (0.25 - 0.75j)) == (0.75 + 1.25j)); |
| 50 |
static_assert(((1.0 + 0.5j) - 0.25) == (0.75 + 0.5j)); |
| 51 |
static_assert((1.0 - (0.25 - 0.75j)) == (0.75 + 0.75j)); |
| 52 |
|
| 53 |
static_assert(((1.25 + 0.5j) * (0.25 - 0.75j)) == (0.6875 - 0.8125j)); |
| 54 |
static_assert(((1.25 + 0.5j) * 0.25) == (0.3125 + 0.125j)); |
| 55 |
static_assert((1.25 * (0.25 - 0.75j)) == (0.3125 - 0.9375j)); |
| 56 |
|
| 57 |
static_assert(((1.25 + 0.5j) / (0.25 - 0.75j)) == (-0.1 + 1.7j)); |
| 58 |
static_assert(((1.25 + 0.5j) / 0.25) == (5.0 + 2.0j)); |
| 59 |
static_assert((1.25 / (0.25 - 0.75j)) == (0.5 + 1.5j)); |
| 60 |
|
| 61 |
// Test that infinities are preserved, don't turn into NaNs, and do form zeros |
| 62 |
// when the divisor. |
| 63 |
constexpr _Complex float InfC = {1.0, __builtin_inf()}; |
| 64 |
constexpr _Complex float InfInf = __builtin_inf() + InfC; |
| 65 |
static_assert(__real__(InfInf) == __builtin_inf()); |
| 66 |
static_assert(__imag__(InfInf) == __builtin_inf()); |
| 67 |
static_assert(__builtin_isnan(__real__(InfInf * InfInf))); |
| 68 |
static_assert(__builtin_isinf_sign(__imag__(InfInf * InfInf)) == 1); |
| 69 |
|
| 70 |
static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * 1.0)) == 1); |
| 71 |
static_assert(__builtin_isinf_sign(__imag__((1.0 + InfC) * 1.0)) == 1); |
| 72 |
static_assert(__builtin_isinf_sign(__real__(1.0 * (__builtin_inf() + 1.0j))) == 1); |
| 73 |
static_assert(__builtin_isinf_sign(__imag__(1.0 * (1.0 + InfC))) == 1); |
| 74 |
static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * (1.0 + 1.0j))) == 1); |
| 75 |
static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) * (__builtin_inf() + 1.0j))) == 1); |
| 76 |
static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) * (__builtin_inf() + 1.0j))) == 1); |
| 77 |
static_assert(__builtin_isinf_sign(__real__((1.0 + InfC) * (1.0 + 1.0j))) == -1); |
| 78 |
static_assert(__builtin_isinf_sign(__imag__((1.0 + InfC) * (1.0 + 1.0j))) == 1); |
| 79 |
static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) * (1.0 + InfC))) == -1); |
| 80 |
static_assert(__builtin_isinf_sign(__imag__((1.0 + 1.0j) * (1.0 + InfC))) == 1); |
| 81 |
static_assert(__builtin_isinf_sign(__real__((1.0 + InfC) * (1.0 + InfC))) == -1); |
| 82 |
static_assert(__builtin_isinf_sign(__real__(InfInf * InfInf)) == 0); |
| 83 |
static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / (1.0 + 1.0j))) == 1); |
| 84 |
static_assert(__builtin_isinf_sign(__imag__(1.0 + (InfC) / (1.0 + 1.0j))) == 1); |
| 85 |
static_assert(__builtin_isinf_sign(__imag__((InfInf) / (1.0 + 1.0j))) == 0); |
| 86 |
static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / 1.0)) == 1); |
| 87 |
static_assert(__builtin_isinf_sign(__imag__(1.0 + (InfC) / 1.0)) == 1); |
| 88 |
static_assert(__builtin_isinf_sign(__imag__((InfInf) / 1.0)) == 1); |
| 89 |
static_assert(((1.0 + 1.0j) / (__builtin_inf() + 1.0j)) == (0.0 + 0.0j)); |
| 90 |
static_assert(((1.0 + 1.0j) / (1.0 + InfC)) == (0.0 + 0.0j)); |
| 91 |
static_assert(((1.0 + 1.0j) / (InfInf)) == (0.0 + 0.0j)); |
| 92 |
static_assert(((1.0 + 1.0j) / __builtin_inf()) == (0.0 + 0.0j)); |
| 93 |
static_assert(1.0j / 0.0 == 1); // expected-error {{static assertion}} \ |
| 94 |
// expected-note {{division by zero}} |
| 95 |
static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) / (0.0 + 0.0j))) == 1); |
| 96 |
static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) / 0.0)) == 1); // expected-error {{static assertion}} \ |
| 97 |
// expected-note {{division by zero}} |
| 98 |
static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / (0.0 + 0.0j))) == 1); |
| 99 |
static_assert(__builtin_isinf_sign(__imag__((1.0 + InfC) / (0.0 + 0.0j))) == 1); |
| 100 |
static_assert(__builtin_isinf_sign(__imag__((InfInf) / (0.0 + 0.0j))) == 1); |
| 101 |
static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / 0.0)) == 1); // expected-error {{static assertion}} \ |
| 102 |
// expected-note {{division by zero}} |
| 103 |
static_assert(__builtin_isinf_sign(__imag__((1.0 + InfC) / 0.0)) == 1); // expected-error {{static assertion}} \ |
| 104 |
// expected-note {{division by zero}} |
| 105 |
static_assert(__builtin_isinf_sign(__imag__((InfInf) / 0.0)) == 1); // expected-error {{static assertion}} \ |
| 106 |
// expected-note {{division by zero}} |
| 107 |
|