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