Skip to content

Commit

Permalink
Merge pull request #4238 from dhalbert/cdc-avoid-extra-float-libs
Browse files Browse the repository at this point in the history
usb_cdc: avoid pulling in extra float-uint64 routines
  • Loading branch information
dhalbert authored Feb 21, 2021
2 parents 2830984 + 93bf269 commit 79b1226
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
1 change: 1 addition & 0 deletions py/obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions py/objfloat.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions shared-module/usb_cdc/Serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 79b1226

Please sign in to comment.