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:
parent
350a66a863
commit
47583d8cbd
@ -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;
|
||||
if (t->len == 2) {
|
||||
// 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) {
|
||||
*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
|
||||
}
|
||||
} 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) {
|
||||
return get_unaligned(val_type, p, self->flags);
|
||||
} else {
|
||||
|
@ -22,3 +22,19 @@ print("%.4f" % S.f64)
|
||||
|
||||
S.uf64 = 12.34
|
||||
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)
|
||||
|
@ -1,3 +1,5 @@
|
||||
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@')
|
||||
|
@ -6,3 +6,5 @@ except ImportError:
|
||||
|
||||
print(uctypes.sizeof({"f": uctypes.FLOAT32}))
|
||||
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)}))
|
||||
|
@ -1,2 +1,4 @@
|
||||
4
|
||||
8
|
||||
8
|
||||
16
|
||||
|
Loading…
Reference in New Issue
Block a user