lamppp
Loading...
Searching...
No Matches
data_type.hpp
1#pragma once
2
3#include <cstdint>
4#include <ostream>
5
6namespace lmp::tensor {
7
16enum class DataType : uint8_t {
17 Bool = 0,
18 Int16 = 1,
19 Int32 = 2,
20 Int64 = 3,
21 Float32 = 4,
22 Float64 = 5
23};
24
26
30template <typename T>
31struct TypeMeta;
32
33template <>
34struct TypeMeta<bool> {
35 static constexpr DataType value = DataType::Bool;
36};
37template <>
38struct TypeMeta<int16_t> {
39 static constexpr DataType value = DataType::Int16;
40};
41template <>
42struct TypeMeta<int> {
43 static constexpr DataType value = DataType::Int32;
44};
45template <>
46struct TypeMeta<int64_t> {
47 static constexpr DataType value = DataType::Int64;
48};
49template <>
50struct TypeMeta<float> {
51 static constexpr DataType value = DataType::Float32;
52};
53template <>
54struct TypeMeta<double> {
55 static constexpr DataType value = DataType::Float64;
56};
57
59
68inline DataType type_upcast(DataType a_type, DataType b_type) {
69 return static_cast<DataType>(
70 std::max(static_cast<uint8_t>(a_type), static_cast<uint8_t>(b_type)));
71}
72
73inline std::ostream& operator<<(std::ostream& os, DataType dtype) {
74 switch (dtype) {
75 case DataType::Bool:
76 os << "Bool";
77 break;
78 case DataType::Int16:
79 os << "Int16";
80 break;
81 case DataType::Int32:
82 os << "Int32";
83 break;
84 case DataType::Int64:
85 os << "Int64";
86 break;
87 case DataType::Float32:
88 os << "Float32";
89 break;
90 case DataType::Float64:
91 os << "Float64";
92 break;
93 default:
94 os << "Unknown DataType";
95 break;
96 }
97 return os;
98}
99
100} // namespace lmp::tensor
simple template to convert from a concrete type (like int) to the enum type, Int32....
Definition data_type.hpp:31