Skip to content

Commit 19880cd

Browse files
committed
[libcxx] <experimental/simd> Add subscript operators of class simd/simd_mask
1 parent 77fc6ef commit 19880cd

File tree

6 files changed

+112
-3
lines changed

6 files changed

+112
-3
lines changed

libcxx/docs/Status/ParallelismProjects.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,12 @@ Section,Description,Dependencies,Assignee,Complete
1919
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd<>::size() <https://reviews.llvm.org/D144363>`_", [parallel.simd.traits] simd_size[_v], Yin Zhang, |Complete|
2020
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd broadcast constructor <https://reviews.llvm.org/D156225>`_", None, Yin Zhang, |Complete|
2121
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd generate constructor <https://reviews.llvm.org/D159442>`_", None, Yin Zhang, |Complete|
22+
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd subscript operators <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
2223
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "Class template simd implementation", None, Yin Zhang, |In Progress|
2324
| `[parallel.simd.nonmembers] <https://wg21.link/N4808>`_, "simd non-member operations", None, Yin Zhang, |In Progress|
2425
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`Class template simd_mask declaration and alias <https://reviews.llvm.org/D144362>`_", [parallel.simd.abi], Yin Zhang, |Complete|
2526
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask<>::size() <https://reviews.llvm.org/D144363>`_", [parallel.simd.class] simd<>::size(), Yin Zhang, |Complete|
2627
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask broadcast constructor <https://reviews.llvm.org/D156225>`_", None, Yin Zhang, |Complete|
28+
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask subscript operators <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
2729
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "Class template simd_mask implementation", None, Yin Zhang, |In Progress|
2830
| `[parallel.simd.mask.nonmembers] <https://wg21.link/N4808>`_, "simd_mask non-member operations", None, Yin Zhang, |In Progress|

libcxx/include/experimental/__simd/reference.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ class __simd_reference {
2626
_Storage& __s_;
2727
size_t __idx_;
2828

29+
_LIBCPP_HIDE_FROM_ABI __simd_reference(_Storage& __s, size_t __idx) : __s_(__s), __idx_(__idx) {}
30+
2931
_LIBCPP_HIDE_FROM_ABI _Vp __get() const noexcept { return __s_.__get(__idx_); }
3032

3133
_LIBCPP_HIDE_FROM_ABI void __set(_Vp __v) {

libcxx/include/experimental/__simd/simd.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,11 @@ class simd {
4646

4747
// generator constructor
4848
template <class _Generator, enable_if_t<__can_generate_v<value_type, _Generator, size()>, int> = 0>
49-
explicit _LIBCPP_HIDE_FROM_ABI simd(_Generator&& __g) noexcept : __s_(_Impl::__generate(std::forward<_Generator>(__g))) {}
49+
explicit _LIBCPP_HIDE_FROM_ABI simd(_Generator&& __g) noexcept
50+
: __s_(_Impl::__generate(std::forward<_Generator>(__g))) {}
5051

5152
// scalar access [simd.subscr]
52-
// Add operator[] temporarily to test braodcast. Add test for it in later patch.
53+
_LIBCPP_HIDE_FROM_ABI reference operator[](size_t __i) noexcept { return reference(__s_, __i); }
5354
_LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
5455
};
5556

libcxx/include/experimental/__simd/simd_mask.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class simd_mask {
4242
_LIBCPP_HIDE_FROM_ABI explicit simd_mask(value_type __v) noexcept : __s_(_Impl::__broadcast(__v)) {}
4343

4444
// scalar access [simd.mask.subscr]
45-
// Add operator[] temporarily to test braodcast. Add test for it in later patch.
45+
_LIBCPP_HIDE_FROM_ABI reference operator[](size_t __i) noexcept { return reference(__s_, __i); }
4646
_LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
4747
};
4848

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.class]
14+
// reference operator[](size_t i);
15+
// value_type operator[](size_t i) const;
16+
17+
#include "../test_utils.h"
18+
#include <experimental/simd>
19+
20+
namespace ex = std::experimental::parallelism_v2;
21+
22+
template <class T, std::size_t>
23+
struct CheckSimdReferenceSubscr {
24+
template <class SimdAbi>
25+
void operator()() {
26+
ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
27+
for (size_t i = 0; i < origin_simd.size(); ++i) {
28+
static_assert(noexcept(origin_simd[i]));
29+
static_assert(std::is_same_v<typename ex::simd<T, SimdAbi>::reference, decltype(origin_simd[i])>);
30+
assert(origin_simd[i] == static_cast<T>(i));
31+
}
32+
}
33+
};
34+
35+
template <class T, std::size_t>
36+
struct CheckSimdValueTypeSubscr {
37+
template <class SimdAbi>
38+
void operator()() {
39+
const ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
40+
for (size_t i = 0; i < origin_simd.size(); ++i) {
41+
static_assert(noexcept(origin_simd[i]));
42+
static_assert(std::is_same_v<T, decltype(origin_simd[i])>);
43+
assert(origin_simd[i] == static_cast<T>(i));
44+
}
45+
}
46+
};
47+
48+
int main(int, char**) {
49+
test_all_simd_abi<CheckSimdReferenceSubscr>();
50+
test_all_simd_abi<CheckSimdValueTypeSubscr>();
51+
return 0;
52+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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.class]
14+
// reference operator[](size_t i);
15+
// value_type operator[](size_t i) const;
16+
17+
#include "../test_utils.h"
18+
#include <experimental/simd>
19+
20+
namespace ex = std::experimental::parallelism_v2;
21+
22+
template <class T, std::size_t>
23+
struct CheckSimdMaskReferenceSubscr {
24+
template <class SimdAbi>
25+
void operator()() {
26+
ex::simd_mask<T, SimdAbi> origin_simd_mask(true);
27+
for (size_t i = 0; i < origin_simd_mask.size(); ++i) {
28+
static_assert(noexcept(origin_simd_mask[i]));
29+
static_assert(std::is_same_v<typename ex::simd_mask<T, SimdAbi>::reference, decltype(origin_simd_mask[i])>);
30+
assert(origin_simd_mask[i] == true);
31+
}
32+
}
33+
};
34+
35+
template <class T, std::size_t>
36+
struct CheckSimdMaskValueTypeSubscr {
37+
template <class SimdAbi>
38+
void operator()() {
39+
const ex::simd_mask<T, SimdAbi> origin_simd_mask(true);
40+
for (size_t i = 0; i < origin_simd_mask.size(); ++i) {
41+
static_assert(noexcept(origin_simd_mask[i]));
42+
static_assert(std::is_same_v<bool, decltype(origin_simd_mask[i])>);
43+
assert(origin_simd_mask[i] == true);
44+
}
45+
}
46+
};
47+
48+
int main(int, char**) {
49+
test_all_simd_abi<CheckSimdMaskReferenceSubscr>();
50+
test_all_simd_abi<CheckSimdMaskValueTypeSubscr>();
51+
return 0;
52+
}

0 commit comments

Comments
 (0)