summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2021-02-21 12:00:32 -0500
committerDan Halbert <halbert@halwitz.org>2021-02-21 12:05:03 -0500
commit93bf269c0d56ec5d842ff908e149c635ab323107 (patch)
tree036b3da97426b08697743d260385994e7a82f01c /py
parent2830984aef2208abe911e1a6143e084293e3fc9c (diff)
Avoid pulling in extra float-uint64 routines
Diffstat (limited to 'py')
-rw-r--r--py/obj.h1
-rw-r--r--py/objfloat.c9
2 files changed, 10 insertions, 0 deletions
diff --git a/py/obj.h b/py/obj.h
index 066562cc4..c3ab63b5c 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 80f10e816..329a8b90e 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