From fb25b35562ccbe18aa1a8cc12ca73815f1263ffa Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Thu, 20 Oct 2022 11:50:42 -0400 Subject: [PATCH] Implement floating point type support Signed-off-by: Mathieu Desnoyers --- include/side/trace.h | 79 ++++++++++++++++++++++++++++++++++++++++++++ src/test.c | 79 ++++++++++++++++++++++++++++++++++++++++++++ src/tracer.c | 64 +++++++++++++++++++++++++++++++++++ 3 files changed, 222 insertions(+) diff --git a/include/side/trace.h b/include/side/trace.h index ed039fa..ca1d8c9 100644 --- a/include/side/trace.h +++ b/include/side/trace.h @@ -10,6 +10,7 @@ #include #include #include +#include #include /* SIDE stands for "Static Instrumentation Dynamically Enabled" */ @@ -36,6 +37,11 @@ enum side_type { SIDE_TYPE_S32, SIDE_TYPE_S64, + SIDE_TYPE_FLOAT_BINARY16, + SIDE_TYPE_FLOAT_BINARY32, + SIDE_TYPE_FLOAT_BINARY64, + SIDE_TYPE_FLOAT_BINARY128, + SIDE_TYPE_STRING, SIDE_TYPE_STRUCT, @@ -78,6 +84,11 @@ enum side_dynamic_type { SIDE_DYNAMIC_TYPE_S32, SIDE_DYNAMIC_TYPE_S64, + SIDE_DYNAMIC_TYPE_FLOAT_BINARY16, + SIDE_DYNAMIC_TYPE_FLOAT_BINARY32, + SIDE_DYNAMIC_TYPE_FLOAT_BINARY64, + SIDE_DYNAMIC_TYPE_FLOAT_BINARY128, + SIDE_DYNAMIC_TYPE_STRING, SIDE_DYNAMIC_TYPE_STRUCT, @@ -186,6 +197,19 @@ struct side_arg_dynamic_vec { int32_t side_s32; int64_t side_s64; +#if __HAVE_FLOAT16 + _Float16 side_float_binary16; +#endif +#if __HAVE_FLOAT32 + _Float32 side_float_binary32; +#endif +#if __HAVE_FLOAT64 + _Float64 side_float_binary64; +#endif +#if __HAVE_FLOAT128 + _Float128 side_float_binary128; +#endif + const char *string; const struct side_arg_dynamic_event_struct *side_dynamic_struct; @@ -226,6 +250,19 @@ struct side_arg_vec { int32_t side_s32; int64_t side_s64; +#if __HAVE_FLOAT16 + _Float16 side_float_binary16; +#endif +#if __HAVE_FLOAT32 + _Float32 side_float_binary32; +#endif +#if __HAVE_FLOAT64 + _Float64 side_float_binary64; +#endif +#if __HAVE_FLOAT128 + _Float128 side_float_binary128; +#endif + const char *string; const struct side_arg_vec_description *side_struct; const struct side_arg_vec_description *side_array; @@ -380,6 +417,11 @@ struct side_tracer_dynamic_vla_visitor_ctx { #define side_arg_s16(val) { .type = SIDE_TYPE_S16, .u = { .side_s16 = (val) } } #define side_arg_s32(val) { .type = SIDE_TYPE_S32, .u = { .side_s32 = (val) } } #define side_arg_s64(val) { .type = SIDE_TYPE_S64, .u = { .side_s64 = (val) } } +#define side_arg_float_binary16(val) { .type = SIDE_TYPE_FLOAT_BINARY16, .u = { .side_float_binary16 = (val) } } +#define side_arg_float_binary32(val) { .type = SIDE_TYPE_FLOAT_BINARY32, .u = { .side_float_binary32 = (val) } } +#define side_arg_float_binary64(val) { .type = SIDE_TYPE_FLOAT_BINARY64, .u = { .side_float_binary64 = (val) } } +#define side_arg_float_binary128(val) { .type = SIDE_TYPE_FLOAT_BINARY128, .u = { .side_float_binary128 = (val) } } + #define side_arg_string(val) { .type = SIDE_TYPE_STRING, .u = { .string = (val) } } #define side_arg_struct(_side_type) { .type = SIDE_TYPE_STRUCT, .u = { .side_struct = (_side_type) } } #define side_arg_array(_side_type) { .type = SIDE_TYPE_ARRAY, .u = { .side_array = (_side_type) } } @@ -503,6 +545,43 @@ struct side_tracer_dynamic_vla_visitor_ctx { }, \ } +#define side_arg_dynamic_float_binary16(_val, _attr) \ + { \ + .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY16, \ + .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \ + .attr = _attr, \ + .u = { \ + .side_float_binary16 = (_val), \ + }, \ + } +#define side_arg_dynamic_float_binary32(_val, _attr) \ + { \ + .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY32, \ + .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \ + .attr = _attr, \ + .u = { \ + .side_float_binary32 = (_val), \ + }, \ + } +#define side_arg_dynamic_float_binary64(_val, _attr) \ + { \ + .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY64, \ + .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \ + .attr = _attr, \ + .u = { \ + .side_float_binary64 = (_val), \ + }, \ + } +#define side_arg_dynamic_float_binary128(_val, _attr) \ + { \ + .dynamic_type = SIDE_DYNAMIC_TYPE_FLOAT_BINARY128, \ + .nr_attr = SIDE_ARRAY_SIZE(SIDE_PARAM(_attr)), \ + .attr = _attr, \ + .u = { \ + .side_float_binary128 = (_val), \ + }, \ + } + #define side_arg_dynamic_string(_val, _attr) \ { \ .dynamic_type = SIDE_DYNAMIC_TYPE_STRING, \ diff --git a/src/test.c b/src/test.c index 4ff2c8d..97cefac 100644 --- a/src/test.c +++ b/src/test.c @@ -854,6 +854,83 @@ void test_variadic_struct_attr(void) } } +static side_define_event(my_provider_event_float, "myprovider", "myeventfloat", SIDE_LOGLEVEL_DEBUG, + side_field_list( +#if __HAVE_FLOAT16 + side_field("binary16", SIDE_TYPE_FLOAT_BINARY16, side_attr_list()), +#endif +#if __HAVE_FLOAT32 + side_field("binary32", SIDE_TYPE_FLOAT_BINARY32, side_attr_list()), +#endif +#if __HAVE_FLOAT64 + side_field("binary64", SIDE_TYPE_FLOAT_BINARY64, side_attr_list()), +#endif +#if __HAVE_FLOAT128 + side_field("binary128", SIDE_TYPE_FLOAT_BINARY128, side_attr_list()), +#endif + ), + side_attr_list() +); + +static +void test_float(void) +{ + my_provider_event_float.enabled = 1; + side_event(&my_provider_event_float, + side_arg_list( +#if __HAVE_FLOAT16 + side_arg_float_binary16(1.1), +#endif +#if __HAVE_FLOAT32 + side_arg_float_binary32(2.2), +#endif +#if __HAVE_FLOAT64 + side_arg_float_binary64(3.3), +#endif +#if __HAVE_FLOAT128 + side_arg_float_binary128(4.4), +#endif + ) + ); +} + +static side_define_event_variadic(my_provider_event_variadic_float, + "myprovider", "myvariadicfloat", SIDE_LOGLEVEL_DEBUG, + side_field_list(), + side_attr_list() +); + +static +void test_variadic_float(void) +{ + my_provider_event_variadic_float.enabled = 1; + side_event_variadic(&my_provider_event_variadic_float, + side_arg_list(), + side_arg_list( +#if __HAVE_FLOAT16 + side_arg_dynamic_field("binary16", + side_arg_dynamic_float_binary16(1.1, side_attr_list()) + ), +#endif +#if __HAVE_FLOAT32 + side_arg_dynamic_field("binary32", + side_arg_dynamic_float_binary32(2.2, side_attr_list()) + ), +#endif +#if __HAVE_FLOAT64 + side_arg_dynamic_field("binary64", + side_arg_dynamic_float_binary64(3.3, side_attr_list()) + ), +#endif +#if __HAVE_FLOAT128 + side_arg_dynamic_field("binary128", + side_arg_dynamic_float_binary128(4.4, side_attr_list()) + ), +#endif + ) + ); +} + int main() { test_fields(); @@ -883,5 +960,7 @@ int main() test_variadic_attr(); test_variadic_vla_attr(); test_variadic_struct_attr(); + test_float(); + test_variadic_float(); return 0; } diff --git a/src/tracer.c b/src/tracer.c index fd8ed5e..dcdf26f 100644 --- a/src/tracer.c +++ b/src/tracer.c @@ -111,6 +111,38 @@ void tracer_print_type(const struct side_type_description *type_desc, const stru case SIDE_TYPE_S64: printf("%" PRId64, item->u.side_s64); break; + case SIDE_TYPE_FLOAT_BINARY16: +#if __HAVE_FLOAT16 + printf("%g", (double) item->u.side_float_binary16); + break; +#else + printf("ERROR: Unsupported binary16 float type\n"); + abort(); +#endif + case SIDE_TYPE_FLOAT_BINARY32: +#if __HAVE_FLOAT32 + printf("%g", (double) item->u.side_float_binary32); + break; +#else + printf("ERROR: Unsupported binary32 float type\n"); + abort(); +#endif + case SIDE_TYPE_FLOAT_BINARY64: +#if __HAVE_FLOAT64 + printf("%g", (double) item->u.side_float_binary64); + break; +#else + printf("ERROR: Unsupported binary64 float type\n"); + abort(); +#endif + case SIDE_TYPE_FLOAT_BINARY128: +#if __HAVE_FLOAT128 + printf("%Lg", (long double) item->u.side_float_binary128); + break; +#else + printf("ERROR: Unsupported binary128 float type\n"); + abort(); +#endif case SIDE_TYPE_STRING: printf("\"%s\"", item->u.string); break; @@ -597,6 +629,38 @@ void tracer_print_dynamic(const struct side_arg_dynamic_vec *item) case SIDE_DYNAMIC_TYPE_S64: printf("%" PRId64, item->u.side_s64); break; + case SIDE_DYNAMIC_TYPE_FLOAT_BINARY16: +#if __HAVE_FLOAT16 + printf("%g", (double) item->u.side_float_binary16); + break; +#else + printf("ERROR: Unsupported binary16 float type\n"); + abort(); +#endif + case SIDE_DYNAMIC_TYPE_FLOAT_BINARY32: +#if __HAVE_FLOAT32 + printf("%g", (double) item->u.side_float_binary32); + break; +#else + printf("ERROR: Unsupported binary32 float type\n"); + abort(); +#endif + case SIDE_DYNAMIC_TYPE_FLOAT_BINARY64: +#if __HAVE_FLOAT64 + printf("%g", (double) item->u.side_float_binary64); + break; +#else + printf("ERROR: Unsupported binary64 float type\n"); + abort(); +#endif + case SIDE_DYNAMIC_TYPE_FLOAT_BINARY128: +#if __HAVE_FLOAT128 + printf("%Lg", (long double) item->u.side_float_binary128); + break; +#else + printf("ERROR: Unsupported binary128 float type\n"); + abort(); +#endif case SIDE_DYNAMIC_TYPE_STRING: printf("\"%s\"", item->u.string); break; -- 2.34.1