[libcxx-commits] [libcxx] [libc++] <experimental/simd> Add assignment operator of simd reference (PR #70020)

via libcxx-commits libcxx-commits at lists.llvm.org
Wed Oct 25 02:36:32 PDT 2023


https://github.com/joy2myself updated https://github.com/llvm/llvm-project/pull/70020

>From 8848eddb7adad8716925d830f53b84bcadc2e5b9 Mon Sep 17 00:00:00 2001
From: Zhangyin <zhangyin2018 at iscas.ac.cn>
Date: Mon, 25 Sep 2023 17:25:37 +0800
Subject: [PATCH] [libc++] <experimental/simd> Add assignment operator of simd
 reference

---
 libcxx/docs/Status/ParallelismProjects.csv    |  1 +
 .../include/experimental/__simd/reference.h   |  7 ++
 .../reference_assignment.pass.cpp             | 81 +++++++++++++++++++
 3 files changed, 89 insertions(+)
 create mode 100644 libcxx/test/std/experimental/simd/simd.reference/reference_assignment.pass.cpp

diff --git a/libcxx/docs/Status/ParallelismProjects.csv b/libcxx/docs/Status/ParallelismProjects.csv
index 667a74d207c9363..05a66080823d421 100644
--- a/libcxx/docs/Status/ParallelismProjects.csv
+++ b/libcxx/docs/Status/ParallelismProjects.csv
@@ -15,6 +15,7 @@ Section,Description,Dependencies,Assignee,Complete
 | `[parallel.simd.traits] <https://wg21.link/N4808>`_, "simd type traits resize_simd", None, Yin Zhang, |In Progress|
 | `[parallel.simd.whereexpr] <https://wg21.link/N4808>`_, "Where expression class templates", None, Yin Zhang, |In Progress|
 | `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references operator value_type() <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
+| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references operator= <https://github.com/llvm/llvm-project/pull/70020>`_", None, Yin Zhang, |Complete|
 | `[parallel.simd.class] <https://wg21.link/N4808>`_, "`Class template simd declaration and alias <https://reviews.llvm.org/D144362>`_", [parallel.simd.abi], Yin Zhang, |Complete|
 | `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd<>::size() <https://reviews.llvm.org/D144363>`_", [parallel.simd.traits] simd_size[_v], Yin Zhang, |Complete|
 | `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd broadcast constructor <https://reviews.llvm.org/D156225>`_", None, Yin Zhang, |Complete|
diff --git a/libcxx/include/experimental/__simd/reference.h b/libcxx/include/experimental/__simd/reference.h
index 335b127bf271fad..8c58d24f2f2dc42 100644
--- a/libcxx/include/experimental/__simd/reference.h
+++ b/libcxx/include/experimental/__simd/reference.h
@@ -10,6 +10,7 @@
 #ifndef _LIBCPP_EXPERIMENTAL___SIMD_REFERENCE_H
 #define _LIBCPP_EXPERIMENTAL___SIMD_REFERENCE_H
 
+#include <__type_traits/is_assignable.h>
 #include <experimental/__simd/utility.h>
 
 #if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
@@ -44,6 +45,12 @@ class __simd_reference {
   __simd_reference(const __simd_reference&) = delete;
 
   _LIBCPP_HIDE_FROM_ABI operator value_type() const noexcept { return __get(); }
+
+  template <class _Up, enable_if_t<is_assignable_v<value_type&, _Up&&>, int> = 0>
+  _LIBCPP_HIDE_FROM_ABI __simd_reference operator=(_Up&& __v) && noexcept {
+    __set(static_cast<value_type>(std::forward<_Up>(__v)));
+    return {__s_, __idx_};
+  }
 };
 
 } // namespace parallelism_v2
diff --git a/libcxx/test/std/experimental/simd/simd.reference/reference_assignment.pass.cpp b/libcxx/test/std/experimental/simd/simd.reference/reference_assignment.pass.cpp
new file mode 100644
index 000000000000000..04862507535362d
--- /dev/null
+++ b/libcxx/test/std/experimental/simd/simd.reference/reference_assignment.pass.cpp
@@ -0,0 +1,81 @@
+//===----------------------------------------------------------------------===//
+//
+// 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]
+// template<class U> reference=(U&& x) && noexcept;
+
+#include "../test_utils.h"
+#include <experimental/simd>
+
+namespace ex = std::experimental::parallelism_v2;
+
+template <class T, class SimdAbi>
+struct CheckSimdReferenceAssignmentHelper {
+  template <class U>
+  void operator()() const {
+    if constexpr (std::is_assignable_v<T&, U&&>) {
+      ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
+      for (size_t i = 0; i < origin_simd.size(); ++i) {
+        static_assert(noexcept(origin_simd[i] = static_cast<U>(i + 1)));
+        origin_simd[i] = static_cast<U>(i + 1);
+        assert(origin_simd[i] == static_cast<T>(std::forward<U>(i + 1)));
+      }
+    }
+  }
+};
+
+template <class T, class SimdAbi>
+struct CheckMaskReferenceAssignmentHelper {
+  template <class U>
+  void operator()() const {
+    if constexpr (std::is_assignable_v<bool&, U&&>) {
+      ex::simd_mask<T, SimdAbi> origin_mask(true);
+      for (size_t i = 0; i < origin_mask.size(); ++i) {
+        static_assert(noexcept(origin_mask[i] = static_cast<U>(i + 1)));
+        origin_mask[i] = static_cast<U>(i % 2);
+        assert(origin_mask[i] == static_cast<T>(std::forward<U>(i % 2)));
+      }
+    }
+  }
+};
+
+template <class T, class SimdAbi>
+struct CheckReferenceAssignmentTraitsHelper {
+  template <class U>
+  void operator()() const {
+    if constexpr (std::is_assignable_v<T&, U&&>)
+      static_assert(std::is_assignable_v<typename ex::simd<T, SimdAbi>::reference&&, U&&>);
+    else
+      static_assert(!std::is_assignable_v<typename ex::simd<T, SimdAbi>::reference&&, U&&>);
+
+    if constexpr (std::is_assignable_v<bool&, U&&>)
+      static_assert(std::is_assignable_v<typename ex::simd_mask<T, SimdAbi>::reference&&, U&&>);
+    else
+      static_assert(!std::is_assignable_v<typename ex::simd_mask<T, SimdAbi>::reference&&, U&&>);
+  }
+};
+
+template <class T, std::size_t>
+struct CheckReferenceAssignment {
+  template <class SimdAbi>
+  void operator()() {
+    types::for_each(arithmetic_no_bool_types(), CheckSimdReferenceAssignmentHelper<T, SimdAbi>());
+    types::for_each(arithmetic_no_bool_types(), CheckMaskReferenceAssignmentHelper<T, SimdAbi>());
+
+    types::for_each(arithmetic_no_bool_types(), CheckReferenceAssignmentTraitsHelper<T, SimdAbi>());
+  }
+};
+
+int main(int, char**) {
+  test_all_simd_abi<CheckReferenceAssignment>();
+  return 0;
+}



More information about the libcxx-commits mailing list