Skip to content

[libc++] <experimental/simd> Add assignment operator of simd reference #70020

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 1 commit into from
Oct 26, 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
1 change: 1 addition & 0 deletions libcxx/docs/Status/ParallelismProjects.csv
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Section,Description,Dependencies,Assignee,Complete
| `[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.reference] <https://wg21.link/N4808>`_, "`Element references operator= <https://github.com/llvm/llvm-project/pull/70020>`_", 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|
Expand Down
7 changes: 7 additions & 0 deletions libcxx/include/experimental/__simd/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#ifndef _LIBCPP_EXPERIMENTAL___SIMD_REFERENCE_H
#define _LIBCPP_EXPERIMENTAL___SIMD_REFERENCE_H

#include <__type_traits/is_assignable.h>
#include <experimental/__simd/utility.h>

#if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
Expand Down Expand Up @@ -44,6 +45,12 @@ class __simd_reference {
__simd_reference(const __simd_reference&) = delete;

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

template <class _Up, enable_if_t<is_assignable_v<value_type&, _Up&&>, int> = 0>
_LIBCPP_HIDE_FROM_ABI __simd_reference operator=(_Up&& __v) && noexcept {
__set(static_cast<value_type>(std::forward<_Up>(__v)));
return {__s_, __idx_};
}
};

} // namespace parallelism_v2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//===----------------------------------------------------------------------===//
//
// 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]
// template<class U> reference=(U&& x) && noexcept;
//
// XFAIL: LIBCXX-AIX-FIXME

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

namespace ex = std::experimental::parallelism_v2;

template <class T, class SimdAbi>
struct CheckSimdReferenceAssignmentHelper {
template <class U>
void operator()() const {
if constexpr (std::is_assignable_v<T&, U&&>) {
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_cast<U>(i + 1)));
origin_simd[i] = static_cast<U>(i + 1);
assert(origin_simd[i] == static_cast<T>(std::forward<U>(i + 1)));
}
}
}
};

template <class T, class SimdAbi>
struct CheckMaskReferenceAssignmentHelper {
template <class U>
void operator()() const {
if constexpr (std::is_assignable_v<bool&, U&&>) {
ex::simd_mask<T, SimdAbi> origin_mask(true);
for (size_t i = 0; i < origin_mask.size(); ++i) {
static_assert(noexcept(origin_mask[i] = static_cast<U>(i + 1)));
origin_mask[i] = static_cast<U>(i % 2);
assert(origin_mask[i] == static_cast<T>(std::forward<U>(i % 2)));
}
}
}
};

template <class T, class SimdAbi>
struct CheckReferenceAssignmentTraitsHelper {
template <class U>
void operator()() const {
if constexpr (std::is_assignable_v<T&, U&&>)
static_assert(std::is_assignable_v<typename ex::simd<T, SimdAbi>::reference&&, U&&>);
else
static_assert(!std::is_assignable_v<typename ex::simd<T, SimdAbi>::reference&&, U&&>);

if constexpr (std::is_assignable_v<bool&, U&&>)
static_assert(std::is_assignable_v<typename ex::simd_mask<T, SimdAbi>::reference&&, U&&>);
else
static_assert(!std::is_assignable_v<typename ex::simd_mask<T, SimdAbi>::reference&&, U&&>);
}
};

template <class T, std::size_t>
struct CheckReferenceAssignment {
template <class SimdAbi>
void operator()() {
types::for_each(arithmetic_no_bool_types(), CheckSimdReferenceAssignmentHelper<T, SimdAbi>());
types::for_each(arithmetic_no_bool_types(), CheckMaskReferenceAssignmentHelper<T, SimdAbi>());

types::for_each(arithmetic_no_bool_types(), CheckReferenceAssignmentTraitsHelper<T, SimdAbi>());
}
};

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