[libcxx-commits] [libcxx] [libc++] <experimental/simd> Add unary operators for class simd (PR #104764)
via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Aug 19 04:26:15 PDT 2024
https://github.com/joy2myself created https://github.com/llvm/llvm-project/pull/104764
None
>From e55dee32143cd6ed6fe5ac4e3b7b7af59111ebf8 Mon Sep 17 00:00:00 2001
From: Yin Zhang <zhangyin2018 at iscas.ac.cn>
Date: Mon, 19 Aug 2024 18:48:33 +0800
Subject: [PATCH 1/2] [libc++] <experimental/simd> Add explicit conversion from
and to implementation-defined types
---
libcxx/include/experimental/__simd/simd.h | 4 ++++
libcxx/include/experimental/__simd/simd_mask.h | 4 ++++
2 files changed, 8 insertions(+)
diff --git a/libcxx/include/experimental/__simd/simd.h b/libcxx/include/experimental/__simd/simd.h
index 37e334aad6da07..b494111b504407 100644
--- a/libcxx/include/experimental/__simd/simd.h
+++ b/libcxx/include/experimental/__simd/simd.h
@@ -44,6 +44,10 @@ class simd {
_LIBCPP_HIDE_FROM_ABI simd() noexcept = default;
+ // explicit conversion from and to implementation-defined types
+ explicit _LIBCPP_HIDE_FROM_ABI operator _Storage() const;
+ explicit _LIBCPP_HIDE_FROM_ABI simd(const _Storage& __s) : __s_(__s) {}
+
// broadcast constructor
template <class _Up, enable_if_t<__can_broadcast_v<value_type, __remove_cvref_t<_Up>>, int> = 0>
_LIBCPP_HIDE_FROM_ABI simd(_Up&& __v) noexcept : __s_(_Impl::__broadcast(static_cast<value_type>(__v))) {}
diff --git a/libcxx/include/experimental/__simd/simd_mask.h b/libcxx/include/experimental/__simd/simd_mask.h
index fd6dee2e28ee91..890edfc4d4d729 100644
--- a/libcxx/include/experimental/__simd/simd_mask.h
+++ b/libcxx/include/experimental/__simd/simd_mask.h
@@ -41,6 +41,10 @@ class simd_mask {
_LIBCPP_HIDE_FROM_ABI simd_mask() noexcept = default;
+ // explicit conversion from and to implementation-defined types
+ explicit _LIBCPP_HIDE_FROM_ABI operator _Storage() const;
+ explicit _LIBCPP_HIDE_FROM_ABI simd_mask(const _Storage& __s) : __s_(__s) {}
+
// broadcast constructor
_LIBCPP_HIDE_FROM_ABI explicit simd_mask(value_type __v) noexcept : __s_(_Impl::__broadcast(__v)) {}
>From 148b7b4b124d55361b5403a8ef01b5a70af9b7f3 Mon Sep 17 00:00:00 2001
From: Yin Zhang <zhangyin2018 at iscas.ac.cn>
Date: Tue, 9 Apr 2024 15:31:52 +0800
Subject: [PATCH 2/2] [libc++] <experimental/simd> Add unary operators for
class simd
---
libcxx/include/experimental/__simd/scalar.h | 10 +
libcxx/include/experimental/__simd/simd.h | 45 ++++-
libcxx/include/experimental/__simd/vec_ext.h | 10 +
.../simd/simd.class/simd_unary.pass.cpp | 183 ++++++++++++++++++
4 files changed, 247 insertions(+), 1 deletion(-)
create mode 100644 libcxx/test/std/experimental/simd/simd.class/simd_unary.pass.cpp
diff --git a/libcxx/include/experimental/__simd/scalar.h b/libcxx/include/experimental/__simd/scalar.h
index 1add4653209ace..246760f5274818 100644
--- a/libcxx/include/experimental/__simd/scalar.h
+++ b/libcxx/include/experimental/__simd/scalar.h
@@ -67,6 +67,16 @@ struct __simd_operations<_Tp, simd_abi::__scalar> {
static _LIBCPP_HIDE_FROM_ABI void __store(_SimdStorage __s, _Up* __mem) noexcept {
*__mem = static_cast<_Up>(__s.__data);
}
+
+ static void __increment(_SimdStorage& __s) noexcept { ++__s.__data; }
+
+ static void __decrement(_SimdStorage& __s) noexcept { --__s.__data; }
+
+ static _MaskStorage __negate(_SimdStorage __s) noexcept { return {!__s.__data}; }
+
+ static _SimdStorage __bitwise_not(_SimdStorage __s) noexcept { return {static_cast<_Tp>(~__s.__data)}; }
+
+ static _SimdStorage __unary_minus(_SimdStorage __s) noexcept { return {static_cast<_Tp>(-__s.__data)}; }
};
template <class _Tp>
diff --git a/libcxx/include/experimental/__simd/simd.h b/libcxx/include/experimental/__simd/simd.h
index b494111b504407..3d464ae488dde7 100644
--- a/libcxx/include/experimental/__simd/simd.h
+++ b/libcxx/include/experimental/__simd/simd.h
@@ -25,15 +25,29 @@
_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL
inline namespace parallelism_v2 {
+template <class _Simd, class _Impl, bool>
+class __simd_int_operators {};
+
+template <class _Simd, class _Impl>
+class __simd_int_operators<_Simd, _Impl, true> {
+public:
+ // unary operators for integral _Tp
+ _LIBCPP_HIDE_FROM_ABI _Simd operator~() const noexcept {
+ return static_cast<_Simd>(_Impl::__bitwise_not((*static_cast<const _Simd*>(this)).__s_));
+ }
+};
+
// class template simd [simd.class]
// TODO: implement simd class
template <class _Tp, class _Abi>
-class simd {
+class simd : public __simd_int_operators<simd<_Tp, _Abi>, __simd_operations<_Tp, _Abi>, is_integral_v<_Tp>> {
using _Impl = __simd_operations<_Tp, _Abi>;
using _Storage = typename _Impl::_SimdStorage;
_Storage __s_;
+ friend class __simd_int_operators<simd, _Impl, true>;
+
public:
using value_type = _Tp;
using reference = __simd_reference<_Tp, _Storage, value_type>;
@@ -88,6 +102,35 @@ class simd {
// scalar access [simd.subscr]
_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); }
+
+ // simd unary operators
+ _LIBCPP_HIDE_FROM_ABI simd& operator++() noexcept {
+ _Impl::__increment(__s_);
+ return *this;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI simd operator++(int) noexcept {
+ simd __r = *this;
+ _Impl::__increment(__s_);
+ return __r;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI simd& operator--() noexcept {
+ _Impl::__decrement(__s_);
+ return *this;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI simd operator--(int) noexcept {
+ simd __r = *this;
+ _Impl::__decrement(__s_);
+ return __r;
+ }
+
+ _LIBCPP_HIDE_FROM_ABI mask_type operator!() const noexcept { return static_cast<mask_type>(_Impl::__negate(__s_)); }
+
+ _LIBCPP_HIDE_FROM_ABI simd operator+() const noexcept { return *this; }
+
+ _LIBCPP_HIDE_FROM_ABI simd operator-() const noexcept { return static_cast<simd>(_Impl::__unary_minus(__s_)); }
};
template <class _Tp, class _Abi>
diff --git a/libcxx/include/experimental/__simd/vec_ext.h b/libcxx/include/experimental/__simd/vec_ext.h
index 316866b84873dd..7b9e2be0216dd0 100644
--- a/libcxx/include/experimental/__simd/vec_ext.h
+++ b/libcxx/include/experimental/__simd/vec_ext.h
@@ -86,6 +86,16 @@ struct __simd_operations<_Tp, simd_abi::__vec_ext<_Np>> {
for (size_t __i = 0; __i < _Np; __i++)
__mem[__i] = static_cast<_Up>(__s.__data[__i]);
}
+
+ static void __increment(_SimdStorage& __s) noexcept { __s.__data = __s.__data + 1; }
+
+ static void __decrement(_SimdStorage& __s) noexcept { __s.__data = __s.__data - 1; }
+
+ static _MaskStorage __negate(_SimdStorage __s) noexcept { return {!__s.__data}; }
+
+ static _SimdStorage __bitwise_not(_SimdStorage __s) noexcept { return {~__s.__data}; }
+
+ static _SimdStorage __unary_minus(_SimdStorage __s) noexcept { return {-__s.__data}; }
};
template <class _Tp, int _Np>
diff --git a/libcxx/test/std/experimental/simd/simd.class/simd_unary.pass.cpp b/libcxx/test/std/experimental/simd/simd.class/simd_unary.pass.cpp
new file mode 100644
index 00000000000000..f903b53b31e555
--- /dev/null
+++ b/libcxx/test/std/experimental/simd/simd.class/simd_unary.pass.cpp
@@ -0,0 +1,183 @@
+//===----------------------------------------------------------------------===//
+//
+// 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]
+// simd& operator++() noexcept;
+// simd operator++(int) noexcept;
+// simd& operator--() noexcept;
+// simd operator--(int) noexcept;
+// mask_type operator!() const noexcept;
+// simd operator~() const noexcept;
+// simd operator+() const noexcept;
+// simd operator-() const noexcept;
+
+#include "../test_utils.h"
+#include <experimental/simd>
+
+namespace ex = std::experimental::parallelism_v2;
+
+template <class T, std::size_t>
+struct CheckSimdPrefixIncrementOperator {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+ ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
+ static_assert(noexcept(++origin_simd));
+ std::array<T, array_size> expected_return_value, expected_value;
+ for (size_t i = 0; i < array_size; ++i) {
+ expected_return_value[i] = static_cast<T>(i) + 1;
+ expected_value[i] = static_cast<T>(i) + 1;
+ }
+ assert_simd_values_equal<array_size>(++origin_simd, expected_return_value);
+ assert_simd_values_equal<array_size>(origin_simd, expected_value);
+ }
+};
+
+template <class T, std::size_t>
+struct CheckSimdPostfixIncrementOperator {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+ ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
+ static_assert(noexcept(origin_simd++));
+ std::array<T, array_size> expected_return_value, expected_value;
+ for (size_t i = 0; i < array_size; ++i) {
+ expected_return_value[i] = static_cast<T>(i);
+ expected_value[i] = static_cast<T>(i) + 1;
+ }
+ assert_simd_values_equal<array_size>(origin_simd++, expected_return_value);
+ assert_simd_values_equal<array_size>(origin_simd, expected_value);
+ }
+};
+
+template <class T, std::size_t>
+struct CheckSimdPrefixDecrementOperator {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+ ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
+ static_assert(noexcept(--origin_simd));
+ std::array<T, array_size> expected_return_value, expected_value;
+ for (size_t i = 0; i < array_size; ++i) {
+ expected_return_value[i] = static_cast<T>(i) - 1;
+ expected_value[i] = static_cast<T>(i) - 1;
+ }
+ assert_simd_values_equal<array_size>(--origin_simd, expected_return_value);
+ assert_simd_values_equal<array_size>(origin_simd, expected_value);
+ }
+};
+
+template <class T, std::size_t>
+struct CheckSimdPostfixDecrementOperator {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+ ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
+ static_assert(noexcept(origin_simd--));
+ std::array<T, array_size> expected_return_value, expected_value;
+ for (size_t i = 0; i < array_size; ++i) {
+ expected_return_value[i] = static_cast<T>(i);
+ expected_value[i] = static_cast<T>(i) - 1;
+ }
+ assert_simd_values_equal<array_size>(origin_simd--, expected_return_value);
+ assert_simd_values_equal<array_size>(origin_simd, expected_value);
+ }
+};
+
+template <class T, std::size_t>
+struct CheckSimdNegationOperator {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+ ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
+ static_assert(noexcept(!origin_simd));
+ std::array<bool, array_size> expected_value;
+ for (size_t i = 0; i < array_size; ++i)
+ expected_value[i] = !static_cast<bool>(i);
+ assert_simd_mask_values_equal<array_size>(!origin_simd, expected_value);
+ }
+};
+
+template <class T, std::size_t>
+struct CheckSimdBitwiseNotOperator {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+ ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
+ static_assert(noexcept(~origin_simd));
+ std::array<T, array_size> expected_value;
+ for (size_t i = 0; i < array_size; ++i)
+ expected_value[i] = ~static_cast<T>(i);
+ assert_simd_values_equal<array_size>(~origin_simd, expected_value);
+ }
+};
+
+template <class T, std::size_t>
+struct CheckSimdPositiveSignOperator {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+ ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
+ static_assert(noexcept(+origin_simd));
+ std::array<T, array_size> expected_value;
+ for (size_t i = 0; i < array_size; ++i)
+ expected_value[i] = +static_cast<T>(i);
+ assert_simd_values_equal<array_size>(+origin_simd, expected_value);
+ }
+};
+
+template <class T, std::size_t>
+struct CheckSimdNegativeSignOperator {
+ template <class SimdAbi>
+ void operator()() {
+ constexpr std::size_t array_size = ex::simd_size_v<T, SimdAbi>;
+ ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
+ static_assert(noexcept(-origin_simd));
+ std::array<T, array_size> expected_value;
+ for (size_t i = 0; i < array_size; ++i)
+ expected_value[i] = -static_cast<T>(i);
+ assert_simd_values_equal<array_size>(-origin_simd, expected_value);
+ }
+};
+
+template <class T, class SimdAbi = ex::simd_abi::compatible<T>, class = void>
+struct has_bitwise_not_op : std::false_type {};
+
+template <class T, class SimdAbi>
+struct has_bitwise_not_op<T, SimdAbi, std::void_t<decltype(~std::declval<ex::simd<T, SimdAbi>>())>> : std::true_type {};
+
+template <class T, std::size_t>
+struct CheckSimdBitwiseNotTraits {
+ template <class SimdAbi>
+ void operator()() {
+ // This function shall not participate in overload resolution unless
+ // T is an integral type.
+ if constexpr (std::is_integral_v<T>)
+ static_assert(has_bitwise_not_op<T, SimdAbi>::value);
+ // T is not an integral type.
+ else
+ static_assert(!has_bitwise_not_op<T, SimdAbi>::value);
+ }
+};
+
+int main(int, char**) {
+ test_all_simd_abi<CheckSimdPrefixIncrementOperator>();
+ test_all_simd_abi<CheckSimdPostfixIncrementOperator>();
+ test_all_simd_abi<CheckSimdPrefixDecrementOperator>();
+ test_all_simd_abi<CheckSimdPostfixDecrementOperator>();
+ test_all_simd_abi<CheckSimdNegationOperator>();
+ types::for_each(types::integer_types(), TestAllSimdAbiFunctor<CheckSimdBitwiseNotOperator>());
+ test_all_simd_abi<CheckSimdPositiveSignOperator>();
+ test_all_simd_abi<CheckSimdNegativeSignOperator>();
+ test_all_simd_abi<CheckSimdBitwiseNotTraits>();
+ return 0;
+}
More information about the libcxx-commits
mailing list