scatter-gather integer: sign-extend signed types
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 2 Nov 2022 13:33:31 +0000 (09:33 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 2 Nov 2022 13:33:31 +0000 (09:33 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
src/test.c
src/tracer.c

index 0d9adbfce49bc578d5f446ca78ef9e41d29fa763..45c38213fdb1462fd8bc8f729bb5dd4e471dbc63 100644 (file)
@@ -1490,6 +1490,12 @@ struct test {
        uint8_t c;
        int32_t d;
        uint16_t e;
+       int8_t f;
+       int16_t g;
+       int32_t h;
+       int64_t i;
+       int64_t j;
+       int64_t k;
 };
 
 static side_define_struct_sg(mystructsgdef,
@@ -1503,6 +1509,24 @@ static side_define_struct_sg(mystructsgdef,
                side_field_sg_unsigned_integer("e", offsetof(struct test, e),
                        side_struct_field_sizeof_bit(struct test, e), 8, 4,
                        side_attr_list(side_attr("std.integer.base", side_attr_u8(16)))),
+               side_field_sg_signed_integer("f", offsetof(struct test, f),
+                       side_struct_field_sizeof_bit(struct test, f), 1, 4,
+                       side_attr_list(side_attr("std.integer.base", side_attr_u8(10)))),
+               side_field_sg_signed_integer("g", offsetof(struct test, g),
+                       side_struct_field_sizeof_bit(struct test, g), 11, 4,
+                       side_attr_list(side_attr("std.integer.base", side_attr_u8(10)))),
+               side_field_sg_signed_integer("h", offsetof(struct test, h),
+                       side_struct_field_sizeof_bit(struct test, h), 1, 31,
+                       side_attr_list(side_attr("std.integer.base", side_attr_u8(10)))),
+               side_field_sg_signed_integer("i", offsetof(struct test, i),
+                       side_struct_field_sizeof_bit(struct test, i), 33, 20,
+                       side_attr_list(side_attr("std.integer.base", side_attr_u8(10)))),
+               side_field_sg_signed_integer("j", offsetof(struct test, j),
+                       side_struct_field_sizeof_bit(struct test, j), 63, 1,
+                       side_attr_list(side_attr("std.integer.base", side_attr_u8(10)))),
+               side_field_sg_signed_integer("k", offsetof(struct test, k),
+                       side_struct_field_sizeof_bit(struct test, k), 1, 63,
+                       side_attr_list(side_attr("std.integer.base", side_attr_u8(10)))),
        ),
        side_attr_list()
 );
@@ -1524,6 +1548,12 @@ void test_struct_sg(void)
                        .c = 2,
                        .d = -55,
                        .e = 0xABCD,
+                       .f = -1,
+                       .g = -1,
+                       .h = -1,
+                       .i = -1,
+                       .j = -1,
+                       .k = -1,
                };
                side_event_call(my_provider_event_structsg, side_arg_list(side_arg_struct_sg(&mystruct)));
        }
index afa11697896761c9d15d9b41b2bde42fec641b71..0709ebba0a267a28f94e36c4c8f8a4649f05177f 100644 (file)
@@ -1114,6 +1114,8 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                {
                        uint8_t v;
 
+                       if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 8)
+                               abort();
                        memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
                        v >>= sg_type->u.side_basic.offset_bits;
                        if (sg_type->u.side_basic.len_bits < 8)
@@ -1140,6 +1142,8 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                {
                        uint16_t v;
 
+                       if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 16)
+                               abort();
                        memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
                        if (sg_type_to_host_reverse_bo(sg_type))
                                v = side_bswap_16(v);
@@ -1168,6 +1172,8 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                {
                        uint32_t v;
 
+                       if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 32)
+                               abort();
                        memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
                        if (sg_type_to_host_reverse_bo(sg_type))
                                v = side_bswap_32(v);
@@ -1196,6 +1202,8 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                {
                        uint64_t v;
 
+                       if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 64)
+                               abort();
                        memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
                        if (sg_type_to_host_reverse_bo(sg_type))
                                v = side_bswap_64(v);
@@ -1234,6 +1242,8 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                {
                        int8_t v;
 
+                       if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 8)
+                               abort();
                        memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
                        v >>= sg_type->u.side_basic.offset_bits;
                        if (sg_type->u.side_basic.len_bits < 8)
@@ -1246,6 +1256,11 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                                printf("0%" PRIo8, v);
                                break;
                        case TRACER_DISPLAY_BASE_10:
+                               /* Sign-extend. */
+                               if (sg_type->u.side_basic.len_bits < 8) {
+                                       if (v & (1U << (sg_type->u.side_basic.len_bits - 1)))
+                                               v |= ~((1U << sg_type->u.side_basic.len_bits) - 1);
+                               }
                                printf("%" PRId8, v);
                                break;
                        case TRACER_DISPLAY_BASE_16:
@@ -1260,6 +1275,8 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                {
                        int16_t v;
 
+                       if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 16)
+                               abort();
                        memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
                        if (sg_type_to_host_reverse_bo(sg_type))
                                v = side_bswap_16(v);
@@ -1274,6 +1291,11 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                                printf("0%" PRIo16, v);
                                break;
                        case TRACER_DISPLAY_BASE_10:
+                               /* Sign-extend. */
+                               if (sg_type->u.side_basic.len_bits < 16) {
+                                       if (v & (1U << (sg_type->u.side_basic.len_bits - 1)))
+                                               v |= ~((1U << sg_type->u.side_basic.len_bits) - 1);
+                               }
                                printf("%" PRId16, v);
                                break;
                        case TRACER_DISPLAY_BASE_16:
@@ -1288,6 +1310,8 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                {
                        uint32_t v;
 
+                       if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 32)
+                               abort();
                        memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
                        if (sg_type_to_host_reverse_bo(sg_type))
                                v = side_bswap_32(v);
@@ -1302,6 +1326,11 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                                printf("0%" PRIo32, v);
                                break;
                        case TRACER_DISPLAY_BASE_10:
+                               /* Sign-extend. */
+                               if (sg_type->u.side_basic.len_bits < 32) {
+                                       if (v & (1U << (sg_type->u.side_basic.len_bits - 1)))
+                                               v |= ~((1U << sg_type->u.side_basic.len_bits) - 1);
+                               }
                                printf("%" PRId32, v);
                                break;
                        case TRACER_DISPLAY_BASE_16:
@@ -1316,6 +1345,8 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                {
                        uint64_t v;
 
+                       if (!sg_type->u.side_basic.len_bits || sg_type->u.side_basic.len_bits + sg_type->u.side_basic.offset_bits > 64)
+                               abort();
                        memcpy(&v, ptr + sg_type->u.side_basic.integer_offset, sizeof(v));
                        if (sg_type_to_host_reverse_bo(sg_type))
                                v = side_bswap_64(v);
@@ -1330,6 +1361,11 @@ void tracer_print_sg_type(const struct side_type_sg_description *sg_type, void *
                                printf("0%" PRIo64, v);
                                break;
                        case TRACER_DISPLAY_BASE_10:
+                               /* Sign-extend. */
+                               if (sg_type->u.side_basic.len_bits < 64) {
+                                       if (v & (1ULL << (sg_type->u.side_basic.len_bits - 1)))
+                                               v |= ~((1ULL << sg_type->u.side_basic.len_bits) - 1);
+                               }
                                printf("%" PRId64, v);
                                break;
                        case TRACER_DISPLAY_BASE_16:
This page took 0.02786 seconds and 4 git commands to generate.