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 constexpr int format_precision = 7;
107
109 static String name()
110 {
111 return "float";
112 }
113
115 static uint64_t feature_hash()
116 {
117 return uint64_t(sizeof(float)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
118 }
119 };
120
127 template<>
128 struct Traits<double>
129 {
131 static constexpr bool is_int = false;
133 static constexpr bool is_float = true;
135 static constexpr bool is_bool = false;
137 static constexpr bool is_signed = true;
138
141
143 static constexpr int format_precision = 16;
144
146 static String name()
147 {
148 return "double";
149 }
150
152 static uint64_t feature_hash()
153 {
154 return uint64_t(sizeof(double)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
155 }
156 };
157
164 template<>
165 struct Traits<long double>
166 {
168 static constexpr bool is_int = false;
170 static constexpr bool is_float = true;
172 static constexpr bool is_bool = false;
174 static constexpr bool is_signed = true;
175
178
180 static constexpr int format_precision = 16;
181
183 static String name()
184 {
185 return "long double";
186 }
187
189 static uint64_t feature_hash()
190 {
191 return uint64_t(sizeof(long double)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
192 }
193 };
194
201 template<>
202 struct Traits<unsigned int>
203 {
205 static constexpr bool is_int = true;
207 static constexpr bool is_float = false;
209 static constexpr bool is_bool = false;
211 static constexpr bool is_signed = false;
212
215
217 static constexpr int format_precision = 0;
218
220 static String name()
221 {
222 return "unsigned int";
223 }
224
226 static uint64_t feature_hash()
227 {
228 return uint64_t(sizeof(unsigned int)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
229 }
230 };
231
238 template<>
239 struct Traits<signed int>
240 {
242 static constexpr bool is_int = true;
244 static constexpr bool is_float = false;
246 static constexpr bool is_bool = false;
248 static constexpr bool is_signed = true;
249
252
254 static constexpr int format_precision = 0;
255
257 static String name()
258 {
259 return "signed int";
260 }
261
263 static uint64_t feature_hash()
264 {
265 return uint64_t(sizeof(signed int)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
266 }
267 };
268
275 template<>
276 struct Traits<unsigned char>
277 {
279 static constexpr bool is_int = true;
281 static constexpr bool is_float = false;
283 static constexpr bool is_bool = false;
285 static constexpr bool is_signed = false;
286
289
291 static constexpr int format_precision = 0;
292
294 static String name()
295 {
296 return "unsigned char";
297 }
298
300 static uint64_t feature_hash()
301 {
302 return uint64_t(sizeof(unsigned char)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
303 }
304 };
305
312 template<>
313 struct Traits<signed char>
314 {
316 static constexpr bool is_int = true;
318 static constexpr bool is_float = false;
320 static constexpr bool is_bool = false;
322 static constexpr bool is_signed = true;
323
326
328 static constexpr int format_precision = 0;
329
331 static String name()
332 {
333 return "signed char";
334 }
335
337 static uint64_t feature_hash()
338 {
339 return uint64_t(sizeof(signed char)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
340 }
341 };
342
349 template<>
350 struct Traits<unsigned short>
351 {
353 static constexpr bool is_int = true;
355 static constexpr bool is_float = false;
357 static constexpr bool is_bool = false;
359 static constexpr bool is_signed = false;
360
363
365 static constexpr int format_precision = 0;
366
368 static String name()
369 {
370 return "unsigned short";
371 }
372
374 static uint64_t feature_hash()
375 {
376 return uint64_t(sizeof(unsigned short)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
377 }
378 };
379
386 template<>
387 struct Traits<signed short>
388 {
390 static constexpr bool is_int = true;
392 static constexpr bool is_float = false;
394 static constexpr bool is_bool = false;
396 static constexpr bool is_signed = true;
397
400
402 static constexpr int format_precision = 0;
403
405 static String name()
406 {
407 return "signed short";
408 }
409
411 static uint64_t feature_hash()
412 {
413 return uint64_t(sizeof(signed short)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
414 }
415 };
416
423 template<>
424 struct Traits<unsigned long>
425 {
427 static constexpr bool is_int = true;
429 static constexpr bool is_float = false;
431 static constexpr bool is_bool = false;
433 static constexpr bool is_signed = false;
434
437
439 static constexpr int format_precision = 0;
440
442 static String name()
443 {
444 return "unsigned long";
445 }
446
448 static uint64_t feature_hash()
449 {
450 return uint64_t(sizeof(unsigned long)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
451 }
452 };
453
460 template<>
461 struct Traits<signed long>
462 {
464 static constexpr bool is_int = true;
466 static constexpr bool is_float = false;
468 static constexpr bool is_bool = false;
470 static constexpr bool is_signed = true;
471
474
476 static constexpr int format_precision = 0;
477
479 static String name()
480 {
481 return "signed long";
482 }
483
485 static uint64_t feature_hash()
486 {
487 return uint64_t(sizeof(signed long)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
488 }
489 };
490
497 template<>
498 struct Traits<unsigned long long>
499 {
501 static constexpr bool is_int = true;
503 static constexpr bool is_float = false;
505 static constexpr bool is_bool = false;
507 static constexpr bool is_signed = false;
508
511
513 static constexpr int format_precision = 0;
514
516 static String name()
517 {
518 return "unsigned long long";
519 }
520
522 static uint64_t feature_hash()
523 {
524 return uint64_t(sizeof(unsigned long long)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
525 }
526 };
527
534 template<>
535 struct Traits<signed long long>
536 {
538 static constexpr bool is_int = true;
540 static constexpr bool is_float = false;
542 static constexpr bool is_bool = false;
544 static constexpr bool is_signed = true;
545
548
550 static constexpr int format_precision = 0;
551
553 static String name()
554 {
555 return "signed long long";
556 }
557
559 static uint64_t feature_hash()
560 {
561 return uint64_t(sizeof(signed long long)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
562 }
563 };
564
571 template<>
572 struct Traits<bool>
573 {
575 static constexpr bool is_int = false;
577 static constexpr bool is_float = false;
579 static constexpr bool is_bool = true;
581 static constexpr bool is_signed = false;
582
585
587 static constexpr int format_precision = 0;
588
590 static String name()
591 {
592 return "bool";
593 }
594
596 static uint64_t feature_hash()
597 {
598 return uint64_t(sizeof(bool)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
599 }
600 };
601
602#if defined(FEAT_HAVE_QUADMATH) && !defined(__CUDACC__)
608 template<>
609 struct Traits<__float128>
610 {
612 static constexpr bool is_int = false;
614 static constexpr bool is_float = true;
616 static constexpr bool is_bool = false;
618 static constexpr bool is_signed = true;
619
621 typedef FloatingClass TypeClass;
622
624 static constexpr int format_precision = 34;
625
627 static String name()
628 {
629 return "__float128";
630 }
631
633 static uint64_t feature_hash()
634 {
635 return uint64_t(sizeof(__float128)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
636 }
637 };
638#endif // FEAT_HAVE_QUADMATH && !__CUDACC__
639
640#if defined(FEAT_HAVE_HALFMATH)
646 template<>
647 struct Traits<Half>
648 {
650 static constexpr bool is_int = false;
652 static constexpr bool is_float = true;
654 static constexpr bool is_bool = false;
656 static constexpr bool is_signed = true;
657
659 typedef FloatingClass TypeClass;
660
662 static constexpr int format_precision = 4;
663
665 static String name()
666 {
667 return "Half";
668 }
669
671 static uint64_t feature_hash()
672 {
673 return uint64_t(sizeof(Half)) | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
674 }
675 };
676#endif // FEAT_HAVE_HALFMATH
677
678#if defined(FEAT_HAVE_FLOATX) && !defined(__CUDACC__)
684 template<int exp_bits_, int sig_bits_, typename Backend_>
685 struct Traits<flx::floatx<exp_bits_, sig_bits_, Backend_>>
686 {
688 static constexpr bool is_int = false;
690 static constexpr bool is_float = true;
692 static constexpr bool is_bool = false;
694 static constexpr bool is_signed = true;
695
697 typedef FloatingClass TypeClass;
698
700 static constexpr int format_precision = 0;
701
703 static String name()
704 {
705 return String("floatx<") + stringify(exp_bits_) + "," + stringify(sig_bits_) + "," + Traits<Backend_>::name() + ">";
706 }
707
709 static uint64_t feature_hash()
710 {
711 // This one is more tricky, because we also have to encode
712 // the chosen number of exponent and significant bits
713 return uint64_t(sizeof(Backend_)) | uint64_t(exp_bits_) << 16 | uint64_t(sig_bits_) << 24
714 | uint64_t(is_int) << 32 | uint64_t(is_float) << 33 | uint64_t(is_signed) << 34;
715 }
716 };
717#endif // FEAT_HAVE_FLOATX && !__CUDACC__
718 } // namespace Type
719} // namespace FEAT
FEAT Kernel base header.
String class implementation.
Definition: string.hpp:47
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:993
__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