Vc  1.3.2-dev
SIMD Vector Classes for C++
maskbool.h
1 /* This file is part of the Vc library. {{{
2 Copyright © 2013-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_MASKENTRY_H_
29 #define VC_COMMON_MASKENTRY_H_
30 
31 #include "macros.h"
32 
33 namespace Vc_VERSIONED_NAMESPACE
34 {
35 namespace Common
36 {
37 
38 namespace
39 {
40  template<size_t Bytes> struct MaskBoolStorage;
41  // the following for typedefs must use std::intN_t and NOT! Vc::intN_t. The latter
42  // segfaults ICC 15.0.3.
43  template<> struct MaskBoolStorage<1> { typedef std::int8_t type; };
44  template<> struct MaskBoolStorage<2> { typedef std::int16_t type; };
45  template<> struct MaskBoolStorage<4> { typedef std::int32_t type; };
46  template<> struct MaskBoolStorage<8> { typedef std::int64_t type; };
47 } // anonymous namespace
48 
49 template<size_t Bytes> class MaskBool
50 {
51  typedef typename MaskBoolStorage<Bytes>::type storage_type Vc_MAY_ALIAS;
52  storage_type data;
53 public:
54  constexpr MaskBool(bool x) noexcept : data(x ? -1 : 0) {}
55  Vc_ALWAYS_INLINE MaskBool &operator=(bool x) noexcept { data = x ? -1 : 0; return *this; }
56  template <typename T, typename = enable_if<(!std::is_same<T, bool>::value &&
57  std::is_fundamental<T>::value)>>
58  Vc_ALWAYS_INLINE MaskBool &operator=(T x) noexcept
59  {
60  data = reinterpret_cast<const storage_type &>(x);
61  return *this;
62  }
63 
64  Vc_ALWAYS_INLINE MaskBool(const MaskBool &) noexcept = default;
65  Vc_ALWAYS_INLINE MaskBool &operator=(const MaskBool &) noexcept = default;
66 
67  template <typename T, typename = enable_if<(std::is_same<T, bool>::value ||
68  (std::is_fundamental<T>::value &&
69  sizeof(storage_type) == sizeof(T)))>>
70  constexpr operator T() const noexcept
71  {
72  return std::is_same<T, bool>::value ? T((data & 1) != 0)
73  : reinterpret_cast<const MayAlias<T> &>(data);
74  }
75 } Vc_MAY_ALIAS;
76 
77 template <typename A,
78  typename B,
79  typename std::enable_if<
80  std::is_convertible<A, bool>::value &&std::is_convertible<B, bool>::value,
81  int>::type = 0>
82 constexpr bool operator==(A &&a, B &&b)
83 {
84  return static_cast<bool>(a) == static_cast<bool>(b);
85 }
86 template <typename A,
87  typename B,
88  typename std::enable_if<
89  std::is_convertible<A, bool>::value &&std::is_convertible<B, bool>::value,
90  int>::type = 0>
91 constexpr bool operator!=(A &&a, B &&b)
92 {
93  return static_cast<bool>(a) != static_cast<bool>(b);
94 }
95 
96 static_assert(true == MaskBool<4>(true), "true == MaskBool<4>(true)");
97 static_assert(true != MaskBool<4>(false), "true != MaskBool<4>(false)");
98 
99 } // namespace Common
100 } // namespace Vc
101 
102 #endif // VC_COMMON_MASKENTRY_H_
Definition: vector.h:257
result_vector_type< L, R >::mask_type operator!=(L &&lhs, R &&rhs)
Applies != component-wise and concurrently.
Definition: simdarray.h:1642
result_vector_type< L, R >::mask_type operator==(L &&lhs, R &&rhs)
Applies == component-wise and concurrently.
Definition: simdarray.h:1642