FEAT 3
Finite Element Analysis Toolbox
Loading...
Searching...
No Matches
type_traits.hpp
1// FEAT3: Finite Element Analysis Toolbox, Version 3
2// Copyright (C) 2010 by Stefan Turek & the FEAT group
3// FEAT3 is released under the GNU General Public License version 3,
4// see the file 'copyright.txt' in the top level directory for details.
5
6#pragma once
7
8// includes, FEAT
10#include <kernel/util/half.hpp>
11#include <kernel/util/string.hpp>
12
13#include <typeindex>
14
15#if defined(FEAT_HAVE_FLOATX) && !defined(__CUDACC__)
16FEAT_DISABLE_WARNINGS
17#include <floatx.hpp>
18FEAT_RESTORE_WARNINGS
19#endif // FEAT_HAVE_FLOATX && !defined(__CUDACC__)
20
21namespace FEAT
22{
28 namespace Type
29 {
30 struct Helper
31 {
33 static inline size_t extract_type_size(uint64_t feature_hash)
34 {
35 return (size_t) feature_hash&0xFFFFFFFF; //take only the lower 32 bits
36 }
37
39 static inline bool extract_intness(uint64_t feature_hash)
40 {
41 return (feature_hash&(uint64_t(1)<<32)) != uint64_t(0);
42 }
43
45 static inline bool extract_floatness(uint64_t feature_hash)
46 {
47 return (feature_hash&(uint64_t(1)<<33)) != uint64_t(0);
48 }
49
51 static inline bool extract_signedness(uint64_t feature_hash)
52 {
53 return (feature_hash&(uint64_t(1)<<34)) != uint64_t(0);
54 }
55 };
56
58 class AnotherClass {};
60 class IntegralClass {};
62 class FloatingClass {};
64 class BooleanClass {};
65
71 template<typename DT_>
72 struct Traits
73 {
76
78 static String name()
79 {
80 return DT_::name();
81 }
82 };
83
90 template<>
91 struct Traits<float>
92 {
94 static constexpr bool is_int = false;
96 static constexpr bool is_float = true;
98 static constexpr bool is_bool = false;
100 static constexpr bool is_signed = true;
101
104
106 static String name()
107 {
108 return "float";
109 }
110
112 static uint64_t feature_hash()
113 {
114 return uint64_t(sizeof(float)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
115 }
116 };
117
124 template<>
125 struct Traits<double>
126 {
128 static constexpr bool is_int = false;
130 static constexpr bool is_float = true;
132 static constexpr bool is_bool = false;
134 static constexpr bool is_signed = true;
135
138
140 static String name()
141 {
142 return "double";
143 }
144
146 static uint64_t feature_hash()
147 {
148 return uint64_t(sizeof(double)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
149 }
150 };
151
158 template<>
159 struct Traits<long double>
160 {
162 static constexpr bool is_int = false;
164 static constexpr bool is_float = true;
166 static constexpr bool is_bool = false;
168 static constexpr bool is_signed = true;
169
172
174 static String name()
175 {
176 return "long double";
177 }
178
180 static uint64_t feature_hash()
181 {
182 return uint64_t(sizeof(long double)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
183 }
184 };
185
192 template<>
193 struct Traits<unsigned int>
194 {
196 static constexpr bool is_int = true;
198 static constexpr bool is_float = false;
200 static constexpr bool is_bool = false;
202 static constexpr bool is_signed = false;
203
206
208 static String name()
209 {
210 return "unsigned int";
211 }
212
214 static uint64_t feature_hash()
215 {
216 return uint64_t(sizeof(unsigned int)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
217 }
218 };
219
226 template<>
227 struct Traits<signed int>
228 {
230 static constexpr bool is_int = true;
232 static constexpr bool is_float = false;
234 static constexpr bool is_bool = false;
236 static constexpr bool is_signed = true;
237
240
242 static String name()
243 {
244 return "signed int";
245 }
246
248 static uint64_t feature_hash()
249 {
250 return uint64_t(sizeof(signed int)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
251 }
252 };
253
260 template<>
261 struct Traits<unsigned char>
262 {
264 static constexpr bool is_int = true;
266 static constexpr bool is_float = false;
268 static constexpr bool is_bool = false;
270 static constexpr bool is_signed = false;
271
274
276 static String name()
277 {
278 return "unsigned char";
279 }
280
282 static uint64_t feature_hash()
283 {
284 return uint64_t(sizeof(unsigned char)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
285 }
286 };
287
294 template<>
295 struct Traits<signed char>
296 {
298 static constexpr bool is_int = true;
300 static constexpr bool is_float = false;
302 static constexpr bool is_bool = false;
304 static constexpr bool is_signed = true;
305
308
310 static String name()
311 {
312 return "signed char";
313 }
314
316 static uint64_t feature_hash()
317 {
318 return uint64_t(sizeof(signed char)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
319 }
320 };
321
328 template<>
329 struct Traits<unsigned short>
330 {
332 static constexpr bool is_int = true;
334 static constexpr bool is_float = false;
336 static constexpr bool is_bool = false;
338 static constexpr bool is_signed = false;
339
342
344 static String name()
345 {
346 return "unsigned short";
347 }
348
350 static uint64_t feature_hash()
351 {
352 return uint64_t(sizeof(unsigned short)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
353 }
354 };
355
362 template<>
363 struct Traits<signed short>
364 {
366 static constexpr bool is_int = true;
368 static constexpr bool is_float = false;
370 static constexpr bool is_bool = false;
372 static constexpr bool is_signed = true;
373
376
378 static String name()
379 {
380 return "signed short";
381 }
382
384 static uint64_t feature_hash()
385 {
386 return uint64_t(sizeof(signed short)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
387 }
388 };
389
396 template<>
397 struct Traits<unsigned long>
398 {
400 static constexpr bool is_int = true;
402 static constexpr bool is_float = false;
404 static constexpr bool is_bool = false;
406 static constexpr bool is_signed = false;
407
410
412 static String name()
413 {
414 return "unsigned long";
415 }
416
418 static uint64_t feature_hash()
419 {
420 return uint64_t(sizeof(unsigned long)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
421 }
422 };
423
430 template<>
431 struct Traits<signed long>
432 {
434 static constexpr bool is_int = true;
436 static constexpr bool is_float = false;
438 static constexpr bool is_bool = false;
440 static constexpr bool is_signed = true;
441
444
446 static String name()
447 {
448 return "signed long";
449 }
450
452 static uint64_t feature_hash()
453 {
454 return uint64_t(sizeof(signed long)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
455 }
456 };
457
464 template<>
465 struct Traits<unsigned long long>
466 {
468 static constexpr bool is_int = true;
470 static constexpr bool is_float = false;
472 static constexpr bool is_bool = false;
474 static constexpr bool is_signed = false;
475
478
480 static String name()
481 {
482 return "unsigned long long";
483 }
484
486 static uint64_t feature_hash()
487 {
488 return uint64_t(sizeof(unsigned long long)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
489 }
490 };
491
498 template<>
499 struct Traits<signed long long>
500 {
502 static constexpr bool is_int = true;
504 static constexpr bool is_float = false;
506 static constexpr bool is_bool = false;
508 static constexpr bool is_signed = true;
509
512
514 static String name()
515 {
516 return "signed long long";
517 }
518
520 static uint64_t feature_hash()
521 {
522 return uint64_t(sizeof(signed long long)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
523 }
524 };
525
532 template<>
533 struct Traits<bool>
534 {
536 static constexpr bool is_int = false;
538 static constexpr bool is_float = false;
540 static constexpr bool is_bool = true;
542 static constexpr bool is_signed = false;
543
546
548 static String name()
549 {
550 return "bool";
551 }
552
554 static uint64_t feature_hash()
555 {
556 return uint64_t(sizeof(bool)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
557 }
558 };
559
560#if defined(FEAT_HAVE_QUADMATH) && !defined(__CUDACC__)
566 template<>
567 struct Traits<__float128>
568 {
570 static constexpr bool is_int = false;
572 static constexpr bool is_float = true;
574 static constexpr bool is_bool = false;
576 static constexpr bool is_signed = true;
577
579 typedef FloatingClass TypeClass;
580
582 static String name()
583 {
584 return "__float128";
585 }
586
588 static uint64_t feature_hash()
589 {
590 return uint64_t(sizeof(__float128)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
591 }
592 };
593#endif // FEAT_HAVE_QUADMATH && !__CUDACC__
594
595#if defined(FEAT_HAVE_HALFMATH)
601 template<>
602 struct Traits<Half>
603 {
605 static constexpr bool is_int = false;
607 static constexpr bool is_float = true;
609 static constexpr bool is_bool = false;
611 static constexpr bool is_signed = true;
612
614 typedef FloatingClass TypeClass;
615
617 static String name()
618 {
619 return "Half";
620 }
621
623 static uint64_t feature_hash()
624 {
625 return uint64_t(sizeof(Half)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
626 }
627 };
628#endif // FEAT_HAVE_HALFMATH
629
630#if defined(FEAT_HAVE_FLOATX) && !defined(__CUDACC__)
636 template<int exp_bits_, int sig_bits_, typename Backend_>
637 struct Traits<flx::floatx<exp_bits_, sig_bits_, Backend_>>
638 {
640 static constexpr bool is_int = false;
642 static constexpr bool is_float = true;
644 static constexpr bool is_bool = false;
646 static constexpr bool is_signed = true;
647
649 typedef FloatingClass TypeClass;
650
652 static String name()
653 {
654 return String("floatx<") + stringify(exp_bits_) + "," + stringify(sig_bits_) + "," + Traits<Backend_>::name() + ">";
655 }
656
658 static uint64_t feature_hash()
659 {
660 // This one is more tricky, because we also have to encode
661 // the chosen number of exponent and significant bits
662 return uint64_t(sizeof(Backend_)) | uint64_t(exp_bits_) << 16 | uint64_t(sig_bits_) << 24
663 | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
664 }
665 };
666#endif // FEAT_HAVE_FLOATX && !__CUDACC__
667 } // namespace Type
668} // namespace FEAT
FEAT Kernel base header.
String class implementation.
Definition: string.hpp:46
Tag class for any data type not matching any other type class.
Definition: type_traits.hpp:58
Tag class for the one and only boolean data type.
Definition: type_traits.hpp:64
Tag class for floating data types.
Definition: type_traits.hpp:62
Tag class for integral data types.
Definition: type_traits.hpp:60
FEAT namespace.
Definition: adjactor.hpp:12
String stringify(const T_ &item)
Converts an item into a String.
Definition: string.hpp:944
__half Half
Half data type.
Definition: half.hpp:25
static bool extract_signedness(uint64_t feature_hash)
extracts sign feature from a given types feature hash
Definition: type_traits.hpp:51
static bool extract_intness(uint64_t feature_hash)
extracts integral feature from a given types feature hash
Definition: type_traits.hpp:39
static bool extract_floatness(uint64_t feature_hash)
extracts floating point feature from a given types feature hash
Definition: type_traits.hpp:45
static size_t extract_type_size(uint64_t feature_hash)
extracts sizeof datatype from a given types feature hash
Definition: type_traits.hpp:33
BooleanClass TypeClass
this type is of boolean class
static String name()
returns a string identifying the datatype
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
FloatingClass TypeClass
this type is of floating class
static String name()
returns a string identifying the datatype
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
static String name()
returns a string identifying the datatype
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
FloatingClass TypeClass
this type is of floating class
static String name()
returns a string identifying the datatype
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
FloatingClass TypeClass
this type is of floating class
IntegralClass TypeClass
this type is of integral class
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
static String name()
returns a string identifying the datatype
static String name()
returns a string identifying the datatype
IntegralClass TypeClass
this type is of integral class
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
static String name()
returns a string identifying the datatype
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
IntegralClass TypeClass
this type is of integral class
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
static String name()
returns a string identifying the datatype
IntegralClass TypeClass
this type is of integral class
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
static String name()
returns a string identifying the datatype
IntegralClass TypeClass
this type is of integral class
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
static String name()
returns a string identifying the datatype
IntegralClass TypeClass
this type is of integral class
static String name()
returns a string identifying the datatype
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
IntegralClass TypeClass
this type is of integral class
IntegralClass TypeClass
this type is of integral class
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
static String name()
returns a string identifying the datatype
static String name()
returns a string identifying the datatype
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
IntegralClass TypeClass
this type is of integral class
IntegralClass TypeClass
this type is of integral class
static uint64_t feature_hash()
returns composition of datatype size, int-, float- and signed feature
static String name()
returns a string identifying the datatype
basic Type Traits struct
Definition: type_traits.hpp:73
static String name()
returns a string identifying the datatype
Definition: type_traits.hpp:78
AnotherClass TypeClass
this type is of another class
Definition: type_traits.hpp:75