Vc  1.0.0-dev
SIMD Vector Classes for C++
maskentry.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2013-2015 Matthias Kretz <kretz@kde.org>
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7  * Redistributions of source code must retain the above copyright
8  notice, this list of conditions and the following disclaimer.
9  * Redistributions in binary form must reproduce the above copyright
10  notice, this list of conditions and the following disclaimer in the
11  documentation and/or other materials provided with the distribution.
12  * Neither the names of contributing organizations nor the
13  names of its contributors may be used to endorse or promote products
14  derived from this software without specific prior written permission.
15 
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 }}}*/
28 
29 #ifndef VC_COMMON_MASKENTRY_H_
30 #define VC_COMMON_MASKENTRY_H_
31 
32 #include "macros.h"
33 
34 namespace Vc_VERSIONED_NAMESPACE
35 {
36 namespace Common
37 {
38 
39 template<typename M> class MaskEntry
40 {
41  M &mask;
42  size_t offset;
43 
44 public:
45  constexpr MaskEntry(M &m, size_t o) : mask(m), offset(o) {}
46 
50  constexpr MaskEntry(MaskEntry &&) = default;
51  MaskEntry(const MaskEntry &) = delete;
52  MaskEntry &operator=(const MaskEntry &) = delete;
53  MaskEntry &operator=(MaskEntry &&) = delete;
54 
55  template <typename B, typename = enable_if<std::is_same<B, bool>::value>>
56  Vc_INTRINSIC Vc_PURE operator B() const
57  {
58  const M &m = mask;
59  return m[offset];
60  }
61  Vc_INTRINSIC MaskEntry &operator=(bool x)
62  {
63  mask.setEntry(offset, x);
64  return *this;
65  }
66 };
67 
68 namespace
69 {
70  template<size_t Bytes> struct MaskBoolStorage;
71  // the following for typedefs must use std::intN_t and NOT! Vc::intN_t. The latter
72  // segfaults ICC 15.0.3.
73  template<> struct MaskBoolStorage<1> { typedef std::int8_t type; };
74  template<> struct MaskBoolStorage<2> { typedef std::int16_t type; };
75  template<> struct MaskBoolStorage<4> { typedef std::int32_t type; };
76  template<> struct MaskBoolStorage<8> { typedef std::int64_t type; };
77 } // anonymous namespace
78 
79 template<size_t Bytes> class MaskBool
80 {
81  typedef typename MaskBoolStorage<Bytes>::type storage_type Vc_MAY_ALIAS;
82  storage_type data;
83 public:
84  constexpr MaskBool(bool x) : data(x ? -1 : 0) {}
85  Vc_ALWAYS_INLINE MaskBool &operator=(bool x) { data = x ? -1 : 0; return *this; }
86  template <typename T, typename = enable_if<(!std::is_same<T, bool>::value &&
87  std::is_fundamental<T>::value)>>
88  Vc_ALWAYS_INLINE MaskBool &operator=(T x)
89  {
90  data = reinterpret_cast<const storage_type &>(x);
91  return *this;
92  }
93 
94  Vc_ALWAYS_INLINE MaskBool(const MaskBool &) = default;
95  Vc_ALWAYS_INLINE MaskBool &operator=(const MaskBool &) = default;
96 
97  template <typename T, typename = enable_if<(std::is_same<T, bool>::value ||
98  (std::is_fundamental<T>::value &&
99  sizeof(storage_type) == sizeof(T)))>>
100  constexpr operator T() const
101  {
102  return std::is_same<T, bool>::value ? T((data & 1) != 0)
103  : reinterpret_cast<const MayAlias<T> &>(data);
104  }
105 } Vc_MAY_ALIAS;
106 
107 template <typename A,
108  typename B,
109  typename std::enable_if<
110  std::is_convertible<A, bool>::value &&std::is_convertible<B, bool>::value,
111  int>::type = 0>
112 constexpr bool operator==(A &&a, B &&b)
113 {
114  return static_cast<bool>(a) == static_cast<bool>(b);
115 }
116 template <typename A,
117  typename B,
118  typename std::enable_if<
119  std::is_convertible<A, bool>::value &&std::is_convertible<B, bool>::value,
120  int>::type = 0>
121 constexpr bool operator!=(A &&a, B &&b)
122 {
123  return static_cast<bool>(a) != static_cast<bool>(b);
124 }
125 
126 static_assert(true == MaskBool<4>(true), "true == MaskBool<4>(true)");
127 static_assert(true != MaskBool<4>(false), "true != MaskBool<4>(false)");
128 
129 } // namespace Common
130 } // namespace Vc
131 
132 #endif // VC_COMMON_MASKENTRY_H_