]> git.uio.no Git - u/mrichter/AliRoot.git/blob - Vc/include/Vc/scalar/mask.h
Vc package added (version 0.6.79-dev)
[u/mrichter/AliRoot.git] / Vc / include / Vc / scalar / mask.h
1 /*  This file is part of the Vc library.
2
3     Copyright (C) 2009-2012 Matthias Kretz <kretz@kde.org>
4
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.
9
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.
14
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/>.
17
18 */
19
20 #ifndef VC_SCALAR_MASK_H
21 #define VC_SCALAR_MASK_H
22
23 #include "types.h"
24
25 namespace Vc
26 {
27 namespace Scalar
28 {
29 template<unsigned int VectorSize = 1> class Mask
30 {
31     public:
32         inline 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) {}
37
38         inline Mask &operator=(const Mask &rhs) { m = rhs.m; return *this; }
39         inline Mask &operator=(bool rhs) { m = rhs; return *this; }
40
41         inline void expand(Mask *x) { x[0].m = m; }
42
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); }
45
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); }
52
53         inline Mask &operator&=(const Mask &rhs) { m &= rhs.m; return *this; }
54         inline Mask &operator|=(const Mask &rhs) { m |= rhs.m; return *this; }
55
56         inline bool isFull () const { return  m; }
57         inline bool isEmpty() const { return !m; }
58         inline bool isMix  () const { return false; }
59
60         inline bool data () const { return m; }
61         inline bool dataI() const { return m; }
62         inline bool dataD() const { return m; }
63
64 #ifndef VC_NO_AUTOMATIC_BOOL_FROM_MASK
65         inline operator bool() const { return isFull(); }
66 #endif
67
68         template<unsigned int OtherSize>
69             inline Mask cast() const { return *this; }
70
71         inline bool operator[](int) const { return m; }
72
73         inline int count() const { return m ? 1 : 0; }
74
75         /**
76          * Returns the index of the first one in the mask.
77          *
78          * The return value is undefined if the mask is empty.
79          */
80         int firstOne() const { return 0; }
81
82     private:
83         bool m;
84 };
85
86 struct ForeachHelper
87 {
88     bool first;
89     inline ForeachHelper(bool mask) : first(mask) {}
90     inline void next() { first = false; }
91 };
92
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())
96
97 } // namespace Scalar
98 } // namespace Vc
99
100 #endif // VC_SCALAR_MASK_H