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

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Oct 23 02:51:38 PDT 2023


Author: ZhangYin
Date: 2023-10-23T11:51:34+02:00
New Revision: dc5341069315fe0129e19062eb0ddcc221b7508f

URL: https://github.com/llvm/llvm-project/commit/dc5341069315fe0129e19062eb0ddcc221b7508f
DIFF: https://github.com/llvm/llvm-project/commit/dc5341069315fe0129e19062eb0ddcc221b7508f.diff

LOG: [libc++] <experimental/simd> Add operator value_type() of simd reference (#68960)

Added: 
    libcxx/test/std/experimental/simd/simd.class/simd_subscr.pass.cpp
    libcxx/test/std/experimental/simd/simd.mask.class/simd_mask_subscr.pass.cpp
    libcxx/test/std/experimental/simd/simd.reference/reference_value_type.pass.cpp

Modified: 
    libcxx/docs/Status/ParallelismProjects.csv
    libcxx/include/experimental/__simd/reference.h
    libcxx/include/experimental/__simd/simd.h
    libcxx/include/experimental/__simd/simd_mask.h

Removed: 
    


################################################################################
diff  --git a/libcxx/docs/Status/ParallelismProjects.csv b/libcxx/docs/Status/ParallelismProjects.csv
index be3663bf087b47e..667a74d207c9363 100644
--- a/libcxx/docs/Status/ParallelismProjects.csv
+++ b/libcxx/docs/Status/ParallelismProjects.csv
@@ -14,14 +14,17 @@ 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|
 | `[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 a9c17a230458d61..335b127bf271fad 100644
--- a/libcxx/include/experimental/__simd/reference.h
+++ b/libcxx/include/experimental/__simd/reference.h
@@ -26,7 +26,9 @@ class __simd_reference {
   _Storage& __s_;
   size_t __idx_;
 
-  _LIBCPP_HIDE_FROM_ABI _Vp __get() const { return __s_.__get(__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) {
     if constexpr (is_same_v<_Vp, bool>)
@@ -40,6 +42,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..c71dd625e46c61e 100644
--- a/libcxx/include/experimental/__simd/simd.h
+++ b/libcxx/include/experimental/__simd/simd.h
@@ -46,11 +46,12 @@ 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 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); }
 };
 
 template <class _Tp>

diff  --git a/libcxx/include/experimental/__simd/simd_mask.h b/libcxx/include/experimental/__simd/simd_mask.h
index 4862fd42c0d06ac..946338542ca0dda 100644
--- a/libcxx/include/experimental/__simd/simd_mask.h
+++ b/libcxx/include/experimental/__simd/simd_mask.h
@@ -42,8 +42,8 @@ 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 value_type operator[](size_t __i) const { return __s_.__get(__i); }
+  _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); }
 };
 
 template <class _Tp>

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;
+}

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;
+}


        


More information about the libcxx-commits mailing list