Skip to content

[libc++] <experimental/simd> Add ++/-- operators for simd reference #88091

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
Aug 14, 2024
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 @@ -18,6 +18,7 @@ Section,Description,Dependencies,Assignee,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.reference] <https://wg21.link/N4808>`_, "`Element references swap functions <https://github.com/llvm/llvm-project/pull/86478>`_", None, Yin Zhang, |Complete|
| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references compound assignment operators <https://github.com/llvm/llvm-project/pull/86761>`_", None, Yin Zhang, |Complete|
| `[parallel.simd.reference] <https://wg21.link/N4808>`_, "`Element references unary operators ++/-- <https://github.com/llvm/llvm-project/pull/88091>`_", 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 default constructor <https://github.com/llvm/llvm-project/pull/70424>`_", None, Yin Zhang, |Complete|
Expand Down
25 changes: 25 additions & 0 deletions libcxx/include/experimental/__simd/reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,31 @@ class __simd_reference {
__set(__get() >> static_cast<value_type>(std::forward<_Up>(__v)));
return {__s_, __idx_};
}

// Note: All legal vectorizable types support operator++/--.
// There doesn't seem to be a way to trigger the constraint.
// Therefore, no SFINAE check is added here.
__simd_reference _LIBCPP_HIDE_FROM_ABI operator++() && noexcept {
__set(__get() + 1);
return {__s_, __idx_};
}

value_type _LIBCPP_HIDE_FROM_ABI operator++(int) && noexcept {
auto __r = __get();
__set(__get() + 1);
return __r;
}

__simd_reference _LIBCPP_HIDE_FROM_ABI operator--() && noexcept {
__set(__get() - 1);
return {__s_, __idx_};
}

value_type _LIBCPP_HIDE_FROM_ABI operator--(int) && noexcept {
auto __r = __get();
__set(__get() - 1);
return __r;
}
};

template <class _Tp, class _Storage, class _Vp>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===----------------------------------------------------------------------===//
//
// 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]
// reference operator++() && noexcept;
// value_type operator++(int) && noexcept;
// reference operator--() && noexcept;
// value_type operator--(int) && noexcept;

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

namespace ex = std::experimental::parallelism_v2;

template <class T, std::size_t>
struct CheckSimdReferenceUnaryOperators {
template <class SimdAbi>
void operator()() const {
ex::simd<T, SimdAbi> origin_simd(static_cast<T>(3));
static_assert(noexcept(++origin_simd[0]));
assert(((T)(++origin_simd[0]) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(4)));
static_assert(noexcept(origin_simd[0]++));
assert(((T)(origin_simd[0]++) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(5)));
static_assert(noexcept(--origin_simd[0]));
assert(((T)(--origin_simd[0]) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(4)));
static_assert(noexcept(origin_simd[0]--));
assert(((T)(origin_simd[0]--) == static_cast<T>(4)) && ((T)origin_simd[0] == static_cast<T>(3)));
}
};

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