From da161fd9f025b624547b6ed5f74a0d30928310f1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Sat, 19 Mar 2016 21:59:42 +0000 Subject: [PATCH] extmod/uctypes: Finish support for FLOAT32 and FLOAT64 types. --- extmod/moductypes.c | 22 +++++++++++++++++++--- tests/extmod/uctypes_le_float.py | 20 ++++++++++++++++++++ tests/extmod/uctypes_le_float.py.exp | 3 +++ tests/extmod/uctypes_native_float.py | 16 ++++++++++++++++ tests/extmod/uctypes_native_float.py.exp | 2 ++ 5 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 tests/extmod/uctypes_le_float.py create mode 100644 tests/extmod/uctypes_le_float.py.exp create mode 100644 tests/extmod/uctypes_native_float.py create mode 100644 tests/extmod/uctypes_native_float.py.exp diff --git a/extmod/moductypes.c b/extmod/moductypes.c index ad16284d95..cafb2cfb69 100644 --- a/extmod/moductypes.c +++ b/extmod/moductypes.c @@ -283,13 +283,13 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_sizeof_obj, uctypes_struct_sizeo STATIC inline mp_obj_t get_unaligned(uint val_type, void *p, int big_endian) { char struct_type = big_endian ? '>' : '<'; - static const char type2char[8] = "BbHhIiQq"; + static const char type2char[16] = "BbHhIiQq------fd"; return mp_binary_get_val(struct_type, type2char[val_type], (byte**)&p); } STATIC inline void set_unaligned(uint val_type, byte *p, int big_endian, mp_obj_t val) { char struct_type = big_endian ? '>' : '<'; - static const char type2char[8] = "BbHhIiQq"; + static const char type2char[16] = "BbHhIiQq------fd"; mp_binary_set_val(struct_type, type2char[val_type], val, &p); } @@ -349,6 +349,17 @@ STATIC mp_obj_t get_aligned(uint val_type, void *p, mp_int_t index) { } STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) { + #if MICROPY_PY_BUILTINS_FLOAT + if (val_type == FLOAT32 || val_type == FLOAT64) { + mp_float_t v = mp_obj_get_float(val); + if (val_type == FLOAT32) { + ((float*)p)[index] = v; + } else { + ((double*)p)[index] = v; + } + return; + } + #endif mp_int_t v = mp_obj_get_int(val); switch (val_type) { 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 offset &= VALUE_MASK(VAL_TYPE_BITS); //printf("scalar type=%d offset=%x\n", val_type, offset); - if (val_type <= INT64) { + if (val_type <= INT64 || val_type == FLOAT32 || val_type == FLOAT64) { // printf("size=%d\n", GET_SCALAR_SIZE(val_type)); if (self->flags == LAYOUT_NATIVE) { if (set_val == MP_OBJ_NULL) { @@ -686,6 +697,11 @@ STATIC const mp_rom_map_elem_t mp_module_uctypes_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_BF_POS), MP_ROM_INT(17) }, { MP_ROM_QSTR(MP_QSTR_BF_LEN), MP_ROM_INT(22) }, + #if MICROPY_PY_BUILTINS_FLOAT + { MP_ROM_QSTR(MP_QSTR_FLOAT32), MP_ROM_INT(TYPE2SMALLINT(FLOAT32, 4)) }, + { MP_ROM_QSTR(MP_QSTR_FLOAT64), MP_ROM_INT(TYPE2SMALLINT(FLOAT64, 4)) }, + #endif + { MP_ROM_QSTR(MP_QSTR_PTR), MP_ROM_INT(TYPE2SMALLINT(PTR, AGG_TYPE_BITS)) }, { MP_ROM_QSTR(MP_QSTR_ARRAY), MP_ROM_INT(TYPE2SMALLINT(ARRAY, AGG_TYPE_BITS)) }, }; diff --git a/tests/extmod/uctypes_le_float.py b/tests/extmod/uctypes_le_float.py new file mode 100644 index 0000000000..c85b75f36c --- /dev/null +++ b/tests/extmod/uctypes_le_float.py @@ -0,0 +1,20 @@ +import uctypes + +desc = { + "f32": uctypes.FLOAT32 | 0, + "f64": uctypes.FLOAT64 | 0, + "uf64": uctypes.FLOAT64 | 2, # unaligned +} + +data = bytearray(10) + +S = uctypes.struct(uctypes.addressof(data), desc, uctypes.LITTLE_ENDIAN) + +S.f32 = 12.34 +print('%.4f' % S.f32) + +S.f64 = 12.34 +print('%.4f' % S.f64) + +S.uf64 = 12.34 +print('%.4f' % S.uf64) diff --git a/tests/extmod/uctypes_le_float.py.exp b/tests/extmod/uctypes_le_float.py.exp new file mode 100644 index 0000000000..a35a1da2dc --- /dev/null +++ b/tests/extmod/uctypes_le_float.py.exp @@ -0,0 +1,3 @@ +12.3400 +12.3400 +12.3400 diff --git a/tests/extmod/uctypes_native_float.py b/tests/extmod/uctypes_native_float.py new file mode 100644 index 0000000000..89aac8bf38 --- /dev/null +++ b/tests/extmod/uctypes_native_float.py @@ -0,0 +1,16 @@ +import uctypes + +desc = { + "f32": uctypes.FLOAT32 | 0, + "f64": uctypes.FLOAT64 | 0, +} + +data = bytearray(8) + +S = uctypes.struct(uctypes.addressof(data), desc, uctypes.NATIVE) + +S.f32 = 12.34 +print('%.4f' % S.f32) + +S.f64 = 12.34 +print('%.4f' % S.f64) diff --git a/tests/extmod/uctypes_native_float.py.exp b/tests/extmod/uctypes_native_float.py.exp new file mode 100644 index 0000000000..6abf0be70e --- /dev/null +++ b/tests/extmod/uctypes_native_float.py.exp @@ -0,0 +1,2 @@ +12.3400 +12.3400