Stratax 0.3.1
Loading...
Searching...
No Matches
DTypeTraits.hpp
1#pragma once
2
3#include <climits>
4#include <cstddef>
5#include <limits>
6#include <string_view>
7#include <type_traits>
8
9#include <stratax/core/dtypes/Types.hpp>
10
11namespace stratax::core {
12
16enum class DTypeKind
17{
18 Bool,
19 SignedInteger,
20 UnsignedInteger,
21 Floating,
22 Complex
23};
24
25namespace dtype_detail {
26
33template<typename T>
35
39#define STRATAX_DEFINE_DTYPE_TRAITS(TYPE, KIND, NAME) \
40 template<> \
41 struct DTypeTraitsImpl<TYPE> \
42 { \
43 using type = TYPE; \
44 static constexpr DTypeKind kind = DTypeKind::KIND; \
45 static constexpr std::size_t bits = sizeof(type) * CHAR_BIT; \
46 static constexpr int digits = std::numeric_limits<type>::digits; \
47 static constexpr std::string_view name = NAME; \
48 }
49
53#define STRATAX_DEFINE_FLOAT_DTYPE_TRAITS(TYPE, RANK, NAME) \
54 template<> \
55 struct DTypeTraitsImpl<TYPE> \
56 { \
57 using type = TYPE; \
58 static constexpr DTypeKind kind = DTypeKind::Floating; \
59 static constexpr std::size_t bits = sizeof(type) * CHAR_BIT; \
60 static constexpr int digits = std::numeric_limits<type>::digits; \
61 static constexpr std::size_t rank = RANK; \
62 static constexpr std::string_view name = NAME; \
63 }
64
71#define STRATAX_DEFINE_COMPLEX_DTYPE_TRAITS(TYPE, COMPONENT, RANK, NAME) \
72 template<> \
73 struct DTypeTraitsImpl<TYPE> \
74 { \
75 using type = TYPE; \
76 using component_type = COMPONENT; \
77 static constexpr DTypeKind kind = DTypeKind::Complex; \
78 static constexpr std::size_t bits = sizeof(type) * CHAR_BIT; \
79 static constexpr int digits = std::numeric_limits<component_type>::digits; \
80 static constexpr std::size_t rank = RANK; \
81 static constexpr std::string_view name = NAME; \
82 }
83
84STRATAX_DEFINE_DTYPE_TRAITS(dtype::bool_, Bool, "bool");
85
86STRATAX_DEFINE_DTYPE_TRAITS(dtype::int8, SignedInteger, "int8");
87STRATAX_DEFINE_DTYPE_TRAITS(dtype::int16, SignedInteger, "int16");
88STRATAX_DEFINE_DTYPE_TRAITS(dtype::int32, SignedInteger, "int32");
89STRATAX_DEFINE_DTYPE_TRAITS(dtype::int64, SignedInteger, "int64");
90
91STRATAX_DEFINE_DTYPE_TRAITS(dtype::uint8, UnsignedInteger, "uint8");
92STRATAX_DEFINE_DTYPE_TRAITS(dtype::uint16, UnsignedInteger, "uint16");
93STRATAX_DEFINE_DTYPE_TRAITS(dtype::uint32, UnsignedInteger, "uint32");
94STRATAX_DEFINE_DTYPE_TRAITS(dtype::uint64, UnsignedInteger, "uint64");
95
96STRATAX_DEFINE_FLOAT_DTYPE_TRAITS(dtype::float32, 0, "float32");
97STRATAX_DEFINE_FLOAT_DTYPE_TRAITS(dtype::float64, 1, "float64");
98STRATAX_DEFINE_FLOAT_DTYPE_TRAITS(dtype::longdouble, 2, "longdouble");
99
100STRATAX_DEFINE_COMPLEX_DTYPE_TRAITS(dtype::complex64, dtype::float32, 0, "complex64");
101STRATAX_DEFINE_COMPLEX_DTYPE_TRAITS(dtype::complex128, dtype::float64, 1, "complex128");
102STRATAX_DEFINE_COMPLEX_DTYPE_TRAITS(dtype::clongdouble, dtype::longdouble, 2, "clongdouble");
103
104#undef STRATAX_DEFINE_DTYPE_TRAITS
105#undef STRATAX_DEFINE_FLOAT_DTYPE_TRAITS
106#undef STRATAX_DEFINE_COMPLEX_DTYPE_TRAITS
107
108} // namespace dtype_detail
109
117template<typename T>
119 : dtype_detail::DTypeTraitsImpl<std::remove_cvref_t<T>>
120{};
121
125template<typename T>
127
128#define STRATAX_DEFINE_COMPLEX_COMPONENT(COMPLEX, REAL) \
129 template<> \
130 struct ComplexComponent<COMPLEX> \
131 { \
132 using type = REAL; \
133 }
134
135STRATAX_DEFINE_COMPLEX_COMPONENT(dtype::complex64, dtype::float32);
136STRATAX_DEFINE_COMPLEX_COMPONENT(dtype::complex128, dtype::float64);
137STRATAX_DEFINE_COMPLEX_COMPONENT(dtype::clongdouble, dtype::longdouble);
138
139#undef STRATAX_DEFINE_COMPLEX_COMPONENT
140
144template<typename T>
145using complex_component_t =
147
151template<typename T>
153
154#define STRATAX_DEFINE_COMPLEX_FROM_REAL(REAL, COMPLEX) \
155 template<> \
156 struct ComplexFromReal<REAL> \
157 { \
158 using type = COMPLEX; \
159 }
160
161STRATAX_DEFINE_COMPLEX_FROM_REAL(dtype::float32, dtype::complex64);
162STRATAX_DEFINE_COMPLEX_FROM_REAL(dtype::float64, dtype::complex128);
163STRATAX_DEFINE_COMPLEX_FROM_REAL(dtype::longdouble, dtype::clongdouble);
164
165#undef STRATAX_DEFINE_COMPLEX_FROM_REAL
166
170template<typename T>
171using complex_from_real_t =
173
174} // namespace stratax::core
Maps a complex dtype to its underlying real component dtype.
Maps a real floating-point dtype to its corresponding complex dtype.
Provides compile-time metadata for a supported Stratax dtype.
Implementation registry for supported Stratax dtypes.