summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2016-03-19 21:59:42 +0000
committerDamien George <damien.p.george@gmail.com>2016-03-19 21:59:42 +0000
commitda161fd9f025b624547b6ed5f74a0d30928310f1 (patch)
treea8ef4d36a75b69910999efade27fb4fc85f1fe76
parent12154b1774c0a05ce97af13307d106a76169ecc7 (diff)
extmod/uctypes: Finish support for FLOAT32 and FLOAT64 types.
-rw-r--r--extmod/moductypes.c22
-rw-r--r--tests/extmod/uctypes_le_float.py20
-rw-r--r--tests/extmod/uctypes_le_float.py.exp3
-rw-r--r--tests/extmod/uctypes_native_float.py16
-rw-r--r--tests/extmod/uctypes_native_float.py.exp2
5 files changed, 60 insertions, 3 deletions
diff --git a/extmod/moductypes.c b/extmod/moductypes.c
index ad16284d9..cafb2cfb6 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 000000000..c85b75f36
--- /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 000000000..a35a1da2d
--- /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 000000000..89aac8bf3
--- /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 000000000..6abf0be70
--- /dev/null
+++ b/tests/extmod/uctypes_native_float.py.exp
@@ -0,0 +1,2 @@
+12.3400
+12.3400