Tannic
A C++ Tensor Library
Loading...
Searching...
No Matches
types.hpp
Go to the documentation of this file.
1// Copyright 2025 Eric Hermosis
2//
3// This file is part of the Tannic Tensor Library.
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16//
17
18#ifndef TYPES_HPP
19#define TYPES_HPP
20
68#include <iostream>
69#include <cstdint>
70#include <string>
71#include <complex>
72#include "runtime/types.h"
73
74namespace tannic {
75
94constexpr inline std::size_t dsizeof(type type) {
95 switch (type) {
96 case boolean: return 0;
97 case int8: return sizeof(int8_t);
98 case int16: return sizeof(int16_t);
99 case int32: return sizeof(int32_t);
100 case int64: return sizeof(int64_t);
101 case float16: return sizeof(float) / 2;
102 case float32: return sizeof(float);
103 case float64: return 2 * sizeof(float);
104 case complex64: return 2 * sizeof(float);
105 case complex128:return 2 * sizeof(double);
106 default: return 0;
107 }
108}
109
129constexpr inline std::size_t nbytesof(type dtype, std::size_t nelements) {
130 if (dtype == boolean) {
131 return (nelements + 7) / 8;
132 }
133 return dsizeof(dtype) * nelements;
134}
135
136
148constexpr inline std::string dnameof(type type) {
149 switch (type) {
150 case boolean: return "boolean";
151 case int8: return "int8";
152 case int16: return "int16";
153 case int32: return "int32";
154 case int64: return "int64";
155 case float16: return "float16";
156 case float32: return "float32";
157 case float64: return "float64";
158 case complex64: return "complex64";
159 case complex128: return "complex128";
160 default: return "none";
161 }
162}
163
185constexpr inline uint8_t dcodeof(type type) {
186 switch (type) {
187 case boolean: return 1;
188 case int8: return 12;
189 case int16: return 13;
190 case int32: return 14;
191 case int64: return 15;
192 case float16: return 23;
193 case float32: return 24;
194 case float64: return 25;
195 case complex64: return 35;
196 case complex128:return 36;
197 default: return 0;
198 }
199}
200
212constexpr inline type dtypeof(uint8_t code) {
213 switch (code) {
214 case 1 : return boolean;
215 case 12: return int8;
216 case 13: return int16;
217 case 14: return int32;
218 case 15: return int64;
219 case 23: return float16;
220 case 24: return float32;
221 case 25: return float64;
222 case 35: return complex64;
223 case 36: return complex128;
224 default: return unknown;
225 }
226}
227
228template <typename T>
229constexpr inline type dtypeof() {
230 if constexpr (std::is_same_v<T, bool>) return boolean;
231 else if constexpr (std::is_same_v<T, int8_t>) return int8;
232 else if constexpr (std::is_same_v<T, int16_t>) return int16;
233 else if constexpr (std::is_same_v<T, int32_t>) return int32;
234 else if constexpr (std::is_same_v<T, int64_t>) return int64;
235 else if constexpr (std::is_same_v<T, float16_t>) return float16;
236 else if constexpr (std::is_same_v<T, float>) return float32;
237 else if constexpr (std::is_same_v<T, double>) return float64;
238 else if constexpr (std::is_same_v<T, std::complex<float>>) return complex64;
239 else if constexpr (std::is_same_v<T, std::complex<double>>) return complex128;
240 else return unknown;
241}
242
243inline std::ostream& operator<<(std::ostream& ostream, type type) {
244 return ostream << dnameof(type);
245}
246
247} // namespace tannic
248
249#endif // TYPES_HPP
Definition: buffer.hpp:41
constexpr std::string dnameof(type type)
Returns the string name of a given tensor data type.
Definition: types.hpp:148
constexpr type dtypeof()
Definition: types.hpp:229
constexpr uint8_t dcodeof(type type)
Returns the numeric code used for serialization of a data type.
Definition: types.hpp:185
std::ostream & operator<<(std::ostream &os, Shape const &shape)
Definition: shape.hpp:313
constexpr std::size_t nbytesof(type dtype, std::size_t nelements)
Returns the total number of bytes required to store nelements elements of the given data type.
Definition: types.hpp:129
constexpr std::size_t dsizeof(type type)
Returns the size in bytes of a given tensor data type.
Definition: types.hpp:94