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 
11 #include <sycl/detail/generic_type_traits.hpp> // for is_sgeninteger, msbIsSet
12 #include <sycl/detail/vector_traits.hpp> // for vector_alignment
13 
14 #include <initializer_list> // for initializer_list
15 #include <stddef.h> // for size_t
16 #include <stdint.h> // for int8_t
17 #include <type_traits> // for is_same
18 
19 namespace sycl {
20 inline namespace _V1 {
21 namespace detail {
22 
23 template <int Num> struct Assigner {
24  template <typename R, typename T> static void assign(R &r, const T x) {
26  r.template swizzle<Num>() = x.value[Num];
27  }
28 
29  template <typename R, typename T, typename ET>
30  static void init(R &r, const T x) {
31  Assigner<Num - 1>::template init<R, T, ET>(r, x);
32  ET v = x.template swizzle<Num>();
33  r.value[Num] = msbIsSet(v) * (-1);
34  }
35 };
36 
37 template <> struct Assigner<0> {
38  template <typename R, typename T> static void assign(R &r, const T x) {
39  r.template swizzle<0>() = x.value[0];
40  }
41  template <typename R, typename T, typename ET>
42  static void init(R &r, const T x) {
43  ET v = x.template swizzle<0>();
44  r.value[0] = msbIsSet(v) * (-1);
45  }
46 };
47 
48 template <int N> struct Boolean {
49  static_assert(((N == 2) || (N == 3) || (N == 4) || (N == 8) || (N == 16)),
50  "Invalid size");
51 
52  using element_type = int8_t;
53 
54 #ifdef __SYCL_DEVICE_ONLY__
55  using DataType = element_type __attribute__((ext_vector_type(N)));
56  using vector_t = DataType;
57 #else
58  using DataType = element_type[N];
59 #endif
60 
61  Boolean() : value{0} {}
62 
63  Boolean(std::initializer_list<element_type> l) {
64  for (size_t I = 0; I < N; ++I) {
65  value[I] = *(l.begin() + I) ? -1 : 0;
66  }
67  }
68 
69  Boolean(const Boolean &rhs) {
70  for (size_t I = 0; I < N; ++I) {
71  value[I] = rhs.value[I];
72  }
73  }
74 
75  template <typename T> Boolean(const T rhs) {
76  static_assert(is_vgeninteger_v<T>, "Invalid constructor");
77  Assigner<N - 1>::template init<Boolean<N>, T, typename T::element_type>(
78  *this, rhs);
79  }
80 
81 #ifdef __SYCL_DEVICE_ONLY__
82  // TODO change this to the vectors assignment when the assignment will be
83  // fixed on Intel GPU NEO OpenCL runtime
84  Boolean(const vector_t rhs) {
85  for (size_t I = 0; I < N; ++I) {
86  value[I] = rhs[I];
87  }
88  }
89 
90  operator vector_t() const { return value; }
91 #endif
92 
93  template <typename T> operator T() const {
94  static_assert(is_vgeninteger_v<T>, "Invalid conversion");
95  T r;
96  Assigner<N - 1>::assign(r, *this);
97  return r;
98  }
99 
100 private:
101  template <int Num> friend struct Assigner;
103 };
104 
105 } // namespace detail
106 } // namespace _V1
107 } // namespace sycl
__attribute__((destructor(110))) static void syclUnload()
constexpr bool msbIsSet(const T x)
autodecltype(x) x
Definition: access.hpp:18
static void assign(R &r, const T x)
Definition: boolean.hpp:38
static void init(R &r, const T x)
Definition: boolean.hpp:42
static void init(R &r, const T x)
Definition: boolean.hpp:30
static void assign(R &r, const T x)
Definition: boolean.hpp:24
Boolean(const Boolean &rhs)
Definition: boolean.hpp:69
element_type[N] DataType
Definition: boolean.hpp:58
Boolean(std::initializer_list< element_type > l)
Definition: boolean.hpp:63