prf.c: handle denormals properly

Denormals need to be normalized to be displayed properly.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
This commit is contained in:
Nicolas Pitre 2019-06-19 22:14:03 -04:00 committed by Anas Nashif
commit 3c0cc08657
2 changed files with 13 additions and 0 deletions

View file

@ -263,6 +263,12 @@ static int _to_float(char *buf, uint64_t double_temp, char c,
} }
if ((exp | fract) != 0) { if ((exp | fract) != 0) {
if (exp == 0) {
/* this is a denormal */
while (((fract <<= 1) & HIGHBIT64) == 0) {
exp--;
}
}
exp -= (1023 - 1); /* +1 since .1 vs 1. */ exp -= (1023 - 1); /* +1 since .1 vs 1. */
fract |= HIGHBIT64; fract |= HIGHBIT64;
} }

View file

@ -322,6 +322,13 @@ void test_sprintf_double(void)
zassert_true((strcmp(buffer, "0.0001505") == 0), zassert_true((strcmp(buffer, "0.0001505") == 0),
"sprintf(0.0001505) - incorrect " "sprintf(0.0001505) - incorrect "
"output '%s'\n", buffer); "output '%s'\n", buffer);
var.u1 = 0x00000001;
var.u2 = 0x00000000; /* smallest denormal value */
sprintf(buffer, "%g", var.d);
zassert_true((strcmp(buffer, "4.94066e-324") == 0),
"sprintf(4.94066e-324) - incorrect "
"output '%s'\n", buffer);
} }
/** /**