-
Notifications
You must be signed in to change notification settings - Fork 1
/
mask_reductions.h
287 lines (244 loc) · 9.67 KB
/
mask_reductions.h
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* SPDX-License-Identifier: GPL-3.0-or-later WITH GCC-exception-3.1 */
/* Copyright © 2023-2024 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
* Matthias Kretz <[email protected]>
*/
#ifndef PROTOTYPE_MASK_REDUCTIONS_H_
#define PROTOTYPE_MASK_REDUCTIONS_H_
#include "simd_mask.h"
#include "simd_reductions.h"
#include "x86_detail.h"
namespace std
{
template <size_t _Bs, typename _Abi>
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr bool
all_of(const basic_simd_mask<_Bs, _Abi>& __k) noexcept
{
using _Kp = basic_simd_mask<_Bs, _Abi>;
constexpr __detail::_SimdSizeType __size = _Kp::size.value;
if constexpr (__size == 1)
return __data(__k);
else if (__builtin_is_constant_evaluated() or __k._M_is_constprop())
{
for (int __i = 0; __i < __size; ++__i)
if (!__k[__i])
return false;
return true;
}
else if constexpr (requires {_Abi::_MaskImpl::_S_all_of(__k);})
return _Abi::_MaskImpl::_S_all_of(__k);
else if constexpr (not _Kp::_S_is_bitmask and sizeof(__k) <= sizeof(int64_t))
{
const auto __as_int
= __builtin_bit_cast(__detail::__mask_integer_from<sizeof(__k)>, __data(__k));
if constexpr (__has_single_bit(__size))
return __as_int == -1;
else
return (__as_int & ((1ll << (__size * _Bs * __CHAR_BIT__)) - 1))
== (1ll << (__size * _Bs * __CHAR_BIT__)) - 1;
}
#if _GLIBCXX_SIMD_HAVE_NEON
else if constexpr (__pv2::__is_neon_abi<_Abi>())
{
using _Tp = __detail::__mask_integer_from<_Bs>;
static_assert(sizeof(__k) == 16);
const auto __kk
= __pv2::__vector_bitcast<char>(__data(__k))
| ~__pv2::__vector_bitcast<char>(_Abi::template _S_implicit_mask<_Tp>());
const auto __x = __pv2::__vector_bitcast<int64_t>(__kk);
return __x[0] + __x[1] == -2;
}
#endif
else
{
return [&]<__detail::_SimdSizeType... _Is> [[__gnu__::__always_inline__]]
(__detail::_SimdIndexSequence<_Is...>) {
return (... and (__k[_Is] != 0));
}(__detail::_MakeSimdIndexSequence<__size>());
}
}
template <size_t _Bs, typename _Abi>
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr bool
any_of(const basic_simd_mask<_Bs, _Abi>& __k) noexcept
{
using _Kp = basic_simd_mask<_Bs, _Abi>;
constexpr __detail::_SimdSizeType __size = _Kp::size.value;
if constexpr (__size == 1)
return __data(__k);
else if (__builtin_is_constant_evaluated() or __k._M_is_constprop())
{
for (int __i = 0; __i < __size; ++__i)
if (__k[__i])
return true;
return false;
}
else if constexpr (requires {_Abi::_MaskImpl::_S_any_of(__k);})
return _Abi::_MaskImpl::_S_any_of(__k);
else if constexpr (not _Kp::_S_is_bitmask and sizeof(__k) <= sizeof(int64_t))
return __builtin_bit_cast(__detail::__mask_integer_from<sizeof(__k)>, __data(__k)) != 0;
#if _GLIBCXX_SIMD_HAVE_NEON
else if constexpr (__pv2::__is_neon_abi<_Abi>())
{
static_assert(sizeof(__k) == 16);
const auto __kk = _Abi::_S_masked(__data(__k));
const auto __x = __pv2::__vector_bitcast<int64_t>(__kk);
return (__x[0] | __x[1]) != 0;
}
#endif
else
{
return [&]<__detail::_SimdSizeType... _Is> [[__gnu__::__always_inline__]]
(__detail::_SimdIndexSequence<_Is...>) {
return (... or (__k[_Is] != 0));
}(__detail::_MakeSimdIndexSequence<__size>());
}
}
template <size_t _Bs, typename _Abi>
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr bool
none_of(const basic_simd_mask<_Bs, _Abi>& __k) noexcept
{
using _Kp = basic_simd_mask<_Bs, _Abi>;
constexpr __detail::_SimdSizeType __size = _Kp::size.value;
if constexpr (__size == 1)
return !__data(__k);
else if (__builtin_is_constant_evaluated() or __k._M_is_constprop())
{
for (int __i = 0; __i < __k.size(); ++__i)
if (__k[__i])
return false;
return true;
}
else if constexpr (requires {_Abi::_MaskImpl::_S_none_of(__k);})
return _Abi::_MaskImpl::_S_none_of(__k);
else if constexpr (not _Kp::_S_is_bitmask and sizeof(__k) <= sizeof(int64_t))
return __builtin_bit_cast(__detail::__mask_integer_from<sizeof(__k)>,
_Abi::_S_masked(__data(__k))) == 0;
#if _GLIBCXX_SIMD_HAVE_NEON
else if constexpr (__pv2::__is_neon_abi<_Abi>())
{
static_assert(sizeof(__k) == 16);
const auto __kk = _Abi::_S_masked(__data(__k));
const auto __x = __vector_bitcast<int64_t>(__kk);
return (__x[0] | __x[1]) == 0;
}
#endif
else
{
return [&]<__detail::_SimdSizeType... _Is> [[__gnu__::__always_inline__]]
(__detail::_SimdIndexSequence<_Is...>) {
return (... and (__k[_Is] == 0));
}(__detail::_MakeSimdIndexSequence<__size>());
}
}
template <size_t _Bs, typename _Abi>
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr __detail::_SimdSizeType
reduce_count(const basic_simd_mask<_Bs, _Abi>& __k) noexcept
{
using _Kp = basic_simd_mask<_Bs, _Abi>;
constexpr __detail::_SimdSizeType __size = _Kp::size.value;
if constexpr (__size == 1)
return +__data(__k);
else if (__builtin_is_constant_evaluated() || __k._M_is_constprop())
{
int __r = [&]<__detail::_SimdSizeType... _Is>(__detail::_SimdIndexSequence<_Is...>) {
return (int(__k[_Is] != 0) + ...);
}(__detail::_MakeSimdIndexSequence<__size>());
if (__builtin_is_constant_evaluated() || __builtin_constant_p(__r))
return __r;
}
else if constexpr (requires {_Abi::_MaskImpl::_S_popcount(__k);})
return _Abi::_MaskImpl::_S_popcount(__k);
else
static_assert(-__size >= __finite_min_v<__detail::__mask_integer_from<_Bs>>);
return -reduce(-__k);
}
/**
* Precondition: any_of(__k) is true.
* on failure: ill-formed (constant expression) or returns unspecified value
*/
template <size_t _Bs, typename _Abi>
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr __detail::_SimdSizeType
reduce_min_index(const basic_simd_mask<_Bs, _Abi>& __k)
{
if (__builtin_is_constant_evaluated() and not any_of(__k))
__detail::__invoke_ub("reduce_max_index(x): precondition any_of(x) failed");
constexpr int __size = basic_simd_mask<_Bs, _Abi>::size.value;
if constexpr (__size == 1)
return 0;
else if (__builtin_is_constant_evaluated() or __k._M_is_constprop())
{
const int __r = [&] {
for (int __i = 0; __i < __size; ++__i)
if (__k[__i])
return __i;
return -1;
}();
if (__builtin_is_constant_evaluated() or __builtin_constant_p(__r))
return __r;
}
if constexpr (__size == 2)
return __k[0] ? 0 : 1;
else if constexpr (requires {_Abi::_MaskImpl::_S_find_first_set(__k);})
return _Abi::_MaskImpl::_S_find_first_set(__k);
else
return __detail::__lowest_bit(_Abi::_MaskImpl::_S_to_bits(__data(__k))._M_to_bits());
}
/**
* Precondition: any_of(__k) is true.
* on failure: ill-formed (constant expression) or returns unspecified value
*/
template <size_t _Bs, typename _Abi>
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr __detail::_SimdSizeType
reduce_max_index(const basic_simd_mask<_Bs, _Abi>& __k)
{
if (__builtin_is_constant_evaluated() and not any_of(__k))
__detail::__invoke_ub("reduce_max_index(x): precondition any_of(x) failed");
constexpr int __size = basic_simd_mask<_Bs, _Abi>::size.value;
if constexpr (__size == 1)
return 0;
else if (__builtin_is_constant_evaluated() or __k._M_is_constprop())
{
const int __r = [&] {
for (int __i = __size - 1; __i >= 0; --__i)
if (__k[__i])
return __i;
return -1;
}();
if (__builtin_is_constant_evaluated() or __builtin_constant_p(__r))
return __r;
}
if constexpr (__size == 2)
return __k[1] ? 1 : 0;
else if constexpr (requires {_Abi::_MaskImpl::_S_find_last_set(__k);})
return _Abi::_MaskImpl::_S_find_last_set(__k);
else
return __detail::__highest_bit(
_Abi::_MaskImpl::_S_to_bits(__data(__k))._M_sanitized()._M_to_bits());
}
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr bool
all_of(same_as<bool> auto __x) noexcept
{ return __x; }
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr bool
any_of(same_as<bool> auto __x) noexcept
{ return __x; }
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr bool
none_of(same_as<bool> auto __x) noexcept
{ return not __x; }
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr __detail::_SimdSizeType
reduce_count(same_as<bool> auto __x) noexcept
{ return static_cast<__detail::_SimdSizeType>(__x); }
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr __detail::_SimdSizeType
reduce_min_index(same_as<bool> auto __x) noexcept
{
if (__builtin_is_constant_evaluated() and !__x)
__detail::__invoke_ub("reduce_max_index(x): precondition any_of(x) failed");
return 0;
}
_GLIBCXX_SIMD_ALWAYS_INLINE constexpr __detail::_SimdSizeType
reduce_max_index(same_as<bool> auto __x) noexcept
{
if (__builtin_is_constant_evaluated() and !__x)
__detail::__invoke_ub("reduce_max_index(x): precondition any_of(x) failed");
return 0;
}
}
#endif // PROTOTYPE_MASK_REDUCTIONS_H_