[En-Nut-Discussion] printing floats on ARM

Krzysztof Sawicki krzysztof.sawicki at mobile.put.edu.pl
Wed May 7 15:01:51 CEST 2014


Hi, everyone!

I'm working on device with AT91SAM7X512, my compiler:
gcc version 4.8.1 (Sourcery CodeBench Lite 2013.11-24)

I found a bug (rather in CS than NutOs) when printing floats, eg.:

printf("pi=%f\n",3.14159265358979);

sometimes it works ok, sometimes not (printing random values)

Further research showed that the problem occurs when ap is not 8-byte aligned in putf.c:525:

_double = va_arg(ap, double);

The hack that works is:

uint32_t *l = (uint32_t *)&_double;
l[0]= va_arg(ap, uint32_t);
l[1]= va_arg(ap, uint32_t);

instead of the above.
Don't know if the hack should appear in official NutOs trunk or should we wait until Mentor Graphics fixes this, but the patch follows:

Index: nut/crt/putf.c
===================================================================
--- nut/crt/putf.c	(revision 5691)
+++ nut/crt/putf.c	(working copy)
@@ -522,7 +522,11 @@

                  if (prec == -1)
                      prec = DEFPREC;
-                _double = va_arg(ap, double);
+
+                uint32_t *l = (uint32_t *)&_double;
+                l[0]= va_arg(ap, uint32_t);
+                l[1]= va_arg(ap, uint32_t);
+
                  cp = _dtoa_r(_REENT, _double, 3, prec, &decpt, &neg, &rve);
                  if (neg)
                      sign = '-';

regards

-- 
Krzysztof Sawicki
Mobile Systems Research Labs, Poznan University of Technology


More information about the En-Nut-Discussion mailing list