libadc-cxx 1.0.0
Structured logging for scientific computing
Loading...
Searching...
No Matches
enums.ipp
Go to the documentation of this file.
1#ifndef adc_builder_impl_enums_ipp_hpp
2#define adc_builder_impl_enums_ipp_hpp
3#include <mutex>
4#include <charconv>
5namespace adc {
6namespace impl {
7
8// This array must line up with the enum scalar_type in types.h
9// If it does not, test_enum_strings() will return non-zero.
10std::map< adc::scalar_type, std::string > scalar_type_name = {
11 { cp_none, "none" },
12 { cp_bool, "bool" },
13 { cp_char, "char" },
14 { cp_char16, "char16" },
15 { cp_char32, "char32" },
16 { cp_cstr, "cstr" },
17 { cp_json_str, "json_str" },
18 { cp_yaml_str, "yaml_str" },
19 { cp_xml_str, "xml_str" },
20 { cp_json, "json" },
21 { cp_path, "path" },
22 { cp_number_str, "number_str" },
23 { cp_uint8, "uint8" },
24 { cp_uint16, "uint16" },
25 { cp_uint32, "uint32" },
26 { cp_uint64, "uint64" },
27 { cp_int8, "int8" },
28 { cp_int16, "int16" },
29 { cp_int32, "int32" },
30 { cp_int64, "int64" },
31 { cp_f32, "f32" },
32 { cp_f64, "f64" },
33 { cp_f80, "f80" },
34 { cp_f128, "f128" },
35 { cp_f8_e4m3, "f8_e4m3" },
36 { cp_f8_e5m2, "f8_e5m2" },
37 { cp_f16_e5m10, "f16_e5m10" },
38 { cp_f16_e8m7, "f16_e8m7" },
39 { cp_c_f32, "c_f32" },
40 { cp_c_f64, "c_f64" },
41 { cp_c_f80, "c_f80" },
42 { cp_c_f128, "c_f128" },
43 { cp_timespec, "timespec" },
44 { cp_timeval, "timeval" },
45 { cp_epoch, "epoch" },
46 { cp_last, "last" },
47};
48
49std::map< adc::scalar_type, boost::json::kind > scalar_type_representation = { // prec fixme: adc::impl::scalar_type_representation
50 { cp_none, boost::json::kind::null },
51 { cp_bool, boost::json::kind::bool_ },
52 { cp_char, boost::json::kind::int64 },
53 { cp_char16, boost::json::kind::uint64 },
54 { cp_char32, boost::json::kind::uint64 },
55 { cp_cstr, boost::json::kind::string },
56 { cp_json_str, boost::json::kind::string },
57 { cp_yaml_str, boost::json::kind::string },
58 { cp_xml_str, boost::json::kind::string },
59 { cp_json, boost::json::kind::string },
60 { cp_path, boost::json::kind::string },
61 { cp_number_str, boost::json::kind::string },
62 { cp_uint8, boost::json::kind::uint64 },
63 { cp_uint16, boost::json::kind::uint64 },
64 { cp_uint32, boost::json::kind::uint64 },
65 { cp_uint64, boost::json::kind::string },
66 { cp_int8, boost::json::kind::int64 },
67 { cp_int16, boost::json::kind::int64 },
68 { cp_int32, boost::json::kind::int64 },
69 { cp_int64, boost::json::kind::int64 },
70 { cp_f32, boost::json::kind::double_ },
71 { cp_f64, boost::json::kind::double_ },
72 { cp_f80, boost::json::kind::null /* "f80" */ }, // prec fixme: rstring? json
73 { cp_f128, boost::json::kind::null /* "f128" */ }, // prec fixme: rstring? json
74 { cp_f8_e4m3, boost::json::kind::null /* "f8_e4m3" */ }, // prec fixme: rstring? json
75 { cp_f8_e5m2, boost::json::kind::null /* "f8_e5m2" */ }, // prec fixme: rstring? json
76 { cp_f16_e5m10, boost::json::kind::null /* "f16_e5m10" */ }, // prec fixme: rstring? json
77 { cp_f16_e8m7, boost::json::kind::null /* "f16_e8m7" */ }, // prec fixme: rstring? json
78 { cp_c_f32, boost::json::kind::double_ },
79 { cp_c_f64, boost::json::kind::double_ },
80 { cp_c_f80, boost::json::kind::null /* "c_f80" */ }, // prec fixme: rstring? json
81 { cp_c_f128, boost::json::kind::null /* "c_f128" */ }, // prec fixme: rstring? json
82 { cp_timespec, boost::json::kind::int64 }, // int64[2]
83 { cp_timeval, boost::json::kind::int64 }, // int64[2]
84 { cp_epoch, boost::json::kind::int64 },
85 { cp_last, boost::json::kind::null }
86};
87
88} // namespace adc::impl
89
90
92 if (st >= cp_none && st <= cp_last)
94 return boost::json::kind::null;
95};
96
97/* return the conversion of name to scalar type,
98 * after stripping prefix "array_" if present.
99 */
100scalar_type scalar_type_from_name(const std::string& name)
101{
102
103
104 std::string basename, prefix = "array_";
105 if (name.length() >= prefix.length() &&
106 name.substr(0, prefix.length()) == prefix) {
107 basename = name.substr(prefix.length());
108 } else {
109 basename = name;
110 }
111
112 static std::map<std::string, adc::scalar_type> type_to_name;
113 static std::mutex mtx;
114 {
115 std::lock_guard<std::mutex> guard(mtx);
116 if (type_to_name.size() == 0) {
117 for (const auto& pair : adc::impl::scalar_type_name) {
118 type_to_name[pair.second] = pair.first;
119 }
120 }
121 }
122 if (type_to_name.count(basename)) {
123 return type_to_name[basename];
124 }
125 return cp_none;
126}
127
128const std::string to_string(float f)
129{
130 const size_t size = 20;
131 char buf[size]{};
132 std::to_chars_result r = std::to_chars(buf, buf + size, f);
133
134 if (r.ec != std::errc()) {
135 std::cout << std::make_error_code(r.ec).message() << '\n';
136 return std::string("NaN_unexpected");
137 } else {
138 std::string str(buf, r.ptr - buf);
139 return str;
140 }
141}
142
143const std::string to_string(double f)
144{
145 const size_t size = 40;
146 char buf[size]{};
147 std::to_chars_result r = std::to_chars(buf, buf + size, f);
148
149 if (r.ec != std::errc()) {
150 std::cout << std::make_error_code(r.ec).message() << '\n';
151 return std::string("NaN_unexpected");
152 } else {
153 std::string str(buf, r.ptr - buf);
154 return str;
155 }
156}
157
158#define elt_to_string(CTYPE) \
159{ \
160 CTYPE *a = (CTYPE *)data; \
161 for (size_t i=0; i < count; i++) { \
162 o << a[i]; \
163 if (i < count-1) \
164 o << ", "; \
165 } \
166}
167
168#define f_elt_to_string(CTYPE) \
169{ \
170 CTYPE *a = (CTYPE *)data; \
171 for (size_t i=0; i < count; i++) { \
172 o << to_string(a[i]); \
173 if (i < count-1) \
174 o << ", "; \
175 } \
176}
177
178#define cf_elt_to_string(CTYPE) \
179{ \
180 std::complex<CTYPE> *a = (std::complex<CTYPE> *)data; \
181 for (size_t i=0; i < count; i++) { \
182 o << "{" << to_string(a[i].real()); \
183 o << ", " << to_string(a[i].imag()); \
184 o << " }"; \
185 if (i < count-1) \
186 o << ", "; \
187 } \
188}
189
190const std::string to_string(void *data, scalar_type cptype, size_t count) // prec fixme
191{
192 // [elts*count]
193 std::string s;
194 s.reserve(count*30);
195 std::ostringstream o(s);
196 o << "[";
197
198 switch (cptype) {
199 case cp_last:
200 [[fallthrough]];
201 case cp_none:
202 o << "bug1";
203 break;
204 case cp_bool:
205 {
206 bool *a = (bool *)data;
207 for (size_t i; i < count; i++) {
208 o << (a[i] ? "true" : "false");
209 if (i < count-1)
210 o << ", ";
211 }
212 }
213 break;
214 case cp_char:
215 elt_to_string(char);
216 break;
217 case cp_char16:
218 elt_to_string(char16_t);
219 break;
220 case cp_char32:
221 elt_to_string(char32_t);
222 break;
223 case cp_cstr:
224 [[fallthrough]];
225 case cp_json_str:
226 [[fallthrough]];
227 case cp_yaml_str:
228 [[fallthrough]];
229 case cp_xml_str:
230 [[fallthrough]];
231 case cp_json:
232 [[fallthrough]];
233 case cp_path:
234 [[fallthrough]];
235 case cp_number_str:
236 elt_to_string(std::string);
237 break;
238 case cp_uint8:
239 elt_to_string(uint8_t);
240 break;
241 case cp_uint16:
242 elt_to_string(uint16_t);
243 break;
244 case cp_uint32:
245 elt_to_string(uint32_t);
246 break;
247 case cp_uint64:
248 elt_to_string(uint64_t);
249 break;
250 case cp_int8:
251 elt_to_string(int8_t);
252 break;
253 case cp_int16:
254 elt_to_string(int16_t);
255 break;
256 case cp_int32:
257 elt_to_string(int32_t);
258 break;
259 case cp_int64:
260 elt_to_string(int64_t);
261 break;
262 case cp_f32:
263 f_elt_to_string(float);
264 break;
265 case cp_f64:
266 f_elt_to_string(double);
267 break;
268#if ADC_SUPPORT_EXTENDED_FLOATS
269 case cp_f80:
270 f_elt_to_string(FLOAT80);
271 break;
272#else
273 case cp_f80:
274 o << "unsupported-by-build-" << to_string(cptype);
275 break;
276#endif
277#if ADC_SUPPORT_QUAD_FLOATS
278 case cp_f128:
279 f_elt_to_string(FLOAT128);
280 break;
281#else
282 case cp_f128:
283 o << "unsupported-by-build-" << to_string(cptype);
284 break;
285#endif
286#if ADC_SUPPORT_GPU_FLOATS
287 case cp_f8_e4m3:
288 f_elt_to_string(FLOAT_4_3);
289 break;
290 case cp_f8_e5m2:
291 f_elt_to_string(FLOAT_5_2);
292 break;
293 case cp_f16_e5m10:
294 f_elt_to_string(FLOAT_5_10);
295 break;
296 case cp_f16_e8m7:
297 f_elt_to_string(FLOAT_8_7);
298 break;
299#else
300 case cp_f8_e4m3:
301 o << "unsupported-by-build-" << to_string(cptype);
302 break;
303 case cp_f8_e5m2:
304 o << "unsupported-by-build-" << to_string(cptype);
305 break;
306 case cp_f16_e5m10:
307 o << "unsupported-by-build-" << to_string(cptype);
308 break;
309 case cp_f16_e8m7:
310 o << "unsupported-by-build-" << to_string(cptype);
311 break;
312#endif
313 case cp_c_f32:
314 cf_elt_to_string(float);
315 break;
316 case cp_c_f64:
317 cf_elt_to_string(double);
318 break;
319#if ADC_SUPPORT_EXTENDED_FLOATS
320 case cp_c_f80:
321 cf_elt_to_string(std::complex<FLOAT80>);
322 break;
323#else
324 case cp_c_f80:
325 o << "unsupported-by-build-" << to_string(cptype);
326 break;
327#endif
328#if ADC_SUPPORT_QUAD_FLOATS
329 case cp_c_f128:
330 cf_elt_to_string(std::complex<FLOAT128>);
331 break;
332#else
333 case cp_c_f128:
334 o << "unsupported-by-build-" << to_string(cptype);
335 break;
336#endif
337 case cp_timespec:
338 [[fallthrough]];
339 case cp_timeval:
340 [[fallthrough]];
341 case cp_epoch: // fixme? array of epochs
342 o << "bug2";
343 break;
344 }
345 o << "]";
346 return o.str();
347}
348
349
350#define BAD_ST "UNKNOWN_scalar_type"
351
352const std::string to_string(scalar_type st) {
353 if (st >= cp_none && st <= cp_last)
355 return BAD_ST;
356};
357
359 int err = 0;
360#define check_enum(X) if (to_string(cp_##X) != #X) { err++; std::cout << "enum " << #X << " is inconsistent with " << cp_##X << " from " << to_string(cp_##X) << std::endl; }
361 check_enum(none);
362 check_enum(bool);
363 check_enum(char);
364 check_enum(char16);
365 check_enum(char32);
366 check_enum(cstr);
367 check_enum(json_str);
368 check_enum(yaml_str);
369 check_enum(xml_str);
370 check_enum(number_str);
371 check_enum(json);
372 check_enum(path);
373 check_enum(uint8);
374 check_enum(uint16);
375 check_enum(uint32);
376 check_enum(uint64);
377 check_enum(int8);
378 check_enum(int16);
379 check_enum(int32);
380 check_enum(int64);
381 check_enum(f32);
382 check_enum(f64);
383 check_enum(f80);
384 check_enum(f128);
385 check_enum(f8_e4m3);
386 check_enum(f8_e5m2);
387 check_enum(f16_e5m10);
388 check_enum(f16_e8m7);
389 check_enum(c_f32);
390 check_enum(c_f64);
391 check_enum(c_f80);
392 check_enum(c_f128);
393 check_enum(timespec);
394 check_enum(timeval);
395 check_enum(epoch);
396 check_enum(last);
397 if (to_string((enum scalar_type)1000) != BAD_ST) {
398 err++;
399 std::cout << "out of range input to to_string() returned incorrect result" << std::endl;
400 }
401 return err;
402}
403
404} // namespace adc
405#endif // adc_builder_impl_enums_ipp_hpp
#define check_enum(X)
#define f_elt_to_string(CTYPE)
Definition enums.ipp:168
#define cf_elt_to_string(CTYPE)
Definition enums.ipp:178
#define elt_to_string(CTYPE)
Definition enums.ipp:158
#define BAD_ST
Definition enums.ipp:350
int test_enum_strings()
return non-zero if to_string and enum scalar_type are inconsisent.
Definition enums.ipp:358
scalar_type scalar_type_from_name(const std::string &name)
get the enum representation of a scalar_type string
Definition enums.ipp:100
const std::string to_string(float f)
get string of float using to_chars.
Definition enums.ipp:128
scalar_type
field types for scientific data encode/decode with json.
Definition types.hpp:58
@ cp_xml_str
c null-terminated string that contains valid xml
Definition types.hpp:68
@ cp_int64
int64_t
Definition types.hpp:81
@ cp_epoch
time(NULL) seconds since the epoch (UNIX) as int64_t
Definition types.hpp:100
@ cp_f16_e8m7
16 bit bfloat (7 mantissa, 8 exponent); requires ADC_SUPPORT_GPU_FLOATS support
Definition types.hpp:91
@ cp_int32
int32_t
Definition types.hpp:80
@ cp_c_f32
complex<float>
Definition types.hpp:93
@ cp_char
char (8 bit)
Definition types.hpp:61
@ cp_path
c null-terminated string which names a file-system path
Definition types.hpp:70
@ cp_char16
char16_t
Definition types.hpp:62
@ cp_json_str
c null-terminated string that contains valid json
Definition types.hpp:66
@ cp_timespec
(second, nanosecond) as int64_t, int64_t pair from clock_gettime
Definition types.hpp:98
@ cp_timeval
gettimeofday struct timeval (second, microsecond) as int64_t pair
Definition types.hpp:99
@ cp_c_f64
complex<double>
Definition types.hpp:94
@ cp_bool
bool (true/false,1/0)
Definition types.hpp:60
@ cp_int8
int8_t
Definition types.hpp:78
@ cp_f128
128 bit float; requires ADC_SUPPORT_QUAD_FLOATS support
Definition types.hpp:86
@ cp_uint16
uint16_t
Definition types.hpp:74
@ cp_json
json value (object, list, etc)
Definition types.hpp:69
@ cp_c_f80
complex<extended>; requires ADC_SUPPORT_EXTENDED_FLOATS support
Definition types.hpp:95
@ cp_number_str
c null-terminated string containing an exact decimal representation of arbitrary precision
Definition types.hpp:71
@ cp_uint32
uint32_t
Definition types.hpp:75
@ cp_int16
int16_t
Definition types.hpp:79
@ cp_f64
64 bit float
Definition types.hpp:84
@ cp_f80
80 bit float; requires ADC_SUPPORT_EXTENDED_FLOATS support
Definition types.hpp:85
@ cp_none
Definition types.hpp:59
@ cp_f32
32 bit float
Definition types.hpp:83
@ cp_yaml_str
c null-terminated string that contains valid yaml
Definition types.hpp:67
@ cp_f16_e5m10
16 bit float (10 mantissa, 5 exponent); requires ADC_SUPPORT_GPU_FLOATS support
Definition types.hpp:90
@ cp_c_f128
complex<quad>; requires ADC_SUPPORT_QUAD_FLOATS support
Definition types.hpp:96
@ cp_cstr
c null-terminated string
Definition types.hpp:65
@ cp_char32
char32_t
Definition types.hpp:63
@ cp_uint64
uint64_t
Definition types.hpp:76
@ cp_uint8
uint8_t
Definition types.hpp:73
@ cp_f8_e5m2
8 bit float (2 mantissa, 5 exponent); requires ADC_SUPPORT_GPU_FLOATS support
Definition types.hpp:89
@ cp_f8_e4m3
8 bit float (3 mantissa, 4 exponent); requires ADC_SUPPORT_GPU_FLOATS support
Definition types.hpp:88
@ cp_last
Definition types.hpp:102
std::map< adc::scalar_type, boost::json::kind > scalar_type_representation
Definition enums.ipp:49
std::map< adc::scalar_type, std::string > scalar_type_name
Definition enums.ipp:10
Definition adc.hpp:75
boost::json::kind scalar_type_representation(scalar_type st)
Definition enums.ipp:91