Skip to content

Commit 47583d8

Browse files
committed
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>
1 parent 350a66a commit 47583d8

5 files changed

Lines changed: 24 additions & 2 deletions

File tree

extmod/moductypes.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ STATIC mp_uint_t uctypes_struct_agg_size(mp_obj_tuple_t *t, int layout_type, mp_
164164
mp_uint_t item_s;
165165
if (t->len == 2) {
166166
// Elements of array are scalar
167-
item_s = GET_SCALAR_SIZE(val_type);
167+
item_s = uctypes_struct_scalar_size(val_type);
168168
if (item_s > *max_field_size) {
169169
*max_field_size = item_s;
170170
}
@@ -541,7 +541,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
541541
return value; // just !MP_OBJ_NULL
542542
}
543543
} else {
544-
byte *p = self->addr + GET_SCALAR_SIZE(val_type) * index;
544+
byte *p = self->addr + uctypes_struct_scalar_size(val_type) * index;
545545
if (value == MP_OBJ_SENTINEL) {
546546
return get_unaligned(val_type, p, self->flags);
547547
} else {

tests/extmod/uctypes_le_float.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,19 @@
2222

2323
S.uf64 = 12.34
2424
print("%.4f" % S.uf64)
25+
26+
# array of float/double
27+
desc = {
28+
"af32": (uctypes.ARRAY | 0, uctypes.FLOAT32 | 2),
29+
"af64": (uctypes.ARRAY | 0, uctypes.FLOAT64 | 2),
30+
}
31+
data = bytearray(16)
32+
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
33+
34+
S.af32[0] = 1
35+
S.af32[1] = 2
36+
print("%.4f %.4f" % (S.af32[0], S.af32[1]), data)
37+
38+
S.af64[0] = 1
39+
S.af64[1] = 2
40+
print("%.4f %.4f" % (S.af64[0], S.af64[1]), data)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
12.3400
22
12.3400
33
12.3400
4+
1.0000 2.0000 bytearray(b'\x00\x00\x80?\x00\x00\x00@\x00\x00\x00\x00\x00\x00\x00\x00')
5+
1.0000 2.0000 bytearray(b'\x00\x00\x00\x00\x00\x00\xf0?\x00\x00\x00\x00\x00\x00\x00@')

tests/extmod/uctypes_sizeof_float.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@
66

77
print(uctypes.sizeof({"f": uctypes.FLOAT32}))
88
print(uctypes.sizeof({"f": uctypes.FLOAT64}))
9+
print(uctypes.sizeof({"f": (uctypes.ARRAY | 0, uctypes.FLOAT32 | 2)}))
10+
print(uctypes.sizeof({"f": (uctypes.ARRAY | 0, uctypes.FLOAT64 | 2)}))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
4
22
8
3+
8
4+
16

0 commit comments

Comments
 (0)