Skip to content

usb_cdc: avoid pulling in extra float-uint64 routines #4238

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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