Ocean
DataType.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_BASE_DATA_TYPE_H
9 #define META_OCEAN_BASE_DATA_TYPE_H
10 
11 #include "ocean/base/Base.h"
12 
13 #include <limits>
14 
15 namespace Ocean
16 {
17 
18 /**
19  * Template class allowing to define an array of data types.
20  * The size of the resulting object is guaranteed to be sizeof(T) * tElements.
21  * @tparam T The data type of each element
22  * @tparam tElements Number of elements in the data type, with range [1, infinity)
23  * @ingroup base
24  */
25 template <typename T, unsigned int tElements>
26 class DataType
27 {
28  public:
29 
30  /// Default definition of a type with tBytes bytes.
31  struct Type
32  {
33  /**
34  * Returns whether two objects store the same values.
35  * @param type The second type to compare
36  * @return True, if so
37  */
38  inline bool operator==(const Type& type) const;
39 
40  /**
41  * Returns whether two objects do not store the same values.
42  * @param type The second type to compare
43  * @return True, if so
44  */
45  inline bool operator!=(const Type& type) const;
46 
47  /// Data values of the type.
48  T values_[tElements];
49  };
50 
51  static_assert(sizeof(Type) == sizeof(T) * tElements, "Invalid data type!");
52 };
53 
54 /**
55  * This class implements a helper class allowing to define the identity data type of a given type.
56  * The class can be used if e.g., to prevent that the compiler is able to implicitly determine a template data type in a function.
57  *
58  * The following code example explains the usage of the helper class:
59  * @code
60  * template <typename T>
61  * void functionA(T value)
62  * {
63  * // do something
64  * }
65  *
66  * template <typename T>
67  * void functionB(typename Identity<T>::Type value)
68  * {
69  * // do something
70  * }
71  *
72  * void main()
73  * {
74  * functionA(1); // will compile
75  *
76  * functionB(1); // will not compile, e.g., with error message 'void Ocean::functionB(Identity<T>::Type)': could not deduce template argument for 'T'
77  *
78  * functionB<double>(1); // will compile
79  * }
80  * @endcode
81  * @tparam T The data type to be used
82  * @ingroup base
83  */
84 template <typename T>
85 class Identity
86 {
87  public:
88 
89  /// The data type of 'T'.
90  using Type = T;
91 };
92 
93 /**
94  * This class is a helper class allowing to define the square difference data type of a given type.
95  * Beware: There is no guarantee that the resulting data type can store any squared value without a value overflow.<br>
96  * By default the resulting data type is identical with the given data type.<br>
97  * The following table shows the resulting data types which are different from the given data types:<br>
98  * <pre>
99  * Data type: Squared data type:
100  * int unsigned int
101  * char unsigned int
102  * signed char unsigned int
103  * unsigned char unsigned int
104  * short unsigned int
105  * long unsigned long
106  * long long unsigned long long
107  * </pre>
108  * The following code example explains the usage of the helper class:
109  * @code
110  * void function()
111  * {
112  * typedef SquareValueTyper<char>::Type SquareType;
113  *
114  * char value = 7;
115  *
116  * SquareType squareValue = SquareType(value * value);
117  * }
118  * @endcode
119  * @tparam T Data type for that the square value type has to be found
120  * @see DifferenceValueTyper
121  * @ingroup base
122  */
123 template <typename T>
125 {
126  public:
127 
128  /**
129  * Definition of the data type for the square value.
130  * The default type is the type itself.
131  */
132  typedef T Type;
133 };
134 
135 /**
136  * This class is a helper class allowing to define the signed difference data type of a given type.
137  * Beware: There is no guarantee that the resulting data type can store any difference value without a value overflow.<br>
138  * By default the resulting data type is identical with the given data type.<br>
139  * The following table shows the resulting data types which are different from the given data types:<br>
140  * <pre>
141  * Data type: Signed difference data type:
142  * char int
143  * signed char int
144  * unsigned char int
145  * unsigned int int
146  * short int
147  * unsigned short int
148  * unsigned long long
149  * unsigned long long long long
150  * </pre>
151  * The following code example explains the usage of the helper class:
152  * @code
153  * void function()
154  * {
155  * typedef DifferenceValueTyper<char>::Type DifferenceType;
156  *
157  * char valueA = 7;
158  * char valueB = 170;
159  *
160  * DifferenceType differenceValue = DifferenceType(valueA - valueB);
161  * }
162  * @endcode
163  * @tparam T Data type for that the signed difference value type has to be found
164  * @see AbsoluteDifferenceValueTyper, SquareValueTyper
165  * @ingroup base
166  */
167 template <typename T>
169 {
170  public:
171 
172  /**
173  * Definition of the data type for the signed difference value.
174  * The default type is the type itself.
175  */
176  typedef T Type;
177 };
178 
179 /**
180  * This class is a helper class allowing to define the resulting data type for an absolute difference operation.
181  * The following table shows the resulting data types:
182  * <pre>
183  * Data type: Absolute difference data type:
184  *
185  * char unsigned int
186  * signed char unsigned int
187  * unsigned char unsigned int
188  *
189  * short unsigned long long
190  * unsigned short unsigned long long
191  * int unsigned long long
192  * unsigned int unsigned long long
193  *
194  * long long unsigned long long
195  * unsigned long long unsigned long long
196  *
197  * float double
198  * double double
199  * </pre>
200  * The following code example shows the usage of the helper class:
201  * @code
202  * void function()
203  * {
204  * typedef AbsoluteDifferenceValueTyper<char>::Type AbsoluteDifferenceType;
205  *
206  * char valueA = 7;
207  * char valueB = 170;
208  *
209  * AbsoluteDifferenceType absoluteDifferenceValue = AbsoluteDifferenceType(valueA - valueB);
210  * }
211  * @endcode
212  * @tparam T Data type for that the signed difference value type has to be found
213  * @see DifferenceValueTyper
214  * @ingroup base
215  */
216 template <typename T>
218 {
219  public:
220 
221  /**
222  * Definition of the data type for the absolute difference value.
223  * The default type is 'unsigned long long'.
224  */
225  typedef unsigned long long Type;
226 };
227 
228 /**
229  * This class is a helper class allowing to determine the next larger data type for a given data type.
230  * By default the resulting data type is identical with the given data type.<br>
231  * The following table shows the resulting data types which are different from the given data types:<br>
232  * <pre>
233  * Data type: Next larger data type: Next larger data type regarding performance:
234  * char UNDEFINED UNDEFINED, (as the signed of 'char' is not standardized)
235  * signed char short int
236  * unsigned char unsigned short unsigned int
237  * short int int
238  * unsigned short unsigned int unsigned int
239  * int long long long long
240  * unsigned int unsigned long long unsigned long long
241  * float double float
242  * </pre>
243  * @tparam T Data type for which the next larger data type has to be found
244  * @ingroup base
245  */
246 template <typename T>
248 {
249  public:
250 
251  /**
252  * Definition of the data type for the next larger data type.
253  * The default type is the type itself.
254  */
255  typedef T Type;
256 
257  /**
258  * Definition of the data type for the next larger data type regarding performance.
259  * The default type is the type itself.
260  */
261  typedef T TypePerformance;
262 };
263 
264 /**
265  * This class is a helper class allowing to determine the signed data type for a given data type if existing, otherwise the identity data type.
266  * By default the resulting data type is identical with the given data type.<br>
267  * The following table shows the resulting data types which are different from the given data types:
268  * <pre>
269  * Data type: Resulting (signed if possible) Data type
270  * bool bool (but it is not signed)
271  * char UNDEFINED, (as the signed of 'char' is not standardized)
272  * unsigned char signed char
273  * short short
274  * unsigned short short
275  * int int
276  * unsigned int int
277  * long long long long
278  * unsigned long long long long
279  * float float
280  * double double
281  * </pre>
282  * Beware: The resulting data type of SignedTyper<T>::Type may be different from std::make_signed<T>::type (as e.g., std::make_signed<double>::type is not double).
283  * @tparam T Data type for which the signed data type has to be found
284  * @ingroup base
285  */
286 template <typename T>
288 {
289  public:
290 
291  /**
292  * Definition of the signed data type, if existing.
293  */
294  typedef T Type;
295 
296  /**
297  * True, if the data type T is an signed data type; False, otherwise.
298  */
299  static const bool isSigned = true;
300 
301  /**
302  * True, if the data has a corresponding signed data type (or itself is signed).
303  */
304  static const bool hasSigned = true;
305 };
306 
307 /**
308  * This class is a helper class allowing to determine the unsigned data type for a given data type if existing, otherwise the identity data type.
309  * By default the resulting data type is identical with the given data type.<br>
310  * The following table shows the resulting data types which are different from the given data types:
311  * <pre>
312  * Data type: Resulting (unsigned if possible) Data type
313  * bool bool (seems to be an unsigned data type)
314  * char UNDEFINED, (as the signed of 'char' is not standardized)
315  * signed char unsigned char
316  * unsigned char unsigned char
317  * short unsigned short
318  * unsigned short unsigned short
319  * int unsigned int
320  * unsigned int unsigned int
321  * long unsigned long
322  * long long unsigned long long
323  * unsigned long long unsigned long long
324  * float float (no corresponding unsigned data type exists)
325  * double double (no corresponding unsigned data type exists)
326  * </pre>
327  * Beware: The resulting data type of UnsignedTyper<T>::Type may be different from std::make_signed<T>::type (as e.g., std::make_signed<double>::type is not double).
328  * @tparam T Data type for which the unsigned data type has to be found
329  * @ingroup base
330  */
331 template <typename T>
333 {
334  public:
335 
336  /**
337  * Definition of the unsigned data type, if existing.
338  */
339  typedef T Type;
340 
341  /**
342  * True, if the data type T is an unsigned data type; False, otherwise.
343  */
344  static const bool isUnsigned = true;
345 
346  /**
347  * True, if the data has a corresponding unsigned data type (or itself is unsigned).
348  */
349  static const bool hasUnsigned = true;
350 };
351 
352 /**
353  * This struct is a helper struct providing a floating point data type best matching for a given data type.
354  * The resulting data type is either a 32 bit floating point data type or a 64 bit floating point data type.<br>
355  * Overall, the Type field of this struct follows this simple pattern:
356  * <pre>
357  * T: MatchingFloat<T>::Type:
358  * double double
359  * float float
360  * char float
361  * int float
362  * 'any other type' float
363  * </pre>
364  * @tparam T The data type for which the matching floating point data type is needed
365  * @ingroup base
366  */
367 template <typename T>
369 {
370  /**
371  * The 32 bit floating point data type for any data type T but 'double'
372  */
373  typedef float Type;
374 };
375 
376 /**
377  * This struct implements a helper struct allowing to use a data type instead of void.
378  * Overall, the Type field of this struct follows this simple pattern:
379  * <pre>
380  * T: NotVoidTyper<T>::Type: NotVoidTyper<T>::defaultValue:
381  * void bool true
382  * 'any other type' 'any other type' T()
383  * </pre>
384  * @tparam T The data type
385  * @ingroup base
386  */
387 template <typename T>
389 {
390  public:
391 
392  /**
393  * Definition of the data type which is 'bool' in case of 'T' is 'void'
394  */
395  typedef T Type;
396 
397  /**
398  * Returns a default value for 'T'.
399  * @return The default value
400  */
401  static constexpr T defaultValue()
402  {
403  return T();
404  }
405 };
406 
407 /**
408  * Specialization of NotVoidTyper<T>.
409  */
410 template <>
411 class NotVoidTyper<void>
412 {
413  public:
414 
415  /**
416  * Definition of the data type which is 'bool' in case of 'T' is 'void'
417  */
418  typedef bool Type;
419 
420  /**
421  * Returns a default value for 'T'.
422  * @return The default value
423  */
424  static constexpr bool defaultValue()
425  {
426  return true;
427  }
428 };
429 
430 /**
431  * This class implements a helper class allowing to determine unique names for most of native C++ data types.
432  * @ingroup base
433  */
435 {
436  public:
437 
438  /**
439  * Returns a user-friendly name for most of native C++ data types.
440  * @return The user friendly name
441  * @tparam T The data type for which the name is returned
442  */
443  template <typename T>
444  static inline const char* name();
445 };
446 
447 /**
448  * This class provides a unique data type for any data type specified by the number of bytes.
449  * The class can be used to reduce binary size of template functions with code mainly defined by the size of a data type instead of the data type itself.<br>
450  * This class provides the following mapping:
451  * <pre>
452  * Size of data type: Resulting mapped data type
453  * 1 (e.g., char) uint8_t
454  * 2 (e.g., unigned short) uint16_t
455  * 4 (e.g., float) uint32_t
456  * 8 (e.g., double) uint64_t
457  * </pre>
458  * @tparam tBytes The number of bytes the data type must have, possible values are {1, 2, 4, 8}
459  * @see TypeMapper.
460  * @ingroup base
461  */
462 template <size_t tBytes>
464 {
465  static_assert(tBytes == 1 || tBytes == 2 || tBytes == 4 || tBytes == 8, "Invalid data type!");
466 
467  public:
468 
469  /**
470  * Definition of an invalid mapped data type.
471  */
472  typedef void Type;
473 };
474 
475 /**
476  * This class provides a unique data type for any data type.
477  * The class can be used to reduce binary size of template functions with code mainly defined by the size of a data type instead of the data type itself.<br>
478  * This class provides the following mapping:
479  * <pre>
480  * Data type: Resulting mapped data type
481  * bool unsigned char
482  * char unsigned char
483  * signed char unsigned char
484  * unsigned char unsigned char
485  * short unsigned short
486  * unsigned short unsigned short
487  * int unsigned int
488  * unsigned int unsigned int
489  * float unsigned int
490  * long long unsigned long long
491  * unsigned long long unsigned long long
492  * double unsigned long long
493  * </pre>
494  * @tparam T The data type for which a corresponding data type will be determined, with possible sizeof {1, 2, 4, 8}
495  * @see TypeMapperBySize.
496  * @ingroup base
497  */
498 template <typename T>
500 {
501  static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8, "Invalid data type!");
502 
503  public:
504 
505  /**
506  * Definition of an invalid mapped data type.
507  */
508  typedef typename TypeMapperBySize<sizeof(T)>::Type Type;
509 };
510 
511 /**
512  * Helper class allowing to define an ordered or unordered map based on the template parameter 'tOrderedKeys'.
513  * @tparam tOrderedKeys True, to allow accessing the keys in order; False, if the order of the keys is not of interest
514  * @ingroup base
515  */
516 template <bool tOrderedKeys>
517 class MapTyper
518 {
519  public:
520 
521  /**
522  * Definition of an ordered map.
523  */
524  template <typename TKey, typename TElement>
525  using TMap = std::map<TKey, TElement>;
526 };
527 
528 /**
529  * Helper class allowing to define an ordered or unordered map based on the template parameter 'tOrderedKeys'.
530  */
531 template <>
532 class MapTyper<false>
533 {
534  public:
535 
536  /**
537  * Definition of an unordered map.
538  */
539  template <typename TKey, typename TElement>
540  using TMap = std::unordered_map<TKey, TElement>;
541 };
542 
543 /**
544  * Definition of a 16-bit float type.
545  * @ingroup base
546  */
547 class OCEAN_BASE_EXPORT Float16
548 {
549  public:
550 
551  /**
552  * Default constructor.
553  */
554  Float16() = default;
555 
556  /**
557  * Creates a new 16-bit float.
558  * @param sign The 1-bit sign to be used, with range [0, 1]
559  * @param fraction The 10-bit fraction to be used, with range [0, 1023]
560  * @param exponent The 5-bit exponent to be used, with range [0, 31]
561  */
562  inline Float16(const uint16_t sign, const uint16_t fraction, const uint16_t exponent);
563 
564  /**
565  * Creates a new 16-bit float based on a 16-bit binary value.
566  * @param binary The binary value to be used
567  */
568  explicit inline Float16(const uint16_t binary);
569 
570  /**
571  * Returns the binary representation of this 16-bit float value.
572  * @return The value's binary representation
573  */
574  inline uint16_t binary() const;
575 
576  /**
577  * Cast operator to 32-bit float.
578  * @return The 32-bit float representation
579  */
580  explicit operator float() const;
581 
582  /**
583  * Negates the 16-bit float value.
584  * @return The negated value
585  */
586  inline Float16 operator-() const;
587 
588  /**
589  * Returns whether two 16-bit float values are identical.
590  * @param second The second value to compare
591  * @return True, if so
592  */
593  inline bool operator==(const Float16& second) const;
594 
595  /**
596  * Returns whether two 16-bit float values are not identical.
597  * @param second The second value to compare
598  * @return True, if so
599  */
600  inline bool operator!=(const Float16& second) const;
601 
602  /**
603  * Returns +infinity.
604  * @return The 16-bit float representing positive infinity
605  */
606  static inline Float16 infinity();
607 
608  protected:
609 
610  /**
611  * Union allowing to access the binary representation or the IEEE representation.
612  */
613  union
614  {
615  /// The binary representation.
616  uint16_t binary_ = 0u;
617 
618  /**
619  * The struct with the IEEE representation.
620  */
621  struct
622  {
623  /// The mantissa composed of 10 bits.
624  uint16_t fraction_ : 10;
625 
626  /// The exponent composed of 5 bits.
627  uint16_t exponent_ : 5;
628 
629  /// The sign composed of 1 bit.
630  uint16_t sign_ : 1;
631  } ieee_;
632  } data_;
633 };
634 
635 template <typename T, unsigned int tElements>
636 inline bool DataType<T, tElements>::Type::operator==(const Type& type) const
637 {
638  for (unsigned int n = 0u; n < tElements; ++n)
639  {
640  if (values_[n] != type.values_[n])
641  {
642  return false;
643  }
644  }
645 
646  return true;
647 }
648 
649 template <typename T, unsigned int tElements>
650 inline bool DataType<T, tElements>::Type::operator!=(const Type& type) const
651 {
652  return !(*this == type);
653 }
654 
655 /**
656  * Specialization of the helper class.
657  */
658 template <>
660 {
661  public:
662 
663  /**
664  * Definition of the square distance type.
665  */
666  typedef unsigned int Type;
667 };
668 
669 /**
670  * Specialization of the helper class.
671  */
672 template <>
673 class SquareValueTyper<char>
674 {
675  public:
676 
677  /**
678  * Definition of the square distance type.
679  */
680  typedef unsigned int Type;
681 };
682 
683 /**
684  * Specialization of the helper class.
685  */
686 template <>
687 class SquareValueTyper<signed char>
688 {
689  public:
690 
691  /**
692  * Definition of the square distance type.
693  */
694  typedef unsigned int Type;
695 };
696 
697 /**
698  * Specialization of the helper class.
699  */
700 template <>
701 class SquareValueTyper<unsigned char>
702 {
703  public:
704 
705  /**
706  * Definition of the square distance type.
707  */
708  typedef unsigned int Type;
709 };
710 
711 /**
712  * Specialization of the helper class.
713  */
714 template <>
715 class SquareValueTyper<short>
716 {
717  public:
718 
719  /**
720  * Definition of the square distance type.
721  */
722  typedef unsigned int Type;
723 };
724 
725 /**
726  * Specialization of the helper class.
727  */
728 template <>
729 class SquareValueTyper<unsigned short>
730 {
731  public:
732 
733  /**
734  * Definition of the square distance type.
735  */
736  typedef unsigned int Type;
737 };
738 
739 /**
740  * Specialization of the helper class.
741  */
742 template <>
743 class SquareValueTyper<long>
744 {
745  public:
746 
747  /**
748  * Definition of the square distance type.
749  */
750  typedef unsigned long Type;
751 };
752 
753 /**
754  * Specialization of the helper class.
755  */
756 template <>
757 class SquareValueTyper<long long>
758 {
759  public:
760 
761  /**
762  * Definition of the square distance type.
763  */
764  typedef unsigned long long Type;
765 };
766 
767 /**
768  * Specialization of the helper class.
769  */
770 template <>
772 {
773  public:
774 
775  /**
776  * Definition of the signed distance type.
777  */
778  typedef int Type;
779 };
780 
781 /**
782  * Specialization of the helper class.
783  */
784 template <>
785 class DifferenceValueTyper<signed char>
786 {
787  public:
788 
789  /**
790  * Definition of the signed distance type.
791  */
792  typedef int Type;
793 };
794 
795 /**
796  * Specialization of the helper class.
797  */
798 template <>
799 class DifferenceValueTyper<unsigned char>
800 {
801  public:
802 
803  /**
804  * Definition of the signed distance type.
805  */
806  typedef int Type;
807 };
808 
809 /**
810  * Specialization of the helper class.
811  */
812 template <>
813 class DifferenceValueTyper<unsigned int>
814 {
815  public:
816 
817  /**
818  * Definition of the signed distance type.
819  */
820  typedef int Type;
821 };
822 
823 /**
824  * Specialization of the helper class.
825  */
826 template <>
828 {
829  public:
830 
831  /**
832  * Definition of the signed distance type.
833  */
834  typedef int Type;
835 };
836 
837 /**
838  * Specialization of the helper class.
839  */
840 template <>
841 class DifferenceValueTyper<unsigned short>
842 {
843  public:
844 
845  /**
846  * Definition of the signed distance type.
847  */
848  typedef int Type;
849 };
850 
851 /**
852  * Specialization of the helper class.
853  */
854 template <>
855 class DifferenceValueTyper<unsigned long>
856 {
857  public:
858 
859  /**
860  * Definition of the signed distance type.
861  */
862  typedef long Type;
863 };
864 
865 /**
866  * Specialization of the helper class.
867  */
868 template <>
869 class DifferenceValueTyper<unsigned long long>
870 {
871  public:
872 
873  /**
874  * Definition of the signed distance type.
875  */
876  typedef long long Type;
877 };
878 
879 /**
880  * Specialization of the helper class.
881  */
882 template <>
884 {
885  public:
886 
887  /**
888  * Definition of the absolute difference value type.
889  */
890  typedef unsigned int Type;
891 };
892 
893 /**
894  * Specialization of the helper class.
895  */
896 template <>
898 {
899  public:
900 
901  /**
902  * Definition of the absolute difference value type.
903  */
904  typedef unsigned int Type;
905 };
906 
907 /**
908  * Specialization of the helper class.
909  */
910 template <>
911 class AbsoluteDifferenceValueTyper<unsigned char>
912 {
913  public:
914 
915  /**
916  * Definition of the absolute difference value type.
917  */
918  typedef unsigned int Type;
919 };
920 
921 /**
922  * Specialization of the helper class.
923  */
924 template <>
926 {
927  public:
928 
929  /**
930  * Definition of the absolute difference value type.
931  */
932  typedef double Type;
933 };
934 
935 /**
936  * Specialization of the helper class.
937  */
938 template <>
940 {
941  public:
942 
943  /**
944  * Definition of the absolute difference value type.
945  */
946  typedef double Type;
947 };
948 
949 /**
950  * Specialization of the helper class.
951  */
952 template <>
953 class NextLargerTyper<char>
954 {
955  // As the sign of 'char' is not standardized, we do not allow the usage of 'char', use 'int8_t' instead.
956 };
957 
958 /**
959  * Specialization of the helper class.
960  */
961 template <>
962 class NextLargerTyper<signed char>
963 {
964  public:
965 
966  /**
967  * Definition of the data type for the next larger data type.
968  */
969  typedef short Type;
970 
971  /**
972  * Definition of the data type for the next larger data type regarding performance.
973  */
974  typedef int TypePerformance;
975 };
976 
977 /**
978  * Specialization of the helper class.
979  */
980 template <>
981 class NextLargerTyper<unsigned char>
982 {
983  public:
984 
985  /**
986  * Definition of the data type for the next larger data type.
987  */
988  typedef unsigned short Type;
989 
990  /**
991  * Definition of the data type for the next larger data type regarding performance.
992  */
993  typedef unsigned int TypePerformance;
994 };
995 
996 /**
997  * Specialization of the helper class.
998  */
999 template <>
1000 class NextLargerTyper<short>
1001 {
1002  public:
1003 
1004  /**
1005  * Definition of the data type for the next larger data type.
1006  */
1007  typedef int Type;
1008 
1009  /**
1010  * Definition of the data type for the next larger data type regarding performance.
1011  */
1012  typedef int TypePerformance;
1013 };
1014 
1015 /**
1016  * Specialization of the helper class.
1017  */
1018 template <>
1019 class NextLargerTyper<unsigned short>
1020 {
1021  public:
1022 
1023  /**
1024  * Definition of the data type for the next larger data type.
1025  */
1026  typedef unsigned int Type;
1027 
1028  /**
1029  * Definition of the data type for the next larger data type regarding performance.
1030  */
1031  typedef unsigned int TypePerformance;
1032 };
1033 
1034 /**
1035  * Specialization of the helper class.
1036  */
1037 template <>
1039 {
1040  public:
1041 
1042  /**
1043  * Definition of the data type for the next larger data type.
1044  */
1045  typedef long long Type;
1046 
1047  /**
1048  * Definition of the data type for the next larger data type regarding performance.
1049  */
1050  typedef long long TypePerformance;
1051 };
1052 
1053 /**
1054  * Specialization of the helper class.
1055  */
1056 template <>
1057 class NextLargerTyper<unsigned int>
1058 {
1059  public:
1060 
1061  /**
1062  * Definition of the data type for the next larger data type.
1063  */
1064  typedef unsigned long long Type;
1065 
1066  /**
1067  * Definition of the data type for the next larger data type regarding performance.
1068  */
1069  typedef unsigned long long TypePerformance;
1070 };
1071 
1072 /**
1073  * Specialization of the helper class.
1074  */
1075 template <>
1076 class NextLargerTyper<float>
1077 {
1078  public:
1079 
1080  /**
1081  * Definition of the data type for the next larger data type.
1082  */
1083  typedef double Type;
1084 
1085  /**
1086  * Definition of the data type for the next larger data type regarding performance.
1087  */
1088  typedef float TypePerformance;
1089 };
1090 
1091 /**
1092  * Specialization of the helper class.
1093  */
1094 template <>
1095 class SignedTyper<bool>
1096 {
1097  public:
1098 
1099  /**
1100  * Definition of the signed data type, if existing.
1101  */
1102  typedef bool Type;
1103 
1104  /**
1105  * True, if the data type T is an signed data type; False, otherwise.
1106  */
1107  static const bool isSigned = false;
1108 
1109  /**
1110  * True, if the data has a corresponding signed data type (or itself is signed).
1111  */
1112  static const bool hasSigned = false;
1113 };
1114 
1115 /**
1116  * Specialization of the helper class.
1117  */
1118 template <>
1119 class SignedTyper<char>
1120 {
1121  // As the sign of 'char' is not standardized, we do not allow the usage of 'char'.
1122 };
1123 
1124 /**
1125  * Specialization of the helper class.
1126  */
1127 template <>
1128 class SignedTyper<unsigned short>
1129 {
1130  public:
1131 
1132  /**
1133  * Definition of the signed data type, if existing.
1134  */
1135  typedef short Type;
1136 
1137  /**
1138  * True, if the data type T is an signed data type; False, otherwise.
1139  */
1140  static const bool isSigned = false;
1141 
1142  /**
1143  * True, if the data has a corresponding signed data type (or itself is signed).
1144  */
1145  static const bool hasSigned = true;
1146 };
1147 
1148 /**
1149  * Specialization of the helper class.
1150  */
1151 template <>
1152 class SignedTyper<unsigned int>
1153 {
1154  public:
1155 
1156  /**
1157  * Definition of the signed data type, if existing.
1158  */
1159  typedef int Type;
1160 
1161  /**
1162  * True, if the data type T is an signed data type; False, otherwise.
1163  */
1164  static const bool isSigned = false;
1165 
1166  /**
1167  * True, if the data has a corresponding signed data type (or itself is signed).
1168  */
1169  static const bool hasSigned = true;
1170 };
1171 
1172 /**
1173  * Specialization of the helper class.
1174  */
1175 template <>
1176 class SignedTyper<unsigned long long>
1177 {
1178  public:
1179 
1180  /**
1181  * Definition of the signed data type, if existing.
1182  */
1183  typedef long long Type;
1184 
1185  /**
1186  * True, if the data type T is an signed data type; False, otherwise.
1187  */
1188  static const bool isSigned = false;
1189 
1190  /**
1191  * True, if the data has a corresponding signed data type (or itself is signed).
1192  */
1193  static const bool hasSigned = true;
1194 };
1195 
1196 /**
1197  * Specialization of the helper class.
1198  */
1199 template <>
1200 class UnsignedTyper<char>
1201 {
1202  // As the sign of 'char' is not standardized, we do not allow the usage of 'char'.
1203 };
1204 
1205 /**
1206  * Specialization of the helper class.
1207  */
1208 template <>
1209 class UnsignedTyper<signed char>
1210 {
1211  public:
1212 
1213  /**
1214  * Definition of the unsigned data type, if existing.
1215  */
1216  typedef unsigned char Type;
1217 
1218  /**
1219  * True, if the data type T is an unsigned data type; False, otherwise.
1220  */
1221  static const bool isUnsigned = false;
1222 
1223  /**
1224  * True, if the data has a corresponding unsigned data type (or itself is unsigned).
1225  */
1226  static const bool hasUnsigned = true;
1227 };
1228 
1229 /**
1230  * Specialization of the helper class.
1231  */
1232 template <>
1233 class UnsignedTyper<short>
1234 {
1235  public:
1236 
1237  /**
1238  * Definition of the unsigned data type, if existing.
1239  */
1240  typedef unsigned short Type;
1241 
1242  /**
1243  * True, if the data type T is an unsigned data type; False, otherwise.
1244  */
1245  static const bool isUnsigned = false;
1246 
1247  /**
1248  * True, if the data has a corresponding unsigned data type (or itself is unsigned).
1249  */
1250  static const bool hasUnsigned = true;
1251 };
1252 
1253 /**
1254  * Specialization of the helper class.
1255  */
1256 template <>
1257 class UnsignedTyper<int>
1258 {
1259  public:
1260 
1261  /**
1262  * Definition of the unsigned data type, if existing.
1263  */
1264  typedef unsigned int Type;
1265 
1266  /**
1267  * True, if the data type T is an unsigned data type; False, otherwise.
1268  */
1269  static const bool isUnsigned = false;
1270 
1271  /**
1272  * True, if the data has a corresponding unsigned data type (or itself is unsigned).
1273  */
1274  static const bool hasUnsigned = true;
1275 };
1276 
1277 /**
1278  * Specialization of the helper class.
1279  */
1280 template <>
1281 class UnsignedTyper<long>
1282 {
1283  public:
1284 
1285  /**
1286  * Definition of the unsigned data type, if existing.
1287  */
1288  typedef unsigned long Type;
1289 
1290  /**
1291  * True, if the data type T is an unsigned data type; False, otherwise.
1292  */
1293  static const bool isUnsigned = false;
1294 
1295  /**
1296  * True, if the data has a corresponding unsigned data type (or itself is unsigned).
1297  */
1298  static const bool hasUnsigned = true;
1299 };
1300 
1301 /**
1302  * Specialization of the helper class.
1303  */
1304 template <>
1305 class UnsignedTyper<long long>
1306 {
1307  public:
1308 
1309  /**
1310  * Definition of the unsigned data type, if existing.
1311  */
1312  typedef unsigned long long Type;
1313 
1314  /**
1315  * True, if the data type T is an unsigned data type; False, otherwise.
1316  */
1317  static const bool isUnsigned = false;
1318 
1319  /**
1320  * True, if the data has a corresponding unsigned data type (or itself is unsigned).
1321  */
1322  static const bool hasUnsigned = true;
1323 };
1324 
1325 /**
1326  * Specialization of the helper class.
1327  */
1328 template <>
1329 class UnsignedTyper<float>
1330 {
1331  public:
1332 
1333  /**
1334  * Definition of the unsigned data type, if existing.
1335  */
1336  typedef float Type;
1337 
1338  /**
1339  * True, if the data type T is an unsigned data type; False, otherwise.
1340  */
1341  static const bool isUnsigned = false;
1342 
1343  /**
1344  * True, if the data has a corresponding unsigned data type (or itself is unsigned).
1345  */
1346  static const bool hasUnsigned = false;
1347 };
1348 
1349 /**
1350  * Specialization of the helper class.
1351  */
1352 template <>
1353 class UnsignedTyper<double>
1354 {
1355  public:
1356 
1357  /**
1358  * Definition of the unsigned data type, if existing.
1359  */
1360  typedef double Type;
1361 
1362  /**
1363  * True, if the data type T is an unsigned data type; False, otherwise.
1364  */
1365  static const bool isUnsigned = false;
1366 
1367  /**
1368  * True, if the data has a corresponding unsigned data type (or itself is unsigned).
1369  */
1370  static const bool hasUnsigned = false;
1371 };
1372 
1373 /**
1374  * Specialization of the helper struct.
1375  */
1376 template <>
1377 struct FloatTyper<double>
1378 {
1379  /**
1380  * The 64 bit floating point data type if T is 'double'
1381  */
1382  typedef double Type;
1383 };
1384 
1385 template <>
1386 inline const char* TypeNamer::name<bool>()
1387 {
1388  static const char* value = "bool";
1389  return value;
1390 }
1391 
1392 template <>
1393 inline const char* TypeNamer::name<char>()
1394 {
1395  static const char* value = "char";
1396  return value;
1397 }
1398 
1399 template <>
1400 inline const char* TypeNamer::name<signed char>()
1401 {
1402  static const char* value = "signed char";
1403  return value;
1404 }
1405 
1406 template <>
1407 inline const char* TypeNamer::name<unsigned char>()
1408 {
1409  static const char* value = "unsigned char";
1410  return value;
1411 }
1412 
1413 template <>
1414 inline const char* TypeNamer::name<short>()
1415 {
1416  static const char* value = "short";
1417  return value;
1418 }
1419 
1420 template <>
1421 inline const char* TypeNamer::name<unsigned short>()
1422 {
1423  static const char* value = "unsigned short";
1424  return value;
1425 }
1426 
1427 template <>
1428 inline const char* TypeNamer::name<int>()
1429 {
1430  static const char* value = "int";
1431  return value;
1432 }
1433 
1434 template <>
1435 inline const char* TypeNamer::name<unsigned int>()
1436 {
1437  static const char* value = "unsigned int";
1438  return value;
1439 }
1440 
1441 template <>
1442 inline const char* TypeNamer::name<long>()
1443 {
1444  static const char* value = "long";
1445  return value;
1446 }
1447 
1448 template <>
1449 inline const char* TypeNamer::name<unsigned long>()
1450 {
1451  static const char* value = "unsigned long";
1452  return value;
1453 }
1454 
1455 template <>
1456 inline const char* TypeNamer::name<long long>()
1457 {
1458  static const char* value = "long long";
1459  return value;
1460 }
1461 
1462 template <>
1463 inline const char* TypeNamer::name<unsigned long long>()
1464 {
1465  static const char* value = "unsigned long long";
1466  return value;
1467 }
1468 
1469 template <>
1470 inline const char* TypeNamer::name<float>()
1471 {
1472  static const char* value = "float";
1473  return value;
1474 }
1475 
1476 template <>
1477 inline const char* TypeNamer::name<double>()
1478 {
1479  static const char* value = "double";
1480  return value;
1481 }
1482 
1483 /**
1484  * Specialized object.
1485  */
1486 template <>
1488 {
1489  public:
1490 
1491  /**
1492  * Definition of the mapped data type.
1493  */
1494  typedef uint8_t Type;
1495 };
1496 
1497 /**
1498  * Specialized object.
1499  */
1500 template <>
1502 {
1503  public:
1504 
1505  /**
1506  * Definition of the mapped data type.
1507  */
1508  typedef uint16_t Type;
1509 };
1510 
1511 /**
1512  * Specialized object.
1513  */
1514 template <>
1516 {
1517  public:
1518 
1519  /**
1520  * Definition of the mapped data type.
1521  */
1522  typedef uint32_t Type;
1523 };
1524 
1525 /**
1526  * Specialized object.
1527  */
1528 template <>
1530 {
1531  public:
1532 
1533  /**
1534  * Definition of the mapped data type.
1535  */
1536  typedef uint64_t Type;
1537 };
1538 
1539 inline Float16::Float16(const uint16_t sign, const uint16_t fraction, const uint16_t exponent)
1540 {
1541  ocean_assert(sign <= 1u);
1542  ocean_assert(fraction <= 1023u);
1543  ocean_assert(exponent <= 31u);
1544 
1545  data_.ieee_.fraction_ = fraction;
1546  data_.ieee_.exponent_ = exponent;
1547  data_.ieee_.sign_ = sign;
1548 }
1549 
1550 inline Float16::Float16(const uint16_t binary)
1551 {
1552  data_.binary_ = binary;
1553 }
1554 
1555 inline uint16_t Float16::binary() const
1556 {
1557  return data_.binary_;
1558 }
1559 
1561 {
1562  return Float16((~data_.ieee_.sign_) & 0x1u, data_.ieee_.fraction_, data_.ieee_.exponent_);
1563 }
1564 
1565 inline bool Float16::operator==(const Float16& second) const
1566 {
1567  return data_.binary_ == second.data_.binary_;
1568 }
1569 
1570 inline bool Float16::operator!=(const Float16& second) const
1571 {
1572  return !(*this == second);
1573 }
1574 
1576 {
1577  constexpr uint16_t maxExponent = 31u;
1578 
1579  return Float16(0u, maxExponent, 0u);
1580 }
1581 
1582 }
1583 
1584 #endif // META_OCEAN_BASE_DATA_TYPE_H
unsigned int Type
Definition of the absolute difference value type.
Definition: DataType.h:890
double Type
Definition of the absolute difference value type.
Definition: DataType.h:946
double Type
Definition of the absolute difference value type.
Definition: DataType.h:932
unsigned int Type
Definition of the absolute difference value type.
Definition: DataType.h:904
unsigned int Type
Definition of the absolute difference value type.
Definition: DataType.h:918
This class is a helper class allowing to define the resulting data type for an absolute difference op...
Definition: DataType.h:218
unsigned long long Type
Definition of the data type for the absolute difference value.
Definition: DataType.h:225
Template class allowing to define an array of data types.
Definition: DataType.h:27
int Type
Definition of the signed distance type.
Definition: DataType.h:778
int Type
Definition of the signed distance type.
Definition: DataType.h:834
int Type
Definition of the signed distance type.
Definition: DataType.h:792
int Type
Definition of the signed distance type.
Definition: DataType.h:806
int Type
Definition of the signed distance type.
Definition: DataType.h:820
long Type
Definition of the signed distance type.
Definition: DataType.h:862
long long Type
Definition of the signed distance type.
Definition: DataType.h:876
int Type
Definition of the signed distance type.
Definition: DataType.h:848
This class is a helper class allowing to define the signed difference data type of a given type.
Definition: DataType.h:169
T Type
Definition of the data type for the signed difference value.
Definition: DataType.h:176
Definition of a 16-bit float type.
Definition: DataType.h:548
bool operator==(const Float16 &second) const
Returns whether two 16-bit float values are identical.
Definition: DataType.h:1565
union Ocean::Float16::@0 data_
Union allowing to access the binary representation or the IEEE representation.
uint16_t binary() const
Returns the binary representation of this 16-bit float value.
Definition: DataType.h:1555
uint16_t exponent_
The exponent composed of 5 bits.
Definition: DataType.h:627
uint16_t sign_
The sign composed of 1 bit.
Definition: DataType.h:630
Float16()=default
Default constructor.
bool operator!=(const Float16 &second) const
Returns whether two 16-bit float values are not identical.
Definition: DataType.h:1570
static Float16 infinity()
Returns +infinity.
Definition: DataType.h:1575
uint16_t binary_
The binary representation.
Definition: DataType.h:616
uint16_t fraction_
The mantissa composed of 10 bits.
Definition: DataType.h:624
Float16 operator-() const
Negates the 16-bit float value.
Definition: DataType.h:1560
This class implements a helper class allowing to define the identity data type of a given type.
Definition: DataType.h:86
T Type
The data type of 'T'.
Definition: DataType.h:90
std::unordered_map< TKey, TElement > TMap
Definition of an unordered map.
Definition: DataType.h:540
Helper class allowing to define an ordered or unordered map based on the template parameter 'tOrdered...
Definition: DataType.h:518
std::map< TKey, TElement > TMap
Definition of an ordered map.
Definition: DataType.h:525
double Type
Definition of the data type for the next larger data type.
Definition: DataType.h:1083
float TypePerformance
Definition of the data type for the next larger data type regarding performance.
Definition: DataType.h:1088
long long TypePerformance
Definition of the data type for the next larger data type regarding performance.
Definition: DataType.h:1050
long long Type
Definition of the data type for the next larger data type.
Definition: DataType.h:1045
int TypePerformance
Definition of the data type for the next larger data type regarding performance.
Definition: DataType.h:1012
int Type
Definition of the data type for the next larger data type.
Definition: DataType.h:1007
int TypePerformance
Definition of the data type for the next larger data type regarding performance.
Definition: DataType.h:974
short Type
Definition of the data type for the next larger data type.
Definition: DataType.h:969
unsigned int TypePerformance
Definition of the data type for the next larger data type regarding performance.
Definition: DataType.h:993
unsigned short Type
Definition of the data type for the next larger data type.
Definition: DataType.h:988
unsigned long long Type
Definition of the data type for the next larger data type.
Definition: DataType.h:1064
unsigned long long TypePerformance
Definition of the data type for the next larger data type regarding performance.
Definition: DataType.h:1069
unsigned int TypePerformance
Definition of the data type for the next larger data type regarding performance.
Definition: DataType.h:1031
unsigned int Type
Definition of the data type for the next larger data type.
Definition: DataType.h:1026
This class is a helper class allowing to determine the next larger data type for a given data type.
Definition: DataType.h:248
T Type
Definition of the data type for the next larger data type.
Definition: DataType.h:255
T TypePerformance
Definition of the data type for the next larger data type regarding performance.
Definition: DataType.h:261
bool Type
Definition of the data type which is 'bool' in case of 'T' is 'void'.
Definition: DataType.h:418
static constexpr bool defaultValue()
Returns a default value for 'T'.
Definition: DataType.h:424
This struct implements a helper struct allowing to use a data type instead of void.
Definition: DataType.h:389
T Type
Definition of the data type which is 'bool' in case of 'T' is 'void'.
Definition: DataType.h:395
static constexpr T defaultValue()
Returns a default value for 'T'.
Definition: DataType.h:401
bool Type
Definition of the signed data type, if existing.
Definition: DataType.h:1102
int Type
Definition of the signed data type, if existing.
Definition: DataType.h:1159
long long Type
Definition of the signed data type, if existing.
Definition: DataType.h:1183
short Type
Definition of the signed data type, if existing.
Definition: DataType.h:1135
This class is a helper class allowing to determine the signed data type for a given data type if exis...
Definition: DataType.h:288
static const bool hasSigned
True, if the data has a corresponding signed data type (or itself is signed).
Definition: DataType.h:304
static const bool isSigned
True, if the data type T is an signed data type; False, otherwise.
Definition: DataType.h:299
T Type
Definition of the signed data type, if existing.
Definition: DataType.h:294
unsigned int Type
Definition of the square distance type.
Definition: DataType.h:680
unsigned int Type
Definition of the square distance type.
Definition: DataType.h:666
unsigned long Type
Definition of the square distance type.
Definition: DataType.h:750
unsigned long long Type
Definition of the square distance type.
Definition: DataType.h:764
unsigned int Type
Definition of the square distance type.
Definition: DataType.h:722
unsigned int Type
Definition of the square distance type.
Definition: DataType.h:694
unsigned int Type
Definition of the square distance type.
Definition: DataType.h:708
unsigned int Type
Definition of the square distance type.
Definition: DataType.h:736
This class is a helper class allowing to define the square difference data type of a given type.
Definition: DataType.h:125
T Type
Definition of the data type for the square value.
Definition: DataType.h:132
uint8_t Type
Definition of the mapped data type.
Definition: DataType.h:1494
uint16_t Type
Definition of the mapped data type.
Definition: DataType.h:1508
uint32_t Type
Definition of the mapped data type.
Definition: DataType.h:1522
uint64_t Type
Definition of the mapped data type.
Definition: DataType.h:1536
This class provides a unique data type for any data type specified by the number of bytes.
Definition: DataType.h:464
void Type
Definition of an invalid mapped data type.
Definition: DataType.h:465
This class provides a unique data type for any data type.
Definition: DataType.h:500
TypeMapperBySize< sizeof(T)>::Type Type
Definition of an invalid mapped data type.
Definition: DataType.h:501
This class implements a helper class allowing to determine unique names for most of native C++ data t...
Definition: DataType.h:435
static const char * name()
Returns a user-friendly name for most of native C++ data types.
double Type
Definition of the unsigned data type, if existing.
Definition: DataType.h:1360
float Type
Definition of the unsigned data type, if existing.
Definition: DataType.h:1336
unsigned int Type
Definition of the unsigned data type, if existing.
Definition: DataType.h:1264
unsigned long Type
Definition of the unsigned data type, if existing.
Definition: DataType.h:1288
unsigned long long Type
Definition of the unsigned data type, if existing.
Definition: DataType.h:1312
unsigned short Type
Definition of the unsigned data type, if existing.
Definition: DataType.h:1240
unsigned char Type
Definition of the unsigned data type, if existing.
Definition: DataType.h:1216
This class is a helper class allowing to determine the unsigned data type for a given data type if ex...
Definition: DataType.h:333
static const bool hasUnsigned
True, if the data has a corresponding unsigned data type (or itself is unsigned).
Definition: DataType.h:349
static const bool isUnsigned
True, if the data type T is an unsigned data type; False, otherwise.
Definition: DataType.h:344
T Type
Definition of the unsigned data type, if existing.
Definition: DataType.h:339
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15
AutomaticDifferentiationT< T1, TNumeric1 > operator-(const T2 &left, const AutomaticDifferentiationT< T1, TNumeric1 > &right)
Definition: AutomaticDifferentiation.h:484
Default definition of a type with tBytes bytes.
Definition: DataType.h:32
T values_[tElements]
Data values of the type.
Definition: DataType.h:48
bool operator!=(const Type &type) const
Returns whether two objects do not store the same values.
Definition: DataType.h:650
bool operator==(const Type &type) const
Returns whether two objects store the same values.
Definition: DataType.h:636
double Type
The 64 bit floating point data type if T is 'double'.
Definition: DataType.h:1382
This struct is a helper struct providing a floating point data type best matching for a given data ty...
Definition: DataType.h:369
float Type
The 32 bit floating point data type for any data type T but 'double'.
Definition: DataType.h:373