Intel xvec
Experimental C++ simd class library
Loading...
Searching...
No Matches
mask.hpp
1//===----------------------------------------------------------------------===//
2//
3// Copyright (C) 2021 Intel Corporation
4//
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include <xvec/detail/config.hpp>
12
13#include <bitset>
14#include <type_traits>
15#include <cstddef>
16#include <concepts>
17#include <functional>
18
19#include <iostream>
20
21#include <xvec/detail/utilities.hpp>
22
23namespace _XVEC_NAMESPACE::simd
24{
25
32
39
46
53
60
67
74
81
88
95
104
119template<std::size_t _Bytes, typename _Abi>
121{
122public:
123
127
130 using builtin_type = typename traits::builtin_type;
131
134
136 using abi_type = _Abi;
137
139 using value_type = bool;
140
143
146
148 static constexpr auto size = size_constant<_Abi::num_elements>{};
149
151
152private:
153
156 builtin_type mask;
157
158public:
159
162
164 constexpr basic_mask() = default;
165
167 constexpr basic_mask(const basic_mask&) = default;
168
170 constexpr basic_mask(basic_mask&&) noexcept = default;
171
173 constexpr basic_mask& operator=(const basic_mask&) = default;
174
176 constexpr basic_mask& operator=(basic_mask&&) noexcept = default;
177
179
180
181
183
185
186
187
190 constexpr explicit basic_mask(std::same_as<value_type> auto b) noexcept :
191 mask([b] { if (b) return !basic_mask(); else return basic_mask(); }()) {}
192
197 template <std::unsigned_integral T> requires (!std::same_as<T, value_type>)
198 constexpr explicit basic_mask(T m) noexcept
199 : basic_mask([m]() {
200 if (std::is_constant_evaluated())
201 return basic_mask([m](auto idx) { return (idx < std::numeric_limits<T>::digits) ? ((m >> idx) & 1) == 1 : 0; });
202 else
203 return detail::generate_mask_from_unsigned<basic_mask>(target, m);
204 }()) {}
205
211 template<std::size_t _UBytes, typename _UAbi>
212 requires (basic_mask<_UBytes, _UAbi>::size() == size())
213 explicit constexpr basic_mask(const basic_mask<_UBytes, _UAbi>& m) : mask(detail::convert_mask<basic_mask>(target, m)) {}
214
217 constexpr basic_mask(std::invocable<std::integral_constant<simd_size_type, 0>> auto fn) : basic_mask(detail::generate_mask<basic_mask>(target, fn)) {}
218
220
223 static constexpr basic_mask __mask_from_count(simd_size_type _n)
224 {
225 if (std::is_constant_evaluated())
226 // get_n_bit_mask may have intrinsic (non-constexpr) calls so use a generator for the constexpr case.
227 return basic_mask([&](simd_size_type i) { return i < _n; });
228 else
229 // x86 has special instructions which can accelerate mask generation.
230 return detail::get_n_bit_mask(target, basic_mask(), _n);
231 }
232
236 // :TODO: Not efficient if the bitset isn't constexpr. Need a better way to deal with that eventually.
237 template<std::same_as<std::bitset<size()>> T>
238 constexpr basic_mask(const T& b) noexcept : basic_mask([&](simd_size_type idx) { return b[idx]; }) {}
239
242
244 constexpr iterator begin() noexcept { return {*this, 0}; }
245
247 constexpr const_iterator begin() const noexcept { return {*this, 0}; }
248
250 constexpr const_iterator cbegin() const noexcept { return {*this, 0}; }
251
253 constexpr std::default_sentinel_t end() const noexcept { return {}; }
254
256 constexpr std::default_sentinel_t cend() const noexcept { return {}; }
257
259
262 // :TODO: Maybe to_builtin is sufficient?
263 constexpr explicit operator builtin_type() { return mask; }
264
269 constexpr operator typename traits::signed_vec_for_mask() const { return detail::mask_to_vec<1, typename traits::element_signed_container_type>(target, *this); }
270
273
275 constexpr auto to_builtin() const { return mask; }
276
281 static constexpr basic_mask from_builtin(const builtin_type& m) { basic_mask bm; bm.mask = traits::truncate_to_size(m); return bm; }
282
284 constexpr auto to_register() const noexcept { return traits::to_register(mask); }
285
287
290
292 constexpr std::bitset<size()> to_bitset() const noexcept { return detail::mask_to_bitset(target, *this); }
293
296 constexpr unsigned long long to_ullong() const {
297 if (std::is_constant_evaluated())
298 {
299 constexpr auto numOutputBits = std::numeric_limits<unsigned long long>::digits;
300
301 if (any_of(*this) && reduce_max_index(*this) >= numOutputBits)
302 throw "Mask has set bits which won't be copied into the output ullong";
303
304 unsigned long long result = 0;
305 for (simd_size_type i = 0; i < std::min(size(), numOutputBits); ++i)
306 result |= static_cast<unsigned long long>((*this)[i]) << i;
307 return result;
308 }
309 else
310 return detail::mask_to_ullong(target, *this);
311 }
312
314
317
319 constexpr basic_mask operator!() const noexcept { return detail::builtin_operator(target, *this, std::bit_not{}); }
320
322 constexpr typename traits::signed_vec_for_mask operator+() const noexcept { return detail::mask_to_vec<1, typename traits::element_signed_container_type>(target, *this); }
323
325 constexpr typename traits::signed_vec_for_mask operator-() const noexcept { return detail::mask_to_vec<-1, typename traits::element_signed_container_type>(target, *this); }
326
328 constexpr typename traits::signed_vec_for_mask operator~() const noexcept { return ~(+*this); }
329
331
334
338 constexpr auto operator[](const vec_integral auto& indexes) const { return permute(*this, indexes); }
339
342 constexpr bool operator[](simd_size_type pos) const& { return detail::subscript(target, *this, pos); }
343
345
348
350 friend constexpr basic_mask operator&&(const basic_mask& lhs, const basic_mask& rhs) noexcept
351 { return detail::builtin_operator(target, lhs, rhs, std::bit_and{}); }
352
354 friend constexpr basic_mask operator||(const basic_mask& lhs, const basic_mask& rhs) noexcept
355 { return detail::builtin_operator(target, lhs, rhs, std::bit_or{}); }
356
358 friend constexpr basic_mask operator&(const basic_mask& lhs, const basic_mask& rhs) noexcept
359 { return detail::builtin_operator(target, lhs, rhs, std::bit_and{}); }
360
362 friend constexpr basic_mask operator|(const basic_mask& lhs, const basic_mask& rhs) noexcept
363 { return detail::builtin_operator(target, lhs, rhs, std::bit_or{}); }
364
366 friend constexpr basic_mask operator^(const basic_mask& lhs, const basic_mask& rhs) noexcept
367 { return detail::builtin_operator(target, lhs, rhs, std::bit_xor{}); }
368
370
373
375 constexpr basic_mask& operator&=(const basic_mask& rhs) noexcept { *this = *this & rhs; return *this; }
376
378 constexpr basic_mask& operator|=(const basic_mask& rhs) noexcept { *this = *this | rhs; return *this; }
379
381 constexpr basic_mask& operator^=(const basic_mask& rhs) noexcept { *this = *this ^ rhs; return *this; }
382
384
387
389 friend constexpr basic_mask operator==(const basic_mask& lhs, const basic_mask& rhs) noexcept
390 { return !(lhs ^ rhs); }
391
393 friend constexpr basic_mask operator!=(const basic_mask& lhs, const basic_mask& rhs) noexcept
394 { return lhs ^ rhs; }
395
397 friend constexpr basic_mask operator<(const basic_mask& lhs, const basic_mask& rhs) noexcept
398 { return !lhs && rhs; }
399
401 friend constexpr basic_mask operator<=(const basic_mask& lhs, const basic_mask& rhs) noexcept
402 { return !lhs || rhs; }
403
405 friend constexpr basic_mask operator>(const basic_mask& lhs, const basic_mask& rhs) noexcept
406 { return lhs && !rhs; }
407
409 friend constexpr basic_mask operator>=(const basic_mask& lhs, const basic_mask& rhs) noexcept
410 { return lhs || !rhs; }
411
413
416
419 friend constexpr basic_mask simd_select_impl(const basic_mask& m, const basic_mask& if_true, const basic_mask& if_false) noexcept
420 { return detail::select_if_else(target, m, if_true, if_false); }
421
424 friend constexpr basic_mask simd_select_impl(const basic_mask& m, std::same_as<bool> auto if_true, std::same_as<bool> auto if_false) noexcept
425 { return detail::select_if_else(target, m, basic_mask(if_true), basic_mask(if_false)); }
426
429 template<vec_type _Tp>
430 friend constexpr _Tp simd_select_impl(const basic_mask& m, const _Tp& if_true, const _Tp& if_false) noexcept
431 requires (sizeof(_Tp) == _Bytes)
432 { return detail::select_if_else(target, m, if_true, if_false); }
433
435
441 friend std::ostream &operator<<(std::ostream &stream, const basic_mask& value) {
442 stream << value.to_bitset();
443 return stream;
444 }
445};
446
449
453constexpr bool all_of(const mask_type auto& mask) noexcept {
454 if (std::is_constant_evaluated())
455 {
456 for (int i=0; i<mask.size(); ++i) if (!mask[i]) return false;
457 return true;
458 }
459 else
460 return detail::mask_all_of(target, mask);
461}
462
466constexpr bool all_of(std::same_as<bool> auto b) noexcept { return b; }
467
471constexpr bool any_of(const mask_type auto& mask) noexcept {
472 if (std::is_constant_evaluated())
473 {
474 for (int i=0; i<mask.size(); ++i) if (mask[i]) return true;
475 return false;
476 }
477 else
478 return detail::mask_any_of(target, mask);
479}
480
484constexpr bool any_of(std::same_as<bool> auto b) noexcept { return b; }
485
489constexpr bool none_of(const mask_type auto& mask) noexcept {
490 if (std::is_constant_evaluated())
491 {
492 for (int i=0; i<mask.size(); ++i) if (mask[i]) return false;
493 return true;
494 }
495 else
496 return detail::mask_none_of(target, mask);
497}
498
502constexpr bool none_of(std::same_as<bool> auto b) noexcept { return !b; }
503
507constexpr simd_size_type reduce_count(const mask_type auto& mask) noexcept {
508 if (std::is_constant_evaluated())
509 {
510 simd_size_type count = 0;
511 for (int i=0; i<mask.size(); ++i) if (mask[i]) count += 1;
512 return count;
513 }
514 else
515 return detail::mask_reduce_count(target, mask);
516}
517
521constexpr simd_size_type reduce_count(std::same_as<bool> auto b) noexcept { return +b; }
522
527constexpr simd_size_type reduce_min_index(const mask_type auto& mask) noexcept {
528 if (std::is_constant_evaluated())
529 {
530 simd_size_type i = 0;
531 while ((i < mask.size()) && !mask[i]) i += 1;
532 return i;
533 }
534 else
535 return detail::mask_reduce_min_index(target, mask);
536}
537
541constexpr simd_size_type reduce_min_index(std::same_as<bool> auto) noexcept { return 0; }
542
547constexpr simd_size_type reduce_max_index(const mask_type auto& mask) noexcept {
548 if (std::is_constant_evaluated())
549 {
550 simd_size_type i = mask.size() - 1;
551 while (i >= 0 && !mask[i]) i -= 1;
552 return i;
553 }
554 else
555 return detail::mask_reduce_max_index(target, mask);
556}
557
561constexpr simd_size_type reduce_max_index(std::same_as<bool> auto) noexcept { return 0; }
562
564
570template<typename _Vp>
571constexpr auto mask_from_count(simd_size_type n) noexcept {
572 if constexpr (vec_type<_Vp>) {
573 using _Mp = typename _Vp::mask_type;
574 return _Mp::__mask_from_count(n);
575 }
576 else
577 return n > 0;
578}
579
580// CTAD, to convert a basic_mask into a basic_vec, equivalent to decltype(+mask).
583template<std::size_t _Bytes, typename _Abi>
586
587
588} // namespace _XVEC_NAMESPACE::simd
A mask containing boolean elements.
Definition mask.hpp:121
static constexpr basic_mask __mask_from_count(simd_size_type _n)
Definition mask.hpp:223
detail::mask_traits< target_tag, _Bytes, _Abi::num_elements > traits
Definition mask.hpp:126
Vector of elements sharing an element type and ABI.
Definition simd:161
xvec implementation of the exposition-only SIMD iterator.
Definition iterator.hpp:32
Definition core.hpp:62
Definition core.hpp:65
Definition core.hpp:61
friend constexpr basic_mask operator||(const basic_mask &lhs, const basic_mask &rhs) noexcept
Compute the element-wise logical OR.
Definition mask.hpp:354
friend constexpr basic_mask operator|(const basic_mask &lhs, const basic_mask &rhs) noexcept
Compute the element-wise bitwise OR.
Definition mask.hpp:362
friend constexpr basic_mask operator^(const basic_mask &lhs, const basic_mask &rhs) noexcept
Compute the element-wise bitwise XOR.
Definition mask.hpp:366
friend constexpr basic_mask operator&(const basic_mask &lhs, const basic_mask &rhs) noexcept
Compute the element-wise bitwise AND.
Definition mask.hpp:358
friend constexpr basic_mask operator&&(const basic_mask &lhs, const basic_mask &rhs) noexcept
Compute the element-wise logical AND.
Definition mask.hpp:350
constexpr basic_mask & operator^=(const basic_mask &rhs) noexcept
Compute the element-wise bitwise XOR and assign the result.
Definition mask.hpp:381
constexpr basic_mask & operator|=(const basic_mask &rhs) noexcept
Compute the element-wise bitwise OR and assign the result.
Definition mask.hpp:378
constexpr basic_mask & operator&=(const basic_mask &rhs) noexcept
Compute the element-wise bitwise AND and assign the result.
Definition mask.hpp:375
friend constexpr basic_mask operator<(const basic_mask &lhs, const basic_mask &rhs) noexcept
Compare corresponding mask elements using less-than.
Definition mask.hpp:397
friend constexpr basic_mask operator==(const basic_mask &lhs, const basic_mask &rhs) noexcept
Compare corresponding mask elements for equality.
Definition mask.hpp:389
friend constexpr basic_mask operator>=(const basic_mask &lhs, const basic_mask &rhs) noexcept
Compare corresponding mask elements using greater-than-or-equal.
Definition mask.hpp:409
friend constexpr basic_mask operator<=(const basic_mask &lhs, const basic_mask &rhs) noexcept
Compare corresponding mask elements using less-than-or-equal.
Definition mask.hpp:401
friend constexpr basic_mask operator>(const basic_mask &lhs, const basic_mask &rhs) noexcept
Compare corresponding mask elements using greater-than.
Definition mask.hpp:405
friend constexpr basic_mask operator!=(const basic_mask &lhs, const basic_mask &rhs) noexcept
Compare corresponding mask elements for inequality.
Definition mask.hpp:393
friend constexpr _Tp simd_select_impl(const basic_mask &m, const _Tp &if_true, const _Tp &if_false) noexcept
Implementation of element-wise vector selection using a mask.
Definition mask.hpp:430
friend constexpr basic_mask simd_select_impl(const basic_mask &m, const basic_mask &if_true, const basic_mask &if_false) noexcept
Implementation of element-wise selection between two masks.
Definition mask.hpp:419
friend constexpr basic_mask simd_select_impl(const basic_mask &m, std::same_as< bool > auto if_true, std::same_as< bool > auto if_false) noexcept
Implementation of selection between two broadcast boolean values.
Definition mask.hpp:424
constexpr std::bitset< size()> to_bitset() const noexcept
Return a bitset containing the value of each mask element.
Definition mask.hpp:292
constexpr unsigned long long to_ullong() const
Definition mask.hpp:296
constexpr basic_mask(const basic_mask< _UBytes, _UAbi > &m)
Convert a mask with the same number of elements. Each output element retains the corresponding input ...
Definition mask.hpp:213
constexpr basic_mask(T m) noexcept
Build a mask from the bits of an unsigned integral value. Each mask element is initialized from the c...
Definition mask.hpp:198
constexpr basic_mask(std::invocable< std::integral_constant< simd_size_type, 0 > > auto fn)
Generate each mask element from its index.
Definition mask.hpp:217
constexpr basic_mask(const T &b) noexcept
Construct a mask from the corresponding bitset.
Definition mask.hpp:238
constexpr const_iterator begin() const noexcept
Return a const iterator to the first element.
Definition mask.hpp:247
basic_mask< sizeof(_Tp), simd_fixed_size_abi< _Np > > mask
Definition core.hpp:383
constexpr basic_mask(basic_mask &&) noexcept=default
Move-construct a mask.
constexpr std::default_sentinel_t end() const noexcept
Return the end sentinel.
Definition mask.hpp:253
constexpr iterator begin() noexcept
Return an iterator to the first element.
Definition mask.hpp:244
simd_iterator< const basic_mask > const_iterator
Definition mask.hpp:145
constexpr std::default_sentinel_t cend() const noexcept
Return the end sentinel.
Definition mask.hpp:256
constexpr const_iterator cbegin() const noexcept
Return a const iterator to the first element.
Definition mask.hpp:250
constexpr basic_mask()=default
Default-construct a mask.
constexpr basic_mask(const basic_mask &)=default
Copy-construct a mask.
constexpr bool all_of(const mask_type auto &mask) noexcept
Return true if every mask element is true.
Definition mask.hpp:453
constexpr bool none_of(const mask_type auto &mask) noexcept
Return true if no mask element is true.
Definition mask.hpp:489
constexpr bool any_of(const mask_type auto &mask) noexcept
Return true if any mask element is true.
Definition mask.hpp:471
constexpr simd_size_type reduce_max_index(const mask_type auto &mask) noexcept
Return the index of the last true mask element.
Definition mask.hpp:547
constexpr simd_size_type reduce_count(const mask_type auto &mask) noexcept
Count the true mask elements.
Definition mask.hpp:507
constexpr simd_size_type reduce_min_index(const mask_type auto &mask) noexcept
Return the index of the first true mask element.
Definition mask.hpp:527
constexpr auto operator[](const vec_integral auto &indexes) const
Select mask elements using an integral index vector.
Definition mask.hpp:338
constexpr bool operator[](simd_size_type pos) const &
Return the mask element at the specified position.
Definition mask.hpp:342
constexpr traits::signed_vec_for_mask operator~() const noexcept
Return the element-wise bitwise complement of the vector produced by unary plus.
Definition mask.hpp:328
constexpr basic_mask operator!() const noexcept
Negate each boolean mask element.
Definition mask.hpp:319
constexpr traits::signed_vec_for_mask operator+() const noexcept
Return a signed integral vector containing 1 for true elements and 0 otherwise.
Definition mask.hpp:322
constexpr traits::signed_vec_for_mask operator-() const noexcept
Return a signed integral vector containing -1 for true elements and 0 otherwise.
Definition mask.hpp:325
constexpr auto permute(const _Vp &v, detail::index_generator_function auto &&fn)
Permute the elements in a vec value using a generator function to compute the indexing.
Definition permute.hpp:94
constexpr auto mask_from_count(simd_size_type n) noexcept
Generate a mask with its first n elements set to true.
Definition mask.hpp:571
typename traits::builtin_type builtin_type
Definition mask.hpp:130
constexpr auto to_register() const noexcept
Convert the mask into a register which can be passed to an intrinsic.
Definition mask.hpp:284
static constexpr basic_mask from_builtin(const builtin_type &m)
Definition mask.hpp:281
constexpr auto to_builtin() const
Return the builtin mask value directly.
Definition mask.hpp:275
friend std::ostream & operator<<(std::ostream &stream, const basic_mask &value)
Output the mask to a stream as individual bits.
Definition mask.hpp:441
Detect a scoped enum. This will be provided in C++23 onwards.
Definition compat.hpp:27
constexpr auto builtin_operator(generic_tag, _Vp value, _Op op)
General purpose unary operator. Passes through to the compiler's own builtin.
Definition operators.hpp:28
Definition core.hpp:526