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