[libcxx-commits] [libcxx] [libc++] <experimental/simd> Add ++/-- operators for simd reference (PR #88091)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Apr 8 23:08:08 PDT 2024


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

>From b119aeda01f55d6181dcde10d3b5990f6a998b77 Mon Sep 17 00:00:00 2001
From: Yin Zhang <zhangyin2018 at iscas.ac.cn>
Date: Tue, 19 Mar 2024 20:30:51 +0800
Subject: [PATCH] [libc++] <experimental/simd> Add ++/-- operators for simd
 reference

---
 .../include/experimental/__simd/reference.h   | 27 ++++++++++++
 .../simd.reference/reference_unary.pass.cpp   | 43 +++++++++++++++++++
 2 files changed, 70 insertions(+)
 create mode 100644 libcxx/test/std/experimental/simd/simd.reference/reference_unary.pass.cpp

diff --git a/libcxx/include/experimental/__simd/reference.h b/libcxx/include/experimental/__simd/reference.h
index 7efbba96ec71b1..3d61751044526d 100644
--- a/libcxx/include/experimental/__simd/reference.h
+++ b/libcxx/include/experimental/__simd/reference.h
@@ -12,6 +12,7 @@
 
 #include <__type_traits/is_assignable.h>
 #include <__type_traits/is_same.h>
+#include <__utility/declval.h>
 #include <__utility/forward.h>
 #include <cstddef>
 #include <experimental/__config>
@@ -55,6 +56,32 @@ class __simd_reference {
     __set(static_cast<value_type>(std::forward<_Up>(__v)));
     return {__s_, __idx_};
   }
+
+  template <class _Up = value_type, class = decltype(std::declval<_Up>() + _Up{1})>
+  __simd_reference _LIBCPP_HIDE_FROM_ABI operator++() && noexcept {
+    __set(__get() + 1);
+    return {__s_, __idx_};
+  }
+
+  template <class _Up = value_type, class = decltype(std::declval<_Up>() + _Up{1})>
+  value_type _LIBCPP_HIDE_FROM_ABI operator++(int) && noexcept {
+    auto __r = __get();
+    __set(__get() + 1);
+    return __r;
+  }
+
+  template <class _Up = value_type, class = decltype(std::declval<_Up>() - _Up{1})>
+  __simd_reference _LIBCPP_HIDE_FROM_ABI operator--() && noexcept {
+    __set(__get() - 1);
+    return {__s_, __idx_};
+  }
+
+  template <class _Up = value_type, class = decltype(std::declval<_Up>() - _Up{1})>
+  value_type _LIBCPP_HIDE_FROM_ABI operator--(int) && noexcept {
+    auto __r = __get();
+    __set(__get() - 1);
+    return __r;
+  }
 };
 
 } // namespace parallelism_v2
diff --git a/libcxx/test/std/experimental/simd/simd.reference/reference_unary.pass.cpp b/libcxx/test/std/experimental/simd/simd.reference/reference_unary.pass.cpp
new file mode 100644
index 00000000000000..7452dc986cd4af
--- /dev/null
+++ b/libcxx/test/std/experimental/simd/simd.reference/reference_unary.pass.cpp
@@ -0,0 +1,43 @@
+//===----------------------------------------------------------------------===//
+//
+// 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]
+// reference operator++() && noexcept;
+// value_type operator++(int) && noexcept;
+// reference operator--() && noexcept;
+// value_type operator--(int) && noexcept;
+
+#include "../test_utils.h"
+#include <experimental/simd>
+
+namespace ex = std::experimental::parallelism_v2;
+
+template <class T, std::size_t>
+struct CheckSimdReferenceUnaryOperators {
+  template <class SimdAbi>
+  void operator()() const {
+    ex::simd<T, SimdAbi> origin_simd(static_cast<T>(3));
+    static_assert(noexcept(++origin_simd[0]));
+    assert(((T)(++origin_simd[0]) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(4)));
+    static_assert(noexcept(origin_simd[0]++));
+    assert(((T)(origin_simd[0]++) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(5)));
+    static_assert(noexcept(--origin_simd[0]));
+    assert(((T)(--origin_simd[0]) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(4)));
+    static_assert(noexcept(origin_simd[0]--));
+    assert(((T)(origin_simd[0]--) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(3)));
+  }
+};
+
+int main(int, char**) {
+  test_all_simd_abi<CheckSimdReferenceUnaryOperators>();
+  return 0;
+}



More information about the libcxx-commits mailing list