diff options
author | Damien George <damien.p.george@gmail.com> | 2017-03-23 23:51:35 +1100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2017-03-23 23:51:35 +1100 |
commit | c073519ec824145676edef88623496c1ded4fae5 (patch) | |
tree | 9ae49e1c768498ec34a92344380519eebbc9f58a /py/objint.c | |
parent | febeff4af429483b41137f0befc70d14c9077ae1 (diff) |
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.
Diffstat (limited to 'py/objint.c')
-rw-r--r-- | py/objint.c | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/py/objint.c b/py/objint.c index 5aa6a95cc..ce091549d 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); } |