Vc  1.4.3
SIMD Vector Classes for C++
mask.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2015 Matthias Kretz <kretz@kde.org>
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
6  * Redistributions of source code must retain the above copyright
7  notice, this list of conditions and the following disclaimer.
8  * Redistributions in binary form must reproduce the above copyright
9  notice, this list of conditions and the following disclaimer in the
10  documentation and/or other materials provided with the distribution.
11  * Neither the names of contributing organizations nor the
12  names of its contributors may be used to endorse or promote products
13  derived from this software without specific prior written permission.
14 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 }}}*/
27 
28 #ifndef VC_COMMON_MASK_H_
29 #define VC_COMMON_MASK_H_
30 
31 #include "macros.h"
32 
33 namespace Vc_VERSIONED_NAMESPACE
34 {
41 template <typename T, typename Abi = VectorAbi::Best<T>> class Mask
42 {
43 public:
57  static constexpr size_t size() { return VectorTraits<T, Abi>::size(); }
60  static constexpr size_t Size = VectorTraits<T, Abi>::size();
61 
66  static constexpr size_t MemoryAlignment = VectorTraits<T, Abi>::maskMemoryAlignment();
67 
69  using abi = Abi;
70 
74  using EntryType = bool;
77 
79  using EntryReference = typename VectorTraits<T, Abi>::EntryReference;
82 
88  using VectorEntryType = typename VectorTraits<T, Abi>::VectorEntryType;
89 
93  using VectorType = typename VectorTraits<T, Abi>::VectorType;
97  using vector_type = VectorType;
98 
99  /*
100  * The associated Vector<T> type.
101  */
102  //using Vector = Vector<T, Abi>;
103 
106 
111  Vc_INTRINSIC static Mask Zero();
112 
118  Vc_INTRINSIC static Mask One();
119 
121  template <typename G> static Vc_INTRINSIC Mask generate(G &&gen);
123 
126 
135  Vc_INTRINSIC Mask() = default;
136 
139  Vc_INTRINSIC explicit Mask(VectorSpecialInitializerZero);
140 
143  Vc_INTRINSIC explicit Mask(VectorSpecialInitializerOne);
145 
148 
155  Vc_INTRINSIC explicit Mask(bool b);
156 
163  template <typename U>
164  Vc_INTRINSIC Mask(U &&otherMask,
165  Common::enable_if_mask_converts_implicitly<Mask, T, U> = nullarg);
166 
167 #if Vc_IS_VERSION_1
176  template <typename U>
177  Vc_DEPRECATED(
178  "use simd_cast instead of explicit type casting to convert between mask types")
179  Vc_INTRINSIC_L
180  explicit Mask(U &&otherMask, Common::enable_if_mask_converts_explicitly<T, U> =
181  nullarg) Vc_INTRINSIC_R;
183 #endif
184 
189 
198  Vc_ALWAYS_INLINE explicit Mask(const bool *mem);
207  template <typename Flags> Vc_ALWAYS_INLINE explicit Mask(const bool *mem, Flags flags);
208 
215  Vc_ALWAYS_INLINE void load(const bool *mem);
224  template <typename Flags> Vc_ALWAYS_INLINE void load(const bool *mem, Flags flags);
225 
232  Vc_ALWAYS_INLINE void store(bool *mem) const;
240  template <typename Flags> Vc_ALWAYS_INLINE void store(bool *mem, Flags flags) const;
242 
245 
258  Vc_ALWAYS_INLINE bool operator==(const Mask &mask) const;
259 
269  Vc_ALWAYS_INLINE bool operator!=(const Mask &mask) const;
271 
281 
283  Vc_ALWAYS_INLINE Mask operator&&(const Mask &mask) const;
285  Vc_ALWAYS_INLINE Mask operator&(const Mask &mask) const;
287  Vc_ALWAYS_INLINE Mask operator||(const Mask &mask) const;
289  Vc_ALWAYS_INLINE Mask operator|(const Mask &mask) const;
291  Vc_ALWAYS_INLINE Mask operator^(const Mask &mask) const;
293  Vc_ALWAYS_INLINE Mask operator!() const;
294 
296  Vc_ALWAYS_INLINE Mask &operator&=(const Mask &mask);
298  Vc_ALWAYS_INLINE Mask &operator|=(const Mask &mask);
300  Vc_ALWAYS_INLINE Mask &operator^=(const Mask &mask);
302 
309 
311  Vc_ALWAYS_INLINE bool isFull() const;
313  Vc_ALWAYS_INLINE bool isNotEmpty() const;
315  Vc_ALWAYS_INLINE bool isEmpty() const;
317  Vc_ALWAYS_INLINE bool isMix() const;
319 
324  Vc_ALWAYS_INLINE bool data() const;
325  Vc_ALWAYS_INLINE bool dataI() const;
326  Vc_ALWAYS_INLINE bool dataD() const;
328 
331 
341  Vc_ALWAYS_INLINE EntryReference operator[](size_t index);
342 
352  Vc_ALWAYS_INLINE EntryType operator[](size_t index) const;
354 
356  Vc_ALWAYS_INLINE int count() const;
357 
368  Vc_ALWAYS_INLINE int firstOne() const;
369 
377  Vc_ALWAYS_INLINE int toInt() const;
378 
380  Vc_INTRINSIC Vc_PURE Mask shifted(int amount) const;
381 
382  Vc_FREE_STORE_OPERATORS_ALIGNED(alignof(Mask));
383 
384 private:
385  VectorType d;
386 };
387 
397 template<typename Mask> constexpr bool all_of(const Mask &m) { return m.isFull(); }
401 constexpr bool all_of(bool b) { return b; }
402 
406 template<typename Mask> constexpr bool any_of(const Mask &m) { return m.isNotEmpty(); }
410 constexpr bool any_of(bool b) { return b; }
411 
415 template<typename Mask> constexpr bool none_of(const Mask &m) { return m.isEmpty(); }
419 constexpr bool none_of(bool b) { return !b; }
420 
425 template<typename Mask> constexpr bool some_of(const Mask &m) { return m.isMix(); }
429 constexpr bool some_of(bool) { return false; }
431 } // namespace Vc
432 
433 #endif // VC_COMMON_MASK_H_
434 
435 // vim: foldmethod=marker
The main SIMD mask class.
Definition: mask.h:42
Mask(VectorSpecialInitializerZero)
Zero-initialize the new mask object (false).
void store(bool *mem) const
Store the values of the mask to an array of bool.
Mask operator!() const
Returns a mask with inverted components.
Mask & operator&=(const Mask &mask)
Modifies the mask using an AND operation with mask.
int count() const
Returns how many components of the mask are true.
void store(bool *mem, Flags flags) const
Overload of the above with a load/store flag argument.
static constexpr size_t size()
Returns the number of boolean components ( ) in a mask of this type.
Definition: mask.h:57
static Mask generate(G &&gen)
Generate a mask object from booleans returned from the function gen.
Mask operator&&(const Mask &mask) const
Returns the component-wise application of a logical AND to mask.
static Mask Zero()
Creates a new mask object initialized to zero/false.
typename VectorTraits< T, Abi >::VectorEntryType VectorEntryType
The VectorEntryType, in contrast to EntryType, reveals information about the SIMD implementation.
Definition: mask.h:88
bool isMix() const
Returns !isFull() && !isEmpty().
void load(const bool *mem, Flags flags)
Overload of the above with a load/store flag argument.
Mask operator||(const Mask &mask) const
Returns the component-wise application of a logical OR to mask.
bool isNotEmpty() const
Returns a logical OR of all components.
EntryType operator[](size_t index) const
Read-only access to mask entries.
Mask operator|(const Mask &mask) const
Returns the component-wise application of a binary OR to mask.
Mask(const bool *mem)
Load constructor from an array of bool.
void load(const bool *mem)
Load the components of the mask from an array of bool.
Mask(U &&otherMask, Common::enable_if_mask_converts_implicitly< Mask, T, U >=nullarg)
Implicit conversion from a compatible (equal on every platform) mask object.
typename VectorTraits< T, Abi >::EntryReference EntryReference
The reference wrapper type used for accessing individual mask components.
Definition: mask.h:79
bool operator==(const Mask &mask) const
Returns whether the two masks are equal in all components.
EntryReference value_reference
The reference wrapper type used for accessing individual mask components.
Definition: mask.h:81
Mask operator^(const Mask &mask) const
Returns the component-wise application of a binary XOR to mask.
Mask operator&(const Mask &mask) const
Returns the component-wise application of a binary AND to mask.
int toInt() const
Convert the boolean components of the mask into bits of an integer.
static Mask One()
Creates a mask object initialized to one/true.
Mask(VectorSpecialInitializerOne)
Initialize the new mask object to one (true).
Mask & operator|=(const Mask &mask)
Modifies the mask using an OR operation with mask.
Abi abi
The ABI tag type of the current template instantiation.
Definition: mask.h:69
bool operator!=(const Mask &mask) const
Returns whether the two masks are different in at least one component.
bool isEmpty() const
Returns true if components are false, false otherwise.
int firstOne() const
Returns the index of the first one in the mask.
bool isFull() const
Returns a logical AND of all components.
Mask shifted(int amount) const
Returns a mask with components shifted by amount places.
EntryReference operator[](size_t index)
Lvalue-reference-like access to mask entries.
Mask & operator^=(const Mask &mask)
Modifies the mask using an XOR operation with mask.
Mask()=default
Construct a zero-initialized vector object.
Mask(const bool *mem, Flags flags)
Overload of the above with a load/store flag argument.
EntryType value_type
The EntryType of masks is always bool, independent of T.
Definition: mask.h:76
Mask(bool b)
Broadcast constructor.
bool EntryType
The EntryType of masks is always bool, independent of T.
Definition: mask.h:74
constexpr bool all_of(bool b)
Returns b.
Definition: mask.h:401
constexpr bool none_of(bool b)
Returns !b.
Definition: mask.h:419
constexpr bool any_of(bool b)
Returns b.
Definition: mask.h:410
constexpr bool some_of(bool)
Returns false.
Definition: mask.h:429
constexpr std::size_t MemoryAlignment
Specifies the most conservative memory alignment necessary for aligned loads and stores of Vector typ...
Definition: vector.h:215