Vc  1.4.1
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
168 
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 
188 
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 
280 
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 
308 
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 
323  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
Vc::any_of
constexpr bool any_of(bool b)
Definition: mask.h:410
Vc::Mask::Mask
Mask(const bool *mem)
Load constructor from an array of bool.
Vc::Mask< T, N0 >::value_type
EntryType value_type
The EntryType of masks is always bool, independent of T.
Definition: mask.h:76
Vc::Mask::isEmpty
bool isEmpty() const
Returns true if components are false, false otherwise.
Vc::Mask::isNotEmpty
bool isNotEmpty() const
Returns a logical OR of all components.
Vc::MemoryAlignment
constexpr std::size_t MemoryAlignment
Definition: vector.h:215
Vc::Mask::isFull
bool isFull() const
Returns a logical AND of all components.
Vc::Mask::operator^=
Mask & operator^=(const Mask &mask)
Modifies the mask using an XOR operation with mask.
Vc::Mask::firstOne
int firstOne() const
Returns the index of the first one in the mask.
Vc::none_of
constexpr bool none_of(bool b)
Definition: mask.h:419
Vc::Mask::Mask
Mask()=default
Construct a zero-initialized vector object.
Vc::Mask::operator|
Mask operator|(const Mask &mask) const
Returns the component-wise application of a binary OR to mask.
Vc::Mask::operator==
bool operator==(const Mask &mask) const
Returns whether the two masks are equal in all components.
Vc::Mask::isMix
bool isMix() const
Returns !isFull() && !isEmpty().
Vc::Mask::Mask
Mask(const bool *mem, Flags flags)
Overload of the above with a load/store flag argument.
Vc::Mask::Mask
Mask(bool b)
Broadcast constructor.
Vc::Mask::Mask
Mask(VectorSpecialInitializerOne)
Initialize the new mask object to one (true).
Vc::Mask::load
void load(const bool *mem)
Load the components of the mask from an array of bool.
Vc::Mask::operator[]
EntryReference operator[](size_t index)
Lvalue-reference-like access to mask entries.
Vc::Mask::operator||
Mask operator||(const Mask &mask) const
Returns the component-wise application of a logical OR to mask.
Vc::Mask< T, N0 >::value_reference
EntryReference value_reference
The reference wrapper type used for accessing individual mask components.
Definition: mask.h:81
Vc::Mask< T, N0 >::VectorEntryType
typename VectorTraits< T, Abi >::VectorEntryType VectorEntryType
The VectorEntryType, in contrast to EntryType, reveals information about the SIMD implementation.
Definition: mask.h:88
Vc::Mask::load
void load(const bool *mem, Flags flags)
Overload of the above with a load/store flag argument.
Vc::Mask< T, N0 >::EntryReference
typename VectorTraits< T, Abi >::EntryReference EntryReference
The reference wrapper type used for accessing individual mask components.
Definition: mask.h:79
Vc::Mask::One
static Mask One()
Creates a mask object initialized to one/true.
Vc::Mask::operator^
Mask operator^(const Mask &mask) const
Returns the component-wise application of a binary XOR to mask.
Vc::Mask::count
int count() const
Returns how many components of the mask are true.
Vc::Mask::toInt
int toInt() const
Convert the boolean components of the mask into bits of an integer.
Vc::Mask::operator[]
EntryType operator[](size_t index) const
Read-only access to mask entries.
Vc::Mask::operator&&
Mask operator&&(const Mask &mask) const
Returns the component-wise application of a logical AND to mask.
Vc::Mask::operator&
Mask operator&(const Mask &mask) const
Returns the component-wise application of a binary AND to mask.
Vc::all_of
constexpr bool all_of(bool b)
Definition: mask.h:401
Vc::Mask
Definition: fwddecl.h:52
Vc::Mask::store
void store(bool *mem, Flags flags) const
Overload of the above with a load/store flag argument.
Vc::Mask::shifted
Mask shifted(int amount) const
Returns a mask with components shifted by amount places.
Vc::Mask::operator!
Mask operator!() const
Returns a mask with inverted components.
Vc::Mask::size
static constexpr size_t size()
Returns the number of boolean components ( ) in a mask of this type.
Definition: mask.h:57
Vc::Mask::Zero
static Mask Zero()
Creates a new mask object initialized to zero/false.
Vc::Mask< T, N0 >::EntryType
bool EntryType
The EntryType of masks is always bool, independent of T.
Definition: mask.h:74
Vc::some_of
constexpr bool some_of(bool)
Definition: mask.h:429
Vc::Mask::generate
static Mask generate(G &&gen)
Generate a mask object from booleans returned from the function gen.
Vc::Mask::operator&=
Mask & operator&=(const Mask &mask)
Modifies the mask using an AND operation with mask.
Vc::Mask::Mask
Mask(VectorSpecialInitializerZero)
Zero-initialize the new mask object (false).
Vc::Mask< T, N0 >::abi
Abi abi
The ABI tag type of the current template instantiation.
Definition: mask.h:69
Vc::Mask::Mask
Mask(U &&otherMask, Common::enable_if_mask_converts_implicitly< Mask, T, U >=nullarg)
Implicit conversion from a compatible (equal on every platform) mask object.
Vc::Mask::operator!=
bool operator!=(const Mask &mask) const
Returns whether the two masks are different in at least one component.
Vc::Mask::operator|=
Mask & operator|=(const Mask &mask)
Modifies the mask using an OR operation with mask.
Vc::Mask::store
void store(bool *mem) const
Store the values of the mask to an array of bool.