1
/* Copyright (C) 2007 MySQL AB
2
This program is free software; you can redistribute it and/or modify
3
it under the terms of the GNU General Public License as published by
4
the Free Software Foundation; either version 2 of the License, or
5
(at your option) any later version.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
/****************************************************************
18
This file incorporates work covered by the following copyright and
21
The author of this software is David M. Gay.
23
Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
25
Permission to use, copy, modify, and distribute this software for any
26
purpose without fee is hereby granted, provided that this entire notice
27
is included in all copies of any software which is or includes a copy
28
or modification of this software and in all copies of the supporting
29
documentation for such software.
31
THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
32
WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
33
REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
34
OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
36
***************************************************************/
38
#include <my_base.h> /* for EOVERFLOW on Windows */
39
#include <my_global.h>
40
#include <m_string.h> /* for memcpy and NOT_FIXED_DEC */
43
Appears to suffice to not call malloc() in most cases.
45
see if it is possible to get rid of malloc().
46
this constant is sufficient to avoid malloc() on all inputs I have tried.
48
#define DTOA_BUFF_SIZE (420 * sizeof(void *))
50
/* Magic value returned by dtoa() to indicate overflow */
51
#define DTOA_OVERFLOW 9999
53
static double my_strtod_int(const char *, char **, int *, char *, size_t);
54
static char *dtoa(double, int, int, int *, int *, char **, char *, size_t);
55
static void dtoa_free(char *, char *, size_t);
59
Converts a given floating point number to a zero-terminated string
60
representation using the 'f' format.
63
This function is a wrapper around dtoa() to do the same as
64
sprintf(to, "%-.*f", precision, x), though the conversion is usually more
65
precise. The only difference is in handling [-,+]infinity and nan values,
66
in which case we print '0\0' to the output string and indicate an overflow.
68
@param x the input floating point number.
69
@param precision the number of digits after the decimal point.
70
All properties of sprintf() apply:
71
- if the number of significant digits after the decimal
72
point is less than precision, the resulting string is
73
right-padded with zeros
74
- if the precision is 0, no decimal point appears
75
- if a decimal point appears, at least one digit appears
77
@param to pointer to the output buffer. The longest string which
78
my_fcvt() can return is FLOATING_POINT_BUFFER bytes
79
(including the terminating '\0').
80
@param error if not NULL, points to a location where the status of
81
conversion is stored upon return.
82
FALSE successful conversion
83
TRUE the input number is [-,+]infinity or nan.
84
The output string in this case is always '0'.
85
@return number of written characters (excluding terminating '\0')
88
size_t my_fcvt(double x, int precision, char *to, my_bool *error)
90
int decpt, sign, len, i;
91
char *res, *src, *end, *dst= to;
92
char buf[DTOA_BUFF_SIZE];
93
DBUG_ASSERT(precision >= 0 && precision < NOT_FIXED_DEC && to != NULL);
95
res= dtoa(x, 5, precision, &decpt, &sign, &end, buf, sizeof(buf));
97
if (decpt == DTOA_OVERFLOW)
99
dtoa_free(res, buf, sizeof(buf));
117
for (i= decpt; i < 0; i++)
121
for (i= 1; i <= len; i++)
124
if (i == decpt && i < len)
135
for (i= precision - max(0, (len - decpt)); i > 0; i--)
143
dtoa_free(res, buf, sizeof(buf));
150
Converts a given floating point number to a zero-terminated string
151
representation with a given field width using the 'e' format
152
(aka scientific notation) or the 'f' one.
155
The format is chosen automatically to provide the most number of significant
156
digits (and thus, precision) with a given field width. In many cases, the
157
result is similar to that of sprintf(to, "%g", x) with a few notable
159
- the conversion is usually more precise than C library functions.
160
- there is no 'precision' argument. instead, we specify the number of
161
characters available for conversion (i.e. a field width).
162
- the result never exceeds the specified field width. If the field is too
163
short to contain even a rounded decimal representation, my_gcvt()
164
indicates overflow and truncates the output string to the specified width.
165
- float-type arguments are handled differently than double ones. For a
166
float input number (i.e. when the 'type' argument is MY_GCVT_ARG_FLOAT)
167
we deliberately limit the precision of conversion by FLT_DIG digits to
168
avoid garbage past the significant digits.
169
- unlike sprintf(), in cases where the 'e' format is preferred, we don't
170
zero-pad the exponent to save space for significant digits. The '+' sign
171
for a positive exponent does not appear for the same reason.
173
@param x the input floating point number.
174
@param type is either MY_GCVT_ARG_FLOAT or MY_GCVT_ARG_DOUBLE.
175
Specifies the type of the input number (see notes above).
176
@param width field width in characters. The minimal field width to
177
hold any number representation (albeit rounded) is 7
178
characters ("-Ne-NNN").
179
@param to pointer to the output buffer. The result is always
180
zero-terminated, and the longest returned string is thus
182
@param error if not NULL, points to a location where the status of
183
conversion is stored upon return.
184
FALSE successful conversion
185
TRUE the input number is [-,+]infinity or nan.
186
The output string in this case is always '0'.
187
@return number of written characters (excluding terminating '\0')
190
Check if it is possible and makes sense to do our own rounding on top of
191
dtoa() instead of calling dtoa() twice in (rare) cases when the resulting
192
string representation does not fit in the specified field width and we want
193
to re-round the input number with fewer significant digits. Examples:
195
my_gcvt(-9e-3, ..., 4, ...);
196
my_gcvt(-9e-3, ..., 2, ...);
197
my_gcvt(1.87e-3, ..., 4, ...);
198
my_gcvt(55, ..., 1, ...);
200
We do our best to minimize such cases by:
202
- passing to dtoa() the field width as the number of significant digits
204
- removing the sign of the number early (and decreasing the width before
205
passing it to dtoa())
207
- choosing the proper format to preserve the most number of significant
211
size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
214
int decpt, sign, len, exp_len;
215
char *res, *src, *end, *dst= to, *dend= dst + width;
216
char buf[DTOA_BUFF_SIZE];
217
my_bool have_space, force_e_format;
218
DBUG_ASSERT(width > 0 && to != NULL);
220
/* We want to remove '-' from equations early */
224
res= dtoa(x, 4, type == MY_GCVT_ARG_DOUBLE ? width : min(width, FLT_DIG),
225
&decpt, &sign, &end, buf, sizeof(buf));
226
if (decpt == DTOA_OVERFLOW)
228
dtoa_free(res, buf, sizeof(buf));
243
Number of digits in the exponent from the 'e' conversion.
244
The sign of the exponent is taken into account separetely, we don't need
247
exp_len= 1 + (decpt >= 101 || decpt <= -99) + (decpt >= 11 || decpt <= -9);
250
Do we have enough space for all digits in the 'f' format?
251
Let 'len' be the number of significant digits returned by dtoa,
252
and F be the length of the resulting decimal representation.
253
Consider the following cases:
254
1. decpt <= 0, i.e. we have "0.NNN" => F = len - decpt + 2
255
2. 0 < decpt < len, i.e. we have "NNN.NNN" => F = len + 1
256
3. len <= decpt, i.e. we have "NNN00" => F = decpt
258
have_space= (decpt <= 0 ? len - decpt + 2 :
259
decpt > 0 && decpt < len ? len + 1 :
262
The following is true when no significant digits can be placed with the
263
specified field width using the 'f' format, and the 'e' format
264
will not be truncated.
266
force_e_format= (decpt <= 0 && width <= 2 - decpt && width >= 3 + exp_len);
268
Assume that we don't have enough space to place all significant digits in
269
the 'f' format. We have to choose between the 'e' format and the 'f' one
270
to keep as many significant digits as possible.
271
Let E and F be the lengths of decimal representaion in the 'e' and 'f'
272
formats, respectively. We want to use the 'f' format if, and only if F <= E.
273
Consider the following cases:
275
F = len - decpt + 2 (see above)
276
E = len + (len > 1) + 1 + 1 (decpt <= -99) + (decpt <= -9) + 1
278
(F <= E) <=> (len == 1 && decpt >= -1) || (len > 1 && decpt >= -2)
279
We also need to ensure that if the 'f' format is chosen,
280
the field width allows us to place at least one significant digit
281
(i.e. width > 2 - decpt). If not, we prefer the 'e' format.
283
F = len + 1 (see above)
284
E = len + 1 + 1 + ... ("N.NNeMMM")
285
F is always less than E.
286
3. len <= decpt <= width
287
In this case we have enough space to represent the number in the 'f'
288
format, so we prefer it with some exceptions.
290
The number cannot be represented in the 'f' format at all, always use
295
Not enough space, let's see if the 'f' format provides the most number
296
of significant digits.
298
((decpt <= width && (decpt >= -1 || (decpt == -2 &&
299
(len > 1 || !force_e_format)))) &&
303
Use the 'e' format in some cases even if we have enough space for the
304
'f' one. See comment for MAX_DECPT_FOR_F_FORMAT.
306
(!have_space || (decpt >= -MAX_DECPT_FOR_F_FORMAT + 1 &&
307
(decpt <= MAX_DECPT_FOR_F_FORMAT || len > decpt))))
312
width-= (decpt < len) + (decpt <= 0 ? 1 - decpt : 0);
314
/* Do we have to truncate any digits? */
325
We want to truncate (len - width) least significant digits after the
326
decimal point. For this we are calling dtoa with mode=5, passing the
327
number of significant digits = (len-decpt) - (len-width) = width-decpt
329
dtoa_free(res, buf, sizeof(buf));
330
res= dtoa(x, 5, width - decpt, &decpt, &sign, &end, buf, sizeof(buf));
337
/* Underflow. Just print '0' and exit */
343
At this point we are sure we have enough space to put all digits
346
if (sign && dst < dend)
352
if (len > 0 && dst < dend)
354
for (; decpt < 0 && dst < dend; decpt++)
358
for (i= 1; i <= len && dst < dend; i++)
361
if (i == decpt && i < len && dst < dend)
364
while (i++ <= decpt && dst < dend)
378
width-= 1 + exp_len; /* eNNN */
391
/* Do we have to truncate any digits? */
394
/* Yes, re-convert with a smaller width */
395
dtoa_free(res, buf, sizeof(buf));
396
res= dtoa(x, 4, width, &decpt, &sign, &end, buf, sizeof(buf));
403
At this point we are sure we have enough space to put all digits
406
if (sign && dst < dend)
410
if (len > 1 && dst < dend)
413
while (src < end && dst < dend)
418
if (decpt_sign && dst < dend)
421
if (decpt >= 100 && dst < dend)
423
*dst++= decpt / 100 + '0';
426
*dst++= decpt / 10 + '0';
428
else if (decpt >= 10 && dst < dend)
429
*dst++= decpt / 10 + '0';
431
*dst++= decpt % 10 + '0';
436
dtoa_free(res, buf, sizeof(buf));
444
Converts string to double (string does not have to be zero-terminated)
447
This is a wrapper around dtoa's version of strtod().
449
@param str input string
450
@param end address of a pointer to the first character after the input
451
string. Upon return the pointer is set to point to the first
453
@param error Upon return is set to EOVERFLOW in case of underflow or
456
@return The resulting double value. In case of underflow, 0.0 is
457
returned. In case overflow, signed DBL_MAX is returned.
460
double my_strtod(const char *str, char **end, int *error)
462
char buf[DTOA_BUFF_SIZE];
464
DBUG_ASSERT(str != NULL && end != NULL && *end != NULL && error != NULL);
466
res= my_strtod_int(str, end, error, buf, sizeof(buf));
467
return (*error == 0) ? res : (res < 0 ? -DBL_MAX : DBL_MAX);
471
double my_atof(const char *nptr)
474
const char *end= nptr+65535; /* Should be enough */
475
return (my_strtod(nptr, (char**) &end, &error));
479
/****************************************************************
481
* The author of this software is David M. Gay.
483
* Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
485
* Permission to use, copy, modify, and distribute this software for any
486
* purpose without fee is hereby granted, provided that this entire notice
487
* is included in all copies of any software which is or includes a copy
488
* or modification of this software and in all copies of the supporting
489
* documentation for such software.
491
* THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
492
* WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
493
* REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
494
* OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
496
***************************************************************/
497
/* Please send bug reports to David M. Gay (dmg at acm dot org,
498
* with " at " changed at "@" and " dot " changed to "."). */
501
Original copy of the software is located at http://www.netlib.org/fp/dtoa.c
502
It was adjusted to serve MySQL server needs:
503
* strtod() was modified to not expect a zero-terminated string.
504
It now honors 'se' (end of string) argument as the input parameter,
505
not just as the output one.
506
* in dtoa(), in case of overflow/underflow/NaN result string now contains "0";
507
decpt is set to DTOA_OVERFLOW to indicate overflow.
508
* support for VAX, IBM mainframe and 16-bit hardware removed
509
* we always assume that 64-bit integer type is available
510
* support for Kernigan-Ritchie style headers (pre-ANSI compilers)
512
* all gcc warnings ironed out
513
* we always assume multithreaded environment, so we had to change
514
memory allocation procedures to use stack in most cases;
515
malloc is used as the last resort.
516
* pow5mult rewritten to use pre-calculated pow5 list instead of
517
the one generated on the fly.
522
On a machine with IEEE extended-precision registers, it is
523
necessary to specify double-precision (53-bit) rounding precision
524
before invoking strtod or dtoa. If the machine uses (the equivalent
525
of) Intel 80x87 arithmetic, the call
526
_control87(PC_53, MCW_PC);
527
does this with many compilers. Whether this or another call is
528
appropriate depends on the compiler; for this to work, it may be
529
necessary to #include "float.h" or another system-dependent header
534
#define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
535
and dtoa should round accordingly.
536
#define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
537
and Honor_FLT_ROUNDS is not #defined.
539
TODO: check if we can get rid of the above two
543
typedef uint32 ULong;
545
typedef uint64 ULLong;
547
typedef union { double d; ULong L[2]; } U;
549
#if defined(WORDS_BIGENDIAN) || (defined(__FLOAT_WORD_ORDER) && \
550
(__FLOAT_WORD_ORDER == __BIG_ENDIAN))
551
#define word0(x) ((U*)&x)->L[0]
552
#define word1(x) ((U*)&x)->L[1]
554
#define word0(x) ((U*)&x)->L[1]
555
#define word1(x) ((U*)&x)->L[0]
558
#define dval(x) ((U*)&x)->d
560
/* #define P DBL_MANT_DIG */
561
/* Ten_pmax= floor(P*log(2)/log(5)) */
562
/* Bletch= (highest power of 2 < DBL_MAX_10_EXP) / 16 */
563
/* Quick_max= floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
564
/* Int_max= floor(P*log(FLT_RADIX)/log(10) - 1) */
567
#define Exp_shift1 20
568
#define Exp_msk1 0x100000
569
#define Exp_mask 0x7ff00000
573
#define Exp_1 0x3ff00000
574
#define Exp_11 0x3ff00000
576
#define Frac_mask 0xfffff
577
#define Frac_mask1 0xfffff
580
#define Bndry_mask 0xfffff
581
#define Bndry_mask1 0xfffff
583
#define Sign_bit 0x80000000
591
#define Flt_Rounds FLT_ROUNDS
595
#endif /*Flt_Rounds*/
597
#ifdef Honor_FLT_ROUNDS
598
#define Rounding rounding
599
#undef Check_FLT_ROUNDS
600
#define Check_FLT_ROUNDS
602
#define Rounding Flt_Rounds
605
#define rounded_product(a,b) a*= b
606
#define rounded_quotient(a,b) a/= b
608
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
609
#define Big1 0xffffffff
610
#define FFFFFFFF 0xffffffffUL
612
/* This is tested to be enough for dtoa */
616
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
617
2*sizeof(int) + y->wds*sizeof(ULong))
619
/* Arbitrary-length integer */
621
typedef struct Bigint
624
ULong *x; /* points right after this Bigint object */
625
struct Bigint *next; /* to maintain free lists */
627
int k; /* 2^k = maxwds */
628
int maxwds; /* maximum length in 32-bit words */
629
int sign; /* not zero if number is negative */
630
int wds; /* current length in 32-bit words */
634
/* A simple stack-memory based allocator for Bigints */
636
typedef struct Stack_alloc
642
Having list of free blocks lets us reduce maximum required amount
643
of memory from ~4000 bytes to < 1680 (tested on x86).
645
Bigint *freelist[Kmax+1];
650
Try to allocate object on stack, and resort to malloc if all
651
stack memory is used.
654
static Bigint *Balloc(int k, Stack_alloc *alloc)
657
if (k <= Kmax && alloc->freelist[k])
659
rv= alloc->freelist[k];
660
alloc->freelist[k]= rv->p.next;
667
len= sizeof(Bigint) + x * sizeof(ULong);
669
if (alloc->free + len <= alloc->end)
671
rv= (Bigint*) alloc->free;
675
rv= (Bigint*) malloc(len);
680
rv->sign= rv->wds= 0;
681
rv->p.x= (ULong*) (rv + 1);
687
If object was allocated on stack, try putting it to the free
688
list. Otherwise call free().
691
static void Bfree(Bigint *v, Stack_alloc *alloc)
693
char *gptr= (char*) v; /* generic pointer */
694
if (gptr < alloc->begin || gptr >= alloc->end)
696
else if (v->k <= Kmax)
699
Maintain free lists only for stack objects: this way we don't
700
have to bother with freeing lists in the end of dtoa;
701
heap should not be used normally anyway.
703
v->p.next= alloc->freelist[v->k];
704
alloc->freelist[v->k]= v;
710
This is to place return value of dtoa in: tries to use stack
711
as well, but passes by free lists management and just aligns len by
715
static char *dtoa_alloc(int i, Stack_alloc *alloc)
718
int aligned_size= (i + sizeof(ULong) - 1) / sizeof(ULong) * sizeof(ULong);
719
if (alloc->free + aligned_size <= alloc->end)
722
alloc->free+= aligned_size;
731
dtoa_free() must be used to free values s returned by dtoa()
732
This is the counterpart of dtoa_alloc()
735
static void dtoa_free(char *gptr, char *buf, size_t buf_size)
737
if (gptr < buf || gptr >= buf + buf_size)
742
/* Bigint arithmetic functions */
744
/* Multiply by m and add a */
746
static Bigint *multadd(Bigint *b, int m, int a, Stack_alloc *alloc)
759
y= *x * (ULLong)m + carry;
761
*x++= (ULong)(y & FFFFFFFF);
766
if (wds >= b->maxwds)
768
b1= Balloc(b->k+1, alloc);
773
b->p.x[wds++]= (ULong) carry;
780
static Bigint *s2b(const char *s, int nd0, int nd, ULong y9, Stack_alloc *alloc)
787
for (k= 0, y= 1; x > y; y <<= 1, k++) ;
797
b= multadd(b, 10, *s++ - '0', alloc);
804
b= multadd(b, 10, *s++ - '0', alloc);
809
static int hi0bits(register ULong x)
813
if (!(x & 0xffff0000))
818
if (!(x & 0xff000000))
823
if (!(x & 0xf0000000))
828
if (!(x & 0xc0000000))
833
if (!(x & 0x80000000))
836
if (!(x & 0x40000000))
843
static int lo0bits(ULong *y)
846
register ULong x= *y;
893
/* Convert integer to Bigint number */
895
static Bigint *i2b(int i, Stack_alloc *alloc)
906
/* Multiply two Bigint numbers */
908
static Bigint *mult(Bigint *a, Bigint *b, Stack_alloc *alloc)
912
ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
929
for (x= c->p.x, xa= x + wc; x < xa; x++)
936
for (; xb < xbe; xc0++)
945
z= *x++ * (ULLong)y + *xc + carry;
947
*xc++= (ULong) (z & FFFFFFFF);
953
for (xc0= c->p.x, xc= xc0 + wc; wc > 0 && !*--xc; --wc) ;
960
Precalculated array of powers of 5: tested to be enough for
961
vasting majority of dtoa_r cases.
964
static ULong powers5[]=
972
2242703233UL, 762134875UL, 1262UL,
974
3211403009UL, 1849224548UL, 3668416493UL, 3913284084UL, 1593091UL,
976
781532673UL, 64985353UL, 253049085UL, 594863151UL, 3553621484UL,
977
3288652808UL, 3167596762UL, 2788392729UL, 3911132675UL, 590UL,
979
2553183233UL, 3201533787UL, 3638140786UL, 303378311UL, 1809731782UL,
980
3477761648UL, 3583367183UL, 649228654UL, 2915460784UL, 487929380UL,
981
1011012442UL, 1677677582UL, 3428152256UL, 1710878487UL, 1438394610UL,
982
2161952759UL, 4100910556UL, 1608314830UL, 349175UL
986
static Bigint p5_a[]=
988
/* { x } - k - maxwds - sign - wds */
989
{ { powers5 }, 1, 1, 0, 1 },
990
{ { powers5 + 1 }, 1, 1, 0, 1 },
991
{ { powers5 + 2 }, 1, 2, 0, 2 },
992
{ { powers5 + 4 }, 2, 3, 0, 3 },
993
{ { powers5 + 7 }, 3, 5, 0, 5 },
994
{ { powers5 + 12 }, 4, 10, 0, 10 },
995
{ { powers5 + 22 }, 5, 19, 0, 19 }
998
#define P5A_MAX (sizeof(p5_a)/sizeof(*p5_a) - 1)
1000
static Bigint *pow5mult(Bigint *b, int k, Stack_alloc *alloc)
1002
Bigint *b1, *p5, *p51;
1004
static int p05[3]= { 5, 25, 125 };
1007
b= multadd(b, p05[i-1], 0, alloc);
1016
b1= mult(b, p5, alloc);
1022
/* Calculate next power of 5 */
1023
if (p5 < p5_a + P5A_MAX)
1025
else if (p5 == p5_a + P5A_MAX)
1026
p5= mult(p5, p5, alloc);
1029
p51= mult(p5, p5, alloc);
1038
static Bigint *lshift(Bigint *b, int k, Stack_alloc *alloc)
1042
ULong *x, *x1, *xe, z;
1047
for (i= b->maxwds; n1 > i; i<<= 1)
1049
b1= Balloc(k1, alloc);
1051
for (i= 0; i < n; i++)
1078
static int cmp(Bigint *a, Bigint *b)
1080
ULong *xa, *xa0, *xb, *xb0;
1094
return *xa < *xb ? -1 : 1;
1102
static Bigint *diff(Bigint *a, Bigint *b, Stack_alloc *alloc)
1106
ULong *xa, *xae, *xb, *xbe, *xc;
1112
c= Balloc(0, alloc);
1126
c= Balloc(a->k, alloc);
1138
y= (ULLong)*xa++ - *xb++ - borrow;
1139
borrow= y >> 32 & (ULong)1;
1140
*xc++= (ULong) (y & FFFFFFFF);
1146
borrow= y >> 32 & (ULong)1;
1147
*xc++= (ULong) (y & FFFFFFFF);
1156
static double ulp(double x)
1161
L= (word0(x) & Exp_mask) - (P - 1)*Exp_msk1;
1168
static double b2d(Bigint *a, int *e)
1170
ULong *xa, *xa0, w, y, z;
1183
d0= Exp_1 | y >> (Ebits - k);
1184
w= xa > xa0 ? *--xa : 0;
1185
d1= y << ((32-Ebits) + k) | w >> (Ebits - k);
1188
z= xa > xa0 ? *--xa : 0;
1191
d0= Exp_1 | y << k | z >> (32 - k);
1192
y= xa > xa0 ? *--xa : 0;
1193
d1= z << k | y >> (32 - k);
1207
static Bigint *d2b(double d, int *e, int *bits, Stack_alloc *alloc)
1216
b= Balloc(1, alloc);
1220
d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1221
if ((de= (int)(d0 >> Exp_shift)))
1225
if ((k= lo0bits(&y)))
1227
x[0]= y | z << (32 - k);
1232
i= b->wds= (x[1]= z) ? 2 : 1;
1243
*e= de - Bias - (P-1) + k;
1248
*e= de - Bias - (P-1) + 1 + k;
1249
*bits= 32*i - hi0bits(x[i-1]);
1257
static double ratio(Bigint *a, Bigint *b)
1262
dval(da)= b2d(a, &ka);
1263
dval(db)= b2d(b, &kb);
1264
k= ka - kb + 32*(a->wds - b->wds);
1266
word0(da)+= k*Exp_msk1;
1270
word0(db)+= k*Exp_msk1;
1272
return dval(da) / dval(db);
1275
static const double tens[] =
1277
1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1278
1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1282
static const double bigtens[]= { 1e16, 1e32, 1e64, 1e128, 1e256 };
1283
static const double tinytens[]=
1284
{ 1e-16, 1e-32, 1e-64, 1e-128,
1285
9007199254740992.*9007199254740992.e-256 /* = 2^106 * 1e-53 */
1288
The factor of 2^53 in tinytens[4] helps us avoid setting the underflow
1289
flag unnecessarily. It leads to a song and dance at the end of strtod.
1291
#define Scale_Bit 0x10
1295
strtod for IEEE--arithmetic machines.
1297
This strtod returns a nearest machine number to the input decimal
1298
string (or sets errno to EOVERFLOW). Ties are broken by the IEEE round-even
1301
Inspired loosely by William D. Clinger's paper "How to Read Floating
1302
Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
1306
1. We only require IEEE (not IEEE double-extended).
1307
2. We get by with floating-point arithmetic in a case that
1308
Clinger missed -- when we're computing d * 10^n
1309
for a small integer d and the integer n is not too
1310
much larger than 22 (the maximum integer k for which
1311
we can represent 10^k exactly), we may be able to
1312
compute (d*10^k) * 10^(e-k) with just one roundoff.
1313
3. Rather than a bit-at-a-time adjustment of the binary
1314
result in the hard case, we use floating-point
1315
arithmetic to determine the adjustment to within
1316
one bit; only in really hard cases do we need to
1317
compute a second residual.
1318
4. Because of 3., we don't need a large table of powers of 10
1319
for ten-to-e (just some small tables, e.g. of 10^k
1323
static double my_strtod_int(const char *s00, char **se, int *error, char *buf, size_t buf_size)
1326
int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1327
e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1328
const char *s, *s0, *s1, *end = *se;
1329
double aadj, aadj1, adj, rv, rv0;
1332
Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
1334
int inexact, oldinexact;
1336
#ifdef Honor_FLT_ROUNDS
1343
alloc.begin= alloc.free= buf;
1344
alloc.end= buf + buf_size;
1345
memset(alloc.freelist, 0, sizeof(alloc.freelist));
1349
for (s= s00; s < end; s++)
1374
while (++s < end && *s == '0') ;
1380
for (nd= nf= 0; s < end && (c= *s) >= '0' && c <= '9'; nd++, s++)
1386
if (s < end - 1 && c == '.')
1391
for (; s < end && c == '0'; c= *++s)
1393
if (s < end && c > '0' && c <= '9')
1402
for (; s < end && c >= '0' && c <= '9'; c = *++s)
1409
for (i= 1; i < nz; i++)
1412
else if (nd <= DBL_DIG + 1)
1416
else if (nd <= DBL_DIG + 1)
1424
if (s < end && (c == 'e' || c == 'E'))
1426
if (!nd && !nz && !nz0)
1437
if (s < end && c >= '0' && c <= '9')
1439
while (s < end && c == '0')
1441
if (s < end && c > '0' && c <= '9') {
1444
while (++s < end && (c= *s) >= '0' && c <= '9')
1446
if (s - s1 > 8 || L > 19999)
1447
/* Avoid confusion from exponents
1448
* so large that e might overflow.
1450
e= 19999; /* safe for 16 bit ints */
1475
Now we have nd0 digits, starting at s0, followed by a
1476
decimal point, followed by nd-nd0 digits. The number we're
1477
after is the integer represented by those digits times
1483
k= nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1489
oldinexact = get_inexact();
1491
dval(rv)= tens[k - 9] * dval(rv) + z;
1495
#ifndef Honor_FLT_ROUNDS
1506
#ifdef Honor_FLT_ROUNDS
1507
/* round correctly FLT_ROUNDS = 2 or 3 */
1514
/* rv = */ rounded_product(dval(rv), tens[e]);
1518
if (e <= Ten_pmax + i)
1521
A fancier test would sometimes let us do
1522
this for larger i values.
1524
#ifdef Honor_FLT_ROUNDS
1525
/* round correctly FLT_ROUNDS = 2 or 3 */
1534
/* rv = */ rounded_product(dval(rv), tens[e]);
1538
#ifndef Inaccurate_Divide
1539
else if (e >= -Ten_pmax)
1541
#ifdef Honor_FLT_ROUNDS
1542
/* round correctly FLT_ROUNDS = 2 or 3 */
1549
/* rv = */ rounded_quotient(dval(rv), tens[-e]);
1559
oldinexact= get_inexact();
1562
#ifdef Honor_FLT_ROUNDS
1563
if ((rounding= Flt_Rounds) >= 2)
1566
rounding= rounding == 2 ? 0 : 2;
1573
/* Get starting approximation = rv * 10**e1 */
1581
if (e1 > DBL_MAX_10_EXP)
1585
/* Can't trust HUGE_VAL */
1586
#ifdef Honor_FLT_ROUNDS
1589
case 0: /* toward 0 */
1590
case 3: /* toward -infinity */
1595
word0(rv)= Exp_mask;
1598
#else /*Honor_FLT_ROUNDS*/
1599
word0(rv)= Exp_mask;
1601
#endif /*Honor_FLT_ROUNDS*/
1603
/* set overflow bit */
1605
dval(rv0)*= dval(rv0);
1612
for(j= 0; e1 > 1; j++, e1>>= 1)
1614
dval(rv)*= bigtens[j];
1615
/* The last multiplication could overflow. */
1616
word0(rv)-= P*Exp_msk1;
1617
dval(rv)*= bigtens[j];
1618
if ((z= word0(rv) & Exp_mask) > Exp_msk1 * (DBL_MAX_EXP + Bias - P))
1620
if (z > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P))
1622
/* set to largest number (Can't trust DBL_MAX) */
1627
word0(rv)+= P*Exp_msk1;
1637
if (e1 >= 1 << n_bigtens)
1641
for(j= 0; e1 > 0; j++, e1>>= 1)
1643
dval(rv)*= tinytens[j];
1644
if (scale && (j = 2 * P + 1 - ((word0(rv) & Exp_mask) >> Exp_shift)) > 0)
1646
/* scaled rv is denormal; zap j low bits */
1651
word0(rv)= (P + 2) * Exp_msk1;
1653
word0(rv)&= 0xffffffff << (j - 32);
1656
word1(rv)&= 0xffffffff << j;
1669
/* Now the hard part -- adjusting rv to the correct value.*/
1671
/* Put digits into bd: true value = bd * 10^e */
1673
bd0= s2b(s0, nd0, nd, y, &alloc);
1677
bd= Balloc(bd0->k, &alloc);
1679
bb= d2b(dval(rv), &bbe, &bbbits, &alloc); /* rv = bb * 2^bbe */
1697
#ifdef Honor_FLT_ROUNDS
1702
i= j + bbbits - 1; /* logb(rv) */
1703
if (i < Emin) /* denormal */
1710
i= bb2 < bd2 ? bb2 : bd2;
1721
bs= pow5mult(bs, bb5, &alloc);
1722
bb1= mult(bs, bb, &alloc);
1727
bb= lshift(bb, bb2, &alloc);
1729
bd= pow5mult(bd, bd5, &alloc);
1731
bd= lshift(bd, bd2, &alloc);
1733
bs= lshift(bs, bs2, &alloc);
1734
delta= diff(bb, bd, &alloc);
1738
#ifdef Honor_FLT_ROUNDS
1743
/* Error is less than an ulp */
1744
if (!delta->x[0] && delta->wds <= 1)
1763
if (!word1(rv) && !(word0(rv) & Frac_mask))
1765
y= word0(rv) & Exp_mask;
1766
if (!scale || y > 2*P*Exp_msk1)
1768
delta= lshift(delta,Log2P);
1769
if (cmp(delta, bs) <= 0)
1774
if (scale && (y= word0(rv) & Exp_mask) <= 2 * P * Exp_msk1)
1775
word0(adj)+= (2 * P + 1) * Exp_msk1 - y;
1776
dval(rv)+= adj * ulp(dval(rv));
1780
adj= ratio(delta, bs);
1783
if (adj <= 0x7ffffffe)
1785
/* adj = rounding ? ceil(adj) : floor(adj); */
1789
if (!((rounding >> 1) ^ dsign))
1794
if (scale && (y= word0(rv) & Exp_mask) <= 2 * P * Exp_msk1)
1795
word0(adj)+= (2 * P + 1) * Exp_msk1 - y;
1796
adj*= ulp(dval(rv));
1803
#endif /*Honor_FLT_ROUNDS*/
1808
Error is less than half an ulp -- check for special case of mantissa
1811
if (dsign || word1(rv) || word0(rv) & Bndry_mask ||
1812
(word0(rv) & Exp_mask) <= (2 * P + 1) * Exp_msk1)
1815
if (!delta->x[0] && delta->wds <= 1)
1820
if (!delta->p.x[0] && delta->wds <= 1)
1828
delta= lshift(delta, Log2P, &alloc);
1829
if (cmp(delta, bs) > 0)
1835
/* exactly half-way between */
1838
if ((word0(rv) & Bndry_mask1) == Bndry_mask1 &&
1840
((scale && (y = word0(rv) & Exp_mask) <= 2 * P * Exp_msk1) ?
1841
(0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
1844
/*boundary case -- increment exponent*/
1845
word0(rv)= (word0(rv) & Exp_mask) + Exp_msk1;
1851
else if (!(word0(rv) & Bndry_mask) && !word1(rv))
1854
/* boundary case -- decrement exponent */
1857
L= word0(rv) & Exp_mask;
1858
if (L <= (2 *P + 1) * Exp_msk1)
1860
if (L > (P + 2) * Exp_msk1)
1861
/* round even ==> accept rv */
1863
/* rv = smallest denormal */
1867
L= (word0(rv) & Exp_mask) - Exp_msk1;
1868
word0(rv)= L | Bndry_mask1;
1869
word1(rv)= 0xffffffff;
1872
if (!(word1(rv) & LSB))
1875
dval(rv)+= ulp(dval(rv));
1878
dval(rv)-= ulp(dval(rv));
1885
if ((aadj= ratio(delta, bs)) <= 2.)
1889
else if (word1(rv) || word0(rv) & Bndry_mask)
1891
if (word1(rv) == Tiny1 && !word0(rv))
1898
/* special case -- power of FLT_RADIX to be rounded down... */
1899
if (aadj < 2. / FLT_RADIX)
1900
aadj= 1. / FLT_RADIX;
1909
aadj1= dsign ? aadj : -aadj;
1910
#ifdef Check_FLT_ROUNDS
1913
case 2: /* towards +infinity */
1916
case 0: /* towards 0 */
1917
case 3: /* towards -infinity */
1921
if (Flt_Rounds == 0)
1923
#endif /*Check_FLT_ROUNDS*/
1925
y= word0(rv) & Exp_mask;
1927
/* Check for overflow */
1929
if (y == Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
1931
dval(rv0)= dval(rv);
1932
word0(rv)-= P * Exp_msk1;
1933
adj= aadj1 * ulp(dval(rv));
1935
if ((word0(rv) & Exp_mask) >= Exp_msk1 * (DBL_MAX_EXP + Bias - P))
1937
if (word0(rv0) == Big0 && word1(rv0) == Big1)
1944
word0(rv)+= P * Exp_msk1;
1948
if (scale && y <= 2 * P * Exp_msk1)
1950
if (aadj <= 0x7fffffff)
1952
if ((z= (ULong) aadj) <= 0)
1955
aadj1= dsign ? aadj : -aadj;
1957
word0(aadj1)+= (2 * P + 1) * Exp_msk1 - y;
1959
adj = aadj1 * ulp(dval(rv));
1962
z= word0(rv) & Exp_mask;
1967
/* Can we stop now? */
1970
/* The tolerances below are conservative. */
1971
if (dsign || word1(rv) || word0(rv) & Bndry_mask)
1973
if (aadj < .4999999 || aadj > .5000001)
1976
else if (aadj < .4999999 / FLT_RADIX)
1984
Bfree(delta, &alloc);
1991
word0(rv0)= Exp_1 + (70 << Exp_shift);
1996
else if (!oldinexact)
2001
word0(rv0)= Exp_1 - 2 * P * Exp_msk1;
2003
dval(rv)*= dval(rv0);
2006
if (inexact && !(word0(rv) & Exp_mask))
2008
/* set underflow bit */
2010
dval(rv0)*= dval(rv0);
2018
Bfree(delta, &alloc);
2021
return sign ? -dval(rv) : dval(rv);
2025
static int quorem(Bigint *b, Bigint *S)
2028
ULong *bx, *bxe, q, *sx, *sxe;
2029
ULLong borrow, carry, y, ys;
2038
q= *bxe / (*sxe + 1); /* ensure q <= true quotient */
2045
ys= *sx++ * (ULLong)q + carry;
2047
y= *bx - (ys & FFFFFFFF) - borrow;
2048
borrow= y >> 32 & (ULong)1;
2049
*bx++= (ULong) (y & FFFFFFFF);
2055
while (--bxe > bx && !*bxe)
2071
y= *bx - (ys & FFFFFFFF) - borrow;
2072
borrow= y >> 32 & (ULong)1;
2073
*bx++= (ULong) (y & FFFFFFFF);
2080
while (--bxe > bx && !*bxe)
2090
dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2092
Inspired by "How to Print Floating-Point Numbers Accurately" by
2093
Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
2096
1. Rather than iterating, we use a simple numeric overestimate
2097
to determine k= floor(log10(d)). We scale relevant
2098
quantities using O(log2(k)) rather than O(k) multiplications.
2099
2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2100
try to generate digits strictly left to right. Instead, we
2101
compute with fewer bits and propagate the carry if necessary
2102
when rounding the final digit up. This is often faster.
2103
3. Under the assumption that input will be rounded nearest,
2104
mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2105
That is, we allow equality in stopping tests when the
2106
round-nearest rule will give the same floating-point value
2107
as would satisfaction of the stopping test with strict
2109
4. We remove common factors of powers of 2 from relevant
2111
5. When converting floating-point integers less than 1e16,
2112
we use floating-point arithmetic rather than resorting
2113
to multiple-precision integers.
2114
6. When asked to produce fewer than 15 digits, we first try
2115
to get by with floating-point arithmetic; we resort to
2116
multiple-precision integer arithmetic only if we cannot
2117
guarantee that the floating-point calculation has given
2118
the correctly rounded result. For k requested digits and
2119
"uniformly" distributed input, the probability is
2120
something like 10^(k-15) that we must resort to the Long
2124
static char *dtoa(double d, int mode, int ndigits, int *decpt, int *sign,
2125
char **rve, char *buf, size_t buf_size)
2128
Arguments ndigits, decpt, sign are similar to those
2129
of ecvt and fcvt; trailing zeros are suppressed from
2130
the returned string. If not null, *rve is set to point
2131
to the end of the return value. If d is +-Infinity or NaN,
2132
then *decpt is set to DTOA_OVERFLOW.
2135
0 ==> shortest string that yields d when read in
2136
and rounded to nearest.
2137
1 ==> like 0, but with Steele & White stopping rule;
2138
e.g. with IEEE P754 arithmetic , mode 0 gives
2139
1e23 whereas mode 1 gives 9.999999999999999e22.
2140
2 ==> max(1,ndigits) significant digits. This gives a
2141
return value similar to that of ecvt, except
2142
that trailing zeros are suppressed.
2143
3 ==> through ndigits past the decimal point. This
2144
gives a return value similar to that from fcvt,
2145
except that trailing zeros are suppressed, and
2146
ndigits can be negative.
2147
4,5 ==> similar to 2 and 3, respectively, but (in
2148
round-nearest mode) with the tests of mode 0 to
2149
possibly return a shorter string that rounds to d.
2150
With IEEE arithmetic and compilation with
2151
-DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
2152
as modes 2 and 3 when FLT_ROUNDS != 1.
2153
6-9 ==> Debugging modes similar to mode - 4: don't try
2154
fast floating-point estimate (if applicable).
2156
Values of mode other than 0-9 are treated as mode 0.
2158
Sufficient space is allocated to the return value
2159
to hold the suppressed trailing zeros.
2162
int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
2163
j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2164
spec_case, try_quick;
2168
Bigint *b, *b1, *delta, *mlo, *mhi, *S;
2171
#ifdef Honor_FLT_ROUNDS
2176
alloc.begin= alloc.free= buf;
2177
alloc.end= buf + buf_size;
2178
memset(alloc.freelist, 0, sizeof(alloc.freelist));
2180
if (word0(d) & Sign_bit)
2182
/* set sign for everything, including 0's and NaNs */
2184
word0(d) &= ~Sign_bit; /* clear sign bit */
2189
/* If infinity, set decpt to DTOA_OVERFLOW, if 0 set it to 1 */
2190
if (((word0(d) & Exp_mask) == Exp_mask && (*decpt= DTOA_OVERFLOW)) ||
2191
(!dval(d) && (*decpt= 1)))
2193
/* Infinity, NaN, 0 */
2194
char *res= (char*) dtoa_alloc(2, &alloc);
2202
#ifdef Honor_FLT_ROUNDS
2203
if ((rounding= Flt_Rounds) >= 2)
2206
rounding= rounding == 2 ? 0 : 2;
2213
b= d2b(dval(d), &be, &bbits, &alloc);
2214
if ((i= (int)(word0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))))
2217
word0(d2) &= Frac_mask1;
2218
word0(d2) |= Exp_11;
2221
log(x) ~=~ log(1.5) + (x-1.5)/1.5
2222
log10(x) = log(x) / log(10)
2223
~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2224
log10(d)= (i-Bias)*log(2)/log(10) + log10(d2)
2226
This suggests computing an approximation k to log10(d) by
2228
k= (i - Bias)*0.301029995663981
2229
+ ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2231
We want k to be too large rather than too small.
2232
The error in the first-order Taylor series approximation
2233
is in our favor, so we just round up the constant enough
2234
to compensate for any error in the multiplication of
2235
(i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2236
and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2237
adding 1e-13 to the constant term more than suffices.
2238
Hence we adjust the constant term to 0.1760912590558.
2239
(We could get a more accurate k by invoking log10,
2240
but this is probably not worthwhile.)
2248
/* d is denormalized */
2250
i= bbits + be + (Bias + (P-1) - 1);
2251
x= i > 32 ? word0(d) << (64 - i) | word1(d) >> (i - 32)
2252
: word1(d) << (32 - i);
2254
word0(d2)-= 31*Exp_msk1; /* adjust exponent */
2255
i-= (Bias + (P-1) - 1) + 1;
2258
ds= (dval(d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
2260
if (ds < 0. && ds != k)
2261
k--; /* want k= floor(ds) */
2263
if (k >= 0 && k <= Ten_pmax)
2265
if (dval(d) < tens[k])
2292
if (mode < 0 || mode > 9)
2295
#ifdef Check_FLT_ROUNDS
2296
try_quick= Rounding == 1;
2320
ilim= ilim1= i= ndigits;
2332
s= s0= dtoa_alloc(i, &alloc);
2334
#ifdef Honor_FLT_ROUNDS
2335
if (mode > 1 && rounding != 1)
2339
if (ilim >= 0 && ilim <= Quick_max && try_quick)
2341
/* Try to get by with floating-point arithmetic. */
2346
ieps= 2; /* conservative */
2353
/* prevent overflows */
2355
dval(d)/= bigtens[n_bigtens-1];
2358
for (; j; j>>= 1, i++)
2370
dval(d)*= tens[j1 & 0xf];
2371
for (j= j1 >> 4; j; j>>= 1, i++)
2376
dval(d)*= bigtens[i];
2380
if (k_check && dval(d) < 1. && ilim > 0)
2389
dval(eps)= ieps*dval(d) + 7.;
2390
word0(eps)-= (P-1)*Exp_msk1;
2395
if (dval(d) > dval(eps))
2397
if (dval(d) < -dval(eps))
2403
/* Use Steele & White method of only generating digits needed. */
2404
dval(eps)= 0.5/tens[ilim-1] - dval(eps);
2410
if (dval(d) < dval(eps))
2412
if (1. - dval(d) < dval(eps))
2422
/* Generate ilim digits, then fix them up. */
2423
dval(eps)*= tens[ilim-1];
2424
for (i= 1;; i++, dval(d)*= 10.)
2432
if (dval(d) > 0.5 + dval(eps))
2434
else if (dval(d) < 0.5 - dval(eps))
2436
while (*--s == '0');
2451
/* Do we have a "small" integer? */
2453
if (be >= 0 && k <= Int_max)
2457
if (ndigits < 0 && ilim <= 0)
2460
if (ilim < 0 || dval(d) <= 5*ds)
2464
for (i= 1;; i++, dval(d)*= 10.)
2466
L= (Long)(dval(d) / ds);
2468
#ifdef Check_FLT_ROUNDS
2469
/* If FLT_ROUNDS == 2, L will usually be high by 1 */
2483
#ifdef Honor_FLT_ROUNDS
2488
case 2: goto bump_up;
2493
if (dval(d) > ds || (dval(d) == ds && L & 1))
2516
i = denorm ? be + (Bias + (P-1) - 1 + 1) : 1 + P - bbits;
2519
mhi= i2b(1, &alloc);
2521
if (m2 > 0 && s2 > 0)
2523
i= m2 < s2 ? m2 : s2;
2534
mhi= pow5mult(mhi, m5, &alloc);
2535
b1= mult(mhi, b, &alloc);
2540
b= pow5mult(b, j, &alloc);
2543
b= pow5mult(b, b5, &alloc);
2547
S= pow5mult(S, s5, &alloc);
2549
/* Check for special case that d is a normalized power of 2. */
2552
if ((mode < 2 || leftright)
2553
#ifdef Honor_FLT_ROUNDS
2558
if (!word1(d) && !(word0(d) & Bndry_mask) &&
2559
word0(d) & (Exp_mask & ~Exp_msk1)
2562
/* The special case */
2570
Arrange for convenient computation of quotients:
2571
shift left if necessary so divisor has 4 leading 0 bits.
2573
Perhaps we should just compute leading 28 bits of S once
2574
a nd for all and pass them and a shift to quorem, so it
2575
can do shifts and ors to compute the numerator for q.
2577
if ((i= ((s5 ? 32 - hi0bits(S->p.x[S->wds-1]) : 1) + s2) & 0x1f))
2594
b= lshift(b, b2, &alloc);
2596
S= lshift(S, s2, &alloc);
2602
/* we botched the k estimate */
2603
b= multadd(b, 10, 0, &alloc);
2605
mhi= multadd(mhi, 10, 0, &alloc);
2609
if (ilim <= 0 && (mode == 3 || mode == 5))
2611
if (ilim < 0 || cmp(b,S= multadd(S,5,0, &alloc)) <= 0)
2613
/* no digits, fcvt style */
2626
mhi= lshift(mhi, m2, &alloc);
2629
Compute mlo -- check for special case that d is a normalized power of 2.
2635
mhi= Balloc(mhi->k, &alloc);
2637
mhi= lshift(mhi, Log2P, &alloc);
2642
dig= quorem(b,S) + '0';
2643
/* Do we yet have the shortest decimal string that will round to d? */
2645
delta= diff(S, mhi, &alloc);
2646
j1= delta->sign ? 1 : cmp(b, delta);
2647
Bfree(delta, &alloc);
2648
if (j1 == 0 && mode != 1 && !(word1(d) & 1)
2649
#ifdef Honor_FLT_ROUNDS
2661
if (j < 0 || (j == 0 && mode != 1 && !(word1(d) & 1)))
2663
if (!b->p.x[0] && b->wds <= 1)
2667
#ifdef Honor_FLT_ROUNDS
2670
case 0: goto accept_dig;
2671
case 2: goto keep_dig;
2673
#endif /*Honor_FLT_ROUNDS*/
2676
b= lshift(b, 1, &alloc);
2678
if ((j1 > 0 || (j1 == 0 && dig & 1))
2688
#ifdef Honor_FLT_ROUNDS
2693
{ /* possible if i == 1 */
2701
#ifdef Honor_FLT_ROUNDS
2707
b= multadd(b, 10, 0, &alloc);
2709
mlo= mhi= multadd(mhi, 10, 0, &alloc);
2712
mlo= multadd(mlo, 10, 0, &alloc);
2713
mhi= multadd(mhi, 10, 0, &alloc);
2720
*s++= dig= quorem(b,S) + '0';
2721
if (!b->p.x[0] && b->wds <= 1)
2727
b= multadd(b, 10, 0, &alloc);
2730
/* Round off last digit */
2732
#ifdef Honor_FLT_ROUNDS
2734
case 0: goto trimzeros;
2735
case 2: goto roundoff;
2738
b= lshift(b, 1, &alloc);
2740
if (j > 0 || (j == 0 && dig & 1))
2754
#ifdef Honor_FLT_ROUNDS
2757
while (*--s == '0');
2764
if (mlo && mlo != mhi)