rocPRIM
type_traits.hpp
1 // Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef ROCPRIM_TYPE_TRAITS_HPP_
22 #define ROCPRIM_TYPE_TRAITS_HPP_
23 
24 #include <type_traits>
25 
26 // Meta configuration for rocPRIM
27 #include "config.hpp"
28 #include "types.hpp"
29 
32 
33 BEGIN_ROCPRIM_NAMESPACE
34 
37 template<class T>
39  : std::integral_constant<
40  bool,
41  std::is_floating_point<T>::value ||
42  std::is_same<::rocprim::half, typename std::remove_cv<T>::type>::value ||
43  std::is_same<::rocprim::bfloat16, typename std::remove_cv<T>::type>::value
44  > {};
45 
47 template<class T>
48 using is_integral = std::is_integral<T>;
49 
52 template<class T>
54  : std::integral_constant<
55  bool,
56  std::is_arithmetic<T>::value ||
57  std::is_same<::rocprim::half, typename std::remove_cv<T>::type>::value ||
58  std::is_same<::rocprim::bfloat16, typename std::remove_cv<T>::type>::value
59  > {};
60 
63 template<class T>
65  : std::integral_constant<
66  bool,
67  std::is_fundamental<T>::value ||
68  std::is_same<::rocprim::half, typename std::remove_cv<T>::type>::value ||
69  std::is_same<::rocprim::bfloat16, typename std::remove_cv<T>::type>::value
70 > {};
71 
73 template<class T>
74 using is_unsigned = std::is_unsigned<T>;
75 
78 template<class T>
79 struct is_signed
80  : std::integral_constant<
81  bool,
82  std::is_signed<T>::value ||
83  std::is_same<::rocprim::half, typename std::remove_cv<T>::type>::value ||
84  std::is_same<::rocprim::bfloat16, typename std::remove_cv<T>::type>::value
85  > {};
86 
89 template<class T>
90 struct is_scalar
91  : std::integral_constant<
92  bool,
93  std::is_scalar<T>::value ||
94  std::is_same<::rocprim::half, typename std::remove_cv<T>::type>::value ||
95  std::is_same<::rocprim::bfloat16, typename std::remove_cv<T>::type>::value
96  > {};
97 
100 template<class T>
102  : std::integral_constant<
103  bool,
104  !is_fundamental<T>::value
105  > {};
106 
110 template<typename T, int size = 0>
112 {
114 };
115 
116 #ifndef DOXYGEN_SHOULD_SKIP_THIS // skip specialized versions
117 template<typename T>
119 {
120  typedef uint8_t unsigned_type;
121 };
122 
123 
124 template<typename T>
126 {
127  typedef uint16_t unsigned_type;
128 };
129 
130 
131 template<typename T>
133 {
134  typedef uint32_t unsigned_type;
135 };
136 
137 template<typename T>
139 {
140  typedef uint64_t unsigned_type;
141 };
142 #endif // DOXYGEN_SHOULD_SKIP_THIS
143 
144 #ifndef DOXYGEN_SHOULD_SKIP_THIS
145 template<typename T, typename UnsignedBits>
146 ROCPRIM_DEVICE ROCPRIM_INLINE
147 auto TwiddleIn(UnsignedBits key)
148  -> typename std::enable_if<is_floating_point<T>::value, UnsignedBits>::type
149 {
150  static const UnsignedBits HIGH_BIT = UnsignedBits(1) << ((sizeof(UnsignedBits) * 8) - 1);
151  UnsignedBits mask = (key & HIGH_BIT) ? UnsignedBits(-1) : HIGH_BIT;
152  return key ^ mask;
153 }
154 
155 template<typename T, typename UnsignedBits>
156 static ROCPRIM_DEVICE ROCPRIM_INLINE
157 auto TwiddleIn(UnsignedBits key)
158  -> typename std::enable_if<is_unsigned<T>::value, UnsignedBits>::type
159 {
160  return key ;
161 };
162 
163 template<typename T, typename UnsignedBits>
164 static ROCPRIM_DEVICE ROCPRIM_INLINE
165 auto TwiddleIn(UnsignedBits key)
166  -> typename std::enable_if<is_integral<T>::value && is_signed<T>::value, UnsignedBits>::type
167 {
168  static const UnsignedBits HIGH_BIT = UnsignedBits(1) << ((sizeof(UnsignedBits) * 8) - 1);
169  return key ^ HIGH_BIT;
170 };
171 
172 template<typename T, typename UnsignedBits>
173 ROCPRIM_DEVICE ROCPRIM_INLINE
174 auto TwiddleOut(UnsignedBits key)
175  -> typename std::enable_if<is_floating_point<T>::value, UnsignedBits>::type
176 {
177  static const UnsignedBits HIGH_BIT = UnsignedBits(1) << ((sizeof(UnsignedBits) * 8) - 1);
178  UnsignedBits mask = (key & HIGH_BIT) ? HIGH_BIT : UnsignedBits(-1);
179  return key ^ mask;
180 }
181 
182 template<typename T, typename UnsignedBits>
183 static ROCPRIM_DEVICE ROCPRIM_INLINE
184 auto TwiddleOut(UnsignedBits key)
185  -> typename std::enable_if<is_unsigned<T>::value, UnsignedBits>::type
186 {
187  return key;
188 };
189 
190 template<typename T, typename UnsignedBits>
191 static ROCPRIM_DEVICE ROCPRIM_INLINE
192 auto TwiddleOut(UnsignedBits key)
193  -> typename std::enable_if<is_integral<T>::value && is_signed<T>::value, UnsignedBits>::type
194 {
195  static const UnsignedBits HIGH_BIT = UnsignedBits(1) << ((sizeof(UnsignedBits) * 8) - 1);
196  return key ^ HIGH_BIT;
197 };
198 #endif // DOXYGEN_SHOULD_SKIP_THIS
199 
200 
201 END_ROCPRIM_NAMESPACE
202 
204 // end of group utilsmodule_typetraits
205 
206 #endif // ROCPRIM_TYPE_TRAITS_HPP_
Behaves like std::is_floating_point, but also includes half-precision and bfloat16-precision floating...
Definition: type_traits.hpp:38
Used to retrieve a type that can be treated as unsigned version of the template parameter.
Definition: type_traits.hpp:111
std::is_integral< T > is_integral
Alias for std::is_integral.
Definition: type_traits.hpp:48
Behaves like std::is_fundamental, but also includes half-precision and bfloat16-precision floating po...
Definition: type_traits.hpp:64
Behaves like std::is_arithmetic, but also includes half-precision and bfloat16-precision floating poi...
Definition: type_traits.hpp:53
std::is_unsigned< T > is_unsigned
Alias for std::is_unsigned.
Definition: type_traits.hpp:74
Behaves like std::is_scalar, but also includes half-precision and bfloat16-precision floating point t...
Definition: type_traits.hpp:90
get_unsigned_bits_type< T, sizeof(T)>::unsigned_type unsigned_type
Typedefed to the unsigned type.
Definition: type_traits.hpp:113
Behaves like std::is_compound, but also supports half-precision floating point type (rocprim::half)...
Definition: type_traits.hpp:101
Behaves like std::is_signed, but also includes half-precision and bfloat16-precision floating point t...
Definition: type_traits.hpp:79