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

via libcxx-commits libcxx-commits at lists.llvm.org
Sun Oct 22 23:52:40 PDT 2023


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

>From 77fc6efa758c516a4c949f8434307a987497d6bd Mon Sep 17 00:00:00 2001
From: Zhangyin <zhangyin2018 at iscas.ac.cn>
Date: Mon, 25 Sep 2023 16:29:03 +0800
Subject: [PATCH 1/2] [libc++] <experimental/simd> Add operator value_type() of
 simd reference

---
 libcxx/docs/Status/ParallelismProjects.csv    |  1 +
 .../include/experimental/__simd/reference.h   |  4 +-
 libcxx/include/experimental/__simd/simd.h     |  4 +-
 .../include/experimental/__simd/simd_mask.h   |  2 +-
 .../reference_value_type.pass.cpp             | 49 +++++++++++++++++++
 5 files changed, 56 insertions(+), 4 deletions(-)
 create mode 100644 libcxx/test/std/experimental/simd/simd.reference/reference_value_type.pass.cpp

diff --git a/libcxx/docs/Status/ParallelismProjects.csv b/libcxx/docs/Status/ParallelismProjects.csv
index be3663bf087b47e..dd14e963281e99f 100644
--- a/libcxx/docs/Status/ParallelismProjects.csv
+++ b/libcxx/docs/Status/ParallelismProjects.csv
@@ -14,6 +14,7 @@ Section,Description,Dependencies,Assignee,Complete
 | `[parallel.simd.traits] <https://wg21.link/N4808>`_, "simd type traits rebind_simd", None, Yin Zhang, |In Progress|
 | `[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.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 a9c17a230458d61..0d38f46fc4408b6 100644
--- a/libcxx/include/experimental/__simd/reference.h
+++ b/libcxx/include/experimental/__simd/reference.h
@@ -26,7 +26,7 @@ class __simd_reference {
   _Storage& __s_;
   size_t __idx_;
 
-  _LIBCPP_HIDE_FROM_ABI _Vp __get() const { return __s_.__get(__idx_); }
+  _LIBCPP_HIDE_FROM_ABI _Vp __get() const noexcept { return __s_.__get(__idx_); }
 
   _LIBCPP_HIDE_FROM_ABI void __set(_Vp __v) {
     if constexpr (is_same_v<_Vp, bool>)
@@ -40,6 +40,8 @@ class __simd_reference {
 
   __simd_reference()                        = delete;
   __simd_reference(const __simd_reference&) = delete;
+
+  _LIBCPP_HIDE_FROM_ABI operator value_type() const noexcept { return __get(); }
 };
 
 } // namespace parallelism_v2
diff --git a/libcxx/include/experimental/__simd/simd.h b/libcxx/include/experimental/__simd/simd.h
index 954f94c90784e2f..64d3223462b3c83 100644
--- a/libcxx/include/experimental/__simd/simd.h
+++ b/libcxx/include/experimental/__simd/simd.h
@@ -46,11 +46,11 @@ class simd {
 
   // generator constructor
   template <class _Generator, enable_if_t<__can_generate_v<value_type, _Generator, size()>, int> = 0>
-  explicit _LIBCPP_HIDE_FROM_ABI simd(_Generator&& __g) : __s_(_Impl::__generate(std::forward<_Generator>(__g))) {}
+  explicit _LIBCPP_HIDE_FROM_ABI simd(_Generator&& __g) noexcept : __s_(_Impl::__generate(std::forward<_Generator>(__g))) {}
 
   // scalar access [simd.subscr]
   // Add operator[] temporarily to test braodcast. Add test for it in later patch.
-  _LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const { return __s_.__get(__i); }
+  _LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
 };
 
 template <class _Tp>
diff --git a/libcxx/include/experimental/__simd/simd_mask.h b/libcxx/include/experimental/__simd/simd_mask.h
index 4862fd42c0d06ac..b093787ee474b42 100644
--- a/libcxx/include/experimental/__simd/simd_mask.h
+++ b/libcxx/include/experimental/__simd/simd_mask.h
@@ -43,7 +43,7 @@ class simd_mask {
 
   // scalar access [simd.mask.subscr]
   // Add operator[] temporarily to test braodcast. Add test for it in later patch.
-  _LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const { return __s_.__get(__i); }
+  _LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
 };
 
 template <class _Tp>
diff --git a/libcxx/test/std/experimental/simd/simd.reference/reference_value_type.pass.cpp b/libcxx/test/std/experimental/simd/simd.reference/reference_value_type.pass.cpp
new file mode 100644
index 000000000000000..8a1b54e3f5dd54c
--- /dev/null
+++ b/libcxx/test/std/experimental/simd/simd.reference/reference_value_type.pass.cpp
@@ -0,0 +1,49 @@
+//===----------------------------------------------------------------------===//
+//
+// 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]
+// operator value_type() const noexcept;
+
+#include "../test_utils.h"
+#include <experimental/simd>
+
+namespace ex = std::experimental::parallelism_v2;
+
+template <class T, std::size_t>
+struct CheckSimdReferenceValueType {
+  template <class SimdAbi>
+  void operator()() {
+    ex::simd<T, SimdAbi> origin_simd([](T i) { return static_cast<T>(i); });
+    for (size_t i = 0; i < origin_simd.size(); ++i) {
+      static_assert(noexcept(T(origin_simd[i])));
+      assert(T(origin_simd[i]) == static_cast<T>(i));
+    }
+  }
+};
+
+template <class T, std::size_t>
+struct CheckMaskReferenceValueType {
+  template <class SimdAbi>
+  void operator()() {
+    ex::simd_mask<T, SimdAbi> origin_simd_mask(true);
+    for (size_t i = 0; i < origin_simd_mask.size(); ++i) {
+      static_assert(noexcept(bool(origin_simd_mask[i])));
+      assert(bool(origin_simd_mask[i]) == true);
+    }
+  }
+};
+
+int main(int, char**) {
+  test_all_simd_abi<CheckSimdReferenceValueType>();
+  test_all_simd_abi<CheckMaskReferenceValueType>();
+  return 0;
+}

>From 19880cd6a283b061e631832cd0cbd77b7c903008 Mon Sep 17 00:00:00 2001
From: Zhangyin <zhangyin2018 at iscas.ac.cn>
Date: Mon, 25 Sep 2023 16:54:11 +0800
Subject: [PATCH 2/2] [libcxx] <experimental/simd> Add subscript operators of
 class simd/simd_mask

---
 libcxx/docs/Status/ParallelismProjects.csv    |  2 +
 .../include/experimental/__simd/reference.h   |  2 +
 libcxx/include/experimental/__simd/simd.h     |  5 +-
 .../include/experimental/__simd/simd_mask.h   |  2 +-
 .../simd/simd.class/simd_subscr.pass.cpp      | 52 +++++++++++++++++++
 .../simd.mask.class/simd_mask_subscr.pass.cpp | 52 +++++++++++++++++++
 6 files changed, 112 insertions(+), 3 deletions(-)
 create mode 100644 libcxx/test/std/experimental/simd/simd.class/simd_subscr.pass.cpp
 create mode 100644 libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_subscr.pass.cpp

diff --git a/libcxx/docs/Status/ParallelismProjects.csv b/libcxx/docs/Status/ParallelismProjects.csv
index dd14e963281e99f..667a74d207c9363 100644
--- a/libcxx/docs/Status/ParallelismProjects.csv
+++ b/libcxx/docs/Status/ParallelismProjects.csv
@@ -19,10 +19,12 @@ Section,Description,Dependencies,Assignee,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|
 | `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd generate constructor <https://reviews.llvm.org/D159442>`_", None, Yin Zhang, |Complete|
+| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd subscript operators <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
 | `[parallel.simd.class] <https://wg21.link/N4808>`_, "Class template simd implementation", None, Yin Zhang, |In Progress|
 | `[parallel.simd.nonmembers] <https://wg21.link/N4808>`_, "simd non-member operations", None, Yin Zhang, |In Progress|
 | `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`Class template simd_mask declaration and alias <https://reviews.llvm.org/D144362>`_", [parallel.simd.abi], Yin Zhang, |Complete|
 | `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask<>::size() <https://reviews.llvm.org/D144363>`_", [parallel.simd.class] simd<>::size(), Yin Zhang, |Complete|
 | `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask broadcast constructor <https://reviews.llvm.org/D156225>`_", None, Yin Zhang, |Complete|
+| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask subscript operators <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
 | `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "Class template simd_mask implementation", None, Yin Zhang, |In Progress|
 | `[parallel.simd.mask.nonmembers] <https://wg21.link/N4808>`_, "simd_mask non-member operations", None, Yin Zhang, |In Progress|
diff --git a/libcxx/include/experimental/__simd/reference.h b/libcxx/include/experimental/__simd/reference.h
index 0d38f46fc4408b6..335b127bf271fad 100644
--- a/libcxx/include/experimental/__simd/reference.h
+++ b/libcxx/include/experimental/__simd/reference.h
@@ -26,6 +26,8 @@ class __simd_reference {
   _Storage& __s_;
   size_t __idx_;
 
+  _LIBCPP_HIDE_FROM_ABI __simd_reference(_Storage& __s, size_t __idx) : __s_(__s), __idx_(__idx) {}
+
   _LIBCPP_HIDE_FROM_ABI _Vp __get() const noexcept { return __s_.__get(__idx_); }
 
   _LIBCPP_HIDE_FROM_ABI void __set(_Vp __v) {
diff --git a/libcxx/include/experimental/__simd/simd.h b/libcxx/include/experimental/__simd/simd.h
index 64d3223462b3c83..c71dd625e46c61e 100644
--- a/libcxx/include/experimental/__simd/simd.h
+++ b/libcxx/include/experimental/__simd/simd.h
@@ -46,10 +46,11 @@ class simd {
 
   // generator constructor
   template <class _Generator, enable_if_t<__can_generate_v<value_type, _Generator, size()>, int> = 0>
-  explicit _LIBCPP_HIDE_FROM_ABI simd(_Generator&& __g) noexcept : __s_(_Impl::__generate(std::forward<_Generator>(__g))) {}
+  explicit _LIBCPP_HIDE_FROM_ABI simd(_Generator&& __g) noexcept
+      : __s_(_Impl::__generate(std::forward<_Generator>(__g))) {}
 
   // scalar access [simd.subscr]
-  // Add operator[] temporarily to test braodcast. Add test for it in later patch.
+  _LIBCPP_HIDE_FROM_ABI reference operator[](size_t __i) noexcept { return reference(__s_, __i); }
   _LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
 };
 
diff --git a/libcxx/include/experimental/__simd/simd_mask.h b/libcxx/include/experimental/__simd/simd_mask.h
index b093787ee474b42..946338542ca0dda 100644
--- a/libcxx/include/experimental/__simd/simd_mask.h
+++ b/libcxx/include/experimental/__simd/simd_mask.h
@@ -42,7 +42,7 @@ class simd_mask {
   _LIBCPP_HIDE_FROM_ABI explicit simd_mask(value_type __v) noexcept : __s_(_Impl::__broadcast(__v)) {}
 
   // scalar access [simd.mask.subscr]
-  // Add operator[] temporarily to test braodcast. Add test for it in later patch.
+  _LIBCPP_HIDE_FROM_ABI reference operator[](size_t __i) noexcept { return reference(__s_, __i); }
   _LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
 };
 
diff --git a/libcxx/test/std/experimental/simd/simd.class/simd_subscr.pass.cpp b/libcxx/test/std/experimental/simd/simd.class/simd_subscr.pass.cpp
new file mode 100644
index 000000000000000..32b0575230a1dde
--- /dev/null
+++ b/libcxx/test/std/experimental/simd/simd.class/simd_subscr.pass.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// 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.class]
+// reference operator[](size_t i);
+// value_type operator[](size_t i) const;
+
+#include "../test_utils.h"
+#include <experimental/simd>
+
+namespace ex = std::experimental::parallelism_v2;
+
+template <class T, std::size_t>
+struct CheckSimdReferenceSubscr {
+  template <class SimdAbi>
+  void operator()() {
+    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_assert(std::is_same_v<typename ex::simd<T, SimdAbi>::reference, decltype(origin_simd[i])>);
+      assert(origin_simd[i] == static_cast<T>(i));
+    }
+  }
+};
+
+template <class T, std::size_t>
+struct CheckSimdValueTypeSubscr {
+  template <class SimdAbi>
+  void operator()() {
+    const 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_assert(std::is_same_v<T, decltype(origin_simd[i])>);
+      assert(origin_simd[i] == static_cast<T>(i));
+    }
+  }
+};
+
+int main(int, char**) {
+  test_all_simd_abi<CheckSimdReferenceSubscr>();
+  test_all_simd_abi<CheckSimdValueTypeSubscr>();
+  return 0;
+}
diff --git a/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_subscr.pass.cpp b/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_subscr.pass.cpp
new file mode 100644
index 000000000000000..4b38ff159306105
--- /dev/null
+++ b/libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_subscr.pass.cpp
@@ -0,0 +1,52 @@
+//===----------------------------------------------------------------------===//
+//
+// 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.class]
+// reference operator[](size_t i);
+// value_type operator[](size_t i) const;
+
+#include "../test_utils.h"
+#include <experimental/simd>
+
+namespace ex = std::experimental::parallelism_v2;
+
+template <class T, std::size_t>
+struct CheckSimdMaskReferenceSubscr {
+  template <class SimdAbi>
+  void operator()() {
+    ex::simd_mask<T, SimdAbi> origin_simd_mask(true);
+    for (size_t i = 0; i < origin_simd_mask.size(); ++i) {
+      static_assert(noexcept(origin_simd_mask[i]));
+      static_assert(std::is_same_v<typename ex::simd_mask<T, SimdAbi>::reference, decltype(origin_simd_mask[i])>);
+      assert(origin_simd_mask[i] == true);
+    }
+  }
+};
+
+template <class T, std::size_t>
+struct CheckSimdMaskValueTypeSubscr {
+  template <class SimdAbi>
+  void operator()() {
+    const ex::simd_mask<T, SimdAbi> origin_simd_mask(true);
+    for (size_t i = 0; i < origin_simd_mask.size(); ++i) {
+      static_assert(noexcept(origin_simd_mask[i]));
+      static_assert(std::is_same_v<bool, decltype(origin_simd_mask[i])>);
+      assert(origin_simd_mask[i] == true);
+    }
+  }
+};
+
+int main(int, char**) {
+  test_all_simd_abi<CheckSimdMaskReferenceSubscr>();
+  test_all_simd_abi<CheckSimdMaskValueTypeSubscr>();
+  return 0;
+}



More information about the libcxx-commits mailing list