Skip to content

Commit cbbcc3d

Browse files
authored
[C2y] Claim conformance to WG14 N3460 (#131196)
This moves some Annex G requirements for complex numbers into the main body of the standard.
1 parent beb4a48 commit cbbcc3d

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

clang/test/C/C2y/n3460.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// RUN: %clang_cc1 -verify -std=c2y -Wall %s
2+
3+
/* WG14 N3460: Clang 12
4+
* Complex operators
5+
*
6+
* This moves some Annex G requirements into the main body of the standard.
7+
*/
8+
9+
// CMPLX(0.0, inf) * 2.0, the result should be CMPLX(0.0, inf), not CMPLX(nan, inf)
10+
static_assert(__builtin_complex(0.0, __builtin_inf()) * 2.0 ==
11+
__builtin_complex(0.0, __builtin_inf()));
12+
13+
// CMPLX(0.0, 1.0) * -0.0 is CMPLX(-0.0, -0.0), not CMPLX(-0.0, +0.0)
14+
static_assert(__builtin_complex(0.0, 1.0) * -0.0 ==
15+
__builtin_complex(-0.0, -0.0));
16+
17+
// Testing for -0.0 is a pain because -0.0 == +0.0, so forcefully generate a
18+
// diagnostic and check the note.
19+
static_assert(__builtin_complex(0.0, 1.0) * -0.0 == 1); /* expected-error {{static assertion failed due to requirement '__builtin_complex(0., 1.) * -0. == 1'}} \
20+
expected-note {{expression evaluates to '(-0 + -0i) == 1'}}
21+
*/
22+
23+
// CMPLX(0.0, inf) / 2.0, the result should be CMPLX(0.0, inf),
24+
// not CMPLX(nan, inf)
25+
static_assert(__builtin_complex(0.0, __builtin_inf()) / 2.0 ==
26+
__builtin_complex(0.0, __builtin_inf()));
27+
28+
// CMPLX(2.0, 3.0) * 2.0, the result should be CMPLX(4.0, 6.0)
29+
static_assert(__builtin_complex(2.0, 3.0) * 2.0 ==
30+
__builtin_complex(4.0, 6.0));
31+
32+
// CMPLX(2.0, 4.0) / 2.0, the result should be CMPLX(1.0, 2.0)
33+
static_assert(__builtin_complex(2.0, 4.0) / 2.0 ==
34+
__builtin_complex(1.0, 2.0));
35+
36+
// CMPLX(2.0, 3.0) * CMPLX(4.0, 5.0), the result should be
37+
// CMPLX(8.0 - 15.0, 12.0 + 10.0)
38+
static_assert(__builtin_complex(2.0, 3.0) * __builtin_complex(4.0, 5.0) ==
39+
__builtin_complex(-7.0, 22.0));
40+
41+
// CMPLX(2.0, 3.0) / CMPLX(4.0, 5.0), the result should be
42+
// CMPLX((8.0 + 15.0)/(4.0^2 + 5.0^2), (12.0 - 10.0)/(4.0^2 + 5.0^2))
43+
static_assert(__builtin_complex(2.0, 3.0) / __builtin_complex(4.0, 5.0) ==
44+
__builtin_complex(23.0 / 41.0, 2.0 / 41.0));
45+
46+
47+
// 2.0 / CMPLX(2.0, 4.0), the result should be
48+
// CMPLX(4.0 /(2.0^2 + 4.0^2), -8.0/(2.0^2 + 4.0^2))
49+
static_assert(2.0 / __builtin_complex(2.0, 4.0) ==
50+
__builtin_complex(4.0 / 20.0, -8.0 / 20.0));
51+

clang/test/C/C2y/n3460_1.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
2+
// RUN: %clang_cc1 -std=c2y -O0 -triple x86_64-unknown-unknown %s -emit-llvm -o - | FileCheck %s
3+
// This tests the codegen for the same test cases as in n3460.c.
4+
5+
// CHECK-LABEL: define dso_local void @test(
6+
// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
7+
// CHECK-NEXT: [[ENTRY:.*]]:
8+
// CHECK-NEXT: [[A:%.*]] = alloca { double, double }, align 8
9+
// CHECK-NEXT: [[B:%.*]] = alloca { double, double }, align 8
10+
// CHECK-NEXT: [[C:%.*]] = alloca { double, double }, align 8
11+
// CHECK-NEXT: [[D:%.*]] = alloca { double, double }, align 8
12+
// CHECK-NEXT: [[E:%.*]] = alloca { double, double }, align 8
13+
// CHECK-NEXT: [[F:%.*]] = alloca { double, double }, align 8
14+
// CHECK-NEXT: [[G:%.*]] = alloca { double, double }, align 8
15+
// CHECK-NEXT: [[H:%.*]] = alloca { double, double }, align 8
16+
// CHECK-NEXT: [[A_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[A]], i32 0, i32 0
17+
// CHECK-NEXT: [[A_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[A]], i32 0, i32 1
18+
// CHECK-NEXT: store double 0.000000e+00, ptr [[A_REALP]], align 8
19+
// CHECK-NEXT: store double 0x7FF0000000000000, ptr [[A_IMAGP]], align 8
20+
// CHECK-NEXT: [[B_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[B]], i32 0, i32 0
21+
// CHECK-NEXT: [[B_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[B]], i32 0, i32 1
22+
// CHECK-NEXT: store double -0.000000e+00, ptr [[B_REALP]], align 8
23+
// CHECK-NEXT: store double -0.000000e+00, ptr [[B_IMAGP]], align 8
24+
// CHECK-NEXT: [[C_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 0
25+
// CHECK-NEXT: [[C_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 1
26+
// CHECK-NEXT: store double 0.000000e+00, ptr [[C_REALP]], align 8
27+
// CHECK-NEXT: store double 0x7FF0000000000000, ptr [[C_IMAGP]], align 8
28+
// CHECK-NEXT: [[D_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[D]], i32 0, i32 0
29+
// CHECK-NEXT: [[D_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[D]], i32 0, i32 1
30+
// CHECK-NEXT: store double 4.000000e+00, ptr [[D_REALP]], align 8
31+
// CHECK-NEXT: store double 6.000000e+00, ptr [[D_IMAGP]], align 8
32+
// CHECK-NEXT: [[E_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[E]], i32 0, i32 0
33+
// CHECK-NEXT: [[E_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[E]], i32 0, i32 1
34+
// CHECK-NEXT: store double 1.000000e+00, ptr [[E_REALP]], align 8
35+
// CHECK-NEXT: store double 2.000000e+00, ptr [[E_IMAGP]], align 8
36+
// CHECK-NEXT: br i1 false, label %[[COMPLEX_MUL_IMAG_NAN:.*]], label %[[COMPLEX_MUL_CONT:.*]], !prof [[PROF2:![0-9]+]]
37+
// CHECK: [[COMPLEX_MUL_IMAG_NAN]]:
38+
// CHECK-NEXT: br i1 false, label %[[COMPLEX_MUL_LIBCALL:.*]], label %[[COMPLEX_MUL_CONT]], !prof [[PROF2]]
39+
// CHECK: [[COMPLEX_MUL_LIBCALL]]:
40+
// CHECK-NEXT: [[CALL:%.*]] = call { double, double } @__muldc3(double noundef 2.000000e+00, double noundef 3.000000e+00, double noundef 4.000000e+00, double noundef 5.000000e+00) #[[ATTR1:[0-9]+]]
41+
// CHECK-NEXT: [[TMP0:%.*]] = extractvalue { double, double } [[CALL]], 0
42+
// CHECK-NEXT: [[TMP1:%.*]] = extractvalue { double, double } [[CALL]], 1
43+
// CHECK-NEXT: br label %[[COMPLEX_MUL_CONT]]
44+
// CHECK: [[COMPLEX_MUL_CONT]]:
45+
// CHECK-NEXT: [[REAL_MUL_PHI:%.*]] = phi double [ -7.000000e+00, %[[ENTRY]] ], [ -7.000000e+00, %[[COMPLEX_MUL_IMAG_NAN]] ], [ [[TMP0]], %[[COMPLEX_MUL_LIBCALL]] ]
46+
// CHECK-NEXT: [[IMAG_MUL_PHI:%.*]] = phi double [ 2.200000e+01, %[[ENTRY]] ], [ 2.200000e+01, %[[COMPLEX_MUL_IMAG_NAN]] ], [ [[TMP1]], %[[COMPLEX_MUL_LIBCALL]] ]
47+
// CHECK-NEXT: [[F_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[F]], i32 0, i32 0
48+
// CHECK-NEXT: [[F_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[F]], i32 0, i32 1
49+
// CHECK-NEXT: store double [[REAL_MUL_PHI]], ptr [[F_REALP]], align 8
50+
// CHECK-NEXT: store double [[IMAG_MUL_PHI]], ptr [[F_IMAGP]], align 8
51+
// CHECK-NEXT: [[CALL1:%.*]] = call { double, double } @__divdc3(double noundef 2.000000e+00, double noundef 3.000000e+00, double noundef 4.000000e+00, double noundef 5.000000e+00) #[[ATTR1]]
52+
// CHECK-NEXT: [[TMP2:%.*]] = extractvalue { double, double } [[CALL1]], 0
53+
// CHECK-NEXT: [[TMP3:%.*]] = extractvalue { double, double } [[CALL1]], 1
54+
// CHECK-NEXT: [[G_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[G]], i32 0, i32 0
55+
// CHECK-NEXT: [[G_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[G]], i32 0, i32 1
56+
// CHECK-NEXT: store double [[TMP2]], ptr [[G_REALP]], align 8
57+
// CHECK-NEXT: store double [[TMP3]], ptr [[G_IMAGP]], align 8
58+
// CHECK-NEXT: [[CALL2:%.*]] = call { double, double } @__divdc3(double noundef 2.000000e+00, double noundef 0.000000e+00, double noundef 2.000000e+00, double noundef 4.000000e+00) #[[ATTR1]]
59+
// CHECK-NEXT: [[TMP4:%.*]] = extractvalue { double, double } [[CALL2]], 0
60+
// CHECK-NEXT: [[TMP5:%.*]] = extractvalue { double, double } [[CALL2]], 1
61+
// CHECK-NEXT: [[H_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[H]], i32 0, i32 0
62+
// CHECK-NEXT: [[H_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[H]], i32 0, i32 1
63+
// CHECK-NEXT: store double [[TMP4]], ptr [[H_REALP]], align 8
64+
// CHECK-NEXT: store double [[TMP5]], ptr [[H_IMAGP]], align 8
65+
// CHECK-NEXT: ret void
66+
//
67+
void test() {
68+
_Complex double a = __builtin_complex(0.0, __builtin_inf()) * 2.0;
69+
_Complex double b = __builtin_complex(0.0, 1.0) * -0.0;
70+
_Complex double c = __builtin_complex(0.0, __builtin_inf()) / 2.0;
71+
_Complex double d = __builtin_complex(2.0, 3.0) * 2.0;
72+
_Complex double e = __builtin_complex(2.0, 4.0) / 2.0;
73+
_Complex double f = __builtin_complex(2.0, 3.0) * __builtin_complex(4.0, 5.0);
74+
_Complex double g = __builtin_complex(2.0, 3.0) / __builtin_complex(4.0, 5.0);
75+
_Complex double h = 2.0 / __builtin_complex(2.0, 4.0);
76+
}
77+
78+
//.
79+
// CHECK: [[PROF2]] = !{!"branch_weights", i32 1, i32 1048575}
80+
//.

clang/www/c_status.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ <h2 id="c2y">C2y implementation status</h2>
318318
<tr>
319319
<td>Complex operators</td>
320320
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3460.pdf">N3460</a></td>
321-
<td class="unknown" align="center">Unknown</td>
321+
<td class="full" align="center">Clang 12</td>
322322
</tr>
323323
</table>
324324
</details>

0 commit comments

Comments
 (0)