extmod/moductypes: Fix size and offset calculation for ARRAY of FLOAT32.

uctypes.FLOAT32 has a special value representation and
uctypes_struct_scalar_size() should be used instead of GET_SCALAR_SIZE().

Signed-off-by: Damien George <damien@micropython.org>
This commit is contained in:
Damien George 2021-05-06 13:11:33 +10:00
parent 350a66a863
commit 47583d8cbd
5 changed files with 24 additions and 2 deletions

View File

@ -164,7 +164,7 @@ STATIC mp_uint_t uctypes_struct_agg_size(mp_obj_tuple_t *t, int layout_type, mp_
mp_uint_t item_s; mp_uint_t item_s;
if (t->len == 2) { if (t->len == 2) {
// Elements of array are scalar // Elements of array are scalar
item_s = GET_SCALAR_SIZE(val_type); item_s = uctypes_struct_scalar_size(val_type);
if (item_s > *max_field_size) { if (item_s > *max_field_size) {
*max_field_size = item_s; *max_field_size = item_s;
} }
@ -541,7 +541,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
return value; // just !MP_OBJ_NULL return value; // just !MP_OBJ_NULL
} }
} else { } else {
byte *p = self->addr + GET_SCALAR_SIZE(val_type) * index; byte *p = self->addr + uctypes_struct_scalar_size(val_type) * index;
if (value == MP_OBJ_SENTINEL) { if (value == MP_OBJ_SENTINEL) {
return get_unaligned(val_type, p, self->flags); return get_unaligned(val_type, p, self->flags);
} else { } else {

View File

@ -22,3 +22,19 @@ print("%.4f" % S.f64)
S.uf64 = 12.34 S.uf64 = 12.34
print("%.4f" % S.uf64) print("%.4f" % S.uf64)
# array of float/double
desc = {
"af32": (uctypes.ARRAY | 0, uctypes.FLOAT32 | 2),
"af64": (uctypes.ARRAY | 0, uctypes.FLOAT64 | 2),
}
data = bytearray(16)
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
S.af32[0] = 1
S.af32[1] = 2
print("%.4f %.4f" % (S.af32[0], S.af32[1]), data)
S.af64[0] = 1
S.af64[1] = 2
print("%.4f %.4f" % (S.af64[0], S.af64[1]), data)

View File

@ -1,3 +1,5 @@
12.3400 12.3400
12.3400 12.3400
12.3400 12.3400
1.0000 2.0000 bytearray(b'\x00\x00\x80?\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00')
1.0000 2.0000 bytearray(b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@')

View File

@ -6,3 +6,5 @@ except ImportError:
print(uctypes.sizeof({"f": uctypes.FLOAT32})) print(uctypes.sizeof({"f": uctypes.FLOAT32}))
print(uctypes.sizeof({"f": uctypes.FLOAT64})) print(uctypes.sizeof({"f": uctypes.FLOAT64}))
print(uctypes.sizeof({"f": (uctypes.ARRAY | 0, uctypes.FLOAT32 | 2)}))
print(uctypes.sizeof({"f": (uctypes.ARRAY | 0, uctypes.FLOAT64 | 2)}))

View File

@ -1,2 +1,4 @@
4 4
8 8
8
16