Argon 0.1.0
Loading...
Searching...
No Matches
predicate.hpp
Go to the documentation of this file.
1#pragma once
2#include <array>
3#include <bit>
4#include <cstddef>
5#include <cstdint>
6#include <type_traits>
7#include "arm_simd.hpp"
8#include "arm_simd/helpers/scalar.hpp"
9#include "features.h"
10#include "helpers/argon_for.hpp"
11#include "helpers/bool.hpp"
12#include "helpers/mve_compare.hpp"
13
14#ifdef __ARM_FEATURE_MVE
15#define simd mve
16#else
17#define simd neon
18#endif
19
20#ifdef ARGON_PLATFORM_SIMDE
21#define ace [[gnu::always_inline]] inline
22#elifdef __clang__
23#define ace [[gnu::always_inline]] constexpr
24#else
25#define ace [[gnu::always_inline]] inline
26#endif
27
30
31namespace argon {
32
38template <typename VectorType>
39class Predicate {
40 using scalar_type = simd::Scalar_t<VectorType>;
41
42 public:
43 using vector_type = VectorType;
45 using mask_scalar = simd::Scalar_t<mask_type>;
47#ifdef ARGON_PLATFORM_MVE
48 using storage_type = mve_pred16_t;
49#else
51#endif
52
54 static constexpr size_t lanes = sizeof(VectorType) / sizeof(scalar_type);
55
57 ace explicit Predicate(storage_type p) : p_{p} {}
58
61 template <typename OtherVectorType>
62 requires(!std::is_same_v<OtherVectorType, VectorType> && Predicate<OtherVectorType>::lanes == lanes &&
63 sizeof(OtherVectorType) == sizeof(VectorType))
64 ace Predicate(Predicate<OtherVectorType> other) : p_{std::bit_cast<storage_type>(other.native())} {}
65
67 ace static Predicate True() {
68#ifdef ARGON_PLATFORM_MVE
69 return Predicate{static_cast<storage_type>(0xFFFF)};
70#else
71 return Predicate{argon_mask_type{static_cast<mask_scalar>(~mask_scalar{0})}.vec()};
72#endif
73 }
74
76 ace static Predicate False() {
77#ifdef ARGON_PLATFORM_MVE
78 return Predicate{storage_type{0}};
79#else
80 return Predicate{argon_mask_type{mask_scalar{0}}.vec()};
81#endif
82 }
83
87 ace static Predicate FirstN(size_t n) {
88#ifdef ARGON_PLATFORM_MVE
89 if constexpr (sizeof(scalar_type) == 1) {
90 return Predicate{mve::create_tail_predicate8(static_cast<uint32_t>(n))};
91 } else if constexpr (sizeof(scalar_type) == 2) {
92 return Predicate{mve::create_tail_predicate16(static_cast<uint32_t>(n))};
93 } else if constexpr (sizeof(scalar_type) == 4) {
94 return Predicate{mve::create_tail_predicate32(static_cast<uint32_t>(n))};
95 } else {
96 return Predicate{mve::create_tail_predicate64(static_cast<uint32_t>(n))};
97 }
98#else
99 if (n >= lanes) {
100 return True();
101 }
102 const auto index = argon_mask_type::Iota(0);
103 return Predicate{(index < argon_mask_type{static_cast<mask_scalar>(n)}).native()};
104#endif
105 }
106
110#ifdef ARGON_PLATFORM_MVE
111 return Predicate{mve::not_equal(mask.vec(), mask_scalar{0})};
112#else
113 return Predicate{mask.vec()};
114#endif
115 }
116
119#ifdef ARGON_PLATFORM_MVE
120 return argon_mask_type{helpers::mve_mask<VectorType>(p_)};
121#else
122 return argon_mask_type{p_};
123#endif
124 }
125
127 [[deprecated("Comparisons now return argon::Predicate; use ToMask() or the Predicate interface")]]
128 ace operator argon_mask_type() const {
129 return ToMask();
130 }
131
133 ace storage_type native() const { return p_; }
134
137#ifdef ARGON_PLATFORM_MVE
138 return Predicate{static_cast<storage_type>(p_ & b.p_)};
139#else
140 return Predicate{(argon_mask_type{p_} & argon_mask_type{b.p_}).vec()};
141#endif
142 }
143
146#ifdef ARGON_PLATFORM_MVE
147 return Predicate{static_cast<storage_type>(p_ | b.p_)};
148#else
149 return Predicate{(argon_mask_type{p_} | argon_mask_type{b.p_}).vec()};
150#endif
151 }
152
155#ifdef ARGON_PLATFORM_MVE
156 return Predicate{static_cast<storage_type>(p_ ^ b.p_)};
157#else
158 return Predicate{(argon_mask_type{p_} ^ argon_mask_type{b.p_}).vec()};
159#endif
160 }
161
163 ace Predicate operator~() const {
164#ifdef ARGON_PLATFORM_MVE
165 return Predicate{mve::predicate_not(p_)};
166#else
167 return Predicate{(~argon_mask_type{p_}).vec()};
168#endif
169 }
170
173 ace Predicate operator&&(Predicate b) const { return *this & b; }
174
177 ace Predicate operator||(Predicate b) const { return *this | b; }
178
180 ace Predicate operator!() const { return ~*this; }
181
187 template <typename ArgType>
188 requires(ArgType::lanes == lanes && sizeof(typename ArgType::vector_type) == sizeof(VectorType))
189 ace ArgType Select(ArgType true_value, ArgType false_value) const {
190#ifdef ARGON_PLATFORM_MVE
191 const auto t = std::bit_cast<mask_type>(true_value.vec());
192 const auto f = std::bit_cast<mask_type>(false_value.vec());
193 return ArgType{std::bit_cast<typename ArgType::vector_type>(mve::predicate_select(t, f, p_))};
194#else
195 return argon_mask_type{p_}.BitwiseSelect(true_value, false_value);
196#endif
197 }
198
200 ace bool Any() const {
201#ifdef ARGON_PLATFORM_MVE
202 return p_ != 0;
203#else
204 for (uint64_t word : words()) {
205 if (word != 0) return true;
206 }
207 return false;
208#endif
209 }
210
212 ace bool All() const {
213#ifdef ARGON_PLATFORM_MVE
214 return p_ == 0xFFFF;
215#else
216 for (uint64_t word : words()) {
217 if (word != ~uint64_t{0}) return false;
218 }
219 return true;
220#endif
221 }
222
224 ace bool None() const { return !Any(); }
225
227 ace size_t Count() const {
228#ifdef ARGON_PLATFORM_MVE
229 if constexpr (sizeof(scalar_type) < 8) {
230 // Armv8.1-M has no popcount instruction; count with a predicated across-vector add instead.
231 return mve::reduce_add(mve::duplicate(mask_scalar{1}), p_);
232 } else {
233 return std::popcount(static_cast<uint16_t>(p_)) / sizeof(scalar_type);
234 }
235#else
236 size_t bits = 0;
237 for (uint64_t word : words()) {
238 bits += std::popcount(word);
239 }
240 return bits / (8 * sizeof(scalar_type));
241#endif
242 }
243
246 ace bool Active(size_t i) const {
247#ifdef ARGON_PLATFORM_MVE
248 return (p_ >> (i * sizeof(scalar_type))) & 1;
249#else
250 return std::bit_cast<std::array<mask_scalar, lanes>>(p_)[i] != 0;
251#endif
252 }
253
255 ace std::array<bool, lanes> to_array() const {
256 std::array<bool, lanes> out{};
257 for (size_t i = 0; i < lanes; ++i) {
258 out[i] = Active(i);
259 }
260 return out;
261 }
262
263 private:
264#ifndef ARGON_PLATFORM_MVE
266 ace std::array<uint64_t, sizeof(mask_type) / 8> words() const {
267 return std::bit_cast<std::array<uint64_t, sizeof(mask_type) / 8>>(p_);
268 }
269#endif
270
271 storage_type p_;
272};
273
274} // namespace argon
275
276#undef ace
277#undef simd
static ace Predicate False()
A predicate with no lanes active.
Definition predicate.hpp:76
ace Predicate operator~() const
Invert every lane.
Definition predicate.hpp:163
static ace Predicate True()
A predicate with every lane active.
Definition predicate.hpp:67
helpers::ArgonFor_t< mask_type > argon_mask_type
The Argon type of the mask vector.
Definition predicate.hpp:46
ace std::array< bool, lanes > to_array() const
The active state of every lane, in lane order.
Definition predicate.hpp:255
ace storage_type native() const
The native predicate representation: a mask vector on NEON, an mve_pred16_t on MVE.
Definition predicate.hpp:133
ace ArgType Select(ArgType true_value, ArgType false_value) const
Choose each lane from true_value where the predicate is active, and from false_value otherwise.
Definition predicate.hpp:189
ace Predicate operator||(Predicate b) const
Lanes active in either predicate; the same as |.
Definition predicate.hpp:177
ace argon_mask_type ToMask() const
Expand the predicate into a mask vector: every bit set in active lanes, clear in inactive ones.
Definition predicate.hpp:118
ace Predicate(Predicate< OtherVectorType > other)
Convert from the predicate of another vector type with the same lanes, e.g.
Definition predicate.hpp:64
ace Predicate operator&(Predicate b) const
Lanes active in both predicates.
Definition predicate.hpp:136
simd::Scalar_t< mask_type > mask_scalar
The lane type of the mask vector.
Definition predicate.hpp:45
ace Predicate(storage_type p)
Construct from the native predicate representation (see storage_type).
Definition predicate.hpp:57
ace bool None() const
Whether no lane is active.
Definition predicate.hpp:224
Bool_t< VectorType > mask_type
The equivalent mask vector type.
Definition predicate.hpp:44
ace bool Any() const
Whether any lane is active.
Definition predicate.hpp:200
ace Predicate operator|(Predicate b) const
Lanes active in either predicate.
Definition predicate.hpp:145
ace Predicate operator!() const
Invert every lane; the same as ~.
Definition predicate.hpp:180
ace size_t Count() const
The number of active lanes.
Definition predicate.hpp:227
ace bool All() const
Whether every lane is active.
Definition predicate.hpp:212
static ace Predicate FromMask(argon_mask_type mask)
Construct from a mask vector, treating any nonzero lane as active.
Definition predicate.hpp:109
static constexpr size_t lanes
The number of lanes the predicate covers.
Definition predicate.hpp:54
ace Predicate operator^(Predicate b) const
Lanes active in exactly one of the predicates.
Definition predicate.hpp:154
ace Predicate operator&&(Predicate b) const
Lanes active in both predicates; the same as &.
Definition predicate.hpp:173
VectorType vector_type
The vector type the predicate applies to.
Definition predicate.hpp:43
mask_type storage_type
The native predicate representation.
Definition predicate.hpp:50
static ace Predicate FirstN(size_t n)
A predicate with the first n lanes active, and the rest inactive.
Definition predicate.hpp:87
ace bool Active(size_t i) const
Whether a single lane is active.
Definition predicate.hpp:246
Header file for SIMD features and platform detection.
typename ArgonFor< std::remove_cv_t< T > >::type ArgonFor_t
Helper alias to get the Argon type for a given vector type.
Definition argon_for.hpp:46
A 128-bit SIMD vector wrapping a scalar type, providing arithmetic, logical, and data-movement operat...
Definition argon.hpp:30
Bool< T >::type Bool_t
Helper alias to get the boolean type of a SIMD operation.
Definition bool.hpp:49