1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtCore module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qlocale_tools_p.h"
43 #include "qlocale_p.h"
54 # include "qfunctions_wince.h" // for _control87
57 #if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
61 // Sizes as defined by the ISO C99 standard - fallback
63 # define LLONG_MAX Q_INT64_C(0x7fffffffffffffff)
66 # define LLONG_MIN (-LLONG_MAX - Q_INT64_C(1))
69 # define ULLONG_MAX Q_UINT64_C(0xffffffffffffffff)
74 #ifndef QT_QLOCALE_USES_FCVT
75 static char *_qdtoa( NEEDS_VOLATILE double d, int mode, int ndigits, int *decpt,
76 int *sign, char **rve, char **digits_str);
79 QString qulltoa(qulonglong l, int base, const QChar _zero)
81 ushort buff[65]; // length of MAX_ULLONG in base 2
82 ushort *p = buff + 65;
84 if (base != 10 || _zero.unicode() == '0') {
102 *(--p) = _zero.unicode() + c;
108 return QString(reinterpret_cast<QChar *>(p), 65 - (p - buff));
111 QString qlltoa(qlonglong l, int base, const QChar zero)
113 return qulltoa(l < 0 ? -l : l, base, zero);
116 QString &decimalForm(QChar zero, QChar decimal, QChar group,
117 QString &digits, int decpt, uint precision,
119 bool always_show_decpt,
120 bool thousands_group)
123 for (int i = 0; i < -decpt; ++i)
124 digits.prepend(zero);
127 else if (decpt > digits.length()) {
128 for (int i = digits.length(); i < decpt; ++i)
132 if (pm == PMDecimalDigits) {
133 uint decimal_digits = digits.length() - decpt;
134 for (uint i = decimal_digits; i < precision; ++i)
137 else if (pm == PMSignificantDigits) {
138 for (uint i = digits.length(); i < precision; ++i)
141 else { // pm == PMChopTrailingZeros
144 if (always_show_decpt || decpt < digits.length())
145 digits.insert(decpt, decimal);
147 if (thousands_group) {
148 for (int i = decpt - 3; i > 0; i -= 3)
149 digits.insert(i, group);
153 digits.prepend(zero);
158 QString &exponentForm(QChar zero, QChar decimal, QChar exponential,
159 QChar group, QChar plus, QChar minus,
160 QString &digits, int decpt, uint precision,
162 bool always_show_decpt)
166 if (pm == PMDecimalDigits) {
167 for (uint i = digits.length(); i < precision + 1; ++i)
170 else if (pm == PMSignificantDigits) {
171 for (uint i = digits.length(); i < precision; ++i)
174 else { // pm == PMChopTrailingZeros
177 if (always_show_decpt || digits.length() > 1)
178 digits.insert(1, decimal);
180 digits.append(exponential);
181 digits.append(QLocalePrivate::longLongToString(zero, group, plus, minus,
182 exp, 2, 10, -1, QLocalePrivate::AlwaysShowSign));
187 // Removes thousand-group separators in "C" locale.
188 bool removeGroupSeparators(QLocalePrivate::CharBuff *num)
190 int group_cnt = 0; // counts number of group chars
193 char *data = num->data();
194 int l = qstrlen(data);
196 // Find the decimal point and check if there are any group chars
202 if (i == 0 || data[i - 1] < '0' || data[i - 1] > '9')
204 if (i == l - 1 || data[i + 1] < '0' || data[i + 1] > '9')
209 // Fail if more than one decimal points
213 } else if (c == 'e' || c == 'E') {
214 // an 'e' or 'E' - if we have not encountered a decimal
215 // point, this is where it "is".
221 // If no group chars, we're done
225 // No decimal point means that it "is" at the end of the string
230 while (i < l && group_cnt > 0) {
234 // Don't allow group chars after the decimal point
238 // Check that it is placed correctly relative to the decpt
239 if ((decpt_idx - i) % 4 != 0)
243 memmove(data + i, data + i + 1, l - i - 1);
249 // Check that we are not missing a separator
251 && (decpt_idx - i) % 4 == 0
252 && !(i == 0 && c == '-')) // check for negative sign at start of string
262 * Copyright (c) 1992, 1993
263 * The Regents of the University of California. All rights reserved.
265 * Redistribution and use in source and binary forms, with or without
266 * modification, are permitted provided that the following conditions
268 * 1. Redistributions of source code must retain the above copyright
269 * notice, this list of conditions and the following disclaimer.
270 * 2. Redistributions in binary form must reproduce the above copyright
271 * notice, this list of conditions and the following disclaimer in the
272 * documentation and/or other materials provided with the distribution.
273 * 3. All advertising materials mentioning features or use of this software
274 * must display the following acknowledgment:
275 * This product includes software developed by the University of
276 * California, Berkeley and its contributors.
277 * 4. Neither the name of the University nor the names of its contributors
278 * may be used to endorse or promote products derived from this software
279 * without specific prior written permission.
281 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
282 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
283 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
284 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
285 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
286 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
287 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
288 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
290 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294 // static char sccsid[] = "@(#)strtouq.c 8.1 (Berkeley) 6/4/93";
295 // "$FreeBSD: src/lib/libc/stdlib/strtoull.c,v 1.5.2.1 2001/03/02 09:45:20 obrien Exp $";
298 * Convert a string to an unsigned long long integer.
300 * Ignores `locale' stuff. Assumes that the upper and lower case
301 * alphabets and digits are each contiguous.
303 qulonglong qstrtoull(const char *nptr, const char **endptr, register int base, bool *ok)
305 register const char *s = nptr;
306 register qulonglong acc;
307 register unsigned char c;
308 register qulonglong qbase, cutoff;
309 register int any, cutlim;
315 * See strtoq for comments as to the logic used.
320 } while (isspace(c));
331 if ((base == 0 || base == 16) &&
332 c == '0' && (*s == 'x' || *s == 'X')) {
338 base = c == '0' ? 8 : 10;
339 qbase = unsigned(base);
340 cutoff = qulonglong(ULLONG_MAX) / qbase;
341 cutlim = qulonglong(ULLONG_MAX) % qbase;
342 for (acc = 0, any = 0;; c = *s++) {
348 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
353 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
364 } else if (any < 0) {
370 *endptr = (any ? s - 1 : nptr);
375 // "$FreeBSD: src/lib/libc/stdlib/strtoll.c,v 1.5.2.1 2001/03/02 09:45:20 obrien Exp $";
379 * Convert a string to a long long integer.
381 * Ignores `locale' stuff. Assumes that the upper and lower case
382 * alphabets and digits are each contiguous.
384 qlonglong qstrtoll(const char *nptr, const char **endptr, register int base, bool *ok)
386 register const char *s;
387 register qulonglong acc;
388 register unsigned char c;
389 register qulonglong qbase, cutoff;
390 register int neg, any, cutlim;
393 * Skip white space and pick up leading +/- sign if any.
394 * If base is 0, allow 0x for hex and 0 for octal, else
395 * assume decimal; if base is already 16, allow 0x.
400 } while (isspace(c));
409 if ((base == 0 || base == 16) &&
410 c == '0' && (*s == 'x' || *s == 'X')) {
416 base = c == '0' ? 8 : 10;
419 * Compute the cutoff value between legal numbers and illegal
420 * numbers. That is the largest legal value, divided by the
421 * base. An input number that is greater than this value, if
422 * followed by a legal input character, is too big. One that
423 * is equal to this value may be valid or not; the limit
424 * between valid and invalid numbers is then based on the last
425 * digit. For instance, if the range for quads is
426 * [-9223372036854775808..9223372036854775807] and the input base
427 * is 10, cutoff will be set to 922337203685477580 and cutlim to
428 * either 7 (neg==0) or 8 (neg==1), meaning that if we have
429 * accumulated a value > 922337203685477580, or equal but the
430 * next digit is > 7 (or 8), the number is too big, and we will
431 * return a range error.
433 * Set any if any `digits' consumed; make it negative to indicate
436 qbase = unsigned(base);
437 cutoff = neg ? qulonglong(0-(LLONG_MIN + LLONG_MAX)) + LLONG_MAX : LLONG_MAX;
438 cutlim = cutoff % qbase;
440 for (acc = 0, any = 0;; c = *s++) {
446 c -= isupper(c) ? 'A' - 10 : 'a' - 10;
451 if (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim))
460 acc = neg ? LLONG_MIN : LLONG_MAX;
467 *endptr = (any >= 0 ? s - 1 : nptr);
475 #ifndef QT_QLOCALE_USES_FCVT
477 /* From: NetBSD: strtod.c,v 1.26 1998/02/03 18:44:21 perry Exp */
478 /* $FreeBSD: src/lib/libc/stdlib/netbsd_strtod.c,v 1.2.2.2 2001/03/02 17:14:15 tegge Exp $ */
480 /* Please send bug reports to
482 AT&T Bell Laboratories, Room 2C-463
484 Murray Hill, NJ 07974-2070
486 dmg@research.att.com or research!dmg
489 /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
491 * This strtod returns a nearest machine number to the input decimal
492 * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
493 * broken by the IEEE round-even rule. Otherwise ties are broken by
494 * biased rounding (add half and chop).
496 * Inspired loosely by William D. Clinger's paper "How to Read Floating
497 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
501 * 1. We only require IEEE, IBM, or VAX double-precision
502 * arithmetic (not IEEE double-extended).
503 * 2. We get by with floating-point arithmetic in a case that
504 * Clinger missed -- when we're computing d * 10^n
505 * for a small integer d and the integer n is not too
506 * much larger than 22 (the maximum integer k for which
507 * we can represent 10^k exactly), we may be able to
508 * compute (d*10^k) * 10^(e-k) with just one roundoff.
509 * 3. Rather than a bit-at-a-time adjustment of the binary
510 * result in the hard case, we use floating-point
511 * arithmetic to determine the adjustment to within
512 * one bit; only in really hard cases do we need to
513 * compute a second residual.
514 * 4. Because of 3., we don't need a large table of powers of 10
515 * for ten-to-e (just some small tables, e.g. of 10^k
520 * #define IEEE_LITTLE_ENDIAN for IEEE-arithmetic machines where the least
521 * significant byte has the lowest address.
522 * #define IEEE_BIG_ENDIAN for IEEE-arithmetic machines where the most
523 * significant byte has the lowest address.
524 * #define Long int on machines with 32-bit ints and 64-bit longs.
525 * #define Sudden_Underflow for IEEE-format machines without gradual
526 * underflow (i.e., that flush to zero on underflow).
527 * #define IBM for IBM mainframe-style floating-point arithmetic.
528 * #define VAX for VAX-style floating-point arithmetic.
529 * #define Unsigned_Shifts if >> does treats its left operand as unsigned.
530 * #define No_leftright to omit left-right logic in fast floating-point
531 * computation of dtoa.
532 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3.
533 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
534 * that use extended-precision instructions to compute rounded
535 * products and quotients) with IBM.
536 * #define ROUND_BIASED for IEEE-format with biased rounding.
537 * #define Inaccurate_Divide for IEEE-format with correctly rounded
538 * products but inaccurate quotients, e.g., for Intel i860.
539 * #define Just_16 to store 16 bits per 32-bit Long when doing high-precision
540 * integer arithmetic. Whether this speeds things up or slows things
541 * down depends on the machine and the number being converted.
542 * #define KR_headers for old-style C function headers.
543 * #define Bad_float_h if your system lacks a float.h or if it does not
544 * define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
545 * FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
546 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
547 * if memory is available and otherwise does something you deem
548 * appropriate. If MALLOC is undefined, malloc will be invoked
549 * directly -- and assumed always to succeed.
552 #if defined(LIBC_SCCS) && !defined(lint)
553 __RCSID("$NetBSD: strtod.c,v 1.26 1998/02/03 18:44:21 perry Exp $");
554 #endif /* LIBC_SCCS and not lint */
557 #if defined(__m68k__) || defined(__sparc__) || defined(__i386__) || \
558 defined(__mips__) || defined(__ns32k__) || defined(__alpha__) || \
559 defined(__powerpc__) || defined(Q_OS_WIN) || defined(Q_OS_DARWIN) || defined(Q_OS_MAC) || \
560 defined(mips) || defined(Q_OS_AIX) || defined(Q_OS_SOLARIS)
561 # define IEEE_BIG_OR_LITTLE_ENDIAN 1
565 // *All* of our architectures have IEEE arithmetic, don't they?
566 #define IEEE_BIG_OR_LITTLE_ENDIAN 1
570 * Although the CPU is little endian the FP has different
571 * byte and word endianness. The byte order is still little endian
572 * but the word order is big endian.
574 #define IEEE_BIG_OR_LITTLE_ENDIAN
582 #define ULong quint32
584 #define MALLOC malloc
586 #ifdef BSD_QDTOA_DEBUG
587 QT_BEGIN_INCLUDE_NAMESPACE
589 QT_END_INCLUDE_NAMESPACE
591 #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
594 #ifdef Unsigned_Shifts
595 #define Sign_Extend(a,b) if (b < 0) a |= 0xffff0000;
597 #define Sign_Extend(a,b) /*no-op*/
600 #if (defined(IEEE_BIG_OR_LITTLE_ENDIAN) + defined(VAX) + defined(IBM)) != 1
601 #error Exactly one of IEEE_BIG_OR_LITTLE_ENDIAN, VAX, or IBM should be defined.
604 static inline ULong getWord0(const NEEDS_VOLATILE double x)
606 const NEEDS_VOLATILE uchar *ptr = reinterpret_cast<const NEEDS_VOLATILE uchar *>(&x);
607 if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
608 return (ptr[0]<<24) + (ptr[1]<<16) + (ptr[2]<<8) + ptr[3];
610 return (ptr[7]<<24) + (ptr[6]<<16) + (ptr[5]<<8) + ptr[4];
614 static inline void setWord0(NEEDS_VOLATILE double *x, ULong l)
616 NEEDS_VOLATILE uchar *ptr = reinterpret_cast<NEEDS_VOLATILE uchar *>(x);
617 if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
618 ptr[0] = uchar(l>>24);
619 ptr[1] = uchar(l>>16);
620 ptr[2] = uchar(l>>8);
623 ptr[7] = uchar(l>>24);
624 ptr[6] = uchar(l>>16);
625 ptr[5] = uchar(l>>8);
630 static inline ULong getWord1(const NEEDS_VOLATILE double x)
632 const NEEDS_VOLATILE uchar *ptr = reinterpret_cast<const NEEDS_VOLATILE uchar *>(&x);
633 if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
634 return (ptr[4]<<24) + (ptr[5]<<16) + (ptr[6]<<8) + ptr[7];
636 return (ptr[3]<<24) + (ptr[2]<<16) + (ptr[1]<<8) + ptr[0];
639 static inline void setWord1(NEEDS_VOLATILE double *x, ULong l)
641 NEEDS_VOLATILE uchar *ptr = reinterpret_cast<uchar NEEDS_VOLATILE *>(x);
642 if (QSysInfo::ByteOrder == QSysInfo::BigEndian) {
643 ptr[4] = uchar(l>>24);
644 ptr[5] = uchar(l>>16);
645 ptr[6] = uchar(l>>8);
648 ptr[3] = uchar(l>>24);
649 ptr[2] = uchar(l>>16);
650 ptr[1] = uchar(l>>8);
655 static inline void Storeinc(ULong *&a, const ULong &b, const ULong &c)
658 *a = (ushort(b) << 16) | ushort(c);
662 /* #define P DBL_MANT_DIG */
663 /* Ten_pmax = floor(P*log(2)/log(5)) */
664 /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
665 /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
666 /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
668 #if defined(IEEE_BIG_OR_LITTLE_ENDIAN)
670 #define Exp_shift1 20
671 #define Exp_msk1 0x100000
672 #define Exp_msk11 0x100000
673 #define Exp_mask 0x7ff00000
678 #define Exp_1 0x3ff00000
679 #define Exp_11 0x3ff00000
681 #define Frac_mask 0xfffff
682 #define Frac_mask1 0xfffff
685 #define Bndry_mask 0xfffff
686 #define Bndry_mask1 0xfffff
687 #if defined(LSB) && defined(Q_OS_VXWORKS)
691 #define Sign_bit 0x80000000
697 #define Infinite(x) (getWord0(x) == 0x7ff00000) /* sufficient test for here */
699 #undef Sudden_Underflow
700 #define Sudden_Underflow
703 #define Exp_shift1 24
704 #define Exp_msk1 0x1000000
705 #define Exp_msk11 0x1000000
706 #define Exp_mask 0x7f000000
709 #define Exp_1 0x41000000
710 #define Exp_11 0x41000000
711 #define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
712 #define Frac_mask 0xffffff
713 #define Frac_mask1 0xffffff
716 #define Bndry_mask 0xefffff
717 #define Bndry_mask1 0xffffff
719 #define Sign_bit 0x80000000
721 #define Tiny0 0x100000
728 #define Exp_msk1 0x80
729 #define Exp_msk11 0x800000
730 #define Exp_mask 0x7f80
733 #define Exp_1 0x40800000
734 #define Exp_11 0x4080
736 #define Frac_mask 0x7fffff
737 #define Frac_mask1 0xffff007f
740 #define Bndry_mask 0xffff007f
741 #define Bndry_mask1 0xffff007f
743 #define Sign_bit 0x8000
757 #define rounded_product(a,b) a = rnd_prod(a, b)
758 #define rounded_quotient(a,b) a = rnd_quot(a, b)
759 extern double rnd_prod(double, double), rnd_quot(double, double);
761 #define rounded_product(a,b) a *= b
762 #define rounded_quotient(a,b) a /= b
765 #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
766 #define Big1 0xffffffff
769 /* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
770 * This makes some inner loops simpler and sometimes saves work
771 * during multiplications, but it often seems to make things slightly
772 * slower. Hence the default is now to store 32 bits per Long.
784 int k, maxwds, sign, wds;
788 typedef struct Bigint Bigint;
790 static Bigint *Balloc(int k)
796 rv = static_cast<Bigint *>(MALLOC(sizeof(Bigint) + (x-1)*sizeof(Long)));
800 rv->sign = rv->wds = 0;
804 static void Bfree(Bigint *v)
809 #define Bcopy(x,y) memcpy(reinterpret_cast<char *>(&x->sign), reinterpret_cast<char *>(&y->sign), \
810 y->wds*sizeof(Long) + 2*sizeof(int))
812 /* multiply by m and add a */
813 static Bigint *multadd(Bigint *b, int m, int a)
828 y = (xi & 0xffff) * m + a;
829 z = (xi >> 16) * m + (y >> 16);
831 *x++ = (z << 16) + (y & 0xffff);
840 if (wds >= b->maxwds) {
852 static Bigint *s2b(const char *s, int nd0, int nd, ULong y9)
859 for(k = 0, y = 1; x > y; y <<= 1, k++) ;
866 b->x[0] = y9 & 0xffff;
867 b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
873 do b = multadd(b, 10, *s++ - '0');
880 b = multadd(b, 10, *s++ - '0');
884 static int hi0bits(ULong x)
888 if (!(x & 0xffff0000)) {
892 if (!(x & 0xff000000)) {
896 if (!(x & 0xf0000000)) {
900 if (!(x & 0xc0000000)) {
904 if (!(x & 0x80000000)) {
906 if (!(x & 0x40000000))
912 static int lo0bits(ULong *y)
954 static Bigint *i2b(int i)
964 static Bigint *mult(Bigint *a, Bigint *b)
969 ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
974 if (a->wds < b->wds) {
986 for(x = c->x, xa = x + wc; x < xa; x++)
994 for(; xb < xbe; xb++, xc0++) {
995 if ((y = *xb & 0xffff) != 0) {
1000 z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
1002 z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
1004 Storeinc(xc, z2, z);
1009 if ((y = *xb >> 16) != 0) {
1015 z = (*x & 0xffff) * y + (*xc >> 16) + carry;
1017 Storeinc(xc, z, z2);
1018 z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
1026 for(; xb < xbe; xc0++) {
1032 z = *x++ * y + *xc + carry;
1041 for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
1053 Bigint *next = p5s->next;
1060 static Bigint *pow5mult(Bigint *b, int k)
1062 Bigint *b1, *p5, *p51;
1064 static const int p05[3] = { 5, 25, 125 };
1066 if ((i = k & 3) != 0)
1067 #if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
1069 // work around a bug on 64 bit IRIX gcc
1070 int *p = (int *) p05;
1071 b = multadd(b, p[i-1], 0);
1074 b = multadd(b, p05[i-1], 0);
1081 static p5s_deleter deleter;
1082 p5 = p5s = i2b(625);
1093 if (!(p51 = p5->next)) {
1094 p51 = p5->next = mult(p5,p5);
1102 static Bigint *lshift(Bigint *b, int k)
1106 ULong *x, *x1, *xe, z;
1114 n1 = n + b->wds + 1;
1115 for(i = b->maxwds; n1 > i; i <<= 1)
1119 for(i = 0; i < n; i++)
1128 *x1++ = *x << k | z;
1140 *x1++ = *x << k & 0xffff | z;
1156 static int cmp(Bigint *a, Bigint *b)
1158 ULong *xa, *xa0, *xb, *xb0;
1163 #ifdef BSD_QDTOA_DEBUG
1164 if (i > 1 && !a->x[i-1])
1165 Bug("cmp called with a->x[a->wds-1] == 0");
1166 if (j > 1 && !b->x[j-1])
1167 Bug("cmp called with b->x[b->wds-1] == 0");
1177 return *xa < *xb ? -1 : 1;
1184 static Bigint *diff(Bigint *a, Bigint *b)
1188 Long borrow, y; /* We need signed shifts here. */
1189 ULong *xa, *xae, *xb, *xbe, *xc;
1221 y = (*xa & 0xffff) - (*xb & 0xffff) + borrow;
1223 Sign_Extend(borrow, y);
1224 z = (*xa++ >> 16) - (*xb++ >> 16) + borrow;
1226 Sign_Extend(borrow, z);
1231 y = (*xa & 0xffff) + borrow;
1233 Sign_Extend(borrow, y);
1234 z = (*xa++ >> 16) + borrow;
1236 Sign_Extend(borrow, z);
1241 y = *xa++ - *xb++ + borrow;
1243 Sign_Extend(borrow, y);
1250 Sign_Extend(borrow, y);
1260 static double ulp(double x)
1265 L = (getWord0(x) & Exp_mask) - (P-1)*Exp_msk1;
1266 #ifndef Sudden_Underflow
1274 #ifndef Sudden_Underflow
1277 L = -L >> Exp_shift;
1278 if (L < Exp_shift) {
1279 setWord0(&a, 0x80000 >> L);
1285 setWord1(&a, L >= 31 ? 1U : 1U << (31 - L));
1292 static double b2d(Bigint *a, int *e)
1294 ULong *xa, *xa0, w, y, z;
1301 #ifdef BSD_QDTOA_DEBUG
1302 if (!y) Bug("zero y in b2d");
1308 setWord0(&d, Exp_1 | y >> (Ebits - k));
1309 w = xa > xa0 ? *--xa : 0;
1310 setWord1(&d, y << ((32-Ebits) + k) | w >> (Ebits - k));
1313 z = xa > xa0 ? *--xa : 0;
1315 setWord0(&d, Exp_1 | y << k | z >> (32 - k));
1316 y = xa > xa0 ? *--xa : 0;
1317 setWord1(&d, z << k | y >> (32 - k));
1320 setWord0(&d, Exp_1 | y);
1324 if (k < Ebits + 16) {
1325 z = xa > xa0 ? *--xa : 0;
1326 setWord0(&d, Exp_1 | y << k - Ebits | z >> Ebits + 16 - k);
1327 w = xa > xa0 ? *--xa : 0;
1328 y = xa > xa0 ? *--xa : 0;
1329 setWord1(&d, z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k);
1332 z = xa > xa0 ? *--xa : 0;
1333 w = xa > xa0 ? *--xa : 0;
1335 setWord0(&d, Exp_1 | y << k + 16 | z << k | w >> 16 - k);
1336 y = xa > xa0 ? *--xa : 0;
1337 setWord1(&d, w << k + 16 | y << k);
1343 static Bigint *d2b(double d, int *e, int *bits)
1356 z = getWord0(d) & Frac_mask;
1357 setWord0(&d, getWord0(d) & 0x7fffffff); /* clear sign bit, which we ignore */
1358 #ifdef Sudden_Underflow
1359 de = (int)(getWord0(d) >> Exp_shift);
1364 if ((de = int(getWord0(d) >> Exp_shift)) != 0)
1368 if ((y = getWord1(d)) != 0) {
1369 if ((k = lo0bits(&y)) != 0) {
1370 x[0] = y | z << (32 - k);
1375 i = b->wds = (x[1] = z) ? 2 : 1;
1378 #ifdef BSD_QDTOA_DEBUG
1380 Bug("Zero passed to d2b");
1388 if (y = getWord1(d)) {
1389 if (k = lo0bits(&y))
1391 x[0] = y | z << 32 - k & 0xffff;
1392 x[1] = z >> k - 16 & 0xffff;
1398 x[1] = y >> 16 | z << 16 - k & 0xffff;
1399 x[2] = z >> k & 0xffff;
1412 #ifdef BSD_QDTOA_DEBUG
1414 Bug("Zero passed to d2b");
1432 #ifndef Sudden_Underflow
1436 *e = (de - Bias - (P-1) << 2) + k;
1437 *bits = 4*P + 8 - k - hi0bits(getWord0(d) & Frac_mask);
1439 *e = de - Bias - (P-1) + k;
1442 #ifndef Sudden_Underflow
1445 *e = de - Bias - (P-1) + 1 + k;
1447 *bits = 32*i - hi0bits(x[i-1]);
1449 *bits = (i+2)*16 - hi0bits(x[i]);
1456 static double ratio(Bigint *a, Bigint *b)
1464 k = ka - kb + 32*(a->wds - b->wds);
1466 k = ka - kb + 16*(a->wds - b->wds);
1470 setWord0(&da, getWord0(da) + (k >> 2)*Exp_msk1);
1476 setWord0(&db, getWord0(db) + (k >> 2)*Exp_msk1);
1482 setWord0(&da, getWord0(da) + k*Exp_msk1);
1485 setWord0(&db, getWord0(db) + k*Exp_msk1);
1491 static const double tens[] = {
1492 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1493 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1501 static const double bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1502 static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128, 1e-256 };
1506 static const double bigtens[] = { 1e16, 1e32, 1e64 };
1507 static const double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1510 static const double bigtens[] = { 1e16, 1e32 };
1511 static const double tinytens[] = { 1e-16, 1e-32 };
1517 The pre-release gcc3.3 shipped with SuSE 8.2 has a bug which causes
1518 the comparison 1e-100 == 0.0 to return true. As a workaround, we
1519 compare it to a global variable containing 0.0, which produces
1520 correct assembler output.
1522 ### consider detecting the broken compilers and using the static
1523 ### double for these, and use a #define for all working compilers
1525 static double g_double_zero = 0.0;
1527 Q_CORE_EXPORT double qstrtod(const char *s00, const char **se, bool *ok)
1529 int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, dsign,
1530 e, e1, esign, i, j, k, nd, nd0, nf, nz, nz0, sign;
1531 const char *s, *s0, *s1;
1532 double aadj, aadj1, adj, rv, rv0;
1536 Bigint *bb = NULL, *bd = NULL, *bs = NULL, *delta = NULL;/* pacify gcc */
1540 const char decimal_point = localeconv()->decimal_point[0];
1542 const char decimal_point = '.';
1547 const char decimal_point = '.';
1549 sign = nz0 = nz = 0;
1553 for(s = s00; isspace(uchar(*s)); s++)
1559 } else if (*s == '+') {
1570 while(*++s == '0') ;
1576 for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
1582 if (c == decimal_point) {
1585 for(; c == '0'; c = *++s)
1587 if (c > '0' && c <= '9') {
1595 for(; c >= '0' && c <= '9'; c = *++s) {
1600 for(i = 1; i < nz; i++)
1603 else if (nd <= DBL_DIG + 1)
1607 else if (nd <= DBL_DIG + 1)
1615 if (c == 'e' || c == 'E') {
1616 if (!nd && !nz && !nz0) {
1628 if (c >= '0' && c <= '9') {
1631 if (c > '0' && c <= '9') {
1634 while((c = *++s) >= '0' && c <= '9')
1636 if (s - s1 > 8 || L > 19999)
1637 /* Avoid confusion from exponents
1638 * so large that e might overflow.
1640 e = 19999; /* safe for 16 bit ints */
1659 /* Now we have nd0 digits, starting at s0, followed by a
1660 * decimal point, followed by nd-nd0 digits. The number we're
1661 * after is the integer represented by those digits times
1666 k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1669 #if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
1671 // work around a bug on 64 bit IRIX gcc
1672 double *t = (double *) tens;
1673 rv = t[k - 9] * rv + z;
1676 rv = tens[k - 9] * rv + z;
1681 #ifndef RND_PRODQUOT
1688 if (e <= Ten_pmax) {
1690 goto vax_ovfl_check;
1692 /* rv = */ rounded_product(rv, tens[e]);
1697 if (e <= Ten_pmax + i) {
1698 /* A fancier test would sometimes let us do
1699 * this for larger i values.
1704 /* VAX exponent range is so narrow we must
1705 * worry about overflow here...
1708 setWord0(&rv, getWord0(rv) - P*Exp_msk1);
1709 /* rv = */ rounded_product(rv, tens[e]);
1710 if ((getWord0(rv) & Exp_mask)
1711 > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
1713 setWord0(&rv, getWord0(rv) + P*Exp_msk1);
1715 /* rv = */ rounded_product(rv, tens[e]);
1720 #ifndef Inaccurate_Divide
1721 else if (e >= -Ten_pmax) {
1722 /* rv = */ rounded_quotient(rv, tens[-e]);
1729 /* Get starting approximation = rv * 10**e1 */
1732 if ((i = e1 & 15) != 0)
1735 if (e1 > DBL_MAX_10_EXP) {
1743 /* Can't trust HUGE_VAL */
1745 setWord0(&rv, Exp_mask);
1748 setWord0(&rv, Big0);
1749 setWord1(&rv, Big1);
1757 for(j = 0; e1 > 1; j++, e1 >>= 1)
1760 /* The last multiplication could overflow. */
1761 setWord0(&rv, getWord0(rv) - P*Exp_msk1);
1763 if ((z = getWord0(rv) & Exp_mask)
1764 > Exp_msk1*(DBL_MAX_EXP+Bias-P))
1766 if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
1767 /* set to largest number */
1768 /* (Can't trust DBL_MAX) */
1769 setWord0(&rv, Big0);
1770 setWord1(&rv, Big1);
1773 setWord0(&rv, getWord0(rv) + P*Exp_msk1);
1780 if ((i = e1 & 15) != 0)
1784 if (e1 >= 1 << n_bigtens)
1786 for(j = 0; e1 > 1; j++, e1 >>= 1)
1789 /* The last multiplication could underflow. */
1792 if (rv == g_double_zero)
1796 if (rv == g_double_zero)
1807 setWord0(&rv, Tiny0);
1808 setWord1(&rv, Tiny1);
1809 /* The refinement below will clean
1810 * this approximation up.
1816 /* Now the hard part -- adjusting rv to the correct value.*/
1818 /* Put digits into bd: true value = bd * 10^e */
1820 bd0 = s2b(s0, nd0, nd, y);
1823 bd = Balloc(bd0->k);
1825 bb = d2b(rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
1841 #ifdef Sudden_Underflow
1843 j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
1848 i = bbe + bbbits - 1; /* logb(rv) */
1849 if (i < Emin) /* denormal */
1856 i = bb2 < bd2 ? bb2 : bd2;
1865 bs = pow5mult(bs, bb5);
1871 bb = lshift(bb, bb2);
1873 bd = pow5mult(bd, bd5);
1875 bd = lshift(bd, bd2);
1877 bs = lshift(bs, bs2);
1878 delta = diff(bb, bd);
1879 dsign = delta->sign;
1883 /* Error is less than half an ulp -- check for
1884 * special case of mantissa a power of two.
1886 if (dsign || getWord1(rv) || getWord0(rv) & Bndry_mask)
1888 delta = lshift(delta,Log2P);
1889 if (cmp(delta, bs) > 0)
1894 /* exactly half-way between */
1896 if ((getWord0(rv) & Bndry_mask1) == Bndry_mask1
1897 && getWord1(rv) == 0xffffffff) {
1898 /*boundary case -- increment exponent*/
1899 setWord0(&rv, (getWord0(rv) & Exp_mask)
1909 else if (!(getWord0(rv) & Bndry_mask) && !getWord1(rv)) {
1911 /* boundary case -- decrement exponent */
1912 #ifdef Sudden_Underflow
1913 L = getWord0(rv) & Exp_mask;
1922 L = (getWord0(rv) & Exp_mask) - Exp_msk1;
1924 setWord0(&rv, L | Bndry_mask1);
1925 setWord1(&rv, 0xffffffff);
1932 #ifndef ROUND_BIASED
1933 if (!(getWord1(rv) & LSB))
1938 #ifndef ROUND_BIASED
1941 #ifndef Sudden_Underflow
1942 if (rv == g_double_zero)
1949 if ((aadj = ratio(delta, bs)) <= 2.) {
1952 else if (getWord1(rv) || getWord0(rv) & Bndry_mask) {
1953 #ifndef Sudden_Underflow
1954 if (getWord1(rv) == Tiny1 && !getWord0(rv))
1961 /* special case -- power of FLT_RADIX to be */
1962 /* rounded down... */
1964 if (aadj < 2./FLT_RADIX)
1965 aadj = 1./FLT_RADIX;
1973 aadj1 = dsign ? aadj : -aadj;
1974 #ifdef Check_FLT_ROUNDS
1975 switch(FLT_ROUNDS) {
1976 case 2: /* towards +infinity */
1979 case 0: /* towards 0 */
1980 case 3: /* towards -infinity */
1984 if (FLT_ROUNDS == 0)
1988 y = getWord0(rv) & Exp_mask;
1990 /* Check for overflow */
1992 if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
1994 setWord0(&rv, getWord0(rv) - P*Exp_msk1);
1995 adj = aadj1 * ulp(rv);
1997 if ((getWord0(rv) & Exp_mask) >=
1998 Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
1999 if (getWord0(rv0) == Big0 && getWord1(rv0) == Big1)
2001 setWord0(&rv, Big0);
2002 setWord1(&rv, Big1);
2006 setWord0(&rv, getWord0(rv) + P*Exp_msk1);
2009 #ifdef Sudden_Underflow
2010 if ((getWord0(rv) & Exp_mask) <= P*Exp_msk1) {
2012 setWord0(&rv, getWord0(rv) + P*Exp_msk1);
2013 adj = aadj1 * ulp(rv);
2016 if ((getWord0(rv) & Exp_mask) < P*Exp_msk1)
2018 if ((getWord0(rv) & Exp_mask) <= P*Exp_msk1)
2021 if (getWord0(rv0) == Tiny0
2022 && getWord1(rv0) == Tiny1)
2024 setWord0(&rv, Tiny0);
2025 setWord1(&rv, Tiny1);
2029 setWord0(&rv, getWord0(rv) - P*Exp_msk1);
2032 adj = aadj1 * ulp(rv);
2036 /* Compute adj so that the IEEE rounding rules will
2037 * correctly round rv + adj in some half-way cases.
2038 * If rv * ulp(rv) is denormalized (i.e.,
2039 * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
2040 * trouble from bits lost to denormalization;
2041 * example: 1.2e-307 .
2043 if (y <= (P-1)*Exp_msk1 && aadj >= 1.) {
2044 aadj1 = int(aadj + 0.5);
2048 adj = aadj1 * ulp(rv);
2052 z = getWord0(rv) & Exp_mask;
2054 /* Can we stop now? */
2057 /* The tolerances below are conservative. */
2058 if (dsign || getWord1(rv) || getWord0(rv) & Bndry_mask) {
2059 if (aadj < .4999999 || aadj > .5000001)
2062 else if (aadj < .4999999/FLT_RADIX)
2080 return sign ? -rv : rv;
2083 static int quorem(Bigint *b, Bigint *S)
2088 ULong *bx, *bxe, *sx, *sxe;
2095 #ifdef BSD_QDTOA_DEBUG
2096 /*debug*/ if (b->wds > n)
2097 /*debug*/ Bug("oversize b in quorem");
2105 q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
2106 #ifdef BSD_QDTOA_DEBUG
2107 /*debug*/ if (q > 9)
2108 /*debug*/ Bug("oversized quotient in quorem");
2116 ys = (si & 0xffff) * q + carry;
2117 zs = (si >> 16) * q + (ys >> 16);
2119 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
2121 Sign_Extend(borrow, y);
2122 z = (*bx >> 16) - (zs & 0xffff) + borrow;
2124 Sign_Extend(borrow, z);
2127 ys = *sx++ * q + carry;
2129 y = *bx - (ys & 0xffff) + borrow;
2131 Sign_Extend(borrow, y);
2138 while(--bxe > bx && !*bxe)
2143 if (cmp(b, S) >= 0) {
2152 ys = (si & 0xffff) + carry;
2153 zs = (si >> 16) + (ys >> 16);
2155 y = (*bx & 0xffff) - (ys & 0xffff) + borrow;
2157 Sign_Extend(borrow, y);
2158 z = (*bx >> 16) - (zs & 0xffff) + borrow;
2160 Sign_Extend(borrow, z);
2165 y = *bx - (ys & 0xffff) + borrow;
2167 Sign_Extend(borrow, y);
2175 while(--bxe > bx && !*bxe)
2183 /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
2185 * Inspired by "How to Print Floating-Point Numbers Accurately" by
2186 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 92-101].
2189 * 1. Rather than iterating, we use a simple numeric overestimate
2190 * to determine k = floor(log10(d)). We scale relevant
2191 * quantities using O(log2(k)) rather than O(k) multiplications.
2192 * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
2193 * try to generate digits strictly left to right. Instead, we
2194 * compute with fewer bits and propagate the carry if necessary
2195 * when rounding the final digit up. This is often faster.
2196 * 3. Under the assumption that input will be rounded nearest,
2197 * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
2198 * That is, we allow equality in stopping tests when the
2199 * round-nearest rule will give the same floating-point value
2200 * as would satisfaction of the stopping test with strict
2202 * 4. We remove common factors of powers of 2 from relevant
2204 * 5. When converting floating-point integers less than 1e16,
2205 * we use floating-point arithmetic rather than resorting
2206 * to multiple-precision integers.
2207 * 6. When asked to produce fewer than 15 digits, we first try
2208 * to get by with floating-point arithmetic; we resort to
2209 * multiple-precision integer arithmetic only if we cannot
2210 * guarantee that the floating-point calculation has given
2211 * the correctly rounded result. For k requested digits and
2212 * "uniformly" distributed input, the probability is
2213 * something like 10^(k-15) that we must resort to the Long
2217 #if defined(Q_OS_WIN) && defined (Q_CC_GNU) && !defined(_clear87) // See QTBUG-7576
2219 __attribute__ ((dllimport)) unsigned int __cdecl __MINGW_NOTHROW _control87 (unsigned int unNew, unsigned int unMask);
2220 __attribute__ ((dllimport)) unsigned int __cdecl __MINGW_NOTHROW _clearfp (void); /* Clear the FPU status word */
2222 # define _clear87 _clearfp
2225 /* This actually sometimes returns a pointer to a string literal
2226 cast to a char*. Do NOT try to modify the return value. */
2228 Q_CORE_EXPORT char *qdtoa ( double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **resultp)
2230 // Some values of the floating-point control word can cause _qdtoa to crash with an underflow.
2231 // We set a safe value here.
2234 unsigned int oldbits = _control87(0, 0);
2237 # define MCW_EM _MCW_EM
2239 # define MCW_EM 0x0008001F
2242 _control87(MCW_EM, MCW_EM);
2245 #if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
2247 feholdexcept(&envp);
2250 char *s = _qdtoa(d, mode, ndigits, decpt, sign, rve, resultp);
2255 _control87(oldbits, 0xFFFFF);
2257 _control87(oldbits, _MCW_EM|_MCW_DN|_MCW_RC);
2261 #if defined(Q_OS_LINUX) && !defined(__UCLIBC__)
2268 static char *_qdtoa( NEEDS_VOLATILE double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **resultp)
2271 Arguments ndigits, decpt, sign are similar to those
2272 of ecvt and fcvt; trailing zeros are suppressed from
2273 the returned string. If not null, *rve is set to point
2274 to the end of the return value. If d is +-Infinity or NaN,
2275 then *decpt is set to 9999.
2278 0 ==> shortest string that yields d when read in
2279 and rounded to nearest.
2280 1 ==> like 0, but with Steele & White stopping rule;
2281 e.g. with IEEE P754 arithmetic , mode 0 gives
2282 1e23 whereas mode 1 gives 9.999999999999999e22.
2283 2 ==> max(1,ndigits) significant digits. This gives a
2284 return value similar to that of ecvt, except
2285 that trailing zeros are suppressed.
2286 3 ==> through ndigits past the decimal point. This
2287 gives a return value similar to that from fcvt,
2288 except that trailing zeros are suppressed, and
2289 ndigits can be negative.
2290 4-9 should give the same return values as 2-3, i.e.,
2291 4 <= mode <= 9 ==> same return as mode
2292 2 + (mode & 1). These modes are mainly for
2293 debugging; often they run slower but sometimes
2294 faster than modes 2-3.
2295 4,5,8,9 ==> left-to-right digit generation.
2296 6-9 ==> don't try fast floating-point estimate
2299 Values of mode other than 0-9 are treated as mode 0.
2301 Sufficient space is allocated to the return value
2302 to hold the suppressed trailing zeros.
2305 int bbits, b2, b5, be, dig, i, ieps, ilim0,
2306 j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
2308 int ilim = 0, ilim1 = 0, spec_case = 0; /* pacify gcc */
2310 #ifndef Sudden_Underflow
2314 Bigint *b, *b1, *delta, *mhi, *S;
2315 Bigint *mlo = NULL; /* pacify gcc */
2320 if (getWord0(d) & Sign_bit) {
2321 /* set sign for everything, including 0's and NaNs */
2323 setWord0(&d, getWord0(d) & ~Sign_bit); /* clear sign bit */
2328 #if defined(IEEE_Arith) + defined(VAX)
2330 if ((getWord0(d) & Exp_mask) == Exp_mask)
2332 if (getWord0(d) == 0x8000)
2335 /* Infinity or NaN */
2339 !getWord1(d) && !(getWord0(d) & 0xfffff) ? const_cast<char*>("Infinity") :
2341 const_cast<char*>("NaN");
2352 d += 0; /* normalize */
2354 if (d == g_double_zero)
2357 s = const_cast<char*>("0");
2363 b = d2b(d, &be, &bbits);
2364 #ifdef Sudden_Underflow
2365 i = (int)(getWord0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
2367 if ((i = int(getWord0(d) >> Exp_shift1 & (Exp_mask>>Exp_shift1))) != 0) {
2370 setWord0(&d2, getWord0(d2) & Frac_mask1);
2371 setWord0(&d2, getWord0(d2) | Exp_11);
2373 if (j = 11 - hi0bits(getWord0(d2) & Frac_mask))
2377 /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
2378 * log10(x) = log(x) / log(10)
2379 * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2380 * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
2382 * This suggests computing an approximation k to log10(d) by
2384 * k = (i - Bias)*0.301029995663981
2385 * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2387 * We want k to be too large rather than too small.
2388 * The error in the first-order Taylor series approximation
2389 * is in our favor, so we just round up the constant enough
2390 * to compensate for any error in the multiplication of
2391 * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2392 * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2393 * adding 1e-13 to the constant term more than suffices.
2394 * Hence we adjust the constant term to 0.1760912590558.
2395 * (We could get a more accurate k by invoking log10,
2396 * but this is probably not worthwhile.)
2404 #ifndef Sudden_Underflow
2408 /* d is denormalized */
2410 i = bbits + be + (Bias + (P-1) - 1);
2411 x = i > 32 ? getWord0(d) << (64 - i) | getWord1(d) >> (i - 32)
2412 : getWord1(d) << (32 - i);
2414 setWord0(&d2, getWord0(d2) - 31*Exp_msk1); /* adjust exponent */
2415 i -= (Bias + (P-1) - 1) + 1;
2419 ds = (d2-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
2421 if (ds < 0. && ds != k)
2422 k--; /* want k = floor(ds) */
2424 if (k >= 0 && k <= Ten_pmax) {
2448 if (mode < 0 || mode > 9)
2469 ilim = ilim1 = i = ndigits;
2475 i = ndigits + k + 1;
2482 *resultp = static_cast<char *>(malloc(i + 1));
2483 Q_CHECK_PTR(*resultp);
2490 if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2492 /* Try to get by with floating-point arithmetic. */
2498 ieps = 2; /* conservative */
2503 /* prevent overflows */
2505 d /= bigtens[n_bigtens-1];
2508 for(; j; j >>= 1, i++)
2515 else if ((j1 = -k) != 0) {
2516 d *= tens[j1 & 0xf];
2517 for(j = j1 >> 4; j; j >>= 1, i++)
2523 if (k_check && d < 1. && ilim > 0) {
2532 setWord0(&eps, getWord0(eps) - (P-1)*Exp_msk1);
2542 #ifndef No_leftright
2544 /* Use Steele & White method of only
2545 * generating digits needed.
2547 eps = 0.5/tens[ilim-1] - eps;
2551 *s++ = '0' + int(L);
2564 /* Generate ilim digits, then fix them up. */
2565 #if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
2566 // work around a bug on 64 bit IRIX gcc
2567 double *t = (double *) tens;
2570 eps *= tens[ilim-1];
2572 for(i = 1;; i++, d *= 10.) {
2575 *s++ = '0' + int(L);
2579 else if (d < 0.5 - eps) {
2580 while(*--s == '0') {}
2587 #ifndef No_leftright
2597 /* Do we have a "small" integer? */
2599 if (be >= 0 && k <= Int_max) {
2602 if (ndigits < 0 && ilim <= 0) {
2604 if (ilim < 0 || d <= 5*ds)
2611 #ifdef Check_FLT_ROUNDS
2612 /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2618 *s++ = '0' + int(L);
2621 if (d > ds || (d == ds && L & 1)) {
2633 if ((d *= 10.) == g_double_zero)
2645 #ifndef Sudden_Underflow
2646 denorm ? be + (Bias + (P-1) - 1 + 1) :
2649 1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
2663 if ((i = ilim) < 0) {
2672 if (m2 > 0 && s2 > 0) {
2673 i = m2 < s2 ? m2 : s2;
2681 mhi = pow5mult(mhi, m5);
2686 if ((j = b5 - m5) != 0)
2690 b = pow5mult(b, b5);
2694 S = pow5mult(S, s5);
2696 /* Check for special case that d is a normalized power of 2. */
2699 if (!getWord1(d) && !(getWord0(d) & Bndry_mask)
2700 #ifndef Sudden_Underflow
2701 && getWord0(d) & Exp_mask
2704 /* The special case */
2713 /* Arrange for convenient computation of quotients:
2714 * shift left if necessary so divisor has 4 leading 0 bits.
2716 * Perhaps we should just compute leading 28 bits of S once
2717 * and for all and pass them and a shift to quorem, so it
2718 * can do shifts and ors to compute the numerator for q.
2721 if ((i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0x1f) != 0)
2724 if (i = ((s5 ? 32 - hi0bits(S->x[S->wds-1]) : 1) + s2) & 0xf)
2746 b = multadd(b, 10, 0); /* we botched the k estimate */
2748 mhi = multadd(mhi, 10, 0);
2752 if (ilim <= 0 && mode > 2) {
2753 if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
2754 /* no digits, fcvt style */
2766 mhi = lshift(mhi, m2);
2768 /* Compute mlo -- check for special case
2769 * that d is a normalized power of 2.
2774 mhi = Balloc(mhi->k);
2776 mhi = lshift(mhi, Log2P);
2780 dig = quorem(b,S) + '0';
2781 /* Do we yet have the shortest decimal string
2782 * that will round to d?
2785 delta = diff(S, mhi);
2786 j1 = delta->sign ? 1 : cmp(b, delta);
2788 #ifndef ROUND_BIASED
2789 if (j1 == 0 && !mode && !(getWord1(d) & 1)) {
2798 if (j < 0 || (j == 0 && !mode
2799 #ifndef ROUND_BIASED
2800 && !(getWord1(d) & 1)
2806 if ((j1 > 0 || (j1 == 0 && dig & 1))
2814 if (dig == '9') { /* possible if i == 1 */
2825 b = multadd(b, 10, 0);
2827 mlo = mhi = multadd(mhi, 10, 0);
2829 mlo = multadd(mlo, 10, 0);
2830 mhi = multadd(mhi, 10, 0);
2836 *s++ = dig = quorem(b,S) + '0';
2839 b = multadd(b, 10, 0);
2842 /* Round off last digit */
2846 if (j > 0 || (j == 0 && dig & 1)) {
2857 while(*--s == '0') {}
2863 if (mlo && mlo != mhi)
2869 if (s == s0) { /* don't return empty string */
2884 Q_CORE_EXPORT char *qdtoa( double d, int mode, int ndigits, int *decpt, int *sign, char **rve, char **resultp)
2894 res = fcvt(d, ndigits, decpt, sign);
2896 res = ecvt(d, ndigits, decpt, sign);
2898 int n = qstrlen(res);
2899 if (mode == 0) { // remove trailing 0's
2900 const int stop = qMax(1, *decpt);
2902 for (i = n-1; i >= stop; --i) {
2908 *resultp = static_cast<char*>(malloc(n + 1));
2909 Q_CHECK_PTR(resultp);
2910 qstrncpy(*resultp, res, n + 1);
2914 Q_CORE_EXPORT double qstrtod(const char *s00, const char **se, bool *ok)
2916 double ret = strtod((char*)s00, (char**)se);
2918 if((ret == 0.0l && errno == ERANGE)
2919 || ret == HUGE_VAL || ret == -HUGE_VAL)
2922 *ok = true; // the result will be that we don't report underflow in this case
2927 #endif // QT_QLOCALE_USES_FCVT