Skip to content

Commit b119aed

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

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

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)