Skip to content

Commit 77fc6ef

Browse files
committed
[libc++] <experimental/simd> Add operator value_type() of simd reference
1 parent 5c3ed39 commit 77fc6ef

File tree

5 files changed

+56
-4
lines changed

5 files changed

+56
-4
lines changed

libcxx/docs/Status/ParallelismProjects.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Section,Description,Dependencies,Assignee,Complete
1414
| `[parallel.simd.traits] <https://wg21.link/N4808>`_, "simd type traits rebind_simd", None, Yin Zhang, |In Progress|
1515
| `[parallel.simd.traits] <https://wg21.link/N4808>`_, "simd type traits resize_simd", None, Yin Zhang, |In Progress|
1616
| `[parallel.simd.whereexpr] <https://wg21.link/N4808>`_, "Where expression class templates", None, Yin Zhang, |In Progress|
17+
| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references operator value_type() <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
1718
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`Class template simd declaration and alias <https://reviews.llvm.org/D144362>`_", [parallel.simd.abi], Yin Zhang, |Complete|
1819
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd<>::size() <https://reviews.llvm.org/D144363>`_", [parallel.simd.traits] simd_size[_v], Yin Zhang, |Complete|
1920
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd broadcast constructor <https://reviews.llvm.org/D156225>`_", None, Yin Zhang, |Complete|

libcxx/include/experimental/__simd/reference.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class __simd_reference {
2626
_Storage& __s_;
2727
size_t __idx_;
2828

29-
_LIBCPP_HIDE_FROM_ABI _Vp __get() const { return __s_.__get(__idx_); }
29+
_LIBCPP_HIDE_FROM_ABI _Vp __get() const noexcept { return __s_.__get(__idx_); }
3030

3131
_LIBCPP_HIDE_FROM_ABI void __set(_Vp __v) {
3232
if constexpr (is_same_v<_Vp, bool>)
@@ -40,6 +40,8 @@ class __simd_reference {
4040

4141
__simd_reference() = delete;
4242
__simd_reference(const __simd_reference&) = delete;
43+
44+
_LIBCPP_HIDE_FROM_ABI operator value_type() const noexcept { return __get(); }
4345
};
4446

4547
} // namespace parallelism_v2

libcxx/include/experimental/__simd/simd.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +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) : __s_(_Impl::__generate(std::forward<_Generator>(__g))) {}
49+
explicit _LIBCPP_HIDE_FROM_ABI simd(_Generator&& __g) noexcept : __s_(_Impl::__generate(std::forward<_Generator>(__g))) {}
5050

5151
// scalar access [simd.subscr]
5252
// Add operator[] temporarily to test braodcast. Add test for it in later patch.
53-
_LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const { return __s_.__get(__i); }
53+
_LIBCPP_HIDE_FROM_ABI value_type operator[](size_t __i) const noexcept { return __s_.__get(__i); }
5454
};
5555

5656
template <class _Tp>

libcxx/include/experimental/__simd/simd_mask.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class simd_mask {
4343

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

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

0 commit comments

Comments
 (0)