py/dynruntime: Add support for float API to make/get floats.

We don't want to add a feature flag to .mpy files that indicate float
support because it will get complex and difficult to use.  Instead the .mpy
is built using whatever precision it chooses (float or double) and the
native glue API will convert between this choice and what the host runtime
actually uses.
This commit is contained in:
Damien George 2019-11-30 23:03:09 +11:00
parent ff58961944
commit abc642973d
3 changed files with 27 additions and 1 deletions

View File

@ -190,4 +190,20 @@ static inline void mp_raise_OSError_dyn(int er) {
nlr_raise(mp_call_function_n_kw(mp_load_global(MP_QSTR_OSError), 1, 0, &args[0]));
}
/******************************************************************************/
// Floating point
#define mp_obj_new_float_from_f(f) (mp_fun_table.obj_new_float_from_f((f)))
#define mp_obj_new_float_from_d(d) (mp_fun_table.obj_new_float_from_d((d)))
#define mp_obj_get_float_to_f(o) (mp_fun_table.obj_get_float_to_f((o)))
#define mp_obj_get_float_to_d(o) (mp_fun_table.obj_get_float_to_d((o)))
#if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_FLOAT
#define mp_obj_new_float(f) (mp_obj_new_float_from_f((f)))
#define mp_obj_get_float(o) (mp_obj_get_float_to_f((o)))
#elif MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
#define mp_obj_new_float(f) (mp_obj_new_float_from_d((f)))
#define mp_obj_get_float(o) (mp_obj_get_float_to_d((o)))
#endif
#endif // MICROPY_INCLUDED_PY_DYNRUNTIME_H

View File

@ -46,6 +46,7 @@ ifeq ($(ARCH),x86)
CROSS =
CFLAGS += -m32 -fno-stack-protector
MPY_CROSS_FLAGS += -mcache-lookup-bc
MICROPY_FLOAT_IMPL ?= double
else ifeq ($(ARCH),x64)
@ -53,12 +54,14 @@ else ifeq ($(ARCH),x64)
CROSS =
CFLAGS += -fno-stack-protector
MPY_CROSS_FLAGS += -mcache-lookup-bc
MICROPY_FLOAT_IMPL ?= double
else ifeq ($(ARCH),armv7m)
# thumb
CROSS = arm-none-eabi-
CFLAGS += -mthumb -mcpu=cortex-m3
MICROPY_FLOAT_IMPL ?= none
else ifeq ($(ARCH),armv7emsp)
@ -66,6 +69,7 @@ else ifeq ($(ARCH),armv7emsp)
CROSS = arm-none-eabi-
CFLAGS += -mthumb -mcpu=cortex-m4
CFLAGS += -mfpu=fpv4-sp-d16 -mfloat-abi=hard
MICROPY_FLOAT_IMPL ?= float
else ifeq ($(ARCH),armv7emdp)
@ -73,23 +77,29 @@ else ifeq ($(ARCH),armv7emdp)
CROSS = arm-none-eabi-
CFLAGS += -mthumb -mcpu=cortex-m7
CFLAGS += -mfpu=fpv5-d16 -mfloat-abi=hard
MICROPY_FLOAT_IMPL ?= double
else ifeq ($(ARCH),xtensa)
# xtensa
CROSS = xtensa-lx106-elf-
CFLAGS += -mforce-l32
MICROPY_FLOAT_IMPL ?= none
else ifeq ($(ARCH),xtensawin)
# xtensawin
CROSS = xtensa-esp32-elf-
CFLAGS +=
MICROPY_FLOAT_IMPL ?= float
else
$(error architecture '$(ARCH)' not supported)
endif
MICROPY_FLOAT_IMPL_UPPER = $(shell echo $(MICROPY_FLOAT_IMPL) | tr '[:lower:]' '[:upper:]')
CFLAGS += -DMICROPY_FLOAT_IMPL=MICROPY_FLOAT_IMPL_$(MICROPY_FLOAT_IMPL_UPPER)
CFLAGS += $(CFLAGS_EXTRA)
################################################################################

View File

@ -663,7 +663,7 @@ def link_objects(env, native_qstr_vals_len, native_qstr_objs_len):
# Resolve unknown symbols
mp_fun_table_sec = Section('.external.mp_fun_table', b'', 0)
fun_table = {key: 63 + idx
fun_table = {key: 67 + idx
for idx, key in enumerate([
'mp_type_type',
'mp_type_str',