Skip to content

Commit c507bb6

Browse files
committed
[libc++] <experimental/simd> Add ++/-- operators for simd reference
1 parent 3a146d5 commit c507bb6

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

libcxx/docs/Status/ParallelismProjects.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Section,Description,Dependencies,Assignee,Complete
1616
| `[parallel.simd.whereexpr] <https://wg21.link/N4808>`_, "Where expression class templates", None, Yin Zhang, |In Progress|
1717
| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references operator value_type() <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
1818
| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references operator= <https://github.com/llvm/llvm-project/pull/70020>`_", None, Yin Zhang, |Complete|
19+
| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references unary operators ++/-- <https://github.com/llvm/llvm-project/pull/88091>`_", None, Yin Zhang, |Complete|
1920
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`Class template simd declaration and alias <https://reviews.llvm.org/D144362>`_", [parallel.simd.abi], Yin Zhang, |Complete|
2021
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd<>::size() <https://reviews.llvm.org/D144363>`_", [parallel.simd.traits] simd_size[_v], Yin Zhang, |Complete|
2122
| `[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: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <__type_traits/is_assignable.h>
1414
#include <__type_traits/is_same.h>
15+
#include <__utility/declval.h>
1516
#include <__utility/forward.h>
1617
#include <cstddef>
1718
#include <experimental/__config>
@@ -55,6 +56,32 @@ class __simd_reference {
5556
__set(static_cast<value_type>(std::forward<_Up>(__v)));
5657
return {__s_, __idx_};
5758
}
59+
60+
template <class _Up = value_type, class = decltype(std::declval<_Up>() + _Up{1})>
61+
__simd_reference _LIBCPP_HIDE_FROM_ABI operator++() && noexcept {
62+
__set(__get() + 1);
63+
return {__s_, __idx_};
64+
}
65+
66+
template <class _Up = value_type, class = decltype(std::declval<_Up>() + _Up{1})>
67+
value_type _LIBCPP_HIDE_FROM_ABI operator++(int) && noexcept {
68+
auto __r = __get();
69+
__set(__get() + 1);
70+
return __r;
71+
}
72+
73+
template <class _Up = value_type, class = decltype(std::declval<_Up>() - _Up{1})>
74+
__simd_reference _LIBCPP_HIDE_FROM_ABI operator--() && noexcept {
75+
__set(__get() - 1);
76+
return {__s_, __idx_};
77+
}
78+
79+
template <class _Up = value_type, class = decltype(std::declval<_Up>() - _Up{1})>
80+
value_type _LIBCPP_HIDE_FROM_ABI operator--(int) && noexcept {
81+
auto __r = __get();
82+
__set(__get() - 1);
83+
return __r;
84+
}
5885
};
5986

6087
} // namespace parallelism_v2
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)