From 93bf269c0d56ec5d842ff908e149c635ab323107 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Sun, 21 Feb 2021 12:00:32 -0500 Subject: [PATCH] Avoid pulling in extra float-uint64 routines --- py/obj.h | 1 + py/objfloat.c | 9 +++++++++ shared-module/usb_cdc/Serial.c | 6 ++++-- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/py/obj.h b/py/obj.h index 066562cc43..c3ab63b5cc 100644 --- a/py/obj.h +++ b/py/obj.h @@ -684,6 +684,7 @@ mp_obj_t mp_obj_new_bytearray_by_ref(size_t n, void *items); mp_obj_t mp_obj_new_int_from_float(mp_float_t val); mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag); extern mp_float_t uint64_to_float(uint64_t ui64); +extern uint64_t float_to_uint64(float f); #endif mp_obj_t mp_obj_new_exception(const mp_obj_type_t *exc_type); mp_obj_t mp_obj_new_exception_arg1(const mp_obj_type_t *exc_type, mp_obj_t arg); diff --git a/py/objfloat.c b/py/objfloat.c index 80f10e816e..329a8b90e0 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -340,6 +340,15 @@ mp_float_t uint64_to_float(uint64_t ui64) { return (mp_float_t) ((uint32_t) (ui64 >> 32) * 4294967296.0f + (uint32_t) (ui64 & 0xffffffff)); } +// Convert a uint64_t to a 32-bit float to a uint64_t without invoking extra math routines. +// which are large. +// Assume f >= 0. +uint64_t float_to_uint64(float f) { + // 4294967296 = 2^32 + const uint32_t upper_half = (uint32_t) (f / 4294967296.0f); + const uint32_t lower_half = (uint32_t) f; + return (((uint64_t) upper_half) << 32) + lower_half; +} #pragma GCC diagnostic pop #endif // MICROPY_PY_BUILTINS_FLOAT diff --git a/shared-module/usb_cdc/Serial.c b/shared-module/usb_cdc/Serial.c index 6037fddcc5..b4d8667ca6 100644 --- a/shared-module/usb_cdc/Serial.c +++ b/shared-module/usb_cdc/Serial.c @@ -41,7 +41,8 @@ size_t common_hal_usb_cdc_serial_read(usb_cdc_serial_obj_t *self, uint8_t *data, if (wait_forever || wait_for_timeout) { // Read more if we have time. - uint64_t timeout_ms = self->timeout * 1000; // Junk value if timeout < 0. + // Use special routine to avoid pulling in uint64-float-compatible math routines. + uint64_t timeout_ms = float_to_uint64(self->timeout * 1000); // Junk value if timeout < 0. uint64_t start_ticks = supervisor_ticks_ms64(); uint32_t num_read = 0; @@ -78,7 +79,8 @@ size_t common_hal_usb_cdc_serial_write(usb_cdc_serial_obj_t *self, const uint8_t if (wait_forever || wait_for_timeout) { // Write more if we have time. - uint64_t timeout_ms = self->write_timeout * 1000; // Junk value if write_timeout < 0. + // Use special routine to avoid pulling in uint64-float-compatible math routines. + uint64_t timeout_ms = float_to_uint64(self->write_timeout * 1000); // Junk value if write_timeout < 0. uint64_t start_ticks = supervisor_ticks_ms64(); uint32_t num_written = 0;