DPC++ Runtime
Runtime libraries for oneAPI DPC++
builtins_utils_scalar.hpp
Go to the documentation of this file.
1 //==--- builtins_utils_scalar.hpp - SYCL built-in function utilities -------==//
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/access/access.hpp> // for address_space, decorated
12 #include <sycl/aliases.hpp> // for half
13 #include <sycl/detail/defines_elementary.hpp> // for __SYCL_ALWAYS_INLINE
14 #include <sycl/detail/generic_type_traits.hpp> // for is_svgenfloat, is_sge...
15 #include <sycl/detail/type_list.hpp> // for is_contained, type_list
16 #include <sycl/detail/type_traits.hpp> // for make_larger_t, marray...
17 #include <sycl/half_type.hpp> // for half, intel
18 #include <sycl/multi_ptr.hpp> // for address_space_cast
19 
20 #include <algorithm>
21 #include <cstring>
22 
23 namespace sycl {
24 inline namespace _V1 {
25 
26 namespace detail {
27 #ifdef __FAST_MATH__
28 template <typename T>
29 struct use_fast_math
30  : std::is_same<std::remove_cv_t<get_elem_type_t<T>>, float> {};
31 #else
32 template <typename> struct use_fast_math : std::false_type {};
33 #endif
34 template <typename T> constexpr bool use_fast_math_v = use_fast_math<T>::value;
35 
36 // Common utility for selecting a type based on the specified size.
37 template <size_t Size, typename T8, typename T16, typename T32, typename T64>
38 using select_scalar_by_size_t = std::conditional_t<
39  Size == 1, T8,
40  std::conditional_t<
41  Size == 2, T16,
42  std::conditional_t<Size == 4, T32,
43  std::conditional_t<Size == 8, T64, void>>>>;
44 
45 template <size_t N, size_t... Ns> constexpr bool CheckSizeIn() {
46  constexpr bool SameSize[] = {(N == Ns)...};
47  // Replace with std::any_of with C++20.
48  for (size_t I = 0; I < sizeof...(Ns); ++I)
49  if (SameSize[I])
50  return true;
51  return false;
52 }
53 
54 // Checks if the type of the operation is the same. For scalars and marray that
55 // requires the types to be exact matches. For vector and swizzles it requires
56 // that the corresponding vector conversion is the same.
57 template <typename T1, typename T2, typename = void>
58 struct is_same_op : std::is_same<T1, T2> {};
59 
60 template <typename T1, typename T2>
62 
63 // Constexpr function for checking that all types are the same, considering
64 // swizzles and vectors the same if they have the same number of elements and
65 // the same element type.
66 template <typename T, typename... Ts> constexpr bool CheckAllSameOpType() {
67  constexpr bool SameType[] = {
68  is_same_op_v<std::remove_cv_t<T>, std::remove_cv_t<Ts>>...};
69  // Replace with std::all_of with C++20.
70  for (size_t I = 0; I < sizeof...(Ts); ++I)
71  if (!SameType[I])
72  return false;
73  return true;
74 }
75 
76 // NOTE: We need a constexpr variable definition for the constexpr functions
77 // as MSVC thinks function definitions are the same otherwise.
78 template <typename... Ts>
79 constexpr bool check_all_same_op_type_v = CheckAllSameOpType<Ts...>();
80 // NOTE: We need a constexpr variable definition for the constexpr functions
81 // as MSVC thinks function definitions are the same otherwise.
82 template <size_t... Ns> constexpr bool check_size_in_v = CheckSizeIn<Ns...>();
83 
84 // Utility traits for getting a signed integer type with the specified size.
85 template <size_t Size> struct get_signed_int_by_size {
87 };
88 template <typename T> struct same_size_signed_int {
89  using type = typename get_signed_int_by_size<sizeof(T)>::type;
90 };
91 
92 template <typename T>
94 
95 // Utility traits for getting a unsigned integer type with the specified size.
96 template <size_t Size> struct get_unsigned_int_by_size {
97  using type =
99 };
100 template <typename T> struct same_size_unsigned_int {
101  using type = typename get_unsigned_int_by_size<sizeof(T)>::type;
102 };
103 template <typename T>
105 
106 template <typename T> struct get_fixed_sized_int {
107  static_assert(std::is_integral_v<T>);
108  using type =
109  std::conditional_t<std::is_signed_v<T>, same_size_signed_int_t<T>,
111 };
112 template <typename T>
114 
115 // Utility for converting a swizzle to a vector or preserve the type if it isn't
116 // a swizzle.
117 template <typename T> struct simplify_if_swizzle {
118  using type = T;
119 };
120 
121 template <typename T>
123 
124 // Utility trait for checking if T's element type is in Ts.
125 template <typename T, typename... Ts>
126 struct is_valid_elem_type : std::false_type {};
127 
128 template <typename T, typename... Ts>
129 constexpr bool is_valid_elem_type_v = is_valid_elem_type<T, Ts...>::value;
130 
131 // Utility trait for getting the decoration of a multi_ptr.
132 template <typename T> struct get_multi_ptr_decoration;
133 template <typename ElementType, access::address_space Space,
134  access::decorated DecorateAddress>
136  multi_ptr<ElementType, Space, DecorateAddress>> {
137  static constexpr access::decorated value = DecorateAddress;
138 };
139 
140 template <typename T>
143 
144 // Utility trait for checking if a multi_ptr has a "writable" address space,
145 // i.e. global, local, private or generic.
146 template <typename T> struct has_writeable_addr_space : std::false_type {};
147 template <typename ElementType, access::address_space Space,
148  access::decorated DecorateAddress>
149 struct has_writeable_addr_space<multi_ptr<ElementType, Space, DecorateAddress>>
150  : std::bool_constant<Space == access::address_space::global_space ||
151  Space == access::address_space::local_space ||
152  Space == access::address_space::private_space ||
153  Space == access::address_space::generic_space> {};
154 
155 template <typename T>
157 
158 } // namespace detail
159 } // namespace _V1
160 } // namespace sycl
constexpr bool check_all_same_op_type_v
typename get_fixed_sized_int< T >::type get_fixed_sized_int_t
typename same_size_unsigned_int< T >::type same_size_unsigned_int_t
constexpr bool check_size_in_v
std::conditional_t< Size==1, T8, std::conditional_t< Size==2, T16, std::conditional_t< Size==4, T32, std::conditional_t< Size==8, T64, void > >> > select_scalar_by_size_t
constexpr bool is_valid_elem_type_v
constexpr access::decorated get_multi_ptr_decoration_v
constexpr bool CheckSizeIn()
typename simplify_if_swizzle< T >::type simplify_if_swizzle_t
constexpr bool CheckAllSameOpType()
constexpr bool use_fast_math_v
typename same_size_signed_int< T >::type same_size_signed_int_t
constexpr bool has_writeable_addr_space_v
Definition: access.hpp:18
std::conditional_t< std::is_signed_v< T >, same_size_signed_int_t< T >, same_size_unsigned_int_t< T > > type
select_scalar_by_size_t< Size, int8_t, int16_t, int32_t, int64_t > type
select_scalar_by_size_t< Size, uint8_t, uint16_t, uint32_t, uint64_t > type
typename get_signed_int_by_size< sizeof(T)>::type type
typename get_unsigned_int_by_size< sizeof(T)>::type type