11#include <xvec/detail/config.hpp>
21#include <xvec/detail/utilities.hpp>
23namespace _XVEC_NAMESPACE::simd
119template<std::
size_t _Bytes,
typename _Abi>
148 static constexpr auto size = size_constant<_Abi::num_elements>{};
197 template <std::
unsigned_
integral T>
requires (!std::same_as<T, value_type>)
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; });
203 return detail::generate_mask_from_unsigned<basic_mask>(target, m);
211 template<std::
size_t _UBytes,
typename _UAbi>
212 requires (basic_mask<_UBytes, _UAbi>::size() == size())
225 if (std::is_constant_evaluated())
227 return basic_mask([&](simd_size_type i) {
return i < _n; });
230 return detail::get_n_bit_mask(target,
basic_mask(), _n);
237 template<std::same_as<std::bitset<size()>> T>
253 constexpr std::default_sentinel_t
end() const noexcept {
return {}; }
256 constexpr std::default_sentinel_t
cend() const noexcept {
return {}; }
269 constexpr operator typename traits::signed_vec_for_mask()
const {
return detail::mask_to_vec<1, typename traits::element_signed_container_type>(target, *
this); }
284 constexpr auto to_register() const noexcept {
return traits::to_register(mask); }
292 constexpr std::bitset<
size()>
to_bitset() const noexcept {
return detail::mask_to_bitset(target, *
this); }
297 if (std::is_constant_evaluated())
299 constexpr auto numOutputBits = std::numeric_limits<unsigned long long>::digits;
302 throw "Mask has set bits which won't be copied into the output ullong";
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;
310 return detail::mask_to_ullong(target, *
this);
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); }
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); }
328 constexpr typename traits::signed_vec_for_mask
operator~() const noexcept {
return ~(+*
this); }
342 constexpr bool operator[](simd_size_type pos)
const& {
return detail::subscript(target, *
this, pos); }
390 {
return !(lhs ^ rhs); }
394 {
return lhs ^ rhs; }
398 {
return !lhs && rhs; }
402 {
return !lhs || rhs; }
406 {
return lhs && !rhs; }
410 {
return lhs || !rhs; }
420 {
return detail::select_if_else(target, m, if_true, if_false); }
429 template<vec_type _Tp>
431 requires (
sizeof(_Tp) == _Bytes)
432 {
return detail::select_if_else(target, m, if_true, if_false); }
454 if (std::is_constant_evaluated())
456 for (
int i=0; i<
mask.size(); ++i)
if (!
mask[i])
return false;
460 return detail::mask_all_of(target,
mask);
466constexpr bool all_of(std::same_as<bool>
auto b)
noexcept {
return b; }
472 if (std::is_constant_evaluated())
474 for (
int i=0; i<
mask.size(); ++i)
if (
mask[i])
return true;
478 return detail::mask_any_of(target,
mask);
484constexpr bool any_of(std::same_as<bool>
auto b)
noexcept {
return b; }
490 if (std::is_constant_evaluated())
492 for (
int i=0; i<
mask.size(); ++i)
if (
mask[i])
return false;
496 return detail::mask_none_of(target,
mask);
502constexpr bool none_of(std::same_as<bool>
auto b)
noexcept {
return !b; }
508 if (std::is_constant_evaluated())
510 simd_size_type count = 0;
511 for (
int i=0; i<
mask.size(); ++i)
if (
mask[i]) count += 1;
515 return detail::mask_reduce_count(target,
mask);
521constexpr simd_size_type
reduce_count(std::same_as<bool>
auto b)
noexcept {
return +b; }
528 if (std::is_constant_evaluated())
530 simd_size_type i = 0;
531 while ((i <
mask.size()) && !
mask[i]) i += 1;
535 return detail::mask_reduce_min_index(target,
mask);
548 if (std::is_constant_evaluated())
550 simd_size_type i =
mask.size() - 1;
551 while (i >= 0 && !
mask[i]) i -= 1;
555 return detail::mask_reduce_max_index(target,
mask);
570template<
typename _Vp>
573 using _Mp =
typename _Vp::mask_type;
574 return _Mp::__mask_from_count(n);
583template<std::
size_t _Bytes,
typename _Abi>
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
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
bool value_type
Definition mask.hpp:139
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
_Abi abi_type
Definition mask.hpp:136
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
simd_iterator< basic_mask > iterator
Definition mask.hpp:142
constexpr basic_mask()=default
Default-construct a mask.
static constexpr auto size
Definition mask.hpp:148
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