Skip to content

Commit aec79c0

Browse files
committed
[libc++] <experimental/simd> Add ++/-- operators for simd reference
1 parent 1919db9 commit aec79c0

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

libcxx/docs/Status/ParallelismProjects.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Section,Description,Dependencies,Assignee,Complete
1818
| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references operator= <https://github.com/llvm/llvm-project/pull/70020>`_", None, Yin Zhang, |Complete|
1919
| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references swap functions <https://github.com/llvm/llvm-project/pull/86478>`_", None, Yin Zhang, |Complete|
2020
| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references compound assignment operators <https://github.com/llvm/llvm-project/pull/86761>`_", None, Yin Zhang, |Complete|
21+
| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references unary operators ++/-- <https://github.com/llvm/llvm-project/pull/88091>`_", None, Yin Zhang, |Complete|
2122
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`Class template simd declaration and alias <https://reviews.llvm.org/D144362>`_", [parallel.simd.abi], Yin Zhang, |Complete|
2223
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd<>::size() <https://reviews.llvm.org/D144363>`_", [parallel.simd.traits] simd_size[_v], Yin Zhang, |Complete|
2324
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd default constructor <https://github.com/llvm/llvm-project/pull/70424>`_", None, Yin Zhang, |Complete|

libcxx/include/experimental/__simd/reference.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,28 @@ class __simd_reference {
132132
__set(__get() >> static_cast<value_type>(std::forward<_Up>(__v)));
133133
return {__s_, __idx_};
134134
}
135+
136+
__simd_reference _LIBCPP_HIDE_FROM_ABI operator++() && noexcept {
137+
__set(__get() + 1);
138+
return {__s_, __idx_};
139+
}
140+
141+
value_type _LIBCPP_HIDE_FROM_ABI operator++(int) && noexcept {
142+
auto __r = __get();
143+
__set(__get() + 1);
144+
return __r;
145+
}
146+
147+
__simd_reference _LIBCPP_HIDE_FROM_ABI operator--() && noexcept {
148+
__set(__get() - 1);
149+
return {__s_, __idx_};
150+
}
151+
152+
value_type _LIBCPP_HIDE_FROM_ABI operator--(int) && noexcept {
153+
auto __r = __get();
154+
__set(__get() - 1);
155+
return __r;
156+
}
135157
};
136158

137159
template <class _Tp, class _Storage, class _Vp>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14
10+
11+
// <experimental/simd>
12+
//
13+
// [simd.reference]
14+
// reference operator++() && noexcept;
15+
// value_type operator++(int) && noexcept;
16+
// reference operator--() && noexcept;
17+
// value_type operator--(int) && noexcept;
18+
19+
#include "../test_utils.h"
20+
#include <experimental/simd>
21+
22+
namespace ex = std::experimental::parallelism_v2;
23+
24+
template <class T, std::size_t>
25+
struct CheckSimdReferenceUnaryOperators {
26+
template <class SimdAbi>
27+
void operator()() const {
28+
ex::simd<T, SimdAbi> origin_simd(static_cast<T>(3));
29+
static_assert(noexcept(++origin_simd[0]));
30+
assert(((T)(++origin_simd[0]) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(4)));
31+
static_assert(noexcept(origin_simd[0]++));
32+
assert(((T)(origin_simd[0]++) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(5)));
33+
static_assert(noexcept(--origin_simd[0]));
34+
assert(((T)(--origin_simd[0]) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(4)));
35+
static_assert(noexcept(origin_simd[0]--));
36+
assert(((T)(origin_simd[0]--) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(3)));
37+
}
38+
};
39+
40+
int main(int, char**) {
41+
test_all_simd_abi<CheckSimdReferenceUnaryOperators>();
42+
return 0;
43+
}

0 commit comments

Comments
 (0)