extmod/uctypes: Finish support for FLOAT32 and FLOAT64 types.

This commit is contained in:
Damien George 2016-03-19 21:59:42 +00:00
parent 12154b1774
commit da161fd9f0
5 changed files with 60 additions and 3 deletions

View File

@ -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)) },
};

View File

@ -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)

View File

@ -0,0 +1,3 @@
12.3400
12.3400
12.3400

View File

@ -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)

View File

@ -0,0 +1,2 @@
12.3400
12.3400