Commit 0b3014ce authored by David Steinberg's avatar David Steinberg Committed by Paul Sokolovsky
Browse files

py: Add support for floats in mp_binary_{get,set}_val()

- This then provides support for floats in the struct package
parent a5efcd47
...@@ -194,6 +194,14 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) { ...@@ -194,6 +194,14 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
} else if (val_type == 'S') { } else if (val_type == 'S') {
const char *s_val = (const char*)(mp_uint_t)val; const char *s_val = (const char*)(mp_uint_t)val;
return mp_obj_new_str(s_val, strlen(s_val), false); return mp_obj_new_str(s_val, strlen(s_val), false);
#if MICROPY_PY_BUILTINS_FLOAT
} else if (val_type == 'f') {
union { uint32_t i; float f; } fpu = {val};
return mp_obj_new_float(fpu.f);
} else if (val_type == 'd') {
union { uint64_t i; double f; } fpu = {val};
return mp_obj_new_float(fpu.f);
#endif
} else if (is_signed(val_type)) { } else if (is_signed(val_type)) {
if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) { if ((long long)MP_SMALL_INT_MIN <= val && val <= (long long)MP_SMALL_INT_MAX) {
return mp_obj_new_int((mp_int_t)val); return mp_obj_new_int((mp_int_t)val);
...@@ -249,6 +257,27 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte ** ...@@ -249,6 +257,27 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
case 'O': case 'O':
val = (mp_uint_t)val_in; val = (mp_uint_t)val_in;
break; break;
#if MICROPY_PY_BUILTINS_FLOAT
case 'f': {
union { uint32_t i; float f; } fp_sp;
fp_sp.f = mp_obj_get_float(val_in);
val = fp_sp.i;
break;
}
case 'd': {
union { uint64_t i64; uint32_t i32[2]; double f; } fp_dp;
fp_dp.f = mp_obj_get_float(val_in);
if (BYTES_PER_WORD == 8) {
val = fp_dp.i64;
} else {
int be = struct_type == '>';
mp_binary_set_int(sizeof(uint32_t), be, p, fp_dp.i32[MP_ENDIANNESS_BIG ^ be]);
p += sizeof(uint32_t);
val = fp_dp.i32[MP_ENDIANNESS_LITTLE ^ be];
}
break;
}
#endif
default: default:
// we handle large ints here by calling the truncated accessor // we handle large ints here by calling the truncated accessor
if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) { if (MP_OBJ_IS_TYPE(val_in, &mp_type_int)) {
......
# test struct package with floats
import struct
i = 1. + 1/2
# TODO: it looks like '=' format modifier is not yet supported
# for fmt in ('f', 'd', '>f', '>d', '<f', '<d', '=f', '=d'):
for fmt in ('f', 'd', '>f', '>d', '<f', '<d'):
x = struct.pack(fmt, i)
v = struct.unpack(fmt, x)[0]
print('%2s: %.17f - %s' % (fmt, v, (i == v) and 'passed' or 'failed'))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment