public inbox for newlib-cvs@sourceware.org
help / color / mirror / Atom feed
* [newlib-cygwin/cygwin-3_3-branch] ldtoa: fix dropping too many digits from output
@ 2021-11-04 12:35 Corinna Vinschen
  0 siblings, 0 replies; only message in thread
From: Corinna Vinschen @ 2021-11-04 12:35 UTC (permalink / raw)
  To: newlib-cvs

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=97dac2bbcb01aebb3a12e37138cfb32ff3ff7286

commit 97dac2bbcb01aebb3a12e37138cfb32ff3ff7286
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Thu Nov 4 11:30:44 2021 +0100

    ldtoa: fix dropping too many digits from output
    
    ldtoa cuts the number of digits it returns based on a computation of
    number of supported bits (144) divide by log10(2).  Not only is the
    integer approximation of log10(2) ~= 8/27 missing a digit here, it
    also fails to take really small double and long double values into
    account.
    
    Allow for the full potential precision of long double values.  At the
    same time, change the local string array allocation to request only as
    much bytes as necessary to support the caller-requested number of
    digits, to keep the stack size low on small targets.
    
    In the long run a better fix would be to switch to gdtoa, as the BSD
    variants, as well as Mingw64 do.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 newlib/libc/stdlib/ldtoa.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/newlib/libc/stdlib/ldtoa.c b/newlib/libc/stdlib/ldtoa.c
index 1cc97151a..7da61457b 100644
--- a/newlib/libc/stdlib/ldtoa.c
+++ b/newlib/libc/stdlib/ldtoa.c
@@ -34,9 +34,9 @@ void _IO_ldtostr (long double *, char *, int, int, char);
 #define NBITS ((NI-4)*16)
 
  /* Maximum number of decimal digits in ASCII conversion
-  * = NBITS*log10(2)
+  * Take full possible size of output into account
   */
-#define NDEC (NBITS*8/27)
+#define NDEC 1023
 
  /* The exponent of 1.0 */
 #define EXONE (0x3fff)
@@ -2794,7 +2794,6 @@ _ldtoa_r (struct _reent *ptr, long double d, int mode, int ndigits,
   LDPARMS rnd;
   LDPARMS *ldp = &rnd;
   char *outstr;
-  char outbuf[NDEC + MAX_EXP_DIGITS + 10];
   union uconv du;
   du.d = d;
 
@@ -2841,6 +2840,8 @@ _ldtoa_r (struct _reent *ptr, long double d, int mode, int ndigits,
   if (ndigits > NDEC)
     ndigits = NDEC;
 
+  char outbuf[ndigits + MAX_EXP_DIGITS + 10];
+
   etoasc (e, outbuf, ndigits, mode, ldp);
   s = outbuf;
   if (eisinf (e) || eisnan (e))
@@ -3111,6 +3112,8 @@ tnzro:
       else
 	{
 	  emovi (y, w);
+	  /* Note that this loop does not access the incoming string array,
+	   * which may be shorter than NDEC + 1 bytes! */
 	  for (i = 0; i < NDEC + 1; i++)
 	    {
 	      if ((w[NI - 1] & 0x7) != 0)


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-11-04 12:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-04 12:35 [newlib-cygwin/cygwin-3_3-branch] ldtoa: fix dropping too many digits from output Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).