Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 %s -std=c++1z -fsyntax-only -verify |
Timm Baeder | 4d7d45e | 2024-06-18 13:49:02 +0200 | [diff] [blame] | 2 | // RUN: %clang_cc1 %s -std=c++1z -fsyntax-only -verify -fexperimental-new-constant-interpreter |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 3 | // |
| 4 | // Test the constant folding of builtin complex numbers. |
| 5 | |
| 6 | static_assert((0.0 + 0.0j) == (0.0 + 0.0j)); |
Timm Baeder | 0e86d3e | 2023-11-27 11:10:02 +0100 | [diff] [blame] | 7 | static_assert((0.0 + 0.0j) != (0.0 + 0.0j)); // expected-error {{static assertion}} \ |
| 8 | // expected-note {{evaluates to}} |
Chandler Carruth | b29a743 | 2014-10-11 11:03:30 +0000 | [diff] [blame] | 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); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 16 | |
| 17 | // Walk around the complex plane stepping between angular differences and |
| 18 | // equality. |
Timm Baeder | 0e86d3e | 2023-11-27 11:10:02 +0100 | [diff] [blame] | 19 | static_assert((1.0 + 0.0j) == (0.0 + 0.0j)); // expected-error {{static assertion}} \ |
| 20 | // expected-note {{evaluates to}} |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 21 | static_assert((1.0 + 0.0j) == (1.0 + 0.0j)); |
Timm Baeder | 0e86d3e | 2023-11-27 11:10:02 +0100 | [diff] [blame] | 22 | static_assert((1.0 + 1.0j) == (1.0 + 0.0j)); // expected-error {{static assertion}} \ |
| 23 | // expected-note {{evaluates to}} |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 24 | static_assert((1.0 + 1.0j) == (1.0 + 1.0j)); |
Timm Baeder | 0e86d3e | 2023-11-27 11:10:02 +0100 | [diff] [blame] | 25 | static_assert((0.0 + 1.0j) == (1.0 + 1.0j)); // expected-error {{static assertion}} \ |
| 26 | // expected-note {{evaluates to}} |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 27 | static_assert((0.0 + 1.0j) == (0.0 + 1.0j)); |
Timm Baeder | 0e86d3e | 2023-11-27 11:10:02 +0100 | [diff] [blame] | 28 | static_assert((-1.0 + 1.0j) == (0.0 + 1.0j)); // expected-error {{static assertion}} \ |
| 29 | // expected-note {{evaluates to}} |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 30 | static_assert((-1.0 + 1.0j) == (-1.0 + 1.0j)); |
Timm Baeder | 0e86d3e | 2023-11-27 11:10:02 +0100 | [diff] [blame] | 31 | static_assert((-1.0 + 0.0j) == (-1.0 + 1.0j)); // expected-error {{static assertion}} \ |
| 32 | // expected-note {{evaluates to}} |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 33 | static_assert((-1.0 + 0.0j) == (-1.0 + 0.0j)); |
Timm Baeder | 0e86d3e | 2023-11-27 11:10:02 +0100 | [diff] [blame] | 34 | static_assert((-1.0 - 1.0j) == (-1.0 + 0.0j)); // expected-error {{static assertion}} \ |
| 35 | // expected-note {{evaluates to}} |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 36 | static_assert((-1.0 - 1.0j) == (-1.0 - 1.0j)); |
Timm Baeder | 0e86d3e | 2023-11-27 11:10:02 +0100 | [diff] [blame] | 37 | static_assert((0.0 - 1.0j) == (-1.0 - 1.0j)); // expected-error {{static assertion}} \ |
| 38 | // expected-note {{evaluates to}} |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 39 | static_assert((0.0 - 1.0j) == (0.0 - 1.0j)); |
Timm Baeder | 0e86d3e | 2023-11-27 11:10:02 +0100 | [diff] [blame] | 40 | static_assert((1.0 - 1.0j) == (0.0 - 1.0j)); // expected-error {{static assertion}} \ |
| 41 | // expected-note {{evaluates to}} |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 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. |
Timm Baeder | 9ddc014 | 2024-06-08 11:20:31 +0200 | [diff] [blame] | 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); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 69 | |
Timm Baeder | 9ddc014 | 2024-06-08 11:20:31 +0200 | [diff] [blame] | 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); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 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); |
Timm Baeder | 9ddc014 | 2024-06-08 11:20:31 +0200 | [diff] [blame] | 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); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 83 | static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / (1.0 + 1.0j))) == 1); |
Timm Baeder | 9ddc014 | 2024-06-08 11:20:31 +0200 | [diff] [blame] | 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); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 86 | static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / 1.0)) == 1); |
Timm Baeder | 9ddc014 | 2024-06-08 11:20:31 +0200 | [diff] [blame] | 87 | static_assert(__builtin_isinf_sign(__imag__(1.0 + (InfC) / 1.0)) == 1); |
| 88 | static_assert(__builtin_isinf_sign(__imag__((InfInf) / 1.0)) == 1); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 89 | static_assert(((1.0 + 1.0j) / (__builtin_inf() + 1.0j)) == (0.0 + 0.0j)); |
Timm Baeder | 9ddc014 | 2024-06-08 11:20:31 +0200 | [diff] [blame] | 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)); |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 92 | static_assert(((1.0 + 1.0j) / __builtin_inf()) == (0.0 + 0.0j)); |
Timm Baeder | 9ddc014 | 2024-06-08 11:20:31 +0200 | [diff] [blame] | 93 | static_assert(1.0j / 0.0 == 1); // expected-error {{static assertion}} \ |
| 94 | // expected-note {{division by zero}} |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 95 | static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) / (0.0 + 0.0j))) == 1); |
Timm Baeder | 9ddc014 | 2024-06-08 11:20:31 +0200 | [diff] [blame] | 96 | static_assert(__builtin_isinf_sign(__real__((1.0 + 1.0j) / 0.0)) == 1); // expected-error {{static assertion}} \ |
| 97 | // expected-note {{division by zero}} |
Chandler Carruth | a216cad | 2014-10-11 00:57:18 +0000 | [diff] [blame] | 98 | static_assert(__builtin_isinf_sign(__real__((__builtin_inf() + 1.0j) / (0.0 + 0.0j))) == 1); |
Timm Baeder | 9ddc014 | 2024-06-08 11:20:31 +0200 | [diff] [blame] | 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 | |