Skip to content
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

[libc++] Add a test case for std::bit_cast with std::complex #97751

Merged
merged 2 commits into from
Jul 8, 2024

Conversation

ldionne
Copy link
Member

@ldionne ldionne commented Jul 4, 2024

This is extracted from #94620. While libc++ doesn't have the problem described in that issue, a test case is a good idea to ensure that we don't regress this behavior in the future. This could happen for example if we decide to use _Complex in the implementation of std::complex while Clang doesn't handle bit_cast with _Complex yet.

@ldionne ldionne requested a review from a team as a code owner July 4, 2024 16:05
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jul 4, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Jul 4, 2024

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

Changes

This is extracted from #94620. While libc++ doesn't have the problem described in that issue, a test case is a good idea to ensure that we don't regress this behavior in the future. This could happen for example if we decide to use _Complex in the implementation of std::complex while Clang doesn't handle bit_cast with _Complex yet.


Full diff: https://github.com/llvm/llvm-project/pull/97751.diff

1 Files Affected:

  • (added) libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp (+37)
diff --git a/libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp b/libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp
new file mode 100644
index 0000000000000..7f15b6f9ade64
--- /dev/null
+++ b/libcxx/test/std/numerics/complex.number/complex/bit_cast.pass.cpp
@@ -0,0 +1,37 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14, c++17
+
+// Make sure that std::bit_cast works with std::complex. Test case extracted from
+// https://github.com/llvm/llvm-project/issues/94620.
+
+#include <bit>
+#include <complex>
+
+template <class T>
+constexpr void test() {
+  using Complex                       = std::complex<T>;
+  unsigned char data[sizeof(Complex)] = {0};
+
+  Complex c = std::bit_cast<Complex>(data);
+  (void)c;
+}
+
+constexpr bool test_all() {
+  test<float>();
+  test<double>();
+  test<long double>();
+  return true;
+}
+
+int main(int, char**) {
+  test_all();
+  static_assert(test_all());
+  return 0;
+}

Copy link
Member

@mordante mordante left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM but one small suggestion.

This is extracted from llvm#94620. While libc++ doesn't have the problem
described in that issue, a test case is a good idea to ensure that
we don't regress this behavior in the future. This could happen for
example if we decide to use `_Complex` in the implementation of
`std::complex` while Clang doesn't handle bit_cast with _Complex yet.
@ldionne ldionne merged commit 8ab82a2 into llvm:main Jul 8, 2024
55 checks passed
@ldionne ldionne deleted the review/bit-cast-complex branch July 8, 2024 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants