docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
primitive_types.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <limits>
5#include <stdexcept>
6
7namespace arg_capture {
8
9enum class PrimitiveType {
10 Void = 0,
11 Bool = 1,
12 Int8 = 2,
13 Int16 = 3,
14 Int32 = 4,
15 Int64 = 5,
16 Int128 = 6,
17 UInt8 = 7,
18 UInt16 = 8,
19 UInt32 = 9,
20 UInt64 = 10,
21 UInt128 = 11,
22 Half = 12,
23 BFloat = 13,
24 Float = 14,
25 Double = 15,
27 FP128 = 17,
30};
31
32static const char* primitive_type_names[] = {
33 "Void",
34 "Bool",
35 "Int8",
36 "Int16",
37 "Int32",
38 "Int64",
39 "Int128",
40 "UInt8",
41 "UInt16",
42 "UInt32",
43 "UInt64",
44 "UInt128",
45 "Half",
46 "BFloat",
47 "Float",
48 "Double",
49 "X86_FP80",
50 "FP128",
51 "PPC_FP128"
52};
53
54static const char* primitive_type_cppspellings[] = {
55 "void", // Void
56 "bool", // Bool
57 "int8_t", // Int8
58 "int16_t", // Int16
59 "int32_t", // Int32
60 "int64_t", // Int64
61 "__int128", // Int128
62 "uint8_t", // UInt8
63 "uint16_t", // UInt16
64 "uint32_t", // UInt32
65 "uint64_t", // UInt64
66 "unsigned __int128", // UInt128
67 "_Float16", // Half
68 "__bf16", // BFloat (clang builtin, 16-bit IEEE bfloat16; storage-only, promotes to float)
69 "float", // Float
70 "double", // Double
71 "long double", // X86_FP80 (x86 80-bit extended precision)
72 "__float128", // FP128 (software quad precision via libquadmath)
73 "__ibm128" // PPC_FP128 (IBM double-double)
74};
75
76static const double primitive_type_machine_epsilons[] = {
77 0.0, // void
78 0.0, // bool
79 0.0, // int8_t
80 0.0, // int16_t
81 0.0, // int32_t
82 0.0, // int64_t
83 0.0, // __int128
84 0.0, // uint8_t
85 0.0, // uint16_t
86 0.0, // uint32_t
87 0.0, // uint64_t
88 0.0, // unsigned __int128
89 9.765625e-04, // Half (_Float16) ≈ 2^-10
90 7.8125e-03, // BFloat16 (__bf16) ≈ 2^-7
91 1.1920929e-07, // Float (float, 32-bit) ≈ 2^-23
92 2.220446049250313e-16, // Double (double, 64-bit) ≈ 2^-52
93 1.0842021724855044e-19, // long double (x86_80-bit) ≈ 2^-63
94 1.9259299443872354e-34, // __float128 (quad-precision) ≈ 2^-112
95 1.0842021724855044e-19 // __ibm128 (double-double) ≈ 2^-63
96};
97
98constexpr const char* to_string(PrimitiveType e) {
99 if (e < PrimitiveType::Void || e >= PrimitiveType::PRIMITIVE_TYPE_COUNT) {
100 return "[unknown]";
101 } else {
102 return primitive_type_names[static_cast<int32_t>(e)];
103 }
104}
105
106// ----------------------------
107// Template to get C++ type from PrimitiveType
108// ----------------------------
109template<PrimitiveType T>
111
112// Floating-point types
113template<>
115 using type = float;
116};
117template<>
119 using type = double;
120};
121template<>
123 using type = long double;
124};
125
126// Integral types
127template<>
129 using type = int8_t;
130};
131template<>
133 using type = int16_t;
134};
135template<>
137 using type = int32_t;
138};
139template<>
141 using type = int64_t;
142};
143template<>
145 using type = uint8_t;
146};
147template<>
149 using type = uint16_t;
150};
151template<>
153 using type = uint32_t;
154};
155template<>
157 using type = uint64_t;
158};
159
160// ----------------------------
161// Concept for types with numeric_limits
162// ----------------------------
163template<typename T>
164concept HasNumericLimits = requires { std::numeric_limits<T>::is_specialized; } &&
165 std::numeric_limits<T>::is_specialized;
166
167// Machine epsilon for any type with numeric_limits
168template<HasNumericLimits T>
169constexpr T machine_epsilon() {
170 return std::numeric_limits<T>::epsilon();
171}
172
173// Fallback for unsupported types triggers compile-time error
174template<typename T>
175constexpr T machine_epsilon() {
176 throw std::runtime_error("machine_epsilon not defined for this type");
177 return T{};
178}
179
181 switch (pt) {
182 // Single source of truth: the machine-epsilon table (there is no portable
183 // numeric_limits specialization for the 16-bit floats).
191 return static_cast<long double>(primitive_type_machine_epsilons[static_cast<int32_t>(pt)]);
192 default:
193 throw std::runtime_error("No machine epsilon defined for this type");
194 }
195}
196
197} // namespace arg_capture
Definition primitive_types.h:164
Definition arg_capture_io.h:15
static const double primitive_type_machine_epsilons[]
Definition primitive_types.h:76
constexpr T machine_epsilon()
Definition primitive_types.h:169
static const char * primitive_type_cppspellings[]
Definition primitive_types.h:54
PrimitiveType
Definition primitive_types.h:9
@ Float
Definition primitive_types.h:24
@ Int128
Definition primitive_types.h:16
@ Int16
Definition primitive_types.h:13
@ BFloat
Definition primitive_types.h:23
@ UInt128
Definition primitive_types.h:21
@ PRIMITIVE_TYPE_COUNT
Definition primitive_types.h:29
@ Int8
Definition primitive_types.h:12
@ Void
Definition primitive_types.h:10
@ UInt16
Definition primitive_types.h:18
@ PPC_FP128
Definition primitive_types.h:28
@ X86_FP80
Definition primitive_types.h:26
@ UInt64
Definition primitive_types.h:20
@ FP128
Definition primitive_types.h:27
@ UInt8
Definition primitive_types.h:17
@ Int32
Definition primitive_types.h:14
@ Bool
Definition primitive_types.h:11
@ Half
Definition primitive_types.h:22
@ Double
Definition primitive_types.h:25
@ UInt32
Definition primitive_types.h:19
@ Int64
Definition primitive_types.h:15
long double primitive_type_epsilon(arg_capture::PrimitiveType pt)
Definition primitive_types.h:180
constexpr const char * to_string(PrimitiveType e)
Definition primitive_types.h:98
static const char * primitive_type_names[]
Definition primitive_types.h:32
double type
Definition primitive_types.h:119
float type
Definition primitive_types.h:115
int16_t type
Definition primitive_types.h:133
int32_t type
Definition primitive_types.h:137
int64_t type
Definition primitive_types.h:141
int8_t type
Definition primitive_types.h:129
uint16_t type
Definition primitive_types.h:149
uint32_t type
Definition primitive_types.h:153
uint64_t type
Definition primitive_types.h:157
uint8_t type
Definition primitive_types.h:145
long double type
Definition primitive_types.h:123
Definition primitive_types.h:110