diff options
author | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2020-06-21 19:58:18 +0300 |
---|---|---|
committer | Petteri Aimonen <jpa@git.mail.kapsi.fi> | 2020-06-21 20:26:17 +0300 |
commit | 8b1ac6e86173144665963f5826096f71e1bba56b (patch) | |
tree | a7c1abfb7c2565c25efffaf8c6d1d118d877698d /pb_decode.c | |
parent | 1b7c477dd7e85dfc2104130d08d473c5676964ba (diff) |
PB_CONVERT_DOUBLE_FLOAT: fix handling of varying NaN representations (#543)
Diffstat (limited to 'pb_decode.c')
-rw-r--r-- | pb_decode.c | 56 |
1 files changed, 30 insertions, 26 deletions
diff --git a/pb_decode.c b/pb_decode.c index f936412..d4ad5a3 100644 --- a/pb_decode.c +++ b/pb_decode.c @@ -1692,36 +1692,40 @@ bool pb_decode_double_as_float(pb_istream_t *stream, float *dest) { /* Special value */ exponent = 128; + mantissa >>= 1; } - else if (exponent > 127) - { - /* Too large, convert to infinity */ - exponent = 128; - mantissa = 0; - } - else if (exponent < -150) - { - /* Too small, convert to zero */ - exponent = -127; - mantissa = 0; - } - else if (exponent < -126) + else { - /* Denormalized */ - mantissa |= 0x1000000; - mantissa >>= (-126 - exponent); - exponent = -127; - } + if (exponent > 127) + { + /* Too large, convert to infinity */ + exponent = 128; + mantissa = 0; + } + else if (exponent < -150) + { + /* Too small, convert to zero */ + exponent = -127; + mantissa = 0; + } + else if (exponent < -126) + { + /* Denormalized */ + mantissa |= 0x1000000; + mantissa >>= (-126 - exponent); + exponent = -127; + } - /* Round off mantissa */ - mantissa = (mantissa + 1) >> 1; + /* Round off mantissa */ + mantissa = (mantissa + 1) >> 1; - /* Check if mantissa went over 2.0 */ - if (mantissa & 0x800000) - { - exponent += 1; - mantissa &= 0x7FFFFF; - mantissa >>= 1; + /* Check if mantissa went over 2.0 */ + if (mantissa & 0x800000) + { + exponent += 1; + mantissa &= 0x7FFFFF; + mantissa >>= 1; + } } /* Combine fields */ |