Kokkos Core Kernels Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Kokkos_NumericTraits.hpp
1 /*
2 //@HEADER
3 // ************************************************************************
4 //
5 // Kokkos v. 3.0
6 // Copyright (2020) National Technology & Engineering
7 // Solutions of Sandia, LLC (NTESS).
8 //
9 // Under the terms of Contract DE-NA0003525 with NTESS,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY NTESS "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NTESS OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Christian R. Trott (crtrott@sandia.gov)
40 //
41 // ************************************************************************
42 //@HEADER
43 */
44 
45 #ifndef KOKKOS_NUMERICTRAITS_HPP
46 #define KOKKOS_NUMERICTRAITS_HPP
47 
48 #include <climits>
49 #include <cfloat>
50 
51 namespace Kokkos {
52 
53 template <class T>
54 struct reduction_identity; /*{
55  KOKKOS_FORCEINLINE_FUNCTION constexpr static T sum() { return T(); } // 0
56  KOKKOS_FORCEINLINE_FUNCTION constexpr static T prod() // 1
57  { static_assert( false, "Missing specialization of
58 Kokkos::reduction_identity for custom prod reduction type"); return T(); }
59  KOKKOS_FORCEINLINE_FUNCTION constexpr static T max() // minimum value
60  { static_assert( false, "Missing specialization of
61 Kokkos::reduction_identity for custom max reduction type"); return T(); }
62  KOKKOS_FORCEINLINE_FUNCTION constexpr static T min() // maximum value
63  { static_assert( false, "Missing specialization of
64 Kokkos::reduction_identity for custom min reduction type"); return T(); }
65  KOKKOS_FORCEINLINE_FUNCTION constexpr static T bor() // 0, only for integer
66 type { static_assert( false, "Missing specialization of
67 Kokkos::reduction_identity for custom bor reduction type"); return T(); }
68  KOKKOS_FORCEINLINE_FUNCTION constexpr static T band() // !0, only for integer
69 type { static_assert( false, "Missing specialization of
70 Kokkos::reduction_identity for custom band reduction type"); return T(); }
71  KOKKOS_FORCEINLINE_FUNCTION constexpr static T lor() // 0, only for integer
72 type { static_assert( false, "Missing specialization of
73 Kokkos::reduction_identity for custom lor reduction type"); return T(); }
74  KOKKOS_FORCEINLINE_FUNCTION constexpr static T land() // !0, only for integer
75 type { static_assert( false, "Missing specialization of
76 Kokkos::reduction_identity for custom land reduction type"); return T(); }
77 };*/
78 
79 template <>
80 struct reduction_identity<signed char> {
81  KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char sum() {
82  return static_cast<signed char>(0);
83  }
84  KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char prod() {
85  return static_cast<signed char>(1);
86  }
87  KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char max() {
88  return SCHAR_MIN;
89  }
90  KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char min() {
91  return SCHAR_MAX;
92  }
93  KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char bor() {
94  return static_cast<signed char>(0x0);
95  }
96  KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char band() {
97  return ~static_cast<signed char>(0x0);
98  }
99  KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char lor() {
100  return static_cast<signed char>(0);
101  }
102  KOKKOS_FORCEINLINE_FUNCTION constexpr static signed char land() {
103  return static_cast<signed char>(1);
104  }
105 };
106 
107 template <>
108 struct reduction_identity<short> {
109  KOKKOS_FORCEINLINE_FUNCTION constexpr static short sum() {
110  return static_cast<short>(0);
111  }
112  KOKKOS_FORCEINLINE_FUNCTION constexpr static short prod() {
113  return static_cast<short>(1);
114  }
115  KOKKOS_FORCEINLINE_FUNCTION constexpr static short max() { return SHRT_MIN; }
116  KOKKOS_FORCEINLINE_FUNCTION constexpr static short min() { return SHRT_MAX; }
117  KOKKOS_FORCEINLINE_FUNCTION constexpr static short bor() {
118  return static_cast<short>(0x0);
119  }
120  KOKKOS_FORCEINLINE_FUNCTION constexpr static short band() {
121  return ~static_cast<short>(0x0);
122  }
123  KOKKOS_FORCEINLINE_FUNCTION constexpr static short lor() {
124  return static_cast<short>(0);
125  }
126  KOKKOS_FORCEINLINE_FUNCTION constexpr static short land() {
127  return static_cast<short>(1);
128  }
129 };
130 
131 template <>
132 struct reduction_identity<int> {
133  KOKKOS_FORCEINLINE_FUNCTION constexpr static int sum() {
134  return static_cast<int>(0);
135  }
136  KOKKOS_FORCEINLINE_FUNCTION constexpr static int prod() {
137  return static_cast<int>(1);
138  }
139  KOKKOS_FORCEINLINE_FUNCTION constexpr static int max() { return INT_MIN; }
140  KOKKOS_FORCEINLINE_FUNCTION constexpr static int min() { return INT_MAX; }
141  KOKKOS_FORCEINLINE_FUNCTION constexpr static int bor() {
142  return static_cast<int>(0x0);
143  }
144  KOKKOS_FORCEINLINE_FUNCTION constexpr static int band() {
145  return ~static_cast<int>(0x0);
146  }
147  KOKKOS_FORCEINLINE_FUNCTION constexpr static int lor() {
148  return static_cast<int>(0);
149  }
150  KOKKOS_FORCEINLINE_FUNCTION constexpr static int land() {
151  return static_cast<int>(1);
152  }
153 };
154 
155 template <>
156 struct reduction_identity<long> {
157  KOKKOS_FORCEINLINE_FUNCTION constexpr static long sum() {
158  return static_cast<long>(0);
159  }
160  KOKKOS_FORCEINLINE_FUNCTION constexpr static long prod() {
161  return static_cast<long>(1);
162  }
163  KOKKOS_FORCEINLINE_FUNCTION constexpr static long max() { return LONG_MIN; }
164  KOKKOS_FORCEINLINE_FUNCTION constexpr static long min() { return LONG_MAX; }
165  KOKKOS_FORCEINLINE_FUNCTION constexpr static long bor() {
166  return static_cast<long>(0x0);
167  }
168  KOKKOS_FORCEINLINE_FUNCTION constexpr static long band() {
169  return ~static_cast<long>(0x0);
170  }
171  KOKKOS_FORCEINLINE_FUNCTION constexpr static long lor() {
172  return static_cast<long>(0);
173  }
174  KOKKOS_FORCEINLINE_FUNCTION constexpr static long land() {
175  return static_cast<long>(1);
176  }
177 };
178 
179 template <>
180 struct reduction_identity<long long> {
181  KOKKOS_FORCEINLINE_FUNCTION constexpr static long long sum() {
182  return static_cast<long long>(0);
183  }
184  KOKKOS_FORCEINLINE_FUNCTION constexpr static long long prod() {
185  return static_cast<long long>(1);
186  }
187  KOKKOS_FORCEINLINE_FUNCTION constexpr static long long max() {
188  return LLONG_MIN;
189  }
190  KOKKOS_FORCEINLINE_FUNCTION constexpr static long long min() {
191  return LLONG_MAX;
192  }
193  KOKKOS_FORCEINLINE_FUNCTION constexpr static long long bor() {
194  return static_cast<long long>(0x0);
195  }
196  KOKKOS_FORCEINLINE_FUNCTION constexpr static long long band() {
197  return ~static_cast<long long>(0x0);
198  }
199  KOKKOS_FORCEINLINE_FUNCTION constexpr static long long lor() {
200  return static_cast<long long>(0);
201  }
202  KOKKOS_FORCEINLINE_FUNCTION constexpr static long long land() {
203  return static_cast<long long>(1);
204  }
205 };
206 
207 template <>
208 struct reduction_identity<unsigned char> {
209  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char sum() {
210  return static_cast<unsigned char>(0);
211  }
212  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char prod() {
213  return static_cast<unsigned char>(1);
214  }
215  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char max() {
216  return static_cast<unsigned char>(0);
217  }
218  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char min() {
219  return UCHAR_MAX;
220  }
221  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char bor() {
222  return static_cast<unsigned char>(0x0);
223  }
224  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char band() {
225  return ~static_cast<unsigned char>(0x0);
226  }
227  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char lor() {
228  return static_cast<unsigned char>(0);
229  }
230  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned char land() {
231  return static_cast<unsigned char>(1);
232  }
233 };
234 
235 template <>
236 struct reduction_identity<unsigned short> {
237  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short sum() {
238  return static_cast<unsigned short>(0);
239  }
240  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short prod() {
241  return static_cast<unsigned short>(1);
242  }
243  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short max() {
244  return static_cast<unsigned short>(0);
245  }
246  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short min() {
247  return USHRT_MAX;
248  }
249  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short bor() {
250  return static_cast<unsigned short>(0x0);
251  }
252  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short band() {
253  return ~static_cast<unsigned short>(0x0);
254  }
255  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short lor() {
256  return static_cast<unsigned short>(0);
257  }
258  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned short land() {
259  return static_cast<unsigned short>(1);
260  }
261 };
262 
263 template <>
264 struct reduction_identity<unsigned int> {
265  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int sum() {
266  return static_cast<unsigned int>(0);
267  }
268  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int prod() {
269  return static_cast<unsigned int>(1);
270  }
271  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int max() {
272  return static_cast<unsigned int>(0);
273  }
274  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int min() {
275  return UINT_MAX;
276  }
277  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int bor() {
278  return static_cast<unsigned int>(0x0);
279  }
280  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int band() {
281  return ~static_cast<unsigned int>(0x0);
282  }
283  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int lor() {
284  return static_cast<unsigned int>(0);
285  }
286  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned int land() {
287  return static_cast<unsigned int>(1);
288  }
289 };
290 
291 template <>
292 struct reduction_identity<unsigned long> {
293  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long sum() {
294  return static_cast<unsigned long>(0);
295  }
296  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long prod() {
297  return static_cast<unsigned long>(1);
298  }
299  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long max() {
300  return static_cast<unsigned long>(0);
301  }
302  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long min() {
303  return ULONG_MAX;
304  }
305  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long bor() {
306  return static_cast<unsigned long>(0x0);
307  }
308  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long band() {
309  return ~static_cast<unsigned long>(0x0);
310  }
311  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long lor() {
312  return static_cast<unsigned long>(0);
313  }
314  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long land() {
315  return static_cast<unsigned long>(1);
316  }
317 };
318 
319 template <>
320 struct reduction_identity<unsigned long long> {
321  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long sum() {
322  return static_cast<unsigned long long>(0);
323  }
324  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long prod() {
325  return static_cast<unsigned long long>(1);
326  }
327  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long max() {
328  return static_cast<unsigned long long>(0);
329  }
330  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long min() {
331  return ULLONG_MAX;
332  }
333  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long bor() {
334  return static_cast<unsigned long long>(0x0);
335  }
336  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long band() {
337  return ~static_cast<unsigned long long>(0x0);
338  }
339  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long lor() {
340  return static_cast<unsigned long long>(0);
341  }
342  KOKKOS_FORCEINLINE_FUNCTION constexpr static unsigned long long land() {
343  return static_cast<unsigned long long>(1);
344  }
345 };
346 
347 template <>
348 struct reduction_identity<float> {
349  KOKKOS_FORCEINLINE_FUNCTION constexpr static float sum() {
350  return static_cast<float>(0.0f);
351  }
352  KOKKOS_FORCEINLINE_FUNCTION constexpr static float prod() {
353  return static_cast<float>(1.0f);
354  }
355  KOKKOS_FORCEINLINE_FUNCTION constexpr static float max() { return -FLT_MAX; }
356  KOKKOS_FORCEINLINE_FUNCTION constexpr static float min() { return FLT_MAX; }
357 };
358 
359 template <>
360 struct reduction_identity<double> {
361  KOKKOS_FORCEINLINE_FUNCTION constexpr static double sum() {
362  return static_cast<double>(0.0);
363  }
364  KOKKOS_FORCEINLINE_FUNCTION constexpr static double prod() {
365  return static_cast<double>(1.0);
366  }
367  KOKKOS_FORCEINLINE_FUNCTION constexpr static double max() { return -DBL_MAX; }
368  KOKKOS_FORCEINLINE_FUNCTION constexpr static double min() { return DBL_MAX; }
369 };
370 
371 #if !defined(KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_CUDA) && \
372  !defined(KOKKOS_ACTIVE_EXECUTION_MEMORY_SPACE_HIP_GPU)
373 template <>
374 struct reduction_identity<long double> {
375  KOKKOS_FORCEINLINE_FUNCTION constexpr static long double sum() {
376  return static_cast<long double>(0.0);
377  }
378  KOKKOS_FORCEINLINE_FUNCTION constexpr static long double prod() {
379  return static_cast<long double>(1.0);
380  }
381  KOKKOS_FORCEINLINE_FUNCTION constexpr static long double max() {
382  return -LDBL_MAX;
383  }
384  KOKKOS_FORCEINLINE_FUNCTION constexpr static long double min() {
385  return LDBL_MAX;
386  }
387 };
388 #endif
389 
390 } // namespace Kokkos
391 
392 #endif