From c073519ec824145676edef88623496c1ded4fae5 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 23 Mar 2017 23:51:35 +1100 Subject: [PATCH] py/objint: Handle special case of -0 when classifying fp as int. Otherwise -0.0 is classified as a bigint, which for builds without bigints will lead unexpectedly to an overflow. --- py/objint.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/py/objint.c b/py/objint.c index 5aa6a95cc0..ce091549d4 100644 --- a/py/objint.c +++ b/py/objint.c @@ -103,7 +103,12 @@ mp_fp_as_int_class_t mp_classify_fp_as_int(mp_float_t val) { #if MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE e |= u.i[MP_ENDIANNESS_BIG] != 0; #endif - e += ((1 << MP_FLOAT_EXP_BITS) - 1) << MP_FLOAT_EXP_SHIFT_I32; + if ((e & ~(1 << MP_FLOAT_SIGN_SHIFT_I32)) == 0) { + // handle case of -0 (when sign is set but rest of bits are zero) + e = 0; + } else { + e += ((1 << MP_FLOAT_EXP_BITS) - 1) << MP_FLOAT_EXP_SHIFT_I32; + } } else { e &= ~((1 << MP_FLOAT_EXP_SHIFT_I32) - 1); }