Skip to content

[libc++] <experimental/simd> Add operator value_type() of simd reference #68960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions libcxx/docs/Status/ParallelismProjects.csv
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ Section,Description,Dependencies,Assignee,Complete
| `[parallel.simd.traits] <https://wg21.link/N4808>`_, "simd type traits rebind_simd", None, Yin Zhang, |In Progress|
| `[parallel.simd.traits] <https://wg21.link/N4808>`_, "simd type traits resize_simd", None, Yin Zhang, |In Progress|
| `[parallel.simd.whereexpr] <https://wg21.link/N4808>`_, "Where expression class templates", None, Yin Zhang, |In Progress|
| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references operator value_type() <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`Class template simd declaration and alias <https://reviews.llvm.org/D144362>`_", [parallel.simd.abi], Yin Zhang, |Complete|
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd<>::size() <https://reviews.llvm.org/D144363>`_", [parallel.simd.traits] simd_size[_v], Yin Zhang, |Complete|
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd broadcast constructor <https://reviews.llvm.org/D156225>`_", None, Yin Zhang, |Complete|
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd generate constructor <https://reviews.llvm.org/D159442>`_", None, Yin Zhang, |Complete|
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "`simd subscript operators <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
| `[parallel.simd.class] <https://wg21.link/N4808>`_, "Class template simd implementation", None, Yin Zhang, |In Progress|
| `[parallel.simd.nonmembers] <https://wg21.link/N4808>`_, "simd non-member operations", None, Yin Zhang, |In Progress|
| `[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|
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask<>::size() <https://reviews.llvm.org/D144363>`_", [parallel.simd.class] simd<>::size(), Yin Zhang, |Complete|
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask broadcast constructor <https://reviews.llvm.org/D156225>`_", None, Yin Zhang, |Complete|
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "`simd_mask subscript operators <https://github.com/llvm/llvm-project/pull/68960>`_", None, Yin Zhang, |Complete|
| `[parallel.simd.mask.class] <https://wg21.link/N4808>`_, "Class template simd_mask implementation", None, Yin Zhang, |In Progress|
| `[parallel.simd.mask.nonmembers] <https://wg21.link/N4808>`_, "simd_mask non-member operations", None, Yin Zhang, |In Progress|
6 changes: 5 additions & 1 deletion libcxx/include/experimental/__simd/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class __simd_reference {
_Storage& __s_;
size_t __idx_;

_LIBCPP_HIDE_FROM_ABI _Vp __get() const { return __s_.__get(__idx_); }
_LIBCPP_HIDE_FROM_ABI __simd_reference(_Storage& __s, size_t __idx) : __s_(__s), __idx_(__idx) {}

_LIBCPP_HIDE_FROM_ABI _Vp __get() const noexcept { return __s_.__get(__idx_); }

_LIBCPP_HIDE_FROM_ABI void __set(_Vp __v) {
if constexpr (is_same_v<_Vp, bool>)
Expand All @@ -40,6 +42,8 @@ class __simd_reference {

__simd_reference() = delete;
__simd_reference(const __simd_reference&) = delete;

_LIBCPP_HIDE_FROM_ABI operator value_type() const noexcept { return __get(); }
};

} // namespace parallelism_v2
Expand Down
7 changes: 4 additions & 3 deletions libcxx/include/experimental/__simd/simd.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ class simd {

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

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

template <class _Tp>
Expand Down
4 changes: 2 additions & 2 deletions libcxx/include/experimental/__simd/simd_mask.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class simd_mask {
_LIBCPP_HIDE_FROM_ABI explicit simd_mask(value_type __v) noexcept : __s_(_Impl::__broadcast(__v)) {}

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

template <class _Tp>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14

// <experimental/simd>
//
// [simd.class]
// reference operator[](size_t i);
// value_type operator[](size_t i) const;

#include "../test_utils.h"
#include <experimental/simd>

namespace ex = std::experimental::parallelism_v2;

template <class T, std::size_t>
struct CheckSimdReferenceSubscr {
template <class SimdAbi>
void operator()() {
ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
for (size_t i = 0; i < origin_simd.size(); ++i) {
static_assert(noexcept(origin_simd[i]));
static_assert(std::is_same_v<typename ex::simd<T, SimdAbi>::reference, decltype(origin_simd[i])>);
assert(origin_simd[i] == static_cast<T>(i));
}
}
};

template <class T, std::size_t>
struct CheckSimdValueTypeSubscr {
template <class SimdAbi>
void operator()() {
const ex::simd<T, SimdAbi> origin_simd([](T i) { return i; });
for (size_t i = 0; i < origin_simd.size(); ++i) {
static_assert(noexcept(origin_simd[i]));
static_assert(std::is_same_v<T, decltype(origin_simd[i])>);
assert(origin_simd[i] == static_cast<T>(i));
}
}
};

int main(int, char**) {
test_all_simd_abi<CheckSimdReferenceSubscr>();
test_all_simd_abi<CheckSimdValueTypeSubscr>();
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14

// <experimental/simd>
//
// [simd.class]
// reference operator[](size_t i);
// value_type operator[](size_t i) const;

#include "../test_utils.h"
#include <experimental/simd>

namespace ex = std::experimental::parallelism_v2;

template <class T, std::size_t>
struct CheckSimdMaskReferenceSubscr {
template <class SimdAbi>
void operator()() {
ex::simd_mask<T, SimdAbi> origin_simd_mask(true);
for (size_t i = 0; i < origin_simd_mask.size(); ++i) {
static_assert(noexcept(origin_simd_mask[i]));
static_assert(std::is_same_v<typename ex::simd_mask<T, SimdAbi>::reference, decltype(origin_simd_mask[i])>);
assert(origin_simd_mask[i] == true);
}
}
};

template <class T, std::size_t>
struct CheckSimdMaskValueTypeSubscr {
template <class SimdAbi>
void operator()() {
const ex::simd_mask<T, SimdAbi> origin_simd_mask(true);
for (size_t i = 0; i < origin_simd_mask.size(); ++i) {
static_assert(noexcept(origin_simd_mask[i]));
static_assert(std::is_same_v<bool, decltype(origin_simd_mask[i])>);
assert(origin_simd_mask[i] == true);
}
}
};

int main(int, char**) {
test_all_simd_abi<CheckSimdMaskReferenceSubscr>();
test_all_simd_abi<CheckSimdMaskValueTypeSubscr>();
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: c++03, c++11, c++14

// <experimental/simd>
//
// [simd.reference]
// operator value_type() const noexcept;

#include "../test_utils.h"
#include <experimental/simd>

namespace ex = std::experimental::parallelism_v2;

template <class T, std::size_t>
struct CheckSimdReferenceValueType {
template <class SimdAbi>
void operator()() {
ex::simd<T, SimdAbi> origin_simd([](T i) { return static_cast<T>(i); });
for (size_t i = 0; i < origin_simd.size(); ++i) {
static_assert(noexcept(T(origin_simd[i])));
assert(T(origin_simd[i]) == static_cast<T>(i));
}
}
};

template <class T, std::size_t>
struct CheckMaskReferenceValueType {
template <class SimdAbi>
void operator()() {
ex::simd_mask<T, SimdAbi> origin_simd_mask(true);
for (size_t i = 0; i < origin_simd_mask.size(); ++i) {
static_assert(noexcept(bool(origin_simd_mask[i])));
assert(bool(origin_simd_mask[i]) == true);
}
}
};

int main(int, char**) {
test_all_simd_abi<CheckSimdReferenceValueType>();
test_all_simd_abi<CheckMaskReferenceValueType>();
return 0;
}