aboutsummaryrefslogtreecommitdiff
path: root/py/objfloat.c
diff options
context:
space:
mode:
authorScott Shawcroft <scott@tannewt.org>2021-02-25 16:59:15 -0800
committerScott Shawcroft <scott@tannewt.org>2021-02-25 16:59:15 -0800
commit3f08cb47b846fff41ef9a45d49758f0cef61e723 (patch)
tree66d5093b24d127a1fb60cc70f5087b39e87fbff5 /py/objfloat.c
parent52bc935fa7aa9e5c94f75fec8b2fa746c83f0be7 (diff)
parentb69cb0144ca0d54011be540213d1e25e673a63de (diff)
Merge remote-tracking branch 'adafruit/main' into busio-uart-rp
Diffstat (limited to 'py/objfloat.c')
-rw-r--r--py/objfloat.c9
1 files changed, 9 insertions, 0 deletions
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