DPC++ Runtime
Runtime libraries for oneAPI DPC++
boolean.hpp
Go to the documentation of this file.
1 //==----------- boolean.hpp - SYCL boolean type ----------------------------==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #pragma once
10 
12 #include <sycl/types.hpp>
13 
14 #include <initializer_list>
15 #include <type_traits>
16 
17 namespace sycl {
19 namespace detail {
20 
21 template <int Num> struct Assigner {
22  template <typename R, typename T> static void assign(R &r, const T x) {
24  r.template swizzle<Num>() = x.value[Num];
25  }
26 
27  template <typename R, typename T, typename ET>
28  static void init(R &r, const T x) {
29  Assigner<Num - 1>::template init<R, T, ET>(r, x);
30  ET v = x.template swizzle<Num>();
31  r.value[Num] = msbIsSet(v) * (-1);
32  }
33 };
34 
35 template <> struct Assigner<0> {
36  template <typename R, typename T> static void assign(R &r, const T x) {
37  r.template swizzle<0>() = x.value[0];
38  }
39  template <typename R, typename T, typename ET>
40  static void init(R &r, const T x) {
41  ET v = x.template swizzle<0>();
42  r.value[0] = msbIsSet(v) * (-1);
43  }
44 };
45 
46 template <int N> struct Boolean {
47  static_assert(((N == 2) || (N == 3) || (N == 4) || (N == 8) || (N == 16)),
48  "Invalid size");
49 
50  using element_type = int8_t;
51 
52 #ifdef __SYCL_DEVICE_ONLY__
53  using DataType = element_type __attribute__((ext_vector_type(N)));
54  using vector_t = DataType;
55 #else
56  using DataType = element_type[N];
57 #endif
58 
59  Boolean() : value{0} {}
60 
61  Boolean(std::initializer_list<element_type> l) {
62  for (size_t I = 0; I < N; ++I) {
63  value[I] = *(l.begin() + I) ? -1 : 0;
64  }
65  }
66 
67  Boolean(const Boolean &rhs) {
68  for (size_t I = 0; I < N; ++I) {
69  value[I] = rhs.value[I];
70  }
71  }
72 
73  template <typename T> Boolean(const T rhs) {
74  static_assert(is_vgeninteger<T>::value, "Invalid constructor");
75  Assigner<N - 1>::template init<Boolean<N>, T, typename T::element_type>(
76  *this, rhs);
77  }
78 
79 #ifdef __SYCL_DEVICE_ONLY__
80  // TODO change this to the vectors assignment when the assignment will be
81  // fixed on Intel GPU NEO OpenCL runtime
82  Boolean(const vector_t rhs) {
83  for (size_t I = 0; I < N; ++I) {
84  value[I] = rhs[I];
85  }
86  }
87 
88  operator vector_t() const { return value; }
89 #endif
90 
91  template <typename T> operator T() const {
92  static_assert(is_vgeninteger<T>::value, "Invalid conversion");
93  T r;
94  Assigner<N - 1>::assign(r, *this);
95  return r;
96  }
97 
98 private:
99  template <int Num> friend struct Assigner;
101 };
102 
103 template <> struct Boolean<1> {
104  Boolean() = default;
105 
106  // Build from a signed interger type
107  template <typename T> Boolean(T val) : value(val) {
108  static_assert(is_sgeninteger<T>::value, "Invalid constructor");
109  }
110 
111  // Cast to a signed interger type
112  template <typename T> operator T() const {
113  static_assert(std::is_same<T, bool>::value || is_sgeninteger<T>::value,
114  "Invalid conversion");
115  return value;
116  }
117 
118 #ifdef __SYCL_DEVICE_ONLY__
119  // Build from a boolean type
120  Boolean(bool f) : value(f) {}
121  // Cast to a boolean type
122  operator bool() const { return value; }
123 #endif
124 
125 private:
126  alignas(1) bool value = false;
127 };
128 
129 } // namespace detail
130 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
131 } // namespace sycl
#define __SYCL_INLINE_VER_NAMESPACE(X)
constexpr bool msbIsSet(const T x)
__attribute__((always_inline)) auto invoke_simd(sycl
The invoke_simd free function invokes a SIMD function using all work-items in a sub_group.
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
static void assign(R &r, const T x)
Definition: boolean.hpp:36
static void init(R &r, const T x)
Definition: boolean.hpp:40
static void init(R &r, const T x)
Definition: boolean.hpp:28
static void assign(R &r, const T x)
Definition: boolean.hpp:22
Boolean(const Boolean &rhs)
Definition: boolean.hpp:67
element_type[N] DataType
Definition: boolean.hpp:56
Boolean(std::initializer_list< element_type > l)
Definition: boolean.hpp:61