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
67#include <iostream>
68#include <cstdint>
69#include <string>
70#include <complex>
71#include "runtime/types.h"
72
73namespace tannic {
74
93constexpr inline std::size_t dsizeof(type type) {
94 switch (type) {
95 case boolean: return 0;
96 case int8: return sizeof(int8_t);
97 case int16: return sizeof(int16_t);
98 case int32: return sizeof(int32_t);
99 case int64: return sizeof(int64_t);
100 case float32: return sizeof(float);
101 case float64: return sizeof(double);
102 case complex64: return 2 * sizeof(float);
103 case complex128:return 2 * sizeof(double);
104 default: return 0;
105 }
106}
107
127constexpr inline std::size_t nbytesof(type dtype, std::size_t nelements) {
128 if (dtype == boolean) {
129 return (nelements + 7) / 8;
130 }
131 return dsizeof(dtype) * nelements;
132}
133
134
146constexpr inline std::string dnameof(type type) {
147 switch (type) {
148 case boolean: return "boolean";
149 case int8: return "int8";
150 case int16: return "int16";
151 case int32: return "int32";
152 case int64: return "int64";
153 case float32: return "float32";
154 case float64: return "float64";
155 case complex64: return "complex64";
156 case complex128: return "complex128";
157 default: return "none";
158 }
159}
160
182constexpr inline uint8_t dcodeof(type type) {
183 switch (type) {
184 case boolean: return 1;
185 case int8: return 12;
186 case int16: return 13;
187 case int32: return 14;
188 case int64: return 15;
189 case float32: return 24;
190 case float64: return 25;
191 case complex64: return 35;
192 case complex128:return 36;
193 default: return 0;
194 }
195}
196
208constexpr inline type dtypeof(uint8_t code) {
209 switch (code) {
210 case 1 : return boolean;
211 case 12: return int8;
212 case 13: return int16;
213 case 14: return int32;
214 case 15: return int64;
215 case 24: return float32;
216 case 25: return float64;
217 case 35: return complex64;
218 case 36: return complex128;
219 default: return unknown;
220 }
221}
222
223template <typename T>
224constexpr inline type dtypeof() {
225 if constexpr (std::is_same_v<T, bool>) return boolean;
226 else if constexpr (std::is_same_v<T, int8_t>) return int8;
227 else if constexpr (std::is_same_v<T, int16_t>) return int16;
228 else if constexpr (std::is_same_v<T, int32_t>) return int32;
229 else if constexpr (std::is_same_v<T, int64_t>) return int64;
230 else if constexpr (std::is_same_v<T, float>) return float32;
231 else if constexpr (std::is_same_v<T, double>) return float64;
232 else if constexpr (std::is_same_v<T, std::complex<float>>) return complex64;
233 else if constexpr (std::is_same_v<T, std::complex<double>>) return complex128;
234 else return unknown;
235}
236
237inline std::ostream& operator<<(std::ostream& ostream, type type) {
238 return ostream << dnameof(type);
239}
240
241} // namespace tannic
242
243#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:146
constexpr type dtypeof()
Definition: types.hpp:224
constexpr uint8_t dcodeof(type type)
Returns the numeric code used for serialization of a data type.
Definition: types.hpp:182
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:127
constexpr std::size_t dsizeof(type type)
Returns the size in bytes of a given tensor data type.
Definition: types.hpp:93