Skip to content

Commit

Permalink
extmod/moductypes: Fix size and offset calculation for ARRAY of FLOAT32.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
dpgeorge committed May 6, 2021
1 parent 350a66a commit 47583d8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions extmod/moductypes.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down
16 changes: 16 additions & 0 deletions tests/extmod/uctypes_le_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,19 @@

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)
2 changes: 2 additions & 0 deletions tests/extmod/uctypes_le_float.py.exp
Original file line number Diff line number Diff line change
@@ -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@')
2 changes: 2 additions & 0 deletions tests/extmod/uctypes_sizeof_float.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@

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)}))
2 changes: 2 additions & 0 deletions tests/extmod/uctypes_sizeof_float.py.exp
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
4
8
8
16

0 comments on commit 47583d8

Please sign in to comment.