Welcome to mirror list, hosted at ThFree Co, Russian Federation.

cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'newlib/libc/stdlib/ldtoa.c')
-rw-r--r--newlib/libc/stdlib/ldtoa.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/newlib/libc/stdlib/ldtoa.c b/newlib/libc/stdlib/ldtoa.c
index ca8b9f363..0e4476e66 100644
--- a/newlib/libc/stdlib/ldtoa.c
+++ b/newlib/libc/stdlib/ldtoa.c
@@ -41,6 +41,9 @@ void _IO_ldtostr(long double *, char *, int, int, char);
/* The exponent of 1.0 */
#define EXONE (0x3fff)
+ /* Maximum exponent digits - base 10 */
+ #define MAX_EXP_DIGITS 5
+
/* Control structure for long doublue conversion including rounding precision values.
* rndprc can be set to 80 (if NE=6), 64, 56, 53, or 24 bits.
*/
@@ -2703,7 +2706,7 @@ _ldtoa_r (struct _reent *ptr, long double d, int mode, int ndigits, int *decpt,
{
unsigned short e[NI];
char *s, *p;
-int k;
+int i, j, k;
LDPARMS rnd;
LDPARMS *ldp = &rnd;
char *outstr;
@@ -2744,16 +2747,21 @@ if( mode != 3 )
For now, just ask for 20 digits which is enough but sometimes too many. */
if( mode == 0 )
ndigits = 20;
+
+/* reentrancy addition to use mprec storage pool */
+/* we want to have enough space to hold the formatted result */
+i = ndigits + (mode == 3 ? (MAX_EXP_DIGITS + 1) : 1);
+j = sizeof (__ULong);
+for (_REENT_MP_RESULT_K(ptr) = 0; sizeof (_Bigint) - sizeof (__ULong) + j <= i; j <<= 1)
+ _REENT_MP_RESULT_K(ptr)++;
+_REENT_MP_RESULT(ptr) = Balloc (ptr, _REENT_MP_RESULT_K(ptr));
+outstr = (char *)_REENT_MP_RESULT(ptr);
+
/* This sanity limit must agree with the corresponding one in etoasc, to
keep straight the returned value of outexpon. */
if( ndigits > NDEC )
ndigits = NDEC;
-/* reentrancy addition to use mprec storage pool */
-_REENT_MP_RESULT(ptr) = Balloc (ptr, 3);
-_REENT_MP_RESULT_K(ptr) = 3;
-outstr = (char *)_REENT_MP_RESULT(ptr);
-
etoasc( e, outstr, ndigits, mode, ldp );
s = outstr;
if( eisinf(e) || eisnan(e) )