1 /* This file is part of the Vc library.
3 Copyright (C) 2009-2012 Matthias Kretz <kretz@kde.org>
5 Vc is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation, either version 3 of
8 the License, or (at your option) any later version.
10 Vc is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with Vc. If not, see <http://www.gnu.org/licenses/>.
20 #ifndef VC_SCALAR_MASK_H
21 #define VC_SCALAR_MASK_H
29 template<unsigned int VectorSize = 1> class Mask
33 inline explicit Mask(bool b) : m(b) {}
34 inline explicit Mask(VectorSpecialInitializerZero::ZEnum) : m(false) {}
35 inline explicit Mask(VectorSpecialInitializerOne::OEnum) : m(true) {}
36 inline Mask(const Mask<VectorSize> *a) : m(a[0].m) {}
38 inline Mask &operator=(const Mask &rhs) { m = rhs.m; return *this; }
39 inline Mask &operator=(bool rhs) { m = rhs; return *this; }
41 inline void expand(Mask *x) { x[0].m = m; }
43 inline bool operator==(const Mask &rhs) const { return Mask(m == rhs.m); }
44 inline bool operator!=(const Mask &rhs) const { return Mask(m != rhs.m); }
46 inline Mask operator&&(const Mask &rhs) const { return Mask(m && rhs.m); }
47 inline Mask operator& (const Mask &rhs) const { return Mask(m && rhs.m); }
48 inline Mask operator||(const Mask &rhs) const { return Mask(m || rhs.m); }
49 inline Mask operator| (const Mask &rhs) const { return Mask(m || rhs.m); }
50 inline Mask operator^ (const Mask &rhs) const { return Mask(m ^ rhs.m); }
51 inline Mask operator!() const { return Mask(!m); }
53 inline Mask &operator&=(const Mask &rhs) { m &= rhs.m; return *this; }
54 inline Mask &operator|=(const Mask &rhs) { m |= rhs.m; return *this; }
56 inline bool isFull () const { return m; }
57 inline bool isEmpty() const { return !m; }
58 inline bool isMix () const { return false; }
60 inline bool data () const { return m; }
61 inline bool dataI() const { return m; }
62 inline bool dataD() const { return m; }
64 #ifndef VC_NO_AUTOMATIC_BOOL_FROM_MASK
65 inline operator bool() const { return isFull(); }
68 template<unsigned int OtherSize>
69 inline Mask cast() const { return *this; }
71 inline bool operator[](int) const { return m; }
73 inline int count() const { return m ? 1 : 0; }
76 * Returns the index of the first one in the mask.
78 * The return value is undefined if the mask is empty.
80 int firstOne() const { return 0; }
89 inline ForeachHelper(bool mask) : first(mask) {}
90 inline void next() { first = false; }
93 #define Vc_foreach_bit(_it_, _mask_) \
94 for (Vc::Scalar::ForeachHelper _Vc_foreach_bit_helper(_mask_); _Vc_foreach_bit_helper.first; ) \
95 for (_it_ = 0; _Vc_foreach_bit_helper.first; _Vc_foreach_bit_helper.next())
100 #endif // VC_SCALAR_MASK_H