DPC++ Runtime
Runtime libraries for oneAPI DPC++
builtins_utils_vec.hpp
Go to the documentation of this file.
1 //==--- builtins_utils_vec.hpp - SYCL built-in function utilities for vec --==//
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 
13 #include <sycl/marray.hpp> // for marray
14 #include <sycl/types.hpp> // for vec
15 
16 namespace sycl {
17 inline namespace _V1 {
18 namespace detail {
19 template <typename> struct is_swizzle : std::false_type {};
20 template <typename VecT, typename OperationLeftT, typename OperationRightT,
21  template <typename> class OperationCurrentT, int... Indexes>
22 struct is_swizzle<SwizzleOp<VecT, OperationLeftT, OperationRightT,
23  OperationCurrentT, Indexes...>> : std::true_type {};
24 
25 template <typename T> constexpr bool is_swizzle_v = is_swizzle<T>::value;
26 
27 template <typename T>
28 constexpr bool is_vec_or_swizzle_v = is_vec_v<T> || is_swizzle_v<T>;
29 
30 // Utility trait for checking if T's element type is in Ts.
31 template <typename T, size_t N, typename... Ts>
32 struct is_valid_elem_type<marray<T, N>, Ts...>
33  : std::bool_constant<check_type_in_v<T, Ts...>> {};
34 template <typename T, int N, typename... Ts>
35 struct is_valid_elem_type<vec<T, N>, Ts...>
36  : std::bool_constant<check_type_in_v<T, Ts...>> {};
37 template <typename VecT, typename OperationLeftT, typename OperationRightT,
38  template <typename> class OperationCurrentT, int... Indexes,
39  typename... Ts>
40 struct is_valid_elem_type<SwizzleOp<VecT, OperationLeftT, OperationRightT,
41  OperationCurrentT, Indexes...>,
42  Ts...>
43  : std::bool_constant<check_type_in_v<typename VecT::element_type, Ts...>> {
44 };
45 template <typename ElementType, access::address_space Space,
46  access::decorated DecorateAddress, typename... Ts>
47 struct is_valid_elem_type<multi_ptr<ElementType, Space, DecorateAddress>, Ts...>
48  : std::bool_constant<check_type_in_v<ElementType, Ts...>> {};
49 
50 // Utility trait for getting the number of elements in T.
51 template <typename T>
52 struct num_elements : std::integral_constant<size_t, 1> {};
53 template <typename T, size_t N>
54 struct num_elements<marray<T, N>> : std::integral_constant<size_t, N> {};
55 template <typename T, int N>
56 struct num_elements<vec<T, N>> : std::integral_constant<size_t, size_t(N)> {};
57 template <typename VecT, typename OperationLeftT, typename OperationRightT,
58  template <typename> class OperationCurrentT, int... Indexes>
59 struct num_elements<SwizzleOp<VecT, OperationLeftT, OperationRightT,
60  OperationCurrentT, Indexes...>>
61  : std::integral_constant<size_t, sizeof...(Indexes)> {};
62 
63 // Utilty trait for checking that the number of elements in T is in Ns.
64 template <typename T, size_t... Ns>
66  : std::bool_constant<check_size_in_v<num_elements<T>::value, Ns...>> {};
67 
68 template <typename T, int... Ns>
69 constexpr bool is_valid_size_v = is_valid_size<T, Ns...>::value;
70 
71 // Utility for converting a swizzle to a vector or preserve the type if it isn't
72 // a swizzle.
73 template <typename VecT, typename OperationLeftT, typename OperationRightT,
74  template <typename> class OperationCurrentT, int... Indexes>
75 struct simplify_if_swizzle<SwizzleOp<VecT, OperationLeftT, OperationRightT,
76  OperationCurrentT, Indexes...>> {
77  using type = vec<typename VecT::element_type, sizeof...(Indexes)>;
78 };
79 
80 template <typename T1, typename T2>
81 struct is_same_op<
82  T1, T2,
83  std::enable_if_t<is_vec_or_swizzle_v<T1> && is_vec_or_swizzle_v<T2>>>
84  : std::is_same<simplify_if_swizzle_t<T1>, simplify_if_swizzle_t<T2>> {};
85 
86 template <typename T, size_t N> struct same_size_signed_int<marray<T, N>> {
88 };
89 template <typename T, int N> struct same_size_signed_int<vec<T, N>> {
91 };
92 template <typename VecT, typename OperationLeftT, typename OperationRightT,
93  template <typename> class OperationCurrentT, int... Indexes>
94 struct same_size_signed_int<SwizzleOp<VecT, OperationLeftT, OperationRightT,
95  OperationCurrentT, Indexes...>> {
96  // Converts to vec for simplicity.
97  using type =
99  sizeof...(Indexes)>;
100 };
101 
102 template <typename T, size_t N> struct same_size_unsigned_int<marray<T, N>> {
104 };
105 template <typename T, int N> struct same_size_unsigned_int<vec<T, N>> {
107 };
108 template <typename VecT, typename OperationLeftT, typename OperationRightT,
109  template <typename> class OperationCurrentT, int... Indexes>
110 struct same_size_unsigned_int<SwizzleOp<VecT, OperationLeftT, OperationRightT,
111  OperationCurrentT, Indexes...>> {
112  // Converts to vec for simplicity.
113  using type =
115  sizeof...(Indexes)>;
116 };
117 
118 // Utility trait for changing the element type of a type T. If T is a scalar,
119 // the new type replaces T completely.
120 template <typename NewElemT, typename T> struct change_elements {
121  using type = NewElemT;
122 };
123 template <typename NewElemT, typename T, size_t N>
124 struct change_elements<NewElemT, marray<T, N>> {
126 };
127 template <typename NewElemT, typename T, int N>
128 struct change_elements<NewElemT, vec<T, N>> {
130 };
131 template <typename NewElemT, typename VecT, typename OperationLeftT,
132  typename OperationRightT, template <typename> class OperationCurrentT,
133  int... Indexes>
134 struct change_elements<NewElemT,
135  SwizzleOp<VecT, OperationLeftT, OperationRightT,
136  OperationCurrentT, Indexes...>> {
137  // Converts to vec for simplicity.
138  using type =
140  sizeof...(Indexes)>;
141 };
142 
143 template <typename NewElemT, typename T>
145 
146 // Utility functions for converting to/from vec/marray.
147 template <class T, size_t N> vec<T, 2> to_vec2(marray<T, N> X, size_t Start) {
148  return {X[Start], X[Start + 1]};
149 }
150 template <class T, size_t N> vec<T, N> to_vec(marray<T, N> X) {
151  vec<T, N> Vec;
152  for (size_t I = 0; I < N; I++)
153  Vec[I] = X[I];
154  return Vec;
155 }
156 template <class T, int N> marray<T, N> to_marray(vec<T, N> X) {
157  marray<T, N> Marray;
158  for (size_t I = 0; I < N; I++)
159  Marray[I] = X[I];
160  return Marray;
161 }
162 
163 } // namespace detail
164 } // namespace _V1
165 } // namespace sycl
Provides a cross-platform math array class template that works on SYCL devices as well as in host C++...
Definition: marray.hpp:49
class sycl::vec ///////////////////////// Provides a cross-patform vector class template that works e...
constexpr bool is_vec_or_swizzle_v
marray< T, N > to_marray(vec< T, N > X)
constexpr bool is_valid_size_v
typename change_elements< NewElemT, T >::type change_elements_t
vec< T, N > to_vec(marray< T, N > X)
vec< T, 2 > to_vec2(marray< T, N > X, size_t Start)
constexpr bool is_swizzle_v
Definition: access.hpp:18