Skip to content

Commit da161fd

Browse files
committed
extmod/uctypes: Finish support for FLOAT32 and FLOAT64 types.
1 parent 12154b1 commit da161fd

5 files changed

Lines changed: 60 additions & 3 deletions

File tree

extmod/moductypes.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_sizeof_obj, uctypes_struct_sizeo
283283

284284
STATIC inline mp_obj_t get_unaligned(uint val_type, void *p, int big_endian) {
285285
char struct_type = big_endian ? '>' : '<';
286-
static const char type2char[8] = "BbHhIiQq";
286+
static const char type2char[16] = "BbHhIiQq------fd";
287287
return mp_binary_get_val(struct_type, type2char[val_type], (byte**)&p);
288288
}
289289

290290
STATIC inline void set_unaligned(uint val_type, byte *p, int big_endian, mp_obj_t val) {
291291
char struct_type = big_endian ? '>' : '<';
292-
static const char type2char[8] = "BbHhIiQq";
292+
static const char type2char[16] = "BbHhIiQq------fd";
293293
mp_binary_set_val(struct_type, type2char[val_type], val, &p);
294294
}
295295

@@ -349,6 +349,17 @@ STATIC mp_obj_t get_aligned(uint val_type, void *p, mp_int_t index) {
349349
}
350350

351351
STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) {
352+
#if MICROPY_PY_BUILTINS_FLOAT
353+
if (val_type == FLOAT32 || val_type == FLOAT64) {
354+
mp_float_t v = mp_obj_get_float(val);
355+
if (val_type == FLOAT32) {
356+
((float*)p)[index] = v;
357+
} else {
358+
((double*)p)[index] = v;
359+
}
360+
return;
361+
}
362+
#endif
352363
mp_int_t v = mp_obj_get_int(val);
353364
switch (val_type) {
354365
case UINT8:
@@ -392,7 +403,7 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set
392403
offset &= VALUE_MASK(VAL_TYPE_BITS);
393404
//printf("scalar type=%d offset=%x\n", val_type, offset);
394405

395-
if (val_type <= INT64) {
406+
if (val_type <= INT64 || val_type == FLOAT32 || val_type == FLOAT64) {
396407
// printf("size=%d\n", GET_SCALAR_SIZE(val_type));
397408
if (self->flags == LAYOUT_NATIVE) {
398409
if (set_val == MP_OBJ_NULL) {
@@ -686,6 +697,11 @@ STATIC const mp_rom_map_elem_t mp_module_uctypes_globals_table[] = {
686697
{ MP_ROM_QSTR(MP_QSTR_BF_POS), MP_ROM_INT(17) },
687698
{ MP_ROM_QSTR(MP_QSTR_BF_LEN), MP_ROM_INT(22) },
688699

700+
#if MICROPY_PY_BUILTINS_FLOAT
701+
{ MP_ROM_QSTR(MP_QSTR_FLOAT32), MP_ROM_INT(TYPE2SMALLINT(FLOAT32, 4)) },
702+
{ MP_ROM_QSTR(MP_QSTR_FLOAT64), MP_ROM_INT(TYPE2SMALLINT(FLOAT64, 4)) },
703+
#endif
704+
689705
{ MP_ROM_QSTR(MP_QSTR_PTR), MP_ROM_INT(TYPE2SMALLINT(PTR, AGG_TYPE_BITS)) },
690706
{ MP_ROM_QSTR(MP_QSTR_ARRAY), MP_ROM_INT(TYPE2SMALLINT(ARRAY, AGG_TYPE_BITS)) },
691707
};

tests/extmod/uctypes_le_float.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import uctypes
2+
3+
desc = {
4+
"f32": uctypes.FLOAT32 | 0,
5+
"f64": uctypes.FLOAT64 | 0,
6+
"uf64": uctypes.FLOAT64 | 2, # unaligned
7+
}
8+
9+
data = bytearray(10)
10+
11+
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN)
12+
13+
S.f32 = 12.34
14+
print('%.4f' % S.f32)
15+
16+
S.f64 = 12.34
17+
print('%.4f' % S.f64)
18+
19+
S.uf64 = 12.34
20+
print('%.4f' % S.uf64)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
12.3400
2+
12.3400
3+
12.3400
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import uctypes
2+
3+
desc = {
4+
"f32": uctypes.FLOAT32 | 0,
5+
"f64": uctypes.FLOAT64 | 0,
6+
}
7+
8+
data = bytearray(8)
9+
10+
S = uctypes.struct(uctypes.addressof(data), desc, uctypes.NATIVE)
11+
12+
S.f32 = 12.34
13+
print('%.4f' % S.f32)
14+
15+
S.f64 = 12.34
16+
print('%.4f' % S.f64)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
12.3400
2+
12.3400

0 commit comments

Comments
 (0)