Skip to content
Permalink
Browse files

prf.c: handle denormals properly

Denormals need to be normalized to be displayed properly.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
  • Loading branch information...
Nicolas Pitre authored and nashif committed Jun 20, 2019
1 parent 5316974 commit 3c0cc086574e029999cdefefcf12b1aa353ab2c2
Showing with 13 additions and 0 deletions.
  1. +6 −0 lib/libc/minimal/source/stdout/prf.c
  2. +7 −0 tests/lib/sprintf/src/main.c
@@ -263,6 +263,12 @@ static int _to_float(char *buf, uint64_t double_temp, char c,
}

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. */
fract |= HIGHBIT64;
}
@@ -322,6 +322,13 @@ void test_sprintf_double(void)
zassert_true((strcmp(buffer, "0.0001505") == 0),
"sprintf(0.0001505) - incorrect "
"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);
}

/**

0 comments on commit 3c0cc08

Please sign in to comment.
You can’t perform that action at this time.