Skip to content

[libc++] <experimental/simd> Add swap functions of simd reference #86478

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 1 commit into from
Jun 23, 2024

Conversation

joy2myself
Copy link
Member

No description provided.

@joy2myself joy2myself requested a review from a team as a code owner March 25, 2024 09:04
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Mar 25, 2024
@llvmbot
Copy link
Member

llvmbot commented Mar 25, 2024

@llvm/pr-subscribers-libcxx

Author: ZhangYin (joy2myself)

Changes

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

3 Files Affected:

  • (modified) libcxx/include/experimental/__simd/reference.h (+19)
  • (modified) libcxx/include/experimental/simd (+1)
  • (added) libcxx/test/std/experimental/simd/simd.reference/reference_swap.pass.cpp (+71)
diff --git a/libcxx/include/experimental/__simd/reference.h b/libcxx/include/experimental/__simd/reference.h
index 7efbba96ec71b1..92ece563845afd 100644
--- a/libcxx/include/experimental/__simd/reference.h
+++ b/libcxx/include/experimental/__simd/reference.h
@@ -13,6 +13,7 @@
 #include <__type_traits/is_assignable.h>
 #include <__type_traits/is_same.h>
 #include <__utility/forward.h>
+#include <__utility/move.h>
 #include <cstddef>
 #include <experimental/__config>
 #include <experimental/__simd/utility.h>
@@ -55,6 +56,24 @@ class __simd_reference {
     __set(static_cast<value_type>(std::forward<_Up>(__v)));
     return {__s_, __idx_};
   }
+
+  friend _LIBCPP_HIDE_FROM_ABI void swap(__simd_reference&& __a, __simd_reference&& __b) noexcept {
+    value_type __tmp(std::move(__a.__get()));
+    __a.__set(std::move(__b.__get()));
+    __b.__set(std::move(__tmp));
+  }
+
+  friend _LIBCPP_HIDE_FROM_ABI void swap(value_type& __a, __simd_reference&& __b) noexcept {
+    value_type __tmp(std::move(__a));
+    __a = std::move(__b.__get());
+    __b.__set(std::move(__tmp));
+  }
+
+  friend _LIBCPP_HIDE_FROM_ABI void swap(__simd_reference&& __a, value_type& __b) noexcept {
+    value_type __tmp(std::move(__a.__get()));
+    __a.__set(std::move(__b));
+    __b = std::move(__tmp);
+  }
 };
 
 } // namespace parallelism_v2
diff --git a/libcxx/include/experimental/simd b/libcxx/include/experimental/simd
index fad6431d13a193..484543b81daf1f 100644
--- a/libcxx/include/experimental/simd
+++ b/libcxx/include/experimental/simd
@@ -78,6 +78,7 @@ inline namespace parallelism_v2 {
 #include <experimental/__config>
 #include <experimental/__simd/aligned_tag.h>
 #include <experimental/__simd/declaration.h>
+#include <experimental/__simd/reference.h>
 #include <experimental/__simd/scalar.h>
 #include <experimental/__simd/simd.h>
 #include <experimental/__simd/simd_mask.h>
diff --git a/libcxx/test/std/experimental/simd/simd.reference/reference_swap.pass.cpp b/libcxx/test/std/experimental/simd/simd.reference/reference_swap.pass.cpp
new file mode 100644
index 00000000000000..9ca28bc9b24e21
--- /dev/null
+++ b/libcxx/test/std/experimental/simd/simd.reference/reference_swap.pass.cpp
@@ -0,0 +1,71 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// <experimental/simd>
+//
+// [simd.reference]
+// friend void swap(reference&& a, reference&& b) noexcept;
+// friend void swap(value_type& a, reference&& b) noexcept;
+// friend void swap(reference&& a, value_type& b) noexcept;
+
+#include "../test_utils.h"
+#include <experimental/simd>
+
+namespace ex = std::experimental::parallelism_v2;
+
+template <class T, std::size_t>
+struct CheckSimdRefSwap {
+  template <class SimdAbi>
+  void operator()() {
+    ex::simd<T, SimdAbi> origin_simd_1(1);
+    ex::simd<T, SimdAbi> origin_simd_2(2);
+    T value = 3;
+
+    static_assert(noexcept(swap(origin_simd_1[0], origin_simd_2[0])));
+    swap(origin_simd_1[0], origin_simd_2[0]);
+    assert((origin_simd_1[0] == 2) && (origin_simd_2[0] == 1));
+
+    static_assert(noexcept(swap(origin_simd_1[0], value)));
+    swap(origin_simd_1[0], value);
+    assert((origin_simd_1[0] == 3) && (value == 2));
+
+    static_assert(noexcept(swap(value, origin_simd_2[0])));
+    swap(value, origin_simd_2[0]);
+    assert((value == 1) && (origin_simd_2[0] == 2));
+  }
+};
+
+template <class T, std::size_t>
+struct CheckMaskRefSwap {
+  template <class SimdAbi>
+  void operator()() {
+    ex::simd_mask<T, SimdAbi> origin_mask_1(true);
+    ex::simd_mask<T, SimdAbi> origin_mask_2(false);
+    bool value = true;
+
+    static_assert(noexcept(swap(origin_mask_1[0], origin_mask_2[0])));
+    swap(origin_mask_1[0], origin_mask_2[0]);
+    assert((origin_mask_1[0] == false) && (origin_mask_2[0] == true));
+
+    static_assert(noexcept(swap(origin_mask_1[0], value)));
+    swap(origin_mask_1[0], value);
+    assert((origin_mask_1[0] == true) && (value == false));
+
+    static_assert(noexcept(swap(value, origin_mask_2[0])));
+    swap(value, origin_mask_2[0]);
+    assert((value == true) && (origin_mask_2[0] == false));
+  }
+};
+
+int main(int, char**) {
+  test_all_simd_abi<CheckSimdRefSwap>();
+  test_all_simd_abi<CheckMaskRefSwap>();
+  return 0;
+}

@joy2myself joy2myself marked this pull request as draft March 25, 2024 09:05
Copy link

✅ With the latest revision this PR passed the Python code formatter.

Copy link

github-actions bot commented Mar 25, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@joy2myself joy2myself force-pushed the reference_swap branch 4 times, most recently from aeb3f3a to a18ff21 Compare May 24, 2024 06:43
@joy2myself joy2myself force-pushed the reference_swap branch 2 times, most recently from 206c8f8 to 1b4f36b Compare June 4, 2024 14:05
@joy2myself
Copy link
Member Author

@philnik777 Gentle ping.

@joy2myself joy2myself merged commit 6ec1ddf into llvm:main Jun 23, 2024
52 of 55 checks passed
AlexisPerry pushed a commit to llvm-project-tlp/llvm-project that referenced this pull request Jul 9, 2024
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