1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP_EXPERIMENTAL___SIMD_REFERENCE_H
#define _LIBCPP_EXPERIMENTAL___SIMD_REFERENCE_H
#include <__config>
#include <__cstddef/size_t.h>
#include <__type_traits/enable_if.h>
#include <__type_traits/is_assignable.h>
#include <__type_traits/is_same.h>
#include <__utility/declval.h>
#include <__utility/forward.h>
#include <__utility/move.h>
#include <experimental/__simd/utility.h>
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
#if _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
_LIBCPP_BEGIN_NAMESPACE_EXPERIMENTAL
inline namespace parallelism_v2 {
template <class _Tp, class _Storage, class _Vp>
class __simd_reference {
template <class, class>
friend class simd;
template <class, class>
friend class simd_mask;
_Storage& __s_;
size_t __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>)
__s_.__set(__idx_, experimental::__set_all_bits<_Tp>(__v));
else
__s_.__set(__idx_, __v);
}
public:
using value_type = _Vp;
__simd_reference() = delete;
__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_};
}
// Note: This approach might not fully align with the specification,
// which might be a wording defect. (https://wg21.link/N4808 section 9.6.3)
template <class _Tp1, class _Storage1, class _Vp1>
friend void
swap(__simd_reference<_Tp1, _Storage1, _Vp1>&& __a, __simd_reference<_Tp1, _Storage1, _Vp1>&& __b) noexcept;
template <class _Tp1, class _Storage1, class _Vp1>
friend void swap(_Vp1& __a, __simd_reference<_Tp1, _Storage1, _Vp1>&& __b) noexcept;
template <class _Tp1, class _Storage1, class _Vp1>
friend void swap(__simd_reference<_Tp1, _Storage1, _Vp1>&& __a, _Vp1& __b) noexcept;
template <class _Up, class = decltype(std::declval<value_type&>() += std::declval<_Up>())>
_LIBCPP_HIDE_FROM_ABI __simd_reference operator+=(_Up&& __v) && noexcept {
__set(__get() + static_cast<value_type>(std::forward<_Up>(__v)));
return {__s_, __idx_};
}
template <class _Up, class = decltype(std::declval<value_type&>() -= std::declval<_Up>())>
_LIBCPP_HIDE_FROM_ABI __simd_reference operator-=(_Up&& __v) && noexcept {
__set(__get() - static_cast<value_type>(std::forward<_Up>(__v)));
return {__s_, __idx_};
}
template <class _Up, class = decltype(std::declval<value_type&>() *= std::declval<_Up>())>
_LIBCPP_HIDE_FROM_ABI __simd_reference operator*=(_Up&& __v) && noexcept {
__set(__get() * static_cast<value_type>(std::forward<_Up>(__v)));
return {__s_, __idx_};
}
template <class _Up, class = decltype(std::declval<value_type&>() /= std::declval<_Up>())>
_LIBCPP_HIDE_FROM_ABI __simd_reference operator/=(_Up&& __v) && noexcept {
__set(__get() / static_cast<value_type>(std::forward<_Up>(__v)));
return {__s_, __idx_};
}
template <class _Up, class = decltype(std::declval<value_type&>() %= std::declval<_Up>())>
_LIBCPP_HIDE_FROM_ABI __simd_reference operator%=(_Up&& __v) && noexcept {
__set(__get() % static_cast<value_type>(std::forward<_Up>(__v)));
return {__s_, __idx_};
}
template <class _Up, class = decltype(std::declval<value_type&>() &= std::declval<_Up>())>
_LIBCPP_HIDE_FROM_ABI __simd_reference operator&=(_Up&& __v) && noexcept {
__set(__get() & static_cast<value_type>(std::forward<_Up>(__v)));
return {__s_, __idx_};
}
template <class _Up, class = decltype(std::declval<value_type&>() |= std::declval<_Up>())>
_LIBCPP_HIDE_FROM_ABI __simd_reference operator|=(_Up&& __v) && noexcept {
__set(__get() | static_cast<value_type>(std::forward<_Up>(__v)));
return {__s_, __idx_};
}
template <class _Up, class = decltype(std::declval<value_type&>() ^= std::declval<_Up>())>
_LIBCPP_HIDE_FROM_ABI __simd_reference operator^=(_Up&& __v) && noexcept {
__set(__get() ^ static_cast<value_type>(std::forward<_Up>(__v)));
return {__s_, __idx_};
}
template <class _Up, class = decltype(std::declval<value_type&>() <<= std::declval<_Up>())>
_LIBCPP_HIDE_FROM_ABI __simd_reference operator<<=(_Up&& __v) && noexcept {
__set(__get() << static_cast<value_type>(std::forward<_Up>(__v)));
return {__s_, __idx_};
}
template <class _Up, class = decltype(std::declval<value_type&>() >>= std::declval<_Up>())>
_LIBCPP_HIDE_FROM_ABI __simd_reference operator>>=(_Up&& __v) && noexcept {
__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>
_LIBCPP_HIDE_FROM_ABI void
swap(__simd_reference<_Tp, _Storage, _Vp>&& __a, __simd_reference<_Tp, _Storage, _Vp>&& __b) noexcept {
_Vp __tmp(std::move(__a));
std::move(__a) = std::move(__b);
std::move(__b) = std::move(__tmp);
}
template <class _Tp, class _Storage, class _Vp>
_LIBCPP_HIDE_FROM_ABI void swap(_Vp& __a, __simd_reference<_Tp, _Storage, _Vp>&& __b) noexcept {
_Vp __tmp(std::move(__a));
__a = std::move(__b);
std::move(__b) = std::move(__tmp);
}
template <class _Tp, class _Storage, class _Vp>
_LIBCPP_HIDE_FROM_ABI void swap(__simd_reference<_Tp, _Storage, _Vp>&& __a, _Vp& __b) noexcept {
_Vp __tmp(std::move(__a));
std::move(__a) = std::move(__b);
__b = std::move(__tmp);
}
} // namespace parallelism_v2
_LIBCPP_END_NAMESPACE_EXPERIMENTAL
#endif // _LIBCPP_STD_VER >= 17 && defined(_LIBCPP_ENABLE_EXPERIMENTAL)
_LIBCPP_POP_MACROS
#endif // _LIBCPP_EXPERIMENTAL___SIMD_REFERENCE_H
|