Skip to content

[C2y] Claim conformance to WG14 N3460 #131196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions clang/test/C/C2y/n3460.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// RUN: %clang_cc1 -verify -std=c2y -Wall %s

/* WG14 N3460: Clang 12
* Complex operators
*
* This moves some Annex G requirements into the main body of the standard.
*/

// CMPLX(0.0, inf) * 2.0, the result should be CMPLX(0.0, inf), not CMPLX(nan, inf)
static_assert(__builtin_complex(0.0, __builtin_inf()) * 2.0 ==
__builtin_complex(0.0, __builtin_inf()));

// CMPLX(0.0, 1.0) * -0.0 is CMPLX(-0.0, -0.0), not CMPLX(-0.0, +0.0)
static_assert(__builtin_complex(0.0, 1.0) * -0.0 ==
__builtin_complex(-0.0, -0.0));

// Testing for -0.0 is a pain because -0.0 == +0.0, so forcefully generate a
// diagnostic and check the note.
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'}} \
expected-note {{expression evaluates to '(-0 + -0i) == 1'}}
*/

// CMPLX(0.0, inf) / 2.0, the result should be CMPLX(0.0, inf),
// not CMPLX(nan, inf)
static_assert(__builtin_complex(0.0, __builtin_inf()) / 2.0 ==
__builtin_complex(0.0, __builtin_inf()));

// CMPLX(2.0, 3.0) * 2.0, the result should be CMPLX(4.0, 6.0)
static_assert(__builtin_complex(2.0, 3.0) * 2.0 ==
__builtin_complex(4.0, 6.0));

// CMPLX(2.0, 4.0) / 2.0, the result should be CMPLX(1.0, 2.0)
static_assert(__builtin_complex(2.0, 4.0) / 2.0 ==
__builtin_complex(1.0, 2.0));

// CMPLX(2.0, 3.0) * CMPLX(4.0, 5.0), the result should be
// CMPLX(8.0 - 15.0, 12.0 + 10.0)
static_assert(__builtin_complex(2.0, 3.0) * __builtin_complex(4.0, 5.0) ==
__builtin_complex(-7.0, 22.0));

// CMPLX(2.0, 3.0) / CMPLX(4.0, 5.0), the result should be
// CMPLX((8.0 + 15.0)/(4.0^2 + 5.0^2), (12.0 - 10.0)/(4.0^2 + 5.0^2))
static_assert(__builtin_complex(2.0, 3.0) / __builtin_complex(4.0, 5.0) ==
__builtin_complex(23.0 / 41.0, 2.0 / 41.0));


// 2.0 / CMPLX(2.0, 4.0), the result should be
// CMPLX(4.0 /(2.0^2 + 4.0^2), -8.0/(2.0^2 + 4.0^2))
static_assert(2.0 / __builtin_complex(2.0, 4.0) ==
__builtin_complex(4.0 / 20.0, -8.0 / 20.0));

80 changes: 80 additions & 0 deletions clang/test/C/C2y/n3460_1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
// RUN: %clang_cc1 -std=c2y -O0 -triple x86_64-unknown-unknown %s -emit-llvm -o - | FileCheck %s
// This tests the codegen for the same test cases as in n3460.c.

// CHECK-LABEL: define dso_local void @test(
// CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
// CHECK-NEXT: [[ENTRY:.*]]:
// CHECK-NEXT: [[A:%.*]] = alloca { double, double }, align 8
// CHECK-NEXT: [[B:%.*]] = alloca { double, double }, align 8
// CHECK-NEXT: [[C:%.*]] = alloca { double, double }, align 8
// CHECK-NEXT: [[D:%.*]] = alloca { double, double }, align 8
// CHECK-NEXT: [[E:%.*]] = alloca { double, double }, align 8
// CHECK-NEXT: [[F:%.*]] = alloca { double, double }, align 8
// CHECK-NEXT: [[G:%.*]] = alloca { double, double }, align 8
// CHECK-NEXT: [[H:%.*]] = alloca { double, double }, align 8
// CHECK-NEXT: [[A_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[A]], i32 0, i32 0
// CHECK-NEXT: [[A_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[A]], i32 0, i32 1
// CHECK-NEXT: store double 0.000000e+00, ptr [[A_REALP]], align 8
// CHECK-NEXT: store double 0x7FF0000000000000, ptr [[A_IMAGP]], align 8
// CHECK-NEXT: [[B_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[B]], i32 0, i32 0
// CHECK-NEXT: [[B_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[B]], i32 0, i32 1
// CHECK-NEXT: store double -0.000000e+00, ptr [[B_REALP]], align 8
// CHECK-NEXT: store double -0.000000e+00, ptr [[B_IMAGP]], align 8
// CHECK-NEXT: [[C_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 0
// CHECK-NEXT: [[C_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[C]], i32 0, i32 1
// CHECK-NEXT: store double 0.000000e+00, ptr [[C_REALP]], align 8
// CHECK-NEXT: store double 0x7FF0000000000000, ptr [[C_IMAGP]], align 8
// CHECK-NEXT: [[D_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[D]], i32 0, i32 0
// CHECK-NEXT: [[D_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[D]], i32 0, i32 1
// CHECK-NEXT: store double 4.000000e+00, ptr [[D_REALP]], align 8
// CHECK-NEXT: store double 6.000000e+00, ptr [[D_IMAGP]], align 8
// CHECK-NEXT: [[E_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[E]], i32 0, i32 0
// CHECK-NEXT: [[E_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[E]], i32 0, i32 1
// CHECK-NEXT: store double 1.000000e+00, ptr [[E_REALP]], align 8
// CHECK-NEXT: store double 2.000000e+00, ptr [[E_IMAGP]], align 8
// CHECK-NEXT: br i1 false, label %[[COMPLEX_MUL_IMAG_NAN:.*]], label %[[COMPLEX_MUL_CONT:.*]], !prof [[PROF2:![0-9]+]]
// CHECK: [[COMPLEX_MUL_IMAG_NAN]]:
// CHECK-NEXT: br i1 false, label %[[COMPLEX_MUL_LIBCALL:.*]], label %[[COMPLEX_MUL_CONT]], !prof [[PROF2]]
// CHECK: [[COMPLEX_MUL_LIBCALL]]:
// 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]+]]
// CHECK-NEXT: [[TMP0:%.*]] = extractvalue { double, double } [[CALL]], 0
// CHECK-NEXT: [[TMP1:%.*]] = extractvalue { double, double } [[CALL]], 1
// CHECK-NEXT: br label %[[COMPLEX_MUL_CONT]]
// CHECK: [[COMPLEX_MUL_CONT]]:
// CHECK-NEXT: [[REAL_MUL_PHI:%.*]] = phi double [ -7.000000e+00, %[[ENTRY]] ], [ -7.000000e+00, %[[COMPLEX_MUL_IMAG_NAN]] ], [ [[TMP0]], %[[COMPLEX_MUL_LIBCALL]] ]
// CHECK-NEXT: [[IMAG_MUL_PHI:%.*]] = phi double [ 2.200000e+01, %[[ENTRY]] ], [ 2.200000e+01, %[[COMPLEX_MUL_IMAG_NAN]] ], [ [[TMP1]], %[[COMPLEX_MUL_LIBCALL]] ]
// CHECK-NEXT: [[F_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[F]], i32 0, i32 0
// CHECK-NEXT: [[F_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[F]], i32 0, i32 1
// CHECK-NEXT: store double [[REAL_MUL_PHI]], ptr [[F_REALP]], align 8
// CHECK-NEXT: store double [[IMAG_MUL_PHI]], ptr [[F_IMAGP]], align 8
// 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]]
// CHECK-NEXT: [[TMP2:%.*]] = extractvalue { double, double } [[CALL1]], 0
// CHECK-NEXT: [[TMP3:%.*]] = extractvalue { double, double } [[CALL1]], 1
// CHECK-NEXT: [[G_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[G]], i32 0, i32 0
// CHECK-NEXT: [[G_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[G]], i32 0, i32 1
// CHECK-NEXT: store double [[TMP2]], ptr [[G_REALP]], align 8
// CHECK-NEXT: store double [[TMP3]], ptr [[G_IMAGP]], align 8
// 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]]
// CHECK-NEXT: [[TMP4:%.*]] = extractvalue { double, double } [[CALL2]], 0
// CHECK-NEXT: [[TMP5:%.*]] = extractvalue { double, double } [[CALL2]], 1
// CHECK-NEXT: [[H_REALP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[H]], i32 0, i32 0
// CHECK-NEXT: [[H_IMAGP:%.*]] = getelementptr inbounds nuw { double, double }, ptr [[H]], i32 0, i32 1
// CHECK-NEXT: store double [[TMP4]], ptr [[H_REALP]], align 8
// CHECK-NEXT: store double [[TMP5]], ptr [[H_IMAGP]], align 8
// CHECK-NEXT: ret void
//
void test() {
_Complex double a = __builtin_complex(0.0, __builtin_inf()) * 2.0;
_Complex double b = __builtin_complex(0.0, 1.0) * -0.0;
_Complex double c = __builtin_complex(0.0, __builtin_inf()) / 2.0;
_Complex double d = __builtin_complex(2.0, 3.0) * 2.0;
_Complex double e = __builtin_complex(2.0, 4.0) / 2.0;
_Complex double f = __builtin_complex(2.0, 3.0) * __builtin_complex(4.0, 5.0);
_Complex double g = __builtin_complex(2.0, 3.0) / __builtin_complex(4.0, 5.0);
_Complex double h = 2.0 / __builtin_complex(2.0, 4.0);
}

//.
// CHECK: [[PROF2]] = !{!"branch_weights", i32 1, i32 1048575}
//.
2 changes: 1 addition & 1 deletion clang/www/c_status.html
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ <h2 id="c2y">C2y implementation status</h2>
<tr>
<td>Complex operators</td>
<td><a href="https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3460.pdf">N3460</a></td>
<td class="unknown" align="center">Unknown</td>
<td class="full" align="center">Clang 12</td>
</tr>
</table>
</details>
Expand Down