type.hpp Source File

type.hpp Source File#

Composable Kernel: type.hpp Source File
type.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2018-2025, Advanced Micro Devices, Inc. All rights reserved.
3
4#pragma once
5
6#include "ck/ck.hpp"
9
10namespace ck {
11#if defined(__HIPCC_RTC__) || defined(CK_CODE_GEN_RTC)
12// NOLINTNEXTLINE
13#define CK_BUILTIN_TYPE_TRAIT1(name) \
14 template <class T> \
15 struct name : bool_constant<__##name(T)> \
16 { \
17 }
18
19// NOLINTNEXTLINE
20#define CK_BUILTIN_TYPE_TRAIT2(name) \
21 template <class T, class U> \
22 struct name : bool_constant<__##name(T, U)> \
23 { \
24 }
25
26// NOLINTNEXTLINE
27#define CK_BUILTIN_TYPE_TRAITN(name) \
28 template <class... Ts> \
29 struct name : bool_constant<__##name(Ts...)> \
30 { \
31 }
32
33CK_BUILTIN_TYPE_TRAIT1(is_class);
34CK_BUILTIN_TYPE_TRAIT1(is_pointer);
35CK_BUILTIN_TYPE_TRAIT1(is_reference);
36CK_BUILTIN_TYPE_TRAIT1(is_trivially_copyable);
37CK_BUILTIN_TYPE_TRAIT1(is_unsigned);
38CK_BUILTIN_TYPE_TRAIT2(is_base_of);
39
40template <class T>
41struct remove_cv
42{
43 using type = T;
44};
45
46template <class T>
47struct remove_cv<const T> : remove_cv<T>
48{
49};
50
51template <class T>
52struct remove_cv<volatile T> : remove_cv<T>
53{
54};
55
56template <class T>
57struct remove_reference
58{
59 typedef T type;
60};
61template <class T>
62struct remove_reference<T&>
63{
64 typedef T type;
65};
66template <class T>
67struct remove_reference<T&&>
68{
69 typedef T type;
70};
71template <class T>
72struct remove_pointer
73{
74 typedef T type;
75};
76template <class T>
77struct remove_pointer<T*>
78{
79 typedef T type;
80};
81template <class T>
82struct remove_pointer<T* const>
83{
84 typedef T type;
85};
86template <class T>
87struct remove_pointer<T* volatile>
88{
89 typedef T type;
90};
91template <class T>
92struct remove_pointer<T* const volatile>
93{
94 typedef T type;
95};
96
97template <typename T>
98constexpr T&& forward(typename remove_reference<T>::type& t_) noexcept
99{
100 return static_cast<T&&>(t_);
101}
102template <typename T>
103constexpr T&& forward(typename remove_reference<T>::type&& t_) noexcept
104{
105 return static_cast<T&&>(t_);
106}
107
108template <class T>
109struct is_const : public integral_constant<bool, false>
110{
111};
112template <class T>
113struct is_const<const T> : public integral_constant<bool, true>
114{
115};
116template <class T>
117inline constexpr bool is_const_v = is_const<T>::value;
118
119template <typename T>
120inline constexpr bool is_reference_v = is_reference<T>::value;
121
122template <class T>
123struct remove_const
124{
125 typedef T type;
126};
127template <class T>
128struct remove_const<const T>
129{
130 typedef T type;
131};
132template <class T>
133using remove_const_t = typename remove_const<T>::type;
134template <class T>
135inline constexpr bool is_class_v = is_class<T>::value;
136
137template <class T>
138inline constexpr bool is_trivially_copyable_v = is_trivially_copyable<T>::value;
139// template <typename T>
140// T&& declval() noexcept;
141
142template <class T, class U = T&&>
143U private_declval(int);
144
145template <class T>
146T private_declval(long);
147
148template <class T>
149auto declval() noexcept -> decltype(private_declval<T>(0));
150
151template <class...>
152using void_t = void;
153#else
154#include <utility>
155#include <type_traits>
156using std::declval;
157using std::forward;
158using std::is_base_of;
159using std::is_class;
160using std::is_class_v;
161using std::is_const_v;
162using std::is_pointer;
163using std::is_reference;
164using std::is_reference_v;
165using std::is_trivially_copyable;
166using std::is_trivially_copyable_v;
167using std::is_unsigned;
168using std::remove_const_t;
169using std::remove_cv;
170using std::remove_pointer;
171using std::remove_reference;
172using std::void_t;
173#endif
174
175template <typename X, typename Y>
176struct is_same : public integral_constant<bool, false>
177{
178};
179
180template <typename X>
181struct is_same<X, X> : public integral_constant<bool, true>
182{
183};
184
185template <typename X>
186struct is_floating_point : public integral_constant<bool, false>
187{
188};
189
190template <>
191struct is_floating_point<float> : public integral_constant<bool, true>
192{
193};
194
195template <>
196struct is_floating_point<double> : public integral_constant<bool, true>
197{
198};
199template <>
200struct is_floating_point<long double> : public integral_constant<bool, true>
201{
202};
203
204template <typename X>
205struct is_integral : public integral_constant<bool, false>
206{
207};
208
209template <>
210struct is_integral<int> : public integral_constant<bool, true>
211{
212};
213
214template <>
215struct is_integral<unsigned int> : public integral_constant<bool, true>
216{
217};
218
219template <>
220struct is_integral<long> : public integral_constant<bool, true>
221{
222};
223
224template <>
225struct is_integral<unsigned long> : public integral_constant<bool, true>
226{
227};
228
229template <>
230struct is_integral<short> : public integral_constant<bool, true>
231{
232};
233template <>
234struct is_integral<unsigned short> : public integral_constant<bool, true>
235{
236};
237
238template <>
239struct is_integral<long long> : public integral_constant<bool, true>
240{
241};
242
243template <>
244struct is_integral<unsigned long long> : public integral_constant<bool, true>
245{
246};
247
248template <>
249struct is_integral<char> : public integral_constant<bool, true>
250{
251};
252
253template <>
254struct is_integral<signed char> : public integral_constant<bool, true>
255{
256};
257
258template <>
259struct is_integral<unsigned char> : public integral_constant<bool, true>
260{
261};
262
263template <>
264struct is_integral<wchar_t> : public integral_constant<bool, true>
265{
266};
267template <>
268struct is_integral<char16_t> : public integral_constant<bool, true>
269{
270};
271
272template <>
273struct is_integral<char32_t> : public integral_constant<bool, true>
274{
275};
276
277template <>
278struct is_integral<bool> : public integral_constant<bool, true>
279{
280};
281
282template <typename X, typename Y>
283inline constexpr bool is_same_v = is_same<X, Y>::value;
284
285template <typename X, typename Y>
286inline constexpr bool is_base_of_v = is_base_of<X, Y>::value;
287
288template <typename T>
289inline constexpr bool is_unsigned_v = is_unsigned<T>::value;
290
291template <typename T>
292using remove_reference_t = typename remove_reference<T>::type;
293
294template <typename T>
295using remove_cv_t = typename remove_cv<T>::type;
296template <typename T>
298
299template <typename T>
300using remove_pointer_t = typename remove_pointer<T>::type;
301
302template <typename T>
303inline constexpr bool is_pointer_v = is_pointer<T>::value;
304
305template <typename Y, typename X, typename enable_if<sizeof(X) == sizeof(Y), bool>::type = false>
306__host__ __device__ constexpr Y bit_cast(const X& x)
307{
308 static_assert(__has_builtin(__builtin_bit_cast), "");
309 static_assert(sizeof(X) == sizeof(Y), "Do not support cast between different size of type");
310
311 return __builtin_bit_cast(Y, x);
312}
313} // namespace ck
Definition ck.hpp:268
remove_cv_t< remove_reference_t< T > > remove_cvref_t
Definition type.hpp:297
constexpr bool is_pointer_v
Definition type.hpp:303
constexpr bool is_base_of_v
Definition type.hpp:286
constexpr bool is_unsigned_v
Definition type.hpp:289
constexpr bool is_same_v
Definition type.hpp:283
typename remove_reference< T >::type remove_reference_t
Definition type.hpp:292
__host__ __device__ constexpr Y bit_cast(const X &x)
Definition type.hpp:306
typename remove_cv< T >::type remove_cv_t
Definition type.hpp:295
typename remove_pointer< T >::type remove_pointer_t
Definition type.hpp:300
static constexpr value_type value
Definition utility/integral_constant.hpp:13
Definition utility/integral_constant.hpp:20
Definition type.hpp:187
Definition type.hpp:206
Definition type.hpp:177