Coverage Report

Created: 2026-10-28 06:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/php-src/Zend/zend_strtod.c
Line
Count
Source
1
/****************************************************************
2
 *
3
 * The author of this software is David M. Gay.
4
 *
5
 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
6
 *
7
 * Permission to use, copy, modify, and distribute this software for any
8
 * purpose without fee is hereby granted, provided that this entire notice
9
 * is included in all copies of any software which is or includes a copy
10
 * or modification of this software and in all copies of the supporting
11
 * documentation for such software.
12
 *
13
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
14
 * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
15
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
16
 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
17
 *
18
 ***************************************************************/
19
20
/* Please send bug reports to David M. Gay (dmg at acm dot org,
21
 * with " at " changed at "@" and " dot " changed to ".").  */
22
23
/* On a machine with IEEE extended-precision registers, it is
24
 * necessary to specify double-precision (53-bit) rounding precision
25
 * before invoking strtod or dtoa.  If the machine uses (the equivalent
26
 * of) Intel 80x87 arithmetic, the call
27
 *  _control87(PC_53, MCW_PC);
28
 * does this with many compilers.  Whether this or another call is
29
 * appropriate depends on the compiler; for this to work, it may be
30
 * necessary to #include "float.h" or another system-dependent header
31
 * file.
32
 */
33
34
/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
35
 * (Note that IEEE arithmetic is disabled by gcc's -ffast-math flag.)
36
 *
37
 * This strtod returns a nearest machine number to the input decimal
38
 * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
39
 * broken by the IEEE round-even rule.  Otherwise ties are broken by
40
 * biased rounding (add half and chop).
41
 *
42
 * Inspired loosely by William D. Clinger's paper "How to Read Floating
43
 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
44
 *
45
 * Modifications:
46
 *
47
 *  1. We only require IEEE, IBM, or VAX double-precision
48
 *    arithmetic (not IEEE double-extended).
49
 *  2. We get by with floating-point arithmetic in a case that
50
 *    Clinger missed -- when we're computing d * 10^n
51
 *    for a small integer d and the integer n is not too
52
 *    much larger than 22 (the maximum integer k for which
53
 *    we can represent 10^k exactly), we may be able to
54
 *    compute (d*10^k) * 10^(e-k) with just one roundoff.
55
 *  3. Rather than a bit-at-a-time adjustment of the binary
56
 *    result in the hard case, we use floating-point
57
 *    arithmetic to determine the adjustment to within
58
 *    one bit; only in really hard cases do we need to
59
 *    compute a second residual.
60
 *  4. Because of 3., we don't need a large table of powers of 10
61
 *    for ten-to-e (just some small tables, e.g. of 10^k
62
 *    for 0 <= k <= 22).
63
 */
64
65
/*
66
 * #define IEEE_8087 for IEEE-arithmetic machines where the least
67
 *  significant byte has the lowest address.
68
 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
69
 *  significant byte has the lowest address.
70
 * #define Long int on machines with 32-bit ints and 64-bit longs.
71
 * #define IBM for IBM mainframe-style floating-point arithmetic.
72
 * #define VAX for VAX-style floating-point arithmetic (D_floating).
73
 * #define No_leftright to omit left-right logic in fast floating-point
74
 *  computation of dtoa.  This will cause dtoa modes 4 and 5 to be
75
 *  treated the same as modes 2 and 3 for some inputs.
76
 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
77
 *  and strtod and dtoa should round accordingly.  Unless Trust_FLT_ROUNDS
78
 *  is also #defined, fegetround() will be queried for the rounding mode.
79
 *  Note that both FLT_ROUNDS and fegetround() are specified by the C99
80
 *  standard (and are specified to be consistent, with fesetround()
81
 *  affecting the value of FLT_ROUNDS), but that some (Linux) systems
82
 *  do not work correctly in this regard, so using fegetround() is more
83
 *  portable than using FLT_ROUNDS directly.
84
 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
85
 *  and Honor_FLT_ROUNDS is not #defined.
86
 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
87
 *  that use extended-precision instructions to compute rounded
88
 *  products and quotients) with IBM.
89
 * #define ROUND_BIASED for IEEE-format with biased rounding and arithmetic
90
 *  that rounds toward +Infinity.
91
 * #define ROUND_BIASED_without_Round_Up for IEEE-format with biased
92
 *  rounding when the underlying floating-point arithmetic uses
93
 *  unbiased rounding.  This prevent using ordinary floating-point
94
 *  arithmetic when the result could be computed with one rounding error.
95
 * #define Inaccurate_Divide for IEEE-format with correctly rounded
96
 *  products but inaccurate quotients, e.g., for Intel i860.
97
 * #define NO_LONG_LONG on machines that do not have a "long long"
98
 *  integer type (of >= 64 bits).  On such machines, you can
99
 *  #define Just_16 to store 16 bits per 32-bit Long when doing
100
 *  high-precision integer arithmetic.  Whether this speeds things
101
 *  up or slows things down depends on the machine and the number
102
 *  being converted.  If long long is available and the name is
103
 *  something other than "long long", #define Llong to be the name,
104
 *  and if "unsigned Llong" does not work as an unsigned version of
105
 *  Llong, #define #ULLong to be the corresponding unsigned type.
106
 * #define KR_headers for old-style C function headers.
107
 * #define Bad_float_h if your system lacks a float.h or if it does not
108
 *  define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
109
 *  FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
110
 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
111
 *  if memory is available and otherwise does something you deem
112
 *  appropriate.  If MALLOC is undefined, malloc will be invoked
113
 *  directly -- and assumed always to succeed.  Similarly, if you
114
 *  want something other than the system's free() to be called to
115
 *  recycle memory acquired from MALLOC, #define FREE to be the
116
 *  name of the alternate routine.  (FREE or free is only called in
117
 *  pathological cases, e.g., in a dtoa call after a dtoa return in
118
 *  mode 3 with thousands of digits requested.)
119
 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
120
 *  memory allocations from a private pool of memory when possible.
121
 *  When used, the private pool is PRIVATE_MEM bytes long:  2304 bytes,
122
 *  unless #defined to be a different length.  This default length
123
 *  suffices to get rid of MALLOC calls except for unusual cases,
124
 *  such as decimal-to-binary conversion of a very long string of
125
 *  digits.  The longest string dtoa can return is about 751 bytes
126
 *  long.  For conversions by strtod of strings of 800 digits and
127
 *  all dtoa conversions in single-threaded executions with 8-byte
128
 *  pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
129
 *  pointers, PRIVATE_MEM >= 7112 appears adequate.
130
 * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK
131
 *  #defined automatically on IEEE systems.  On such systems,
132
 *  when INFNAN_CHECK is #defined, strtod checks
133
 *  for Infinity and NaN (case insensitively).  On some systems
134
 *  (e.g., some HP systems), it may be necessary to #define NAN_WORD0
135
 *  appropriately -- to the most significant word of a quiet NaN.
136
 *  (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
137
 *  When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
138
 *  strtod also accepts (case insensitively) strings of the form
139
 *  NaN(x), where x is a string of hexadecimal digits and spaces;
140
 *  if there is only one string of hexadecimal digits, it is taken
141
 *  for the 52 fraction bits of the resulting NaN; if there are two
142
 *  or more strings of hex digits, the first is for the high 20 bits,
143
 *  the second and subsequent for the low 32 bits, with intervening
144
 *  white space ignored; but if this results in none of the 52
145
 *  fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
146
 *  and NAN_WORD1 are used instead.
147
 * #define MULTIPLE_THREADS if the system offers preemptively scheduled
148
 *  multiple threads.  In this case, you must provide (or suitably
149
 *  #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
150
 *  by FREE_DTOA_LOCK(n) for n = 0 or 1.  (The second lock, accessed
151
 *  in pow5mult, ensures lazy evaluation of only one copy of high
152
 *  powers of 5; omitting this lock would introduce a small
153
 *  probability of wasting memory, but would otherwise be harmless.)
154
 *  You must also invoke freedtoa(s) to free the value s returned by
155
 *  dtoa.  You may do so whether or not MULTIPLE_THREADS is #defined.
156
 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
157
 *  avoids underflows on inputs whose result does not underflow.
158
 *  If you #define NO_IEEE_Scale on a machine that uses IEEE-format
159
 *  floating-point numbers and flushes underflows to zero rather
160
 *  than implementing gradual underflow, then you must also #define
161
 *  Sudden_Underflow.
162
 * #define USE_LOCALE to use the current locale's decimal_point value.
163
 * #define SET_INEXACT if IEEE arithmetic is being used and extra
164
 *  computation should be done to set the inexact flag when the
165
 *  result is inexact and avoid setting inexact when the result
166
 *  is exact.  In this case, dtoa.c must be compiled in
167
 *  an environment, perhaps provided by #include "dtoa.c" in a
168
 *  suitable wrapper, that defines two functions,
169
 *    int get_inexact(void);
170
 *    void clear_inexact(void);
171
 *  such that get_inexact() returns a nonzero value if the
172
 *  inexact bit is already set, and clear_inexact() sets the
173
 *  inexact bit to 0.  When SET_INEXACT is #defined, strtod
174
 *  also does extra computations to set the underflow and overflow
175
 *  flags when appropriate (i.e., when the result is tiny and
176
 *  inexact or when it is a numeric value rounded to +-infinity).
177
 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
178
 *  the result overflows to +-Infinity or underflows to 0.
179
 * #define NO_HEX_FP to omit recognition of hexadecimal floating-point
180
 *  values by strtod.
181
 * #define NO_STRTOD_BIGCOMP (on IEEE-arithmetic systems only for now)
182
 *  to disable logic for "fast" testing of very long input strings
183
 *  to strtod.  This testing proceeds by initially truncating the
184
 *  input string, then if necessary comparing the whole string with
185
 *  a decimal expansion to decide close cases. This logic is only
186
 *  used for input more than STRTOD_DIGLIM digits long (default 40).
187
 */
188
189
#include <zend_operators.h>
190
#include <zend_strtod.h>
191
#include "zend_strtod_int.h"
192
#include "zend_globals.h"
193
194
#ifndef Long
195
4.38M
#define Long int32_t
196
#endif
197
#ifndef ULong
198
176M
#define ULong uint32_t
199
#endif
200
201
#undef Bigint
202
#undef freelist
203
#undef p5s
204
#undef dtoa_result
205
206
127M
#define Bigint      _zend_strtod_bigint
207
165M
#define freelist    (EG(strtod_state).freelist)
208
2.50M
#define p5s         (EG(strtod_state).p5s)
209
8.87M
#define dtoa_result (EG(strtod_state).result)
210
211
#ifdef DEBUG
212
static void Bug(const char *message) {
213
  fprintf(stderr, "%s\n", message);
214
}
215
#endif
216
217
#include "stdlib.h"
218
#include "string.h"
219
220
#ifdef USE_LOCALE
221
#include "locale.h"
222
#endif
223
224
#ifdef Honor_FLT_ROUNDS
225
#ifndef Trust_FLT_ROUNDS
226
#include <fenv.h>
227
#endif
228
#endif
229
230
#ifdef MALLOC
231
#ifdef KR_headers
232
extern char *MALLOC();
233
#else
234
extern void *MALLOC(size_t);
235
#endif
236
#else
237
263
#define MALLOC malloc
238
0
#define FREE   free
239
#endif
240
241
#ifndef Omit_Private_Memory
242
#ifndef PRIVATE_MEM
243
#define PRIVATE_MEM 2304
244
#endif
245
#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
246
static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
247
#endif
248
249
#undef IEEE_Arith
250
#undef Avoid_Underflow
251
#ifdef IEEE_MC68k
252
#define IEEE_Arith
253
#endif
254
#ifdef IEEE_8087
255
#define IEEE_Arith
256
#endif
257
258
#ifdef IEEE_Arith
259
#ifndef NO_INFNAN_CHECK
260
#undef INFNAN_CHECK
261
#define INFNAN_CHECK
262
#endif
263
#else
264
#undef INFNAN_CHECK
265
#define NO_STRTOD_BIGCOMP
266
#endif
267
268
#include "errno.h"
269
270
#ifdef Bad_float_h
271
272
#ifdef IEEE_Arith
273
#define DBL_DIG 15
274
#define DBL_MAX_10_EXP 308
275
#define DBL_MAX_EXP 1024
276
#define FLT_RADIX 2
277
#endif /*IEEE_Arith*/
278
279
#ifdef IBM
280
#define DBL_DIG 16
281
#define DBL_MAX_10_EXP 75
282
#define DBL_MAX_EXP 63
283
#define FLT_RADIX 16
284
#define DBL_MAX 7.2370055773322621e+75
285
#endif
286
287
#ifdef VAX
288
#define DBL_DIG 16
289
#define DBL_MAX_10_EXP 38
290
#define DBL_MAX_EXP 127
291
#define FLT_RADIX 2
292
#define DBL_MAX 1.7014118346046923e+38
293
#endif
294
295
#else /* ifndef Bad_float_h */
296
#include "float.h"
297
#endif /* Bad_float_h */
298
299
#ifndef __MATH_H__
300
#include "math.h"
301
#endif
302
303
#ifndef CONST
304
#ifdef KR_headers
305
#define CONST /* blank */
306
#else
307
1.12M
#define CONST const
308
#endif
309
#endif
310
311
#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
312
Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
313
#endif
314
315
typedef union { double d; ULong L[2]; } U;
316
317
#ifdef IEEE_8087
318
24.8M
#define word0(x) (x)->L[1]
319
9.35M
#define word1(x) (x)->L[0]
320
#else
321
#define word0(x) (x)->L[0]
322
#define word1(x) (x)->L[1]
323
#endif
324
36.4M
#define dval(x) (x)->d
325
326
#ifndef STRTOD_DIGLIM
327
666k
#define STRTOD_DIGLIM 40
328
#endif
329
330
#ifdef DIGLIM_DEBUG
331
extern int strtod_diglim;
332
#else
333
666k
#define strtod_diglim STRTOD_DIGLIM
334
#endif
335
336
/* The following definition of Storeinc is appropriate for MIPS processors.
337
 * An alternative that might be better on some machines is
338
 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
339
 */
340
#if defined(IEEE_8087) + defined(VAX) + defined(__arm__)
341
#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
342
((unsigned short *)a)[0] = (unsigned short)c, a++)
343
#else
344
#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
345
((unsigned short *)a)[1] = (unsigned short)c, a++)
346
#endif
347
348
/* #define P DBL_MANT_DIG */
349
/* Ten_pmax = floor(P*log(2)/log(5)) */
350
/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
351
/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
352
/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
353
354
#ifdef IEEE_Arith
355
3.28M
#define Exp_shift  20
356
4.15M
#define Exp_shift1 20
357
5.61M
#define Exp_msk1    0x100000
358
#define Exp_msk11   0x100000
359
8.20M
#define Exp_mask  0x7ff00000
360
11.1M
#define P 53
361
#define Nbits 53
362
5.84M
#define Bias 1023
363
#define Emax 1023
364
1.31M
#define Emin (-1022)
365
922k
#define Exp_1  0x3ff00000
366
2.03M
#define Exp_11 0x3ff00000
367
1.60M
#define Ebits 11
368
3.03M
#define Frac_mask  0xfffff
369
2.04M
#define Frac_mask1 0xfffff
370
2.26M
#define Ten_pmax 22
371
202k
#define Bletch 0x10
372
1.20M
#define Bndry_mask  0xfffff
373
30.9k
#define Bndry_mask1 0xfffff
374
894k
#define LSB 1
375
2.29M
#define Sign_bit 0x80000000
376
33.5k
#define Log2P 1
377
#define Tiny0 0
378
218k
#define Tiny1 1
379
2.37M
#define Quick_max 14
380
216k
#define Int_max 14
381
#ifndef NO_IEEE_Scale
382
#define Avoid_Underflow
383
#ifdef Flush_Denorm /* debugging option */
384
#undef Sudden_Underflow
385
#endif
386
#endif
387
388
#ifndef Flt_Rounds
389
#ifdef FLT_ROUNDS
390
645k
#define Flt_Rounds FLT_ROUNDS
391
#else
392
#define Flt_Rounds 1
393
#endif
394
#endif /*Flt_Rounds*/
395
396
#ifdef Honor_FLT_ROUNDS
397
#undef Check_FLT_ROUNDS
398
#define Check_FLT_ROUNDS
399
#else
400
#define Rounding Flt_Rounds
401
#endif
402
403
#else /* ifndef IEEE_Arith */
404
#undef Check_FLT_ROUNDS
405
#undef Honor_FLT_ROUNDS
406
#undef SET_INEXACT
407
#undef  Sudden_Underflow
408
#define Sudden_Underflow
409
#ifdef IBM
410
#undef Flt_Rounds
411
#define Flt_Rounds 0
412
#define Exp_shift  24
413
#define Exp_shift1 24
414
#define Exp_msk1   0x1000000
415
#define Exp_msk11  0x1000000
416
#define Exp_mask  0x7f000000
417
#define P 14
418
#define Nbits 56
419
#define Bias 65
420
#define Emax 248
421
#define Emin (-260)
422
#define Exp_1  0x41000000
423
#define Exp_11 0x41000000
424
#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
425
#define Frac_mask  0xffffff
426
#define Frac_mask1 0xffffff
427
#define Bletch 4
428
#define Ten_pmax 22
429
#define Bndry_mask  0xefffff
430
#define Bndry_mask1 0xffffff
431
#define LSB 1
432
#define Sign_bit 0x80000000
433
#define Log2P 4
434
#define Tiny0 0x100000
435
#define Tiny1 0
436
#define Quick_max 14
437
#define Int_max 15
438
#else /* VAX */
439
#undef Flt_Rounds
440
#define Flt_Rounds 1
441
#define Exp_shift  23
442
#define Exp_shift1 7
443
#define Exp_msk1    0x80
444
#define Exp_msk11   0x800000
445
#define Exp_mask  0x7f80
446
#define P 56
447
#define Nbits 56
448
#define Bias 129
449
#define Emax 126
450
#define Emin (-129)
451
#define Exp_1  0x40800000
452
#define Exp_11 0x4080
453
#define Ebits 8
454
#define Frac_mask  0x7fffff
455
#define Frac_mask1 0xffff007f
456
#define Ten_pmax 24
457
#define Bletch 2
458
#define Bndry_mask  0xffff007f
459
#define Bndry_mask1 0xffff007f
460
#define LSB 0x10000
461
#define Sign_bit 0x8000
462
#define Log2P 1
463
#define Tiny0 0x80
464
#define Tiny1 0
465
#define Quick_max 15
466
#define Int_max 15
467
#endif /* IBM, VAX */
468
#endif /* IEEE_Arith */
469
470
#ifndef IEEE_Arith
471
#define ROUND_BIASED
472
#else
473
#ifdef ROUND_BIASED_without_Round_Up
474
#undef  ROUND_BIASED
475
#define ROUND_BIASED
476
#endif
477
#endif
478
479
#ifdef RND_PRODQUOT
480
#define rounded_product(a,b) a = rnd_prod(a, b)
481
#define rounded_quotient(a,b) a = rnd_quot(a, b)
482
#ifdef KR_headers
483
extern double rnd_prod(), rnd_quot();
484
#else
485
extern double rnd_prod(double, double), rnd_quot(double, double);
486
#endif
487
#else
488
16.3k
#define rounded_product(a,b) a *= b
489
60.6k
#define rounded_quotient(a,b) a /= b
490
#endif
491
492
11.4k
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
493
5.31k
#define Big1 0xffffffff
494
495
#ifndef Pack_32
496
#define Pack_32
497
#endif
498
499
typedef struct BCinfo BCinfo;
500
 struct
501
BCinfo { int dp0, dp1, dplen, dsign, e0, inexact, nd, nd0, rounding, scale, uflchk; };
502
503
#ifdef KR_headers
504
#define FFFFFFFF ((((unsigned long)0xffff)<<16)|(unsigned long)0xffff)
505
#else
506
407M
#define FFFFFFFF 0xffffffffUL
507
#endif
508
509
#ifdef NO_LONG_LONG
510
#undef ULLong
511
#ifdef Just_16
512
#undef Pack_32
513
/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
514
 * This makes some inner loops simpler and sometimes saves work
515
 * during multiplications, but it often seems to make things slightly
516
 * slower.  Hence the default is now to store 32 bits per Long.
517
 */
518
#endif
519
#else /* long long available */
520
#ifndef Llong
521
#define Llong long long
522
#endif
523
#ifndef ULLong
524
81.4M
#define ULLong unsigned Llong
525
#endif
526
#endif /* NO_LONG_LONG */
527
528
#ifndef MULTIPLE_THREADS
529
#define ACQUIRE_DTOA_LOCK(n)  /*nothing*/
530
#define FREE_DTOA_LOCK(n) /*nothing*/
531
#endif
532
533
82.8M
#define Kmax ZEND_STRTOD_K_MAX
534
535
 struct
536
Bigint {
537
  struct Bigint *next;
538
  int k, maxwds, sign, wds;
539
  ULong x[1];
540
  };
541
542
 typedef struct Bigint Bigint;
543
544
#ifndef Bigint
545
 static Bigint *freelist[Kmax+1];
546
#endif
547
548
static void destroy_freelist(void);
549
static void free_p5s(void);
550
551
#ifdef MULTIPLE_THREADS
552
static MUTEX_T dtoa_mutex;
553
static MUTEX_T pow5mult_mutex;
554
#endif /* ZTS */
555
556
ZEND_API int zend_shutdown_strtod(void) /* {{{ */
557
0
{
558
0
  destroy_freelist();
559
0
  free_p5s();
560
561
0
  return 1;
562
0
}
563
/* }}} */
564
565
 static Bigint *
566
Balloc
567
#ifdef KR_headers
568
  (k) int k;
569
#else
570
  (int k)
571
#endif
572
41.4M
{
573
41.4M
  int x;
574
41.4M
  Bigint *rv;
575
#ifndef Omit_Private_Memory
576
  unsigned int len;
577
#endif
578
579
41.4M
  ACQUIRE_DTOA_LOCK(0);
580
  /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */
581
  /* but this case seems very unlikely. */
582
41.4M
  if (k <= Kmax && (rv = freelist[k]))
583
41.4M
    freelist[k] = rv->next;
584
263
  else {
585
263
    x = 1 << k;
586
263
#ifdef Omit_Private_Memory
587
263
    rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong));
588
263
    if (!rv) {
589
0
      FREE_DTOA_LOCK(0);
590
0
      zend_error_noreturn(E_ERROR, "Balloc() failed to allocate memory");
591
0
    }
592
#else
593
    len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
594
      /sizeof(double);
595
    if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) {
596
      rv = (Bigint*)pmem_next;
597
      pmem_next += len;
598
      }
599
    else
600
      rv = (Bigint*)MALLOC(len*sizeof(double));
601
      if (!rv) {
602
        FREE_DTOA_LOCK(0);
603
        zend_error_noreturn(E_ERROR, "Balloc() failed to allocate memory");
604
      }
605
#endif
606
263
    rv->k = k;
607
263
    rv->maxwds = x;
608
263
    }
609
41.4M
  FREE_DTOA_LOCK(0);
610
41.4M
  rv->sign = rv->wds = 0;
611
41.4M
  return rv;
612
41.4M
  }
613
614
 static void
615
Bfree
616
#ifdef KR_headers
617
  (v) Bigint *v;
618
#else
619
  (Bigint *v)
620
#endif
621
41.4M
{
622
41.4M
  if (v) {
623
41.4M
    if (v->k > Kmax)
624
0
      FREE((void*)v);
625
41.4M
    else {
626
41.4M
      ACQUIRE_DTOA_LOCK(0);
627
41.4M
      v->next = freelist[v->k];
628
41.4M
      freelist[v->k] = v;
629
41.4M
      FREE_DTOA_LOCK(0);
630
41.4M
      }
631
41.4M
    }
632
41.4M
  }
633
634
950k
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
635
950k
y->wds*sizeof(Long) + 2*sizeof(int))
636
637
 static Bigint *
638
multadd
639
#ifdef KR_headers
640
  (b, m, a) Bigint *b; int m, a;
641
#else
642
  (Bigint *b, int m, int a) /* multiply by m and add a */
643
#endif
644
39.2M
{
645
39.2M
  int i, wds;
646
39.2M
#ifdef ULLong
647
39.2M
  ULong *x;
648
39.2M
  ULLong carry, y;
649
#else
650
  ULong carry, *x, y;
651
#ifdef Pack_32
652
  ULong xi, z;
653
#endif
654
#endif
655
39.2M
  Bigint *b1;
656
657
39.2M
  wds = b->wds;
658
39.2M
  x = b->x;
659
39.2M
  i = 0;
660
39.2M
  carry = a;
661
127M
  do {
662
127M
#ifdef ULLong
663
127M
    y = *x * (ULLong)m + carry;
664
127M
    carry = y >> 32;
665
127M
    *x++ = y & FFFFFFFF;
666
#else
667
#ifdef Pack_32
668
    xi = *x;
669
    y = (xi & 0xffff) * m + carry;
670
    z = (xi >> 16) * m + (y >> 16);
671
    carry = z >> 16;
672
    *x++ = (z << 16) + (y & 0xffff);
673
#else
674
    y = *x * m + carry;
675
    carry = y >> 16;
676
    *x++ = y & 0xffff;
677
#endif
678
#endif
679
127M
    }
680
127M
    while(++i < wds);
681
39.2M
  if (carry) {
682
1.39M
    if (wds >= b->maxwds) {
683
48.8k
      b1 = Balloc(b->k+1);
684
48.8k
      Bcopy(b1, b);
685
48.8k
      Bfree(b);
686
48.8k
      b = b1;
687
48.8k
      }
688
1.39M
    b->x[wds++] = carry;
689
1.39M
    b->wds = wds;
690
1.39M
    }
691
39.2M
  return b;
692
39.2M
  }
693
694
 static Bigint *
695
s2b
696
#ifdef KR_headers
697
  (s, nd0, nd, y9, dplen) CONST char *s; int nd0, nd, dplen; ULong y9;
698
#else
699
  (const char *s, int nd0, int nd, ULong y9, int dplen)
700
#endif
701
666k
{
702
666k
  Bigint *b;
703
666k
  int i, k;
704
666k
  Long x, y;
705
706
666k
  x = (nd + 8) / 9;
707
1.29M
  for(k = 0, y = 1; x > y; y <<= 1, k++) ;
708
666k
#ifdef Pack_32
709
666k
  b = Balloc(k);
710
666k
  b->x[0] = y9;
711
666k
  b->wds = 1;
712
#else
713
  b = Balloc(k+1);
714
  b->x[0] = y9 & 0xffff;
715
  b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
716
#endif
717
718
666k
  i = 9;
719
666k
  if (9 < nd0) {
720
334k
    s += 9;
721
3.65M
    do b = multadd(b, 10, *s++ - '0');
722
3.65M
      while(++i < nd0);
723
334k
    s += dplen;
724
334k
    }
725
332k
  else
726
332k
    s += dplen + 9;
727
1.72M
  for(; i < nd; i++)
728
1.05M
    b = multadd(b, 10, *s++ - '0');
729
666k
  return b;
730
666k
  }
731
732
 static int
733
hi0bits
734
#ifdef KR_headers
735
  (x) ULong x;
736
#else
737
  (ULong x)
738
#endif
739
2.61M
{
740
2.61M
  int k = 0;
741
742
2.61M
  if (!(x & 0xffff0000)) {
743
2.13M
    k = 16;
744
2.13M
    x <<= 16;
745
2.13M
    }
746
2.61M
  if (!(x & 0xff000000)) {
747
755k
    k += 8;
748
755k
    x <<= 8;
749
755k
    }
750
2.61M
  if (!(x & 0xf0000000)) {
751
2.17M
    k += 4;
752
2.17M
    x <<= 4;
753
2.17M
    }
754
2.61M
  if (!(x & 0xc0000000)) {
755
1.19M
    k += 2;
756
1.19M
    x <<= 2;
757
1.19M
    }
758
2.61M
  if (!(x & 0x80000000)) {
759
774k
    k++;
760
774k
    if (!(x & 0x40000000))
761
0
      return 32;
762
774k
    }
763
2.61M
  return k;
764
2.61M
  }
765
766
 static int
767
lo0bits
768
#ifdef KR_headers
769
  (y) ULong *y;
770
#else
771
  (ULong *y)
772
#endif
773
3.03M
{
774
3.03M
  int k;
775
3.03M
  ULong x = *y;
776
777
3.03M
  if (x & 7) {
778
1.71M
    if (x & 1)
779
735k
      return 0;
780
980k
    if (x & 2) {
781
436k
      *y = x >> 1;
782
436k
      return 1;
783
436k
      }
784
544k
    *y = x >> 2;
785
544k
    return 2;
786
980k
    }
787
1.32M
  k = 0;
788
1.32M
  if (!(x & 0xffff)) {
789
271k
    k = 16;
790
271k
    x >>= 16;
791
271k
    }
792
1.32M
  if (!(x & 0xff)) {
793
414k
    k += 8;
794
414k
    x >>= 8;
795
414k
    }
796
1.32M
  if (!(x & 0xf)) {
797
680k
    k += 4;
798
680k
    x >>= 4;
799
680k
    }
800
1.32M
  if (!(x & 0x3)) {
801
654k
    k += 2;
802
654k
    x >>= 2;
803
654k
    }
804
1.32M
  if (!(x & 1)) {
805
726k
    k++;
806
726k
    x >>= 1;
807
726k
    if (!x)
808
0
      return 32;
809
726k
    }
810
1.32M
  *y = x;
811
1.32M
  return k;
812
1.32M
  }
813
814
 static Bigint *
815
i2b
816
#ifdef KR_headers
817
  (i) int i;
818
#else
819
  (int i)
820
#endif
821
4.43M
{
822
4.43M
  Bigint *b;
823
824
4.43M
  b = Balloc(1);
825
4.43M
  b->x[0] = i;
826
4.43M
  b->wds = 1;
827
4.43M
  return b;
828
4.43M
  }
829
830
 static Bigint *
831
mult
832
#ifdef KR_headers
833
  (a, b) Bigint *a, *b;
834
#else
835
  (Bigint *a, Bigint *b)
836
#endif
837
4.99M
{
838
4.99M
  Bigint *c;
839
4.99M
  int k, wa, wb, wc;
840
4.99M
  ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
841
4.99M
  ULong y;
842
4.99M
#ifdef ULLong
843
4.99M
  ULLong carry, z;
844
#else
845
  ULong carry, z;
846
#ifdef Pack_32
847
  ULong z2;
848
#endif
849
#endif
850
851
4.99M
  if (a->wds < b->wds) {
852
1.60M
    c = a;
853
1.60M
    a = b;
854
1.60M
    b = c;
855
1.60M
    }
856
4.99M
  k = a->k;
857
4.99M
  wa = a->wds;
858
4.99M
  wb = b->wds;
859
4.99M
  wc = wa + wb;
860
4.99M
  if (wc > a->maxwds)
861
1.00M
    k++;
862
4.99M
  c = Balloc(k);
863
41.5M
  for(x = c->x, xa = x + wc; x < xa; x++)
864
36.5M
    *x = 0;
865
4.99M
  xa = a->x;
866
4.99M
  xae = xa + wa;
867
4.99M
  xb = b->x;
868
4.99M
  xbe = xb + wb;
869
4.99M
  xc0 = c->x;
870
4.99M
#ifdef ULLong
871
13.2M
  for(; xb < xbe; xc0++) {
872
8.26M
    if ((y = *xb++)) {
873
8.26M
      x = xa;
874
8.26M
      xc = xc0;
875
8.26M
      carry = 0;
876
73.0M
      do {
877
73.0M
        z = *x++ * (ULLong)y + *xc + carry;
878
73.0M
        carry = z >> 32;
879
73.0M
        *xc++ = z & FFFFFFFF;
880
73.0M
        }
881
73.0M
        while(x < xae);
882
8.26M
      *xc = carry;
883
8.26M
      }
884
8.26M
    }
885
#else
886
#ifdef Pack_32
887
  for(; xb < xbe; xb++, xc0++) {
888
    if (y = *xb & 0xffff) {
889
      x = xa;
890
      xc = xc0;
891
      carry = 0;
892
      do {
893
        z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
894
        carry = z >> 16;
895
        z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
896
        carry = z2 >> 16;
897
        Storeinc(xc, z2, z);
898
        }
899
        while(x < xae);
900
      *xc = carry;
901
      }
902
    if (y = *xb >> 16) {
903
      x = xa;
904
      xc = xc0;
905
      carry = 0;
906
      z2 = *xc;
907
      do {
908
        z = (*x & 0xffff) * y + (*xc >> 16) + carry;
909
        carry = z >> 16;
910
        Storeinc(xc, z, z2);
911
        z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
912
        carry = z2 >> 16;
913
        }
914
        while(x < xae);
915
      *xc = z2;
916
      }
917
    }
918
#else
919
  for(; xb < xbe; xc0++) {
920
    if (y = *xb++) {
921
      x = xa;
922
      xc = xc0;
923
      carry = 0;
924
      do {
925
        z = *x++ * y + *xc + carry;
926
        carry = z >> 16;
927
        *xc++ = z & 0xffff;
928
        }
929
        while(x < xae);
930
      *xc = carry;
931
      }
932
    }
933
#endif
934
#endif
935
8.91M
  for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
936
4.99M
  c->wds = wc;
937
4.99M
  return c;
938
4.99M
  }
939
940
#ifndef p5s
941
 static Bigint *p5s;
942
#endif
943
944
 static Bigint *
945
pow5mult
946
#ifdef KR_headers
947
  (b, k) Bigint *b; int k;
948
#else
949
  (Bigint *b, int k)
950
#endif
951
2.61M
{
952
2.61M
  Bigint *b1, *p5, *p51;
953
2.61M
  int i;
954
2.61M
  static const int p05[3] = { 5, 25, 125 };
955
956
2.61M
  if ((i = k & 3))
957
1.70M
    b = multadd(b, p05[i-1], 0);
958
959
2.61M
  if (!(k >>= 2))
960
112k
    return b;
961
2.50M
  if (!(p5 = p5s)) {
962
    /* first time */
963
#ifdef MULTIPLE_THREADS
964
    ACQUIRE_DTOA_LOCK(1);
965
    if (!(p5 = p5s)) {
966
      p5 = p5s = i2b(625);
967
      p5->next = 0;
968
      }
969
    FREE_DTOA_LOCK(1);
970
#else
971
8
    p5 = p5s = i2b(625);
972
8
    p5->next = 0;
973
8
#endif
974
8
    }
975
7.11M
  for(;;) {
976
7.11M
    if (k & 1) {
977
4.29M
      b1 = mult(b, p5);
978
4.29M
      Bfree(b);
979
4.29M
      b = b1;
980
4.29M
      }
981
7.11M
    if (!(k >>= 1))
982
2.50M
      break;
983
4.61M
    if (!(p51 = p5->next)) {
984
#ifdef MULTIPLE_THREADS
985
      ACQUIRE_DTOA_LOCK(1);
986
      if (!(p51 = p5->next)) {
987
        p51 = p5->next = mult(p5,p5);
988
        p51->next = 0;
989
        }
990
      FREE_DTOA_LOCK(1);
991
#else
992
47
      p51 = p5->next = mult(p5,p5);
993
47
      p51->next = 0;
994
47
#endif
995
47
      }
996
4.61M
    p5 = p51;
997
4.61M
    }
998
2.50M
  return b;
999
2.61M
  }
1000
1001
 static Bigint *
1002
lshift
1003
#ifdef KR_headers
1004
  (b, k) Bigint *b; int k;
1005
#else
1006
  (Bigint *b, int k)
1007
#endif
1008
7.54M
{
1009
7.54M
  int i, k1, n, n1;
1010
7.54M
  Bigint *b1;
1011
7.54M
  ULong *x, *x1, *xe, z;
1012
1013
7.54M
#ifdef Pack_32
1014
7.54M
  n = k >> 5;
1015
#else
1016
  n = k >> 4;
1017
#endif
1018
7.54M
  k1 = b->k;
1019
7.54M
  n1 = n + b->wds + 1;
1020
14.7M
  for(i = b->maxwds; n1 > i; i <<= 1)
1021
7.19M
    k1++;
1022
7.54M
  b1 = Balloc(k1);
1023
7.54M
  x1 = b1->x;
1024
24.7M
  for(i = 0; i < n; i++)
1025
17.2M
    *x1++ = 0;
1026
7.54M
  x = b->x;
1027
7.54M
  xe = x + b->wds;
1028
7.54M
#ifdef Pack_32
1029
7.54M
  if (k &= 0x1f) {
1030
7.47M
    k1 = 32 - k;
1031
7.47M
    z = 0;
1032
23.9M
    do {
1033
23.9M
      *x1++ = *x << k | z;
1034
23.9M
      z = *x++ >> k1;
1035
23.9M
      }
1036
23.9M
      while(x < xe);
1037
7.47M
    if ((*x1 = z))
1038
506k
      ++n1;
1039
7.47M
    }
1040
#else
1041
  if (k &= 0xf) {
1042
    k1 = 16 - k;
1043
    z = 0;
1044
    do {
1045
      *x1++ = *x << k  & 0xffff | z;
1046
      z = *x++ >> k1;
1047
      }
1048
      while(x < xe);
1049
    if (*x1 = z)
1050
      ++n1;
1051
    }
1052
#endif
1053
69.4k
  else do
1054
194k
    *x1++ = *x++;
1055
194k
    while(x < xe);
1056
7.54M
  b1->wds = n1 - 1;
1057
7.54M
  Bfree(b);
1058
7.54M
  return b1;
1059
7.54M
  }
1060
1061
 static int
1062
cmp
1063
#ifdef KR_headers
1064
  (a, b) Bigint *a, *b;
1065
#else
1066
  (Bigint *a, Bigint *b)
1067
#endif
1068
71.4M
{
1069
71.4M
  ULong *xa, *xa0, *xb, *xb0;
1070
71.4M
  int i, j;
1071
1072
71.4M
  i = a->wds;
1073
71.4M
  j = b->wds;
1074
#ifdef DEBUG
1075
  if (i > 1 && !a->x[i-1])
1076
    Bug("cmp called with a->x[a->wds-1] == 0");
1077
  if (j > 1 && !b->x[j-1])
1078
    Bug("cmp called with b->x[b->wds-1] == 0");
1079
#endif
1080
71.4M
  if (i -= j)
1081
23.2M
    return i;
1082
48.2M
  xa0 = a->x;
1083
48.2M
  xa = xa0 + j;
1084
48.2M
  xb0 = b->x;
1085
48.2M
  xb = xb0 + j;
1086
51.1M
  for(;;) {
1087
51.1M
    if (*--xa != *--xb)
1088
47.6M
      return *xa < *xb ? -1 : 1;
1089
3.52M
    if (xa <= xa0)
1090
646k
      break;
1091
3.52M
    }
1092
646k
  return 0;
1093
48.2M
  }
1094
1095
 static Bigint *
1096
diff
1097
#ifdef KR_headers
1098
  (a, b) Bigint *a, *b;
1099
#else
1100
  (Bigint *a, Bigint *b)
1101
#endif
1102
17.5M
{
1103
17.5M
  Bigint *c;
1104
17.5M
  int i, wa, wb;
1105
17.5M
  ULong *xa, *xae, *xb, *xbe, *xc;
1106
17.5M
#ifdef ULLong
1107
17.5M
  ULLong borrow, y;
1108
#else
1109
  ULong borrow, y;
1110
#ifdef Pack_32
1111
  ULong z;
1112
#endif
1113
#endif
1114
1115
17.5M
  i = cmp(a,b);
1116
17.5M
  if (!i) {
1117
21.1k
    c = Balloc(0);
1118
21.1k
    c->wds = 1;
1119
21.1k
    c->x[0] = 0;
1120
21.1k
    return c;
1121
21.1k
    }
1122
17.5M
  if (i < 0) {
1123
599k
    c = a;
1124
599k
    a = b;
1125
599k
    b = c;
1126
599k
    i = 1;
1127
599k
    }
1128
16.9M
  else
1129
16.9M
    i = 0;
1130
17.5M
  c = Balloc(a->k);
1131
17.5M
  c->sign = i;
1132
17.5M
  wa = a->wds;
1133
17.5M
  xa = a->x;
1134
17.5M
  xae = xa + wa;
1135
17.5M
  wb = b->wds;
1136
17.5M
  xb = b->x;
1137
17.5M
  xbe = xb + wb;
1138
17.5M
  xc = c->x;
1139
17.5M
  borrow = 0;
1140
17.5M
#ifdef ULLong
1141
59.5M
  do {
1142
59.5M
    y = (ULLong)*xa++ - *xb++ - borrow;
1143
59.5M
    borrow = y >> 32 & (ULong)1;
1144
59.5M
    *xc++ = y & FFFFFFFF;
1145
59.5M
    }
1146
59.5M
    while(xb < xbe);
1147
29.1M
  while(xa < xae) {
1148
11.5M
    y = *xa++ - borrow;
1149
11.5M
    borrow = y >> 32 & (ULong)1;
1150
11.5M
    *xc++ = y & FFFFFFFF;
1151
11.5M
    }
1152
#else
1153
#ifdef Pack_32
1154
  do {
1155
    y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
1156
    borrow = (y & 0x10000) >> 16;
1157
    z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
1158
    borrow = (z & 0x10000) >> 16;
1159
    Storeinc(xc, z, y);
1160
    }
1161
    while(xb < xbe);
1162
  while(xa < xae) {
1163
    y = (*xa & 0xffff) - borrow;
1164
    borrow = (y & 0x10000) >> 16;
1165
    z = (*xa++ >> 16) - borrow;
1166
    borrow = (z & 0x10000) >> 16;
1167
    Storeinc(xc, z, y);
1168
    }
1169
#else
1170
  do {
1171
    y = *xa++ - *xb++ - borrow;
1172
    borrow = (y & 0x10000) >> 16;
1173
    *xc++ = y & 0xffff;
1174
    }
1175
    while(xb < xbe);
1176
  while(xa < xae) {
1177
    y = *xa++ - borrow;
1178
    borrow = (y & 0x10000) >> 16;
1179
    *xc++ = y & 0xffff;
1180
    }
1181
#endif
1182
#endif
1183
18.4M
  while(!*--xc)
1184
900k
    wa--;
1185
17.5M
  c->wds = wa;
1186
17.5M
  return c;
1187
17.5M
  }
1188
1189
 static double
1190
ulp
1191
#ifdef KR_headers
1192
  (x) U *x;
1193
#else
1194
  (U *x)
1195
#endif
1196
371k
{
1197
371k
  Long L;
1198
371k
  U u;
1199
1200
371k
  L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
1201
#ifndef Avoid_Underflow
1202
#ifndef Sudden_Underflow
1203
  if (L > 0) {
1204
#endif
1205
#endif
1206
#ifdef IBM
1207
    L |= Exp_msk1 >> 4;
1208
#endif
1209
371k
    word0(&u) = L;
1210
371k
    word1(&u) = 0;
1211
#ifndef Avoid_Underflow
1212
#ifndef Sudden_Underflow
1213
    }
1214
  else {
1215
    L = -L >> Exp_shift;
1216
    if (L < Exp_shift) {
1217
      word0(&u) = 0x80000 >> L;
1218
      word1(&u) = 0;
1219
      }
1220
    else {
1221
      word0(&u) = 0;
1222
      L -= Exp_shift;
1223
      word1(&u) = L >= 31 ? 1 : 1 << 31 - L;
1224
      }
1225
    }
1226
#endif
1227
#endif
1228
371k
  return dval(&u);
1229
371k
  }
1230
1231
 static double
1232
b2d
1233
#ifdef KR_headers
1234
  (a, e) Bigint *a; int *e;
1235
#else
1236
  (Bigint *a, int *e)
1237
#endif
1238
712k
{
1239
712k
  ULong *xa, *xa0, w, y, z;
1240
712k
  int k;
1241
712k
  U d;
1242
#ifdef VAX
1243
  ULong d0, d1;
1244
#else
1245
712k
#define d0 word0(&d)
1246
712k
#define d1 word1(&d)
1247
712k
#endif
1248
1249
712k
  xa0 = a->x;
1250
712k
  xa = xa0 + a->wds;
1251
712k
  y = *--xa;
1252
#ifdef DEBUG
1253
  if (!y) Bug("zero y in b2d");
1254
#endif
1255
712k
  k = hi0bits(y);
1256
712k
  *e = 32 - k;
1257
712k
#ifdef Pack_32
1258
712k
  if (k < Ebits) {
1259
88.1k
    d0 = Exp_1 | y >> (Ebits - k);
1260
88.1k
    w = xa > xa0 ? *--xa : 0;
1261
88.1k
    d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
1262
88.1k
    goto ret_d;
1263
88.1k
    }
1264
624k
  z = xa > xa0 ? *--xa : 0;
1265
624k
  if (k -= Ebits) {
1266
607k
    d0 = Exp_1 | y << k | z >> (32 - k);
1267
607k
    y = xa > xa0 ? *--xa : 0;
1268
607k
    d1 = z << k | y >> (32 - k);
1269
607k
    }
1270
17.3k
  else {
1271
17.3k
    d0 = Exp_1 | y;
1272
17.3k
    d1 = z;
1273
17.3k
    }
1274
#else
1275
  if (k < Ebits + 16) {
1276
    z = xa > xa0 ? *--xa : 0;
1277
    d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
1278
    w = xa > xa0 ? *--xa : 0;
1279
    y = xa > xa0 ? *--xa : 0;
1280
    d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
1281
    goto ret_d;
1282
    }
1283
  z = xa > xa0 ? *--xa : 0;
1284
  w = xa > xa0 ? *--xa : 0;
1285
  k -= Ebits + 16;
1286
  d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
1287
  y = xa > xa0 ? *--xa : 0;
1288
  d1 = w << k + 16 | y << k;
1289
#endif
1290
712k
 ret_d:
1291
#ifdef VAX
1292
  word0(&d) = d0 >> 16 | d0 << 16;
1293
  word1(&d) = d1 >> 16 | d1 << 16;
1294
#else
1295
712k
#undef d0
1296
712k
#undef d1
1297
712k
#endif
1298
712k
  return dval(&d);
1299
624k
  }
1300
1301
 static Bigint *
1302
d2b
1303
#ifdef KR_headers
1304
  (d, e, bits) U *d; int *e, *bits;
1305
#else
1306
  (U *d, int *e, int *bits)
1307
#endif
1308
3.03M
{
1309
3.03M
  Bigint *b;
1310
3.03M
  int de, k;
1311
3.03M
  ULong *x, y, z;
1312
3.03M
#ifndef Sudden_Underflow
1313
3.03M
  int i;
1314
3.03M
#endif
1315
#ifdef VAX
1316
  ULong d0, d1;
1317
  d0 = word0(d) >> 16 | word0(d) << 16;
1318
  d1 = word1(d) >> 16 | word1(d) << 16;
1319
#else
1320
9.11M
#define d0 word0(d)
1321
3.03M
#define d1 word1(d)
1322
3.03M
#endif
1323
1324
3.03M
#ifdef Pack_32
1325
3.03M
  b = Balloc(1);
1326
#else
1327
  b = Balloc(2);
1328
#endif
1329
3.03M
  x = b->x;
1330
1331
3.03M
  z = d0 & Frac_mask;
1332
3.03M
  d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1333
#ifdef Sudden_Underflow
1334
  de = (int)(d0 >> Exp_shift);
1335
#ifndef IBM
1336
  z |= Exp_msk11;
1337
#endif
1338
#else
1339
3.03M
  if ((de = (int)(d0 >> Exp_shift)))
1340
2.99M
    z |= Exp_msk1;
1341
3.03M
#endif
1342
3.03M
#ifdef Pack_32
1343
3.03M
  if ((y = d1)) {
1344
2.01M
    if ((k = lo0bits(&y))) {
1345
1.28M
      x[0] = y | z << (32 - k);
1346
1.28M
      z >>= k;
1347
1.28M
      }
1348
725k
    else
1349
725k
      x[0] = y;
1350
2.01M
#ifndef Sudden_Underflow
1351
2.01M
    i =
1352
2.01M
#endif
1353
2.01M
        b->wds = (x[1] = z) ? 2 : 1;
1354
2.01M
    }
1355
1.02M
  else {
1356
1.02M
    k = lo0bits(&z);
1357
1.02M
    x[0] = z;
1358
1.02M
#ifndef Sudden_Underflow
1359
1.02M
    i =
1360
1.02M
#endif
1361
1.02M
        b->wds = 1;
1362
1.02M
    k += 32;
1363
1.02M
    }
1364
#else
1365
  if (y = d1) {
1366
    if (k = lo0bits(&y))
1367
      if (k >= 16) {
1368
        x[0] = y | z << 32 - k & 0xffff;
1369
        x[1] = z >> k - 16 & 0xffff;
1370
        x[2] = z >> k;
1371
        i = 2;
1372
        }
1373
      else {
1374
        x[0] = y & 0xffff;
1375
        x[1] = y >> 16 | z << 16 - k & 0xffff;
1376
        x[2] = z >> k & 0xffff;
1377
        x[3] = z >> k+16;
1378
        i = 3;
1379
        }
1380
    else {
1381
      x[0] = y & 0xffff;
1382
      x[1] = y >> 16;
1383
      x[2] = z & 0xffff;
1384
      x[3] = z >> 16;
1385
      i = 3;
1386
      }
1387
    }
1388
  else {
1389
#ifdef DEBUG
1390
    if (!z)
1391
      Bug("Zero passed to d2b");
1392
#endif
1393
    k = lo0bits(&z);
1394
    if (k >= 16) {
1395
      x[0] = z;
1396
      i = 0;
1397
      }
1398
    else {
1399
      x[0] = z & 0xffff;
1400
      x[1] = z >> 16;
1401
      i = 1;
1402
      }
1403
    k += 32;
1404
    }
1405
  while(!x[i])
1406
    --i;
1407
  b->wds = i + 1;
1408
#endif
1409
3.03M
#ifndef Sudden_Underflow
1410
3.03M
  if (de) {
1411
2.99M
#endif
1412
#ifdef IBM
1413
    *e = (de - Bias - (P-1) << 2) + k;
1414
    *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
1415
#else
1416
2.99M
    *e = de - Bias - (P-1) + k;
1417
2.99M
    *bits = P - k;
1418
2.99M
#endif
1419
2.99M
#ifndef Sudden_Underflow
1420
2.99M
    }
1421
42.9k
  else {
1422
42.9k
    *e = de - Bias - (P-1) + 1 + k;
1423
42.9k
#ifdef Pack_32
1424
42.9k
    *bits = 32*i - hi0bits(x[i-1]);
1425
#else
1426
    *bits = (i+2)*16 - hi0bits(x[i]);
1427
#endif
1428
42.9k
    }
1429
3.03M
#endif
1430
3.03M
  return b;
1431
3.03M
  }
1432
#undef d0
1433
#undef d1
1434
1435
 static double
1436
ratio
1437
#ifdef KR_headers
1438
  (a, b) Bigint *a, *b;
1439
#else
1440
  (Bigint *a, Bigint *b)
1441
#endif
1442
356k
{
1443
356k
  U da, db;
1444
356k
  int k, ka, kb;
1445
1446
356k
  dval(&da) = b2d(a, &ka);
1447
356k
  dval(&db) = b2d(b, &kb);
1448
356k
#ifdef Pack_32
1449
356k
  k = ka - kb + 32*(a->wds - b->wds);
1450
#else
1451
  k = ka - kb + 16*(a->wds - b->wds);
1452
#endif
1453
#ifdef IBM
1454
  if (k > 0) {
1455
    word0(&da) += (k >> 2)*Exp_msk1;
1456
    if (k &= 3)
1457
      dval(&da) *= 1 << k;
1458
    }
1459
  else {
1460
    k = -k;
1461
    word0(&db) += (k >> 2)*Exp_msk1;
1462
    if (k &= 3)
1463
      dval(&db) *= 1 << k;
1464
    }
1465
#else
1466
356k
  if (k > 0)
1467
223k
    word0(&da) += k*Exp_msk1;
1468
132k
  else {
1469
132k
    k = -k;
1470
132k
    word0(&db) += k*Exp_msk1;
1471
132k
    }
1472
356k
#endif
1473
356k
  return dval(&da) / dval(&db);
1474
356k
  }
1475
1476
 static CONST double
1477
tens[] = {
1478
    1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
1479
    1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
1480
    1e20, 1e21, 1e22
1481
#ifdef VAX
1482
    , 1e23, 1e24
1483
#endif
1484
    };
1485
1486
 static CONST double
1487
#ifdef IEEE_Arith
1488
bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
1489
static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
1490
#ifdef Avoid_Underflow
1491
    9007199254740992.*9007199254740992.e-256
1492
    /* = 2^106 * 1e-256 */
1493
#else
1494
    1e-256
1495
#endif
1496
    };
1497
/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
1498
/* flag unnecessarily.  It leads to a song and dance at the end of strtod. */
1499
324k
#define Scale_Bit 0x10
1500
330k
#define n_bigtens 5
1501
#else
1502
#ifdef IBM
1503
bigtens[] = { 1e16, 1e32, 1e64 };
1504
static CONST double tinytens[] = { 1e-16, 1e-32, 1e-64 };
1505
#define n_bigtens 3
1506
#else
1507
bigtens[] = { 1e16, 1e32 };
1508
static CONST double tinytens[] = { 1e-16, 1e-32 };
1509
#define n_bigtens 2
1510
#endif
1511
#endif
1512
1513
#undef Need_Hexdig
1514
#ifdef INFNAN_CHECK
1515
#ifndef No_Hex_NaN
1516
#define Need_Hexdig
1517
#endif
1518
#endif
1519
1520
#ifndef Need_Hexdig
1521
#ifndef NO_HEX_FP
1522
#define Need_Hexdig
1523
#endif
1524
#endif
1525
1526
#ifdef Need_Hexdig /*{*/
1527
#if 0
1528
static unsigned char hexdig[256];
1529
1530
 static void
1531
htinit(unsigned char *h, unsigned char *s, int inc)
1532
{
1533
  int i, j;
1534
  for(i = 0; (j = s[i]) !=0; i++)
1535
    h[j] = i + inc;
1536
  }
1537
1538
 static void
1539
hexdig_init(void) /* Use of hexdig_init omitted 20121220 to avoid a */
1540
      /* race condition when multiple threads are used. */
1541
{
1542
#define USC (unsigned char *)
1543
  htinit(hexdig, USC "0123456789", 0x10);
1544
  htinit(hexdig, USC "abcdef", 0x10 + 10);
1545
  htinit(hexdig, USC "ABCDEF", 0x10 + 10);
1546
  }
1547
#else
1548
static const unsigned char hexdig[256] = {
1549
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1550
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1551
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1552
  16,17,18,19,20,21,22,23,24,25,0,0,0,0,0,0,
1553
  0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
1554
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1555
  0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
1556
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1557
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1558
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1559
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1560
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1561
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1562
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1563
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1564
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1565
  };
1566
#endif
1567
#endif /* } Need_Hexdig */
1568
1569
#ifdef INFNAN_CHECK
1570
1571
#ifndef NAN_WORD0
1572
#define NAN_WORD0 0x7ff80000
1573
#endif
1574
1575
#ifndef NAN_WORD1
1576
#define NAN_WORD1 0
1577
#endif
1578
1579
 static int
1580
match
1581
#ifdef KR_headers
1582
  (sp, t) char **sp, *t;
1583
#else
1584
  (const char **sp, const char *t)
1585
#endif
1586
{
1587
  int c, d;
1588
  CONST char *s = *sp;
1589
1590
  while((d = *t++)) {
1591
    if ((c = *++s) >= 'A' && c <= 'Z')
1592
      c += 'a' - 'A';
1593
    if (c != d)
1594
      return 0;
1595
    }
1596
  *sp = s + 1;
1597
  return 1;
1598
  }
1599
1600
#ifndef No_Hex_NaN
1601
 static void
1602
hexnan
1603
#ifdef KR_headers
1604
  (rvp, sp) U *rvp; CONST char **sp;
1605
#else
1606
  (U *rvp, const char **sp)
1607
#endif
1608
{
1609
  ULong c, x[2];
1610
  CONST char *s;
1611
  int c1, havedig, udx0, xshift;
1612
1613
  /**** if (!hexdig['0']) hexdig_init(); ****/
1614
  x[0] = x[1] = 0;
1615
  havedig = xshift = 0;
1616
  udx0 = 1;
1617
  s = *sp;
1618
  /* allow optional initial 0x or 0X */
1619
  while((c = *(CONST unsigned char*)(s+1)) && c <= ' ')
1620
    ++s;
1621
  if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X'))
1622
    s += 2;
1623
  while((c = *(CONST unsigned char*)++s)) {
1624
    if ((c1 = hexdig[c]))
1625
      c  = c1 & 0xf;
1626
    else if (c <= ' ') {
1627
      if (udx0 && havedig) {
1628
        udx0 = 0;
1629
        xshift = 1;
1630
        }
1631
      continue;
1632
      }
1633
#ifdef GDTOA_NON_PEDANTIC_NANCHECK
1634
    else if (/*(*/ c == ')' && havedig) {
1635
      *sp = s + 1;
1636
      break;
1637
      }
1638
    else
1639
      return; /* invalid form: don't change *sp */
1640
#else
1641
    else {
1642
      do {
1643
        if (/*(*/ c == ')') {
1644
          *sp = s + 1;
1645
          break;
1646
          }
1647
        } while((c = *++s));
1648
      break;
1649
      }
1650
#endif
1651
    havedig = 1;
1652
    if (xshift) {
1653
      xshift = 0;
1654
      x[0] = x[1];
1655
      x[1] = 0;
1656
      }
1657
    if (udx0)
1658
      x[0] = (x[0] << 4) | (x[1] >> 28);
1659
    x[1] = (x[1] << 4) | c;
1660
    }
1661
  if ((x[0] &= 0xfffff) || x[1]) {
1662
    word0(rvp) = Exp_mask | x[0];
1663
    word1(rvp) = x[1];
1664
    }
1665
  }
1666
#endif /*No_Hex_NaN*/
1667
#endif /* INFNAN_CHECK */
1668
1669
#ifdef Pack_32
1670
#define ULbits 32
1671
#define kshift 5
1672
1.86M
#define kmask 31
1673
#else
1674
#define ULbits 16
1675
#define kshift 4
1676
#define kmask 15
1677
#endif
1678
1679
#if !defined(NO_HEX_FP) || defined(Honor_FLT_ROUNDS) /*{*/
1680
 static Bigint *
1681
#ifdef KR_headers
1682
increment(b) Bigint *b;
1683
#else
1684
increment(Bigint *b)
1685
#endif
1686
{
1687
  ULong *x, *xe;
1688
  Bigint *b1;
1689
1690
  x = b->x;
1691
  xe = x + b->wds;
1692
  do {
1693
    if (*x < (ULong)0xffffffffL) {
1694
      ++*x;
1695
      return b;
1696
      }
1697
    *x++ = 0;
1698
    } while(x < xe);
1699
  {
1700
    if (b->wds >= b->maxwds) {
1701
      b1 = Balloc(b->k+1);
1702
      Bcopy(b1,b);
1703
      Bfree(b);
1704
      b = b1;
1705
      }
1706
    b->x[b->wds++] = 1;
1707
    }
1708
  return b;
1709
  }
1710
1711
#endif /*}*/
1712
1713
#ifndef NO_HEX_FP /*{*/
1714
1715
 static void
1716
#ifdef KR_headers
1717
rshift(b, k) Bigint *b; int k;
1718
#else
1719
rshift(Bigint *b, int k)
1720
#endif
1721
{
1722
  ULong *x, *x1, *xe, y;
1723
  int n;
1724
1725
  x = x1 = b->x;
1726
  n = k >> kshift;
1727
  if (n < b->wds) {
1728
    xe = x + b->wds;
1729
    x += n;
1730
    if (k &= kmask) {
1731
      n = 32 - k;
1732
      y = *x++ >> k;
1733
      while(x < xe) {
1734
        *x1++ = (y | (*x << n)) & 0xffffffff;
1735
        y = *x++ >> k;
1736
        }
1737
      if ((*x1 = y) !=0)
1738
        x1++;
1739
      }
1740
    else
1741
      while(x < xe)
1742
        *x1++ = *x++;
1743
    }
1744
  if ((b->wds = x1 - b->x) == 0)
1745
    b->x[0] = 0;
1746
  }
1747
1748
 static ULong
1749
#ifdef KR_headers
1750
any_on(b, k) Bigint *b; int k;
1751
#else
1752
any_on(Bigint *b, int k)
1753
#endif
1754
{
1755
  int n, nwds;
1756
  ULong *x, *x0, x1, x2;
1757
1758
  x = b->x;
1759
  nwds = b->wds;
1760
  n = k >> kshift;
1761
  if (n > nwds)
1762
    n = nwds;
1763
  else if (n < nwds && (k &= kmask)) {
1764
    x1 = x2 = x[n];
1765
    x1 >>= k;
1766
    x1 <<= k;
1767
    if (x1 != x2)
1768
      return 1;
1769
    }
1770
  x0 = x;
1771
  x += n;
1772
  while(x > x0)
1773
    if (*--x)
1774
      return 1;
1775
  return 0;
1776
  }
1777
1778
enum {  /* rounding values: same as FLT_ROUNDS */
1779
  Round_zero = 0,
1780
  Round_near = 1,
1781
  Round_up = 2,
1782
  Round_down = 3
1783
  };
1784
1785
 void
1786
#ifdef KR_headers
1787
gethex(sp, rvp, rounding, sign)
1788
  CONST char **sp; U *rvp; int rounding, sign;
1789
#else
1790
gethex( CONST char **sp, U *rvp, int rounding, int sign)
1791
#endif
1792
{
1793
  Bigint *b;
1794
  CONST unsigned char *decpt, *s0, *s, *s1;
1795
  Long e, e1;
1796
  ULong L, lostbits, *x;
1797
  int big, denorm, esign, havedig, k, n, nbits, up, zret;
1798
#ifdef IBM
1799
  int j;
1800
#endif
1801
  enum {
1802
#ifdef IEEE_Arith /*{{*/
1803
    emax = 0x7fe - Bias - P + 1,
1804
    emin = Emin - P + 1
1805
#else /*}{*/
1806
    emin = Emin - P,
1807
#ifdef VAX
1808
    emax = 0x7ff - Bias - P + 1
1809
#endif
1810
#ifdef IBM
1811
    emax = 0x7f - Bias - P
1812
#endif
1813
#endif /*}}*/
1814
    };
1815
#ifdef USE_LOCALE
1816
  int i;
1817
#ifdef NO_LOCALE_CACHE
1818
  const unsigned char *decimalpoint = (unsigned char*)
1819
    localeconv()->decimal_point;
1820
#else
1821
  const unsigned char *decimalpoint;
1822
  static unsigned char *decimalpoint_cache;
1823
  if (!(s0 = decimalpoint_cache)) {
1824
    s0 = (unsigned char*)localeconv()->decimal_point;
1825
    if ((decimalpoint_cache = (unsigned char*)
1826
        MALLOC(strlen((CONST char*)s0) + 1))) {
1827
      strcpy((char*)decimalpoint_cache, (CONST char*)s0);
1828
      s0 = decimalpoint_cache;
1829
      }
1830
    }
1831
  decimalpoint = s0;
1832
#endif
1833
#endif
1834
1835
  /**** if (!hexdig['0']) hexdig_init(); ****/
1836
  havedig = 0;
1837
  s0 = *(CONST unsigned char **)sp + 2;
1838
  while(s0[havedig] == '0')
1839
    havedig++;
1840
  s0 += havedig;
1841
  s = s0;
1842
  decpt = 0;
1843
  zret = 0;
1844
  e = 0;
1845
  if (hexdig[*s])
1846
    havedig++;
1847
  else {
1848
    zret = 1;
1849
#ifdef USE_LOCALE
1850
    for(i = 0; decimalpoint[i]; ++i) {
1851
      if (s[i] != decimalpoint[i])
1852
        goto pcheck;
1853
      }
1854
    decpt = s += i;
1855
#else
1856
    if (*s != '.')
1857
      goto pcheck;
1858
    decpt = ++s;
1859
#endif
1860
    if (!hexdig[*s])
1861
      goto pcheck;
1862
    while(*s == '0')
1863
      s++;
1864
    if (hexdig[*s])
1865
      zret = 0;
1866
    havedig = 1;
1867
    s0 = s;
1868
    }
1869
  while(hexdig[*s])
1870
    s++;
1871
#ifdef USE_LOCALE
1872
  if (*s == *decimalpoint && !decpt) {
1873
    for(i = 1; decimalpoint[i]; ++i) {
1874
      if (s[i] != decimalpoint[i])
1875
        goto pcheck;
1876
      }
1877
    decpt = s += i;
1878
#else
1879
  if (*s == '.' && !decpt) {
1880
    decpt = ++s;
1881
#endif
1882
    while(hexdig[*s])
1883
      s++;
1884
    }/*}*/
1885
  if (decpt)
1886
    e = -(((Long)(s-decpt)) << 2);
1887
 pcheck:
1888
  s1 = s;
1889
  big = esign = 0;
1890
  switch(*s) {
1891
    case 'p':
1892
    case 'P':
1893
    switch(*++s) {
1894
      case '-':
1895
      esign = 1;
1896
      ZEND_FALLTHROUGH;
1897
      case '+':
1898
      s++;
1899
      }
1900
    if ((n = hexdig[*s]) == 0 || n > 0x19) {
1901
      s = s1;
1902
      break;
1903
      }
1904
    e1 = n - 0x10;
1905
    while((n = hexdig[*++s]) !=0 && n <= 0x19) {
1906
      if (e1 & 0xf8000000)
1907
        big = 1;
1908
      e1 = 10*e1 + n - 0x10;
1909
      }
1910
    if (esign)
1911
      e1 = -e1;
1912
    e += e1;
1913
    }
1914
  *sp = (char*)s;
1915
  if (!havedig)
1916
    *sp = (char*)s0 - 1;
1917
  if (zret)
1918
    goto retz1;
1919
  if (big) {
1920
    if (esign) {
1921
#ifdef IEEE_Arith
1922
      switch(rounding) {
1923
        case Round_up:
1924
        if (sign)
1925
          break;
1926
        goto ret_tiny;
1927
        case Round_down:
1928
        if (!sign)
1929
          break;
1930
        goto ret_tiny;
1931
        }
1932
#endif
1933
      goto retz;
1934
#ifdef IEEE_Arith
1935
 ret_tinyf:
1936
      Bfree(b);
1937
 ret_tiny:
1938
#ifndef NO_ERRNO
1939
      errno = ERANGE;
1940
#endif
1941
      word0(rvp) = 0;
1942
      word1(rvp) = 1;
1943
      return;
1944
#endif /* IEEE_Arith */
1945
      }
1946
    switch(rounding) {
1947
      case Round_near:
1948
      goto ovfl1;
1949
      case Round_up:
1950
      if (!sign)
1951
        goto ovfl1;
1952
      goto ret_big;
1953
      case Round_down:
1954
      if (sign)
1955
        goto ovfl1;
1956
      goto ret_big;
1957
      }
1958
 ret_big:
1959
    word0(rvp) = Big0;
1960
    word1(rvp) = Big1;
1961
    return;
1962
    }
1963
  n = s1 - s0 - 1;
1964
  for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1)
1965
    k++;
1966
  b = Balloc(k);
1967
  x = b->x;
1968
  n = 0;
1969
  L = 0;
1970
#ifdef USE_LOCALE
1971
  for(i = 0; decimalpoint[i+1]; ++i);
1972
#endif
1973
  while(s1 > s0) {
1974
#ifdef USE_LOCALE
1975
    if (*--s1 == decimalpoint[i]) {
1976
      s1 -= i;
1977
      continue;
1978
      }
1979
#else
1980
    if (*--s1 == '.')
1981
      continue;
1982
#endif
1983
    if (n == ULbits) {
1984
      *x++ = L;
1985
      L = 0;
1986
      n = 0;
1987
      }
1988
    L |= (hexdig[*s1] & 0x0f) << n;
1989
    n += 4;
1990
    }
1991
  *x++ = L;
1992
  b->wds = n = x - b->x;
1993
  n = ULbits*n - hi0bits(L);
1994
  nbits = Nbits;
1995
  lostbits = 0;
1996
  x = b->x;
1997
  if (n > nbits) {
1998
    n -= nbits;
1999
    if (any_on(b,n)) {
2000
      lostbits = 1;
2001
      k = n - 1;
2002
      if (x[k>>kshift] & 1 << (k & kmask)) {
2003
        lostbits = 2;
2004
        if (k > 0 && any_on(b,k))
2005
          lostbits = 3;
2006
        }
2007
      }
2008
    rshift(b, n);
2009
    e += n;
2010
    }
2011
  else if (n < nbits) {
2012
    n = nbits - n;
2013
    b = lshift(b, n);
2014
    e -= n;
2015
    x = b->x;
2016
    }
2017
  if (e > Emax) {
2018
 ovfl:
2019
    Bfree(b);
2020
 ovfl1:
2021
#ifndef NO_ERRNO
2022
    errno = ERANGE;
2023
#endif
2024
    word0(rvp) = Exp_mask;
2025
    word1(rvp) = 0;
2026
    return;
2027
    }
2028
  denorm = 0;
2029
  if (e < emin) {
2030
    denorm = 1;
2031
    n = emin - e;
2032
    if (n >= nbits) {
2033
#ifdef IEEE_Arith /*{*/
2034
      switch (rounding) {
2035
        case Round_near:
2036
        if (n == nbits && (n < 2 || any_on(b,n-1)))
2037
          goto ret_tinyf;
2038
        break;
2039
        case Round_up:
2040
        if (!sign)
2041
          goto ret_tinyf;
2042
        break;
2043
        case Round_down:
2044
        if (sign)
2045
          goto ret_tinyf;
2046
        }
2047
#endif /* } IEEE_Arith */
2048
      Bfree(b);
2049
 retz:
2050
#ifndef NO_ERRNO
2051
      errno = ERANGE;
2052
#endif
2053
 retz1:
2054
      rvp->d = 0.;
2055
      return;
2056
      }
2057
    k = n - 1;
2058
    if (lostbits)
2059
      lostbits = 1;
2060
    else if (k > 0)
2061
      lostbits = any_on(b,k);
2062
    if (x[k>>kshift] & 1 << (k & kmask))
2063
      lostbits |= 2;
2064
    nbits -= n;
2065
    rshift(b,n);
2066
    e = emin;
2067
    }
2068
  if (lostbits) {
2069
    up = 0;
2070
    switch(rounding) {
2071
      case Round_zero:
2072
      break;
2073
      case Round_near:
2074
      if (lostbits & 2
2075
       && (lostbits & 1) | (x[0] & 1))
2076
        up = 1;
2077
      break;
2078
      case Round_up:
2079
      up = 1 - sign;
2080
      break;
2081
      case Round_down:
2082
      up = sign;
2083
      }
2084
    if (up) {
2085
      k = b->wds;
2086
      b = increment(b);
2087
      x = b->x;
2088
      if (denorm) {
2089
#if 0
2090
        if (nbits == Nbits - 1
2091
         && x[nbits >> kshift] & 1 << (nbits & kmask))
2092
          denorm = 0; /* not currently used */
2093
#endif
2094
        }
2095
      else if (b->wds > k
2096
       || ((n = nbits & kmask) !=0
2097
           && hi0bits(x[k-1]) < 32-n)) {
2098
        rshift(b,1);
2099
        if (++e > Emax)
2100
          goto ovfl;
2101
        }
2102
      }
2103
    }
2104
#ifdef IEEE_Arith
2105
  if (denorm)
2106
    word0(rvp) = b->wds > 1 ? b->x[1] & ~0x100000 : 0;
2107
  else
2108
    word0(rvp) = (b->x[1] & ~0x100000) | ((e + 0x3ff + 52) << 20);
2109
  word1(rvp) = b->x[0];
2110
#endif
2111
#ifdef IBM
2112
  if ((j = e & 3)) {
2113
    k = b->x[0] & ((1 << j) - 1);
2114
    rshift(b,j);
2115
    if (k) {
2116
      switch(rounding) {
2117
        case Round_up:
2118
        if (!sign)
2119
          increment(b);
2120
        break;
2121
        case Round_down:
2122
        if (sign)
2123
          increment(b);
2124
        break;
2125
        case Round_near:
2126
        j = 1 << (j-1);
2127
        if (k & j && ((k & (j-1)) | lostbits))
2128
          increment(b);
2129
        }
2130
      }
2131
    }
2132
  e >>= 2;
2133
  word0(rvp) = b->x[1] | ((e + 65 + 13) << 24);
2134
  word1(rvp) = b->x[0];
2135
#endif
2136
#ifdef VAX
2137
  /* The next two lines ignore swap of low- and high-order 2 bytes. */
2138
  /* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */
2139
  /* word1(rvp) = b->x[0]; */
2140
  word0(rvp) = ((b->x[1] & ~0x800000) >> 16) | ((e + 129 + 55) << 7) | (b->x[1] << 16);
2141
  word1(rvp) = (b->x[0] >> 16) | (b->x[0] << 16);
2142
#endif
2143
  Bfree(b);
2144
  }
2145
#endif /*!NO_HEX_FP}*/
2146
2147
 static int
2148
#ifdef KR_headers
2149
dshift(b, p2) Bigint *b; int p2;
2150
#else
2151
dshift(Bigint *b, int p2)
2152
#endif
2153
1.86M
{
2154
1.86M
  int rv = hi0bits(b->x[b->wds-1]) - 4;
2155
1.86M
  if (p2 > 0)
2156
1.72M
    rv -= p2;
2157
1.86M
  return rv & kmask;
2158
1.86M
  }
2159
2160
 static int
2161
quorem
2162
#ifdef KR_headers
2163
  (b, S) Bigint *b, *S;
2164
#else
2165
  (Bigint *b, Bigint *S)
2166
#endif
2167
19.5M
{
2168
19.5M
  int n;
2169
19.5M
  ULong *bx, *bxe, q, *sx, *sxe;
2170
19.5M
#ifdef ULLong
2171
19.5M
  ULLong borrow, carry, y, ys;
2172
#else
2173
  ULong borrow, carry, y, ys;
2174
#ifdef Pack_32
2175
  ULong si, z, zs;
2176
#endif
2177
#endif
2178
2179
19.5M
  n = S->wds;
2180
#ifdef DEBUG
2181
  /*debug*/ if (b->wds > n)
2182
  /*debug*/ Bug("oversize b in quorem");
2183
#endif
2184
19.5M
  if (b->wds < n)
2185
318k
    return 0;
2186
19.2M
  sx = S->x;
2187
19.2M
  sxe = sx + --n;
2188
19.2M
  bx = b->x;
2189
19.2M
  bxe = bx + n;
2190
19.2M
  q = *bxe / (*sxe + 1);  /* ensure q <= true quotient */
2191
#ifdef DEBUG
2192
#ifdef NO_STRTOD_BIGCOMP
2193
  /*debug*/ if (q > 9)
2194
#else
2195
  /* An oversized q is possible when quorem is called from bigcomp and */
2196
  /* the input is near, e.g., twice the smallest denormalized number. */
2197
  /*debug*/ if (q > 15)
2198
#endif
2199
  /*debug*/ Bug("oversized quotient in quorem");
2200
#endif
2201
19.2M
  if (q) {
2202
16.3M
    borrow = 0;
2203
16.3M
    carry = 0;
2204
65.8M
    do {
2205
65.8M
#ifdef ULLong
2206
65.8M
      ys = *sx++ * (ULLong)q + carry;
2207
65.8M
      carry = ys >> 32;
2208
65.8M
      y = *bx - (ys & FFFFFFFF) - borrow;
2209
65.8M
      borrow = y >> 32 & (ULong)1;
2210
65.8M
      *bx++ = y & FFFFFFFF;
2211
#else
2212
#ifdef Pack_32
2213
      si = *sx++;
2214
      ys = (si & 0xffff) * q + carry;
2215
      zs = (si >> 16) * q + (ys >> 16);
2216
      carry = zs >> 16;
2217
      y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2218
      borrow = (y & 0x10000) >> 16;
2219
      z = (*bx >> 16) - (zs & 0xffff) - borrow;
2220
      borrow = (z & 0x10000) >> 16;
2221
      Storeinc(bx, z, y);
2222
#else
2223
      ys = *sx++ * q + carry;
2224
      carry = ys >> 16;
2225
      y = *bx - (ys & 0xffff) - borrow;
2226
      borrow = (y & 0x10000) >> 16;
2227
      *bx++ = y & 0xffff;
2228
#endif
2229
#endif
2230
65.8M
      }
2231
65.8M
      while(sx <= sxe);
2232
16.3M
    if (!*bxe) {
2233
5.64k
      bx = b->x;
2234
5.64k
      while(--bxe > bx && !*bxe)
2235
0
        --n;
2236
5.64k
      b->wds = n;
2237
5.64k
      }
2238
16.3M
    }
2239
19.2M
  if (cmp(b, S) >= 0) {
2240
1.07M
    q++;
2241
1.07M
    borrow = 0;
2242
1.07M
    carry = 0;
2243
1.07M
    bx = b->x;
2244
1.07M
    sx = S->x;
2245
2.32M
    do {
2246
2.32M
#ifdef ULLong
2247
2.32M
      ys = *sx++ + carry;
2248
2.32M
      carry = ys >> 32;
2249
2.32M
      y = *bx - (ys & FFFFFFFF) - borrow;
2250
2.32M
      borrow = y >> 32 & (ULong)1;
2251
2.32M
      *bx++ = y & FFFFFFFF;
2252
#else
2253
#ifdef Pack_32
2254
      si = *sx++;
2255
      ys = (si & 0xffff) + carry;
2256
      zs = (si >> 16) + (ys >> 16);
2257
      carry = zs >> 16;
2258
      y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
2259
      borrow = (y & 0x10000) >> 16;
2260
      z = (*bx >> 16) - (zs & 0xffff) - borrow;
2261
      borrow = (z & 0x10000) >> 16;
2262
      Storeinc(bx, z, y);
2263
#else
2264
      ys = *sx++ + carry;
2265
      carry = ys >> 16;
2266
      y = *bx - (ys & 0xffff) - borrow;
2267
      borrow = (y & 0x10000) >> 16;
2268
      *bx++ = y & 0xffff;
2269
#endif
2270
#endif
2271
2.32M
      }
2272
2.32M
      while(sx <= sxe);
2273
1.07M
    bx = b->x;
2274
1.07M
    bxe = bx + n;
2275
1.07M
    if (!*bxe) {
2276
1.07M
      while(--bxe > bx && !*bxe)
2277
10.5k
        --n;
2278
1.06M
      b->wds = n;
2279
1.06M
      }
2280
1.07M
    }
2281
19.2M
  return q;
2282
19.5M
  }
2283
2284
#if defined(Avoid_Underflow) || !defined(NO_STRTOD_BIGCOMP) /*{*/
2285
 static double
2286
sulp
2287
#ifdef KR_headers
2288
  (x, bc) U *x; BCinfo *bc;
2289
#else
2290
  (U *x, BCinfo *bc)
2291
#endif
2292
15.4k
{
2293
15.4k
  U u;
2294
15.4k
  double rv;
2295
15.4k
  int i;
2296
2297
15.4k
  rv = ulp(x);
2298
15.4k
  if (!bc->scale || (i = 2*P + 1 - ((word0(x) & Exp_mask) >> Exp_shift)) <= 0)
2299
14.8k
    return rv; /* Is there an example where i <= 0 ? */
2300
664
  word0(&u) = Exp_1 + (i << Exp_shift);
2301
664
  word1(&u) = 0;
2302
664
  return rv * u.d;
2303
15.4k
  }
2304
#endif /*}*/
2305
2306
#ifndef NO_STRTOD_BIGCOMP
2307
 static void
2308
bigcomp
2309
#ifdef KR_headers
2310
  (rv, s0, bc)
2311
  U *rv; CONST char *s0; BCinfo *bc;
2312
#else
2313
  (U *rv, const char *s0, BCinfo *bc)
2314
#endif
2315
77.9k
{
2316
77.9k
  Bigint *b, *d;
2317
77.9k
  int b2, bbits, d2, dd, dig, dsign, i, j, nd, nd0, p2, p5, speccase;
2318
2319
77.9k
  dsign = bc->dsign;
2320
77.9k
  nd = bc->nd;
2321
77.9k
  nd0 = bc->nd0;
2322
77.9k
  p5 = nd + bc->e0 - 1;
2323
77.9k
  speccase = 0;
2324
77.9k
#ifndef Sudden_Underflow
2325
77.9k
  if (rv->d == 0.) { /* special case: value near underflow-to-zero */
2326
        /* threshold was rounded to zero */
2327
8.90k
    b = i2b(1);
2328
8.90k
    p2 = Emin - P + 1;
2329
8.90k
    bbits = 1;
2330
8.90k
#ifdef Avoid_Underflow
2331
8.90k
    word0(rv) = (P+2) << Exp_shift;
2332
#else
2333
    word1(rv) = 1;
2334
#endif
2335
8.90k
    i = 0;
2336
#ifdef Honor_FLT_ROUNDS
2337
    if (bc->rounding == 1)
2338
#endif
2339
8.90k
      {
2340
8.90k
      speccase = 1;
2341
8.90k
      --p2;
2342
8.90k
      dsign = 0;
2343
8.90k
      goto have_i;
2344
8.90k
      }
2345
8.90k
    }
2346
69.0k
  else
2347
69.0k
#endif
2348
69.0k
    b = d2b(rv, &p2, &bbits);
2349
69.0k
#ifdef Avoid_Underflow
2350
69.0k
  p2 -= bc->scale;
2351
69.0k
#endif
2352
  /* floor(log2(rv)) == bbits - 1 + p2 */
2353
  /* Check for denormal case. */
2354
69.0k
  i = P - bbits;
2355
69.0k
  if (i > (j = P - Emin - 1 + p2)) {
2356
#ifdef Sudden_Underflow
2357
    Bfree(b);
2358
    b = i2b(1);
2359
    p2 = Emin;
2360
    i = P - 1;
2361
#ifdef Avoid_Underflow
2362
    word0(rv) = (1 + bc->scale) << Exp_shift;
2363
#else
2364
    word0(rv) = Exp_msk1;
2365
#endif
2366
    word1(rv) = 0;
2367
#else
2368
4.46k
    i = j;
2369
4.46k
#endif
2370
4.46k
    }
2371
#ifdef Honor_FLT_ROUNDS
2372
  if (bc->rounding != 1) {
2373
    if (i > 0)
2374
      b = lshift(b, i);
2375
    if (dsign)
2376
      b = increment(b);
2377
    }
2378
  else
2379
#endif
2380
69.0k
    {
2381
69.0k
    b = lshift(b, ++i);
2382
69.0k
    b->x[0] |= 1;
2383
69.0k
    }
2384
69.0k
#ifndef Sudden_Underflow
2385
77.9k
 have_i:
2386
77.9k
#endif
2387
77.9k
  p2 -= p5 + i;
2388
77.9k
  d = i2b(1);
2389
  /* Arrange for convenient computation of quotients:
2390
   * shift left if necessary so divisor has 4 leading 0 bits.
2391
   */
2392
77.9k
  if (p5 > 0)
2393
54.8k
    d = pow5mult(d, p5);
2394
23.1k
  else if (p5 < 0)
2395
21.0k
    b = pow5mult(b, -p5);
2396
77.9k
  if (p2 > 0) {
2397
42.4k
    b2 = p2;
2398
42.4k
    d2 = 0;
2399
42.4k
    }
2400
35.5k
  else {
2401
35.5k
    b2 = 0;
2402
35.5k
    d2 = -p2;
2403
35.5k
    }
2404
77.9k
  i = dshift(d, d2);
2405
77.9k
  if ((b2 += i) > 0)
2406
74.5k
    b = lshift(b, b2);
2407
77.9k
  if ((d2 += i) > 0)
2408
70.3k
    d = lshift(d, d2);
2409
2410
  /* Now b/d = exactly half-way between the two floating-point values */
2411
  /* on either side of the input string.  Compute first digit of b/d. */
2412
2413
77.9k
  if (!(dig = quorem(b,d))) {
2414
0
    b = multadd(b, 10, 0);  /* very unlikely */
2415
0
    dig = quorem(b,d);
2416
0
    }
2417
2418
  /* Compare b/d with s0 */
2419
2420
1.14M
  for(i = 0; i < nd0; ) {
2421
1.12M
    if ((dd = s0[i++] - '0' - dig))
2422
49.7k
      goto ret;
2423
1.07M
    if (!b->x[0] && b->wds == 1) {
2424
6.51k
      if (i < nd)
2425
2.85k
        dd = 1;
2426
6.51k
      goto ret;
2427
6.51k
      }
2428
1.06M
    b = multadd(b, 10, 0);
2429
1.06M
    dig = quorem(b,d);
2430
1.06M
    }
2431
148k
  for(j = bc->dp1; i++ < nd;) {
2432
143k
    if ((dd = s0[j++] - '0' - dig))
2433
15.6k
      goto ret;
2434
128k
    if (!b->x[0] && b->wds == 1) {
2435
1.32k
      if (i < nd)
2436
615
        dd = 1;
2437
1.32k
      goto ret;
2438
1.32k
      }
2439
126k
    b = multadd(b, 10, 0);
2440
126k
    dig = quorem(b,d);
2441
126k
    }
2442
4.73k
  if (dig > 0 || b->x[0] || b->wds > 1)
2443
4.73k
    dd = -1;
2444
77.9k
 ret:
2445
77.9k
  Bfree(b);
2446
77.9k
  Bfree(d);
2447
#ifdef Honor_FLT_ROUNDS
2448
  if (bc->rounding != 1) {
2449
    if (dd < 0) {
2450
      if (bc->rounding == 0) {
2451
        if (!dsign)
2452
          goto retlow1;
2453
        }
2454
      else if (dsign)
2455
        goto rethi1;
2456
      }
2457
    else if (dd > 0) {
2458
      if (bc->rounding == 0) {
2459
        if (dsign)
2460
          goto rethi1;
2461
        goto ret1;
2462
        }
2463
      if (!dsign)
2464
        goto rethi1;
2465
      dval(rv) += 2.*sulp(rv,bc);
2466
      }
2467
    else {
2468
      bc->inexact = 0;
2469
      if (dsign)
2470
        goto rethi1;
2471
      }
2472
    }
2473
  else
2474
#endif
2475
77.9k
  if (speccase) {
2476
8.90k
    if (dd <= 0)
2477
8.17k
      rv->d = 0.;
2478
8.90k
    }
2479
69.0k
  else if (dd < 0) {
2480
57.4k
    if (!dsign)  /* does not happen for round-near */
2481
0
retlow1:
2482
0
      dval(rv) -= sulp(rv,bc);
2483
57.4k
    }
2484
11.6k
  else if (dd > 0) {
2485
7.25k
    if (dsign) {
2486
8.90k
 rethi1:
2487
8.90k
      dval(rv) += sulp(rv,bc);
2488
8.90k
      }
2489
7.25k
    }
2490
4.36k
  else {
2491
    /* Exact half-way case:  apply round-even rule. */
2492
4.36k
    if ((j = ((word0(rv) & Exp_mask) >> Exp_shift) - bc->scale) <= 0) {
2493
0
      i = 1 - j;
2494
0
      if (i <= 31) {
2495
0
        if (word1(rv) & (0x1 << i))
2496
0
          goto odd;
2497
0
        }
2498
0
      else if (word0(rv) & (0x1 << (i-32)))
2499
0
        goto odd;
2500
0
      }
2501
4.36k
    else if (word1(rv) & 1) {
2502
1.64k
 odd:
2503
1.64k
      if (dsign)
2504
1.64k
        goto rethi1;
2505
0
      goto retlow1;
2506
1.64k
      }
2507
4.36k
    }
2508
2509
#ifdef Honor_FLT_ROUNDS
2510
 ret1:
2511
#endif
2512
77.9k
  return;
2513
77.9k
  }
2514
#endif /* NO_STRTOD_BIGCOMP */
2515
2516
ZEND_API double
2517
zend_strtod
2518
#ifdef KR_headers
2519
  (s00, se) CONST char *s00; char **se;
2520
#else
2521
  (const char *s00, const char **se)
2522
#endif
2523
1.12M
{
2524
1.12M
  int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1;
2525
1.12M
  int esign, i, j, k, nd, nd0, nf, nz, nz0, nz1, sign;
2526
1.12M
  CONST char *s, *s0, *s1;
2527
1.12M
  volatile double aadj, aadj1;
2528
1.12M
  Long L;
2529
1.12M
  U aadj2, adj, rv, rv0;
2530
1.12M
  ULong y, z;
2531
1.12M
  BCinfo bc;
2532
1.12M
  Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
2533
1.12M
#ifdef Avoid_Underflow
2534
1.12M
  ULong Lsb, Lsb1;
2535
1.12M
#endif
2536
#ifdef SET_INEXACT
2537
  int oldinexact;
2538
#endif
2539
1.12M
#ifndef NO_STRTOD_BIGCOMP
2540
1.12M
  int req_bigcomp = 0;
2541
1.12M
#endif
2542
#ifdef Honor_FLT_ROUNDS /*{*/
2543
#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
2544
  bc.rounding = Flt_Rounds;
2545
#else /*}{*/
2546
  bc.rounding = 1;
2547
  switch(fegetround()) {
2548
    case FE_TOWARDZERO: bc.rounding = 0; break;
2549
    case FE_UPWARD: bc.rounding = 2; break;
2550
    case FE_DOWNWARD: bc.rounding = 3;
2551
    }
2552
#endif /*}}*/
2553
#endif /*}*/
2554
#ifdef USE_LOCALE
2555
  CONST char *s2;
2556
#endif
2557
2558
1.12M
  sign = nz0 = nz1 = nz = bc.dplen = bc.uflchk = 0;
2559
1.12M
  dval(&rv) = 0.;
2560
1.13M
  for(s = s00;;s++) switch(*s) {
2561
79.4k
    case '-':
2562
79.4k
      sign = 1;
2563
79.4k
      ZEND_FALLTHROUGH;
2564
131k
    case '+':
2565
131k
      if (*++s)
2566
131k
        goto break2;
2567
486
      ZEND_FALLTHROUGH;
2568
691
    case 0:
2569
691
      goto ret0;
2570
1.08k
    case '\t':
2571
1.73k
    case '\n':
2572
2.77k
    case '\v':
2573
3.52k
    case '\f':
2574
4.30k
    case '\r':
2575
5.98k
    case ' ':
2576
5.98k
      continue;
2577
993k
    default:
2578
993k
      goto break2;
2579
1.13M
    }
2580
1.12M
 break2:
2581
1.12M
  if (*s == '0') {
2582
#ifndef NO_HEX_FP /*{*/
2583
    switch(s[1]) {
2584
      case 'x':
2585
      case 'X':
2586
#ifdef Honor_FLT_ROUNDS
2587
      gethex(&s, &rv, bc.rounding, sign);
2588
#else
2589
      gethex(&s, &rv, 1, sign);
2590
#endif
2591
      goto ret;
2592
      }
2593
#endif /*}*/
2594
243k
    nz0 = 1;
2595
798k
    while(*++s == '0') ;
2596
243k
    if (!*s)
2597
244
      goto ret;
2598
243k
    }
2599
1.12M
  s0 = s;
2600
1.12M
  y = z = 0;
2601
79.5M
  for(nd = nf = 0; (c = *s) >= '0' && c <= '9'; nd++, s++)
2602
78.4M
    if (nd < 9)
2603
4.73M
      y = 10*y + c - '0';
2604
73.6M
    else if (nd < DBL_DIG + 2)
2605
2.33M
      z = 10*z + c - '0';
2606
1.12M
  nd0 = nd;
2607
1.12M
  bc.dp0 = bc.dp1 = s - s0;
2608
6.80M
  for(s1 = s; s1 > s0 && *--s1 == '0'; )
2609
5.67M
    ++nz1;
2610
#ifdef USE_LOCALE
2611
  s1 = localeconv()->decimal_point;
2612
  if (c == *s1) {
2613
    c = '.';
2614
    if (*++s1) {
2615
      s2 = s;
2616
      for(;;) {
2617
        if (*++s2 != *s1) {
2618
          c = 0;
2619
          break;
2620
          }
2621
        if (!*++s1) {
2622
          s = s2;
2623
          break;
2624
          }
2625
        }
2626
      }
2627
    }
2628
#endif
2629
1.12M
  if (c == '.') {
2630
438k
    c = *++s;
2631
438k
    bc.dp1 = s - s0;
2632
438k
    bc.dplen = bc.dp1 - bc.dp0;
2633
438k
    if (!nd) {
2634
165M
      for(; c == '0'; c = *++s)
2635
165M
        nz++;
2636
173k
      if (c > '0' && c <= '9') {
2637
140k
        bc.dp0 = s0 - s;
2638
140k
        bc.dp1 = bc.dp0 + bc.dplen;
2639
140k
        s0 = s;
2640
140k
        nf += nz;
2641
140k
        nz = 0;
2642
140k
        goto have_dig;
2643
140k
        }
2644
32.7k
      goto dig_done;
2645
173k
      }
2646
292M
    for(; c >= '0' && c <= '9'; c = *++s) {
2647
292M
 have_dig:
2648
292M
      nz++;
2649
292M
      if (c -= '0') {
2650
4.23M
        nf += nz;
2651
284M
        for(i = 1; i < nz; i++)
2652
280M
          if (nd++ < 9)
2653
683k
            y *= 10;
2654
279M
          else if (nd <= DBL_DIG + 2)
2655
939k
            z *= 10;
2656
4.23M
        if (nd++ < 9)
2657
1.03M
          y = 10*y + c;
2658
3.19M
        else if (nd <= DBL_DIG + 2)
2659
596k
          z = 10*z + c;
2660
4.23M
        nz = nz1 = 0;
2661
4.23M
        }
2662
292M
      }
2663
265k
    }
2664
1.12M
 dig_done:
2665
1.12M
  if (nd < 0) {
2666
    /* overflow */
2667
0
    nd = DBL_DIG + 2;
2668
0
  }
2669
1.12M
  if (nf < 0) {
2670
    /* overflow */
2671
0
    nf = DBL_DIG + 2;
2672
0
  }
2673
1.12M
  e = 0;
2674
1.12M
  if (c == 'e' || c == 'E') {
2675
390k
    if (!nd && !nz && !nz0) {
2676
444
      goto ret0;
2677
444
      }
2678
390k
    s00 = s;
2679
390k
    esign = 0;
2680
390k
    switch(c = *++s) {
2681
228k
      case '-':
2682
228k
        esign = 1;
2683
228k
        ZEND_FALLTHROUGH;
2684
233k
      case '+':
2685
233k
        c = *++s;
2686
390k
      }
2687
390k
    if (c >= '0' && c <= '9') {
2688
615k
      while(c == '0')
2689
228k
        c = *++s;
2690
386k
      if (c > '0' && c <= '9') {
2691
379k
        L = c - '0';
2692
379k
        s1 = s;
2693
8.12M
        while((c = *++s) >= '0' && c <= '9')
2694
7.74M
          L = (Long) (10*(ULong)L + (c - '0'));
2695
379k
        if (s - s1 > 8 || L > 19999)
2696
          /* Avoid confusion from exponents
2697
           * so large that e might overflow.
2698
           */
2699
11.1k
          e = 19999; /* safe for 16 bit ints */
2700
367k
        else
2701
367k
          e = (int)L;
2702
379k
        if (esign)
2703
227k
          e = -e;
2704
379k
        }
2705
7.62k
      else
2706
7.62k
        e = 0;
2707
386k
      }
2708
3.50k
    else
2709
3.50k
      s = s00;
2710
390k
    }
2711
1.12M
  if (!nd) {
2712
88.4k
    if (!nz && !nz0) {
2713
#ifdef INFNAN_CHECK
2714
      /* Check for Nan and Infinity */
2715
      if (!bc.dplen)
2716
       switch(c) {
2717
        case 'i':
2718
        case 'I':
2719
        if (match(&s,"nf")) {
2720
          --s;
2721
          if (!match(&s,"inity"))
2722
            ++s;
2723
          word0(&rv) = 0x7ff00000;
2724
          word1(&rv) = 0;
2725
          goto ret;
2726
          }
2727
        break;
2728
        case 'n':
2729
        case 'N':
2730
        if (match(&s, "an")) {
2731
          word0(&rv) = NAN_WORD0;
2732
          word1(&rv) = NAN_WORD1;
2733
#ifndef No_Hex_NaN
2734
          if (*s == '(') /*)*/
2735
            hexnan(&rv, &s);
2736
#endif
2737
          goto ret;
2738
          }
2739
        }
2740
#endif /* INFNAN_CHECK */
2741
1.53k
 ret0:
2742
1.53k
      s = s00;
2743
1.53k
      sign = 0;
2744
1.53k
      }
2745
89.6k
    goto ret;
2746
88.4k
    }
2747
1.03M
  bc.e0 = e1 = e -= nf;
2748
2749
  /* Now we have nd0 digits, starting at s0, followed by a
2750
   * decimal point, followed by nd-nd0 digits.  The number we're
2751
   * after is the integer represented by those digits times
2752
   * 10**e */
2753
2754
1.03M
  if (!nd0)
2755
140k
    nd0 = nd;
2756
1.03M
  k = nd < DBL_DIG + 2 ? nd : DBL_DIG + 2;
2757
1.03M
  dval(&rv) = y;
2758
1.03M
  if (k > 9) {
2759
#ifdef SET_INEXACT
2760
    if (k > DBL_DIG)
2761
      oldinexact = get_inexact();
2762
#endif
2763
540k
    dval(&rv) = tens[k - 9] * dval(&rv) + z;
2764
540k
    }
2765
1.03M
  bd0 = 0;
2766
1.03M
  if (nd <= DBL_DIG
2767
590k
#ifndef RND_PRODQUOT
2768
590k
#ifndef Honor_FLT_ROUNDS
2769
590k
    && Flt_Rounds == 1
2770
1.03M
#endif
2771
1.03M
#endif
2772
1.03M
      ) {
2773
590k
    if (!e)
2774
252k
      goto ret;
2775
337k
#ifndef ROUND_BIASED_without_Round_Up
2776
337k
    if (e > 0) {
2777
63.6k
      if (e <= Ten_pmax) {
2778
#ifdef VAX
2779
        goto vax_ovfl_check;
2780
#else
2781
#ifdef Honor_FLT_ROUNDS
2782
        /* round correctly FLT_ROUNDS = 2 or 3 */
2783
        if (sign) {
2784
          rv.d = -rv.d;
2785
          sign = 0;
2786
          }
2787
#endif
2788
13.2k
        /* rv = */ rounded_product(dval(&rv), tens[e]);
2789
13.2k
        goto ret;
2790
13.2k
#endif
2791
13.2k
        }
2792
50.3k
      i = DBL_DIG - nd;
2793
50.3k
      if (e <= Ten_pmax + i) {
2794
        /* A fancier test would sometimes let us do
2795
         * this for larger i values.
2796
         */
2797
#ifdef Honor_FLT_ROUNDS
2798
        /* round correctly FLT_ROUNDS = 2 or 3 */
2799
        if (sign) {
2800
          rv.d = -rv.d;
2801
          sign = 0;
2802
          }
2803
#endif
2804
3.06k
        e -= i;
2805
3.06k
        dval(&rv) *= tens[i];
2806
#ifdef VAX
2807
        /* VAX exponent range is so narrow we must
2808
         * worry about overflow here...
2809
         */
2810
 vax_ovfl_check:
2811
        word0(&rv) -= P*Exp_msk1;
2812
        /* rv = */ rounded_product(dval(&rv), tens[e]);
2813
        if ((word0(&rv) & Exp_mask)
2814
         > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
2815
          goto ovfl;
2816
        word0(&rv) += P*Exp_msk1;
2817
#else
2818
3.06k
        /* rv = */ rounded_product(dval(&rv), tens[e]);
2819
3.06k
#endif
2820
3.06k
        goto ret;
2821
3.06k
        }
2822
50.3k
      }
2823
273k
#ifndef Inaccurate_Divide
2824
273k
    else if (e >= -Ten_pmax) {
2825
#ifdef Honor_FLT_ROUNDS
2826
      /* round correctly FLT_ROUNDS = 2 or 3 */
2827
      if (sign) {
2828
        rv.d = -rv.d;
2829
        sign = 0;
2830
        }
2831
#endif
2832
60.6k
      /* rv = */ rounded_quotient(dval(&rv), tens[-e]);
2833
60.6k
      goto ret;
2834
60.6k
      }
2835
337k
#endif
2836
337k
#endif /* ROUND_BIASED_without_Round_Up */
2837
337k
    }
2838
705k
  e1 += nd - k;
2839
2840
705k
#ifdef IEEE_Arith
2841
#ifdef SET_INEXACT
2842
  bc.inexact = 1;
2843
  if (k <= DBL_DIG)
2844
    oldinexact = get_inexact();
2845
#endif
2846
705k
#ifdef Avoid_Underflow
2847
705k
  bc.scale = 0;
2848
705k
#endif
2849
#ifdef Honor_FLT_ROUNDS
2850
  if (bc.rounding >= 2) {
2851
    if (sign)
2852
      bc.rounding = bc.rounding == 2 ? 0 : 2;
2853
    else
2854
      if (bc.rounding != 2)
2855
        bc.rounding = 0;
2856
    }
2857
#endif
2858
705k
#endif /*IEEE_Arith*/
2859
2860
  /* Get starting approximation = rv * 10**e1 */
2861
2862
705k
  if (e1 > 0) {
2863
268k
    if ((i = e1 & 15))
2864
261k
      dval(&rv) *= tens[i];
2865
268k
    if (e1 &= ~15) {
2866
159k
      if (e1 > DBL_MAX_10_EXP) {
2867
20.8k
 ovfl:
2868
        /* Can't trust HUGE_VAL */
2869
20.8k
#ifdef IEEE_Arith
2870
#ifdef Honor_FLT_ROUNDS
2871
        switch(bc.rounding) {
2872
          case 0: /* toward 0 */
2873
          case 3: /* toward -infinity */
2874
          word0(&rv) = Big0;
2875
          word1(&rv) = Big1;
2876
          break;
2877
          default:
2878
          word0(&rv) = Exp_mask;
2879
          word1(&rv) = 0;
2880
          }
2881
#else /*Honor_FLT_ROUNDS*/
2882
20.8k
        word0(&rv) = Exp_mask;
2883
20.8k
        word1(&rv) = 0;
2884
20.8k
#endif /*Honor_FLT_ROUNDS*/
2885
#ifdef SET_INEXACT
2886
        /* set overflow bit */
2887
        dval(&rv0) = 1e300;
2888
        dval(&rv0) *= dval(&rv0);
2889
#endif
2890
#else /*IEEE_Arith*/
2891
        word0(&rv) = Big0;
2892
        word1(&rv) = Big1;
2893
#endif /*IEEE_Arith*/
2894
50.7k
 range_err:
2895
50.7k
        if (bd0) {
2896
2.28k
          Bfree(bb);
2897
2.28k
          Bfree(bd);
2898
2.28k
          Bfree(bs);
2899
2.28k
          Bfree(bd0);
2900
2.28k
          Bfree(delta);
2901
2.28k
          }
2902
#ifndef NO_ERRNO
2903
        errno = ERANGE;
2904
#endif
2905
50.7k
        goto ret;
2906
20.8k
        }
2907
144k
      e1 >>= 4;
2908
371k
      for(j = 0; e1 > 1; j++, e1 >>= 1)
2909
226k
        if (e1 & 1)
2910
109k
          dval(&rv) *= bigtens[j];
2911
    /* The last multiplication could overflow. */
2912
144k
      word0(&rv) -= P*Exp_msk1;
2913
144k
      dval(&rv) *= bigtens[j];
2914
144k
      if ((z = word0(&rv) & Exp_mask)
2915
144k
       > Exp_msk1*(DBL_MAX_EXP+Bias-P))
2916
2.42k
        goto ovfl;
2917
142k
      if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
2918
        /* set to largest number */
2919
        /* (Can't trust DBL_MAX) */
2920
3.02k
        word0(&rv) = Big0;
2921
3.02k
        word1(&rv) = Big1;
2922
3.02k
        }
2923
139k
      else
2924
139k
        word0(&rv) += P*Exp_msk1;
2925
142k
      }
2926
268k
    }
2927
436k
  else if (e1 < 0) {
2928
420k
    e1 = -e1;
2929
420k
    if ((i = e1 & 15))
2930
405k
      dval(&rv) /= tens[i];
2931
420k
    if (e1 >>= 4) {
2932
330k
      if (e1 >= 1 << n_bigtens)
2933
6.10k
        goto undfl;
2934
324k
#ifdef Avoid_Underflow
2935
324k
      if (e1 & Scale_Bit)
2936
233k
        bc.scale = 2*P;
2937
1.71M
      for(j = 0; e1 > 0; j++, e1 >>= 1)
2938
1.39M
        if (e1 & 1)
2939
664k
          dval(&rv) *= tinytens[j];
2940
324k
      if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
2941
233k
            >> Exp_shift)) > 0) {
2942
        /* scaled rv is denormal; clear j low bits */
2943
202k
        if (j >= 32) {
2944
173k
          if (j > 54)
2945
15.6k
            goto undfl;
2946
158k
          word1(&rv) = 0;
2947
158k
          if (j >= 53)
2948
10.3k
           word0(&rv) = (P+2)*Exp_msk1;
2949
148k
          else
2950
148k
           word0(&rv) &= 0xffffffff << (j-32);
2951
158k
          }
2952
28.0k
        else
2953
28.0k
          word1(&rv) &= 0xffffffff << j;
2954
202k
        }
2955
#else
2956
      for(j = 0; e1 > 1; j++, e1 >>= 1)
2957
        if (e1 & 1)
2958
          dval(&rv) *= tinytens[j];
2959
      /* The last multiplication could underflow. */
2960
      dval(&rv0) = dval(&rv);
2961
      dval(&rv) *= tinytens[j];
2962
      if (!dval(&rv)) {
2963
        dval(&rv) = 2.*dval(&rv0);
2964
        dval(&rv) *= tinytens[j];
2965
#endif
2966
308k
        if (!dval(&rv)) {
2967
29.8k
 undfl:
2968
29.8k
          dval(&rv) = 0.;
2969
29.8k
          goto range_err;
2970
0
          }
2971
#ifndef Avoid_Underflow
2972
        word0(&rv) = Tiny0;
2973
        word1(&rv) = Tiny1;
2974
        /* The refinement below will clean
2975
         * this approximation up.
2976
         */
2977
        }
2978
#endif
2979
308k
      }
2980
420k
    }
2981
2982
  /* Now the hard part -- adjusting rv to the correct value.*/
2983
2984
  /* Put digits into bd: true value = bd * 10^e */
2985
2986
666k
  bc.nd = nd - nz1;
2987
666k
#ifndef NO_STRTOD_BIGCOMP
2988
666k
  bc.nd0 = nd0; /* Only needed if nd > strtod_diglim, but done here */
2989
      /* to silence an erroneous warning about bc.nd0 */
2990
      /* possibly not being initialized. */
2991
666k
  if (nd > strtod_diglim) {
2992
    /* ASSERT(strtod_diglim >= 18); 18 == one more than the */
2993
    /* minimum number of decimal digits to distinguish double values */
2994
    /* in IEEE arithmetic. */
2995
148k
    i = j = 18;
2996
148k
    if (i > nd0)
2997
24.0k
      j += bc.dplen;
2998
848k
    for(;;) {
2999
848k
      if (--j < bc.dp1 && j >= bc.dp0)
3000
5.06k
        j = bc.dp0 - 1;
3001
848k
      if (s0[j] != '0')
3002
148k
        break;
3003
700k
      --i;
3004
700k
      }
3005
148k
    e += nd - i;
3006
148k
    nd = i;
3007
148k
    if (nd0 > nd)
3008
124k
      nd0 = nd;
3009
148k
    if (nd < 9) { /* must recompute y */
3010
38.3k
      y = 0;
3011
140k
      for(i = 0; i < nd0; ++i)
3012
102k
        y = 10*y + s0[i] - '0';
3013
54.3k
      for(j = bc.dp1; i < nd; ++i)
3014
15.9k
        y = 10*y + s0[j++] - '0';
3015
38.3k
      }
3016
148k
    }
3017
666k
#endif
3018
666k
  bd0 = s2b(s0, nd0, nd, y, bc.dplen);
3019
3020
894k
  for(;;) {
3021
894k
    bd = Balloc(bd0->k);
3022
894k
    Bcopy(bd, bd0);
3023
894k
    bb = d2b(&rv, &bbe, &bbbits); /* rv = bb * 2^bbe */
3024
894k
    bs = i2b(1);
3025
3026
894k
    if (e >= 0) {
3027
313k
      bb2 = bb5 = 0;
3028
313k
      bd2 = bd5 = e;
3029
313k
      }
3030
580k
    else {
3031
580k
      bb2 = bb5 = -e;
3032
580k
      bd2 = bd5 = 0;
3033
580k
      }
3034
894k
    if (bbe >= 0)
3035
345k
      bb2 += bbe;
3036
549k
    else
3037
549k
      bd2 -= bbe;
3038
894k
    bs2 = bb2;
3039
#ifdef Honor_FLT_ROUNDS
3040
    if (bc.rounding != 1)
3041
      bs2++;
3042
#endif
3043
894k
#ifdef Avoid_Underflow
3044
894k
    Lsb = LSB;
3045
894k
    Lsb1 = 0;
3046
894k
    j = bbe - bc.scale;
3047
894k
    i = j + bbbits - 1; /* logb(rv) */
3048
894k
    j = P + 1 - bbbits;
3049
894k
    if (i < Emin) { /* denormal */
3050
343k
      i = Emin - i;
3051
343k
      j -= i;
3052
343k
      if (i < 32)
3053
41.5k
        Lsb <<= i;
3054
302k
      else if (i < 52)
3055
291k
        Lsb1 = Lsb << (i-32);
3056
10.9k
      else
3057
10.9k
        Lsb1 = Exp_mask;
3058
343k
      }
3059
#else /*Avoid_Underflow*/
3060
#ifdef Sudden_Underflow
3061
#ifdef IBM
3062
    j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
3063
#else
3064
    j = P + 1 - bbbits;
3065
#endif
3066
#else /*Sudden_Underflow*/
3067
    j = bbe;
3068
    i = j + bbbits - 1; /* logb(rv) */
3069
    if (i < Emin) /* denormal */
3070
      j += P - Emin;
3071
    else
3072
      j = P + 1 - bbbits;
3073
#endif /*Sudden_Underflow*/
3074
#endif /*Avoid_Underflow*/
3075
894k
    bb2 += j;
3076
894k
    bd2 += j;
3077
894k
#ifdef Avoid_Underflow
3078
894k
    bd2 += bc.scale;
3079
894k
#endif
3080
894k
    i = bb2 < bd2 ? bb2 : bd2;
3081
894k
    if (i > bs2)
3082
558k
      i = bs2;
3083
894k
    if (i > 0) {
3084
890k
      bb2 -= i;
3085
890k
      bd2 -= i;
3086
890k
      bs2 -= i;
3087
890k
      }
3088
894k
    if (bb5 > 0) {
3089
580k
      bs = pow5mult(bs, bb5);
3090
580k
      bb1 = mult(bs, bb);
3091
580k
      Bfree(bb);
3092
580k
      bb = bb1;
3093
580k
      }
3094
894k
    if (bb2 > 0)
3095
894k
      bb = lshift(bb, bb2);
3096
894k
    if (bd5 > 0)
3097
180k
      bd = pow5mult(bd, bd5);
3098
894k
    if (bd2 > 0)
3099
558k
      bd = lshift(bd, bd2);
3100
894k
    if (bs2 > 0)
3101
327k
      bs = lshift(bs, bs2);
3102
894k
    delta = diff(bb, bd);
3103
894k
    bc.dsign = delta->sign;
3104
894k
    delta->sign = 0;
3105
894k
    i = cmp(delta, bs);
3106
894k
#ifndef NO_STRTOD_BIGCOMP /*{*/
3107
894k
    if (bc.nd > nd && i <= 0) {
3108
149k
      if (bc.dsign) {
3109
        /* Must use bigcomp(). */
3110
69.0k
        req_bigcomp = 1;
3111
69.0k
        break;
3112
69.0k
        }
3113
#ifdef Honor_FLT_ROUNDS
3114
      if (bc.rounding != 1) {
3115
        if (i < 0) {
3116
          req_bigcomp = 1;
3117
          break;
3118
          }
3119
        }
3120
      else
3121
#endif
3122
79.9k
        i = -1; /* Discarded digits make delta smaller. */
3123
79.9k
      }
3124
825k
#endif /*}*/
3125
#ifdef Honor_FLT_ROUNDS /*{*/
3126
    if (bc.rounding != 1) {
3127
      if (i < 0) {
3128
        /* Error is less than an ulp */
3129
        if (!delta->x[0] && delta->wds <= 1) {
3130
          /* exact */
3131
#ifdef SET_INEXACT
3132
          bc.inexact = 0;
3133
#endif
3134
          break;
3135
          }
3136
        if (bc.rounding) {
3137
          if (bc.dsign) {
3138
            adj.d = 1.;
3139
            goto apply_adj;
3140
            }
3141
          }
3142
        else if (!bc.dsign) {
3143
          adj.d = -1.;
3144
          if (!word1(&rv)
3145
           && !(word0(&rv) & Frac_mask)) {
3146
            y = word0(&rv) & Exp_mask;
3147
#ifdef Avoid_Underflow
3148
            if (!bc.scale || y > 2*P*Exp_msk1)
3149
#else
3150
            if (y)
3151
#endif
3152
              {
3153
              delta = lshift(delta,Log2P);
3154
              if (cmp(delta, bs) <= 0)
3155
              adj.d = -0.5;
3156
              }
3157
            }
3158
 apply_adj:
3159
#ifdef Avoid_Underflow /*{*/
3160
          if (bc.scale && (y = word0(&rv) & Exp_mask)
3161
            <= 2*P*Exp_msk1)
3162
            word0(&adj) += (2*P+1)*Exp_msk1 - y;
3163
#else
3164
#ifdef Sudden_Underflow
3165
          if ((word0(&rv) & Exp_mask) <=
3166
              P*Exp_msk1) {
3167
            word0(&rv) += P*Exp_msk1;
3168
            dval(&rv) += adj.d*ulp(dval(&rv));
3169
            word0(&rv) -= P*Exp_msk1;
3170
            }
3171
          else
3172
#endif /*Sudden_Underflow*/
3173
#endif /*Avoid_Underflow}*/
3174
          dval(&rv) += adj.d*ulp(&rv);
3175
          }
3176
        break;
3177
        }
3178
      adj.d = ratio(delta, bs);
3179
      if (adj.d < 1.)
3180
        adj.d = 1.;
3181
      if (adj.d <= 0x7ffffffe) {
3182
        /* adj = rounding ? ceil(adj) : floor(adj); */
3183
        y = adj.d;
3184
        if (y != adj.d) {
3185
          if (!((bc.rounding>>1) ^ bc.dsign))
3186
            y++;
3187
          adj.d = y;
3188
          }
3189
        }
3190
#ifdef Avoid_Underflow /*{*/
3191
      if (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
3192
        word0(&adj) += (2*P+1)*Exp_msk1 - y;
3193
#else
3194
#ifdef Sudden_Underflow
3195
      if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
3196
        word0(&rv) += P*Exp_msk1;
3197
        adj.d *= ulp(dval(&rv));
3198
        if (bc.dsign)
3199
          dval(&rv) += adj.d;
3200
        else
3201
          dval(&rv) -= adj.d;
3202
        word0(&rv) -= P*Exp_msk1;
3203
        goto cont;
3204
        }
3205
#endif /*Sudden_Underflow*/
3206
#endif /*Avoid_Underflow}*/
3207
      adj.d *= ulp(&rv);
3208
      if (bc.dsign) {
3209
        if (word0(&rv) == Big0 && word1(&rv) == Big1)
3210
          goto ovfl;
3211
        dval(&rv) += adj.d;
3212
        }
3213
      else
3214
        dval(&rv) -= adj.d;
3215
      goto cont;
3216
      }
3217
#endif /*}Honor_FLT_ROUNDS*/
3218
3219
825k
    if (i < 0) {
3220
      /* Error is less than half an ulp -- check for
3221
       * special case of mantissa a power of two.
3222
       */
3223
450k
      if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask
3224
22.2k
#ifdef IEEE_Arith /*{*/
3225
22.2k
#ifdef Avoid_Underflow
3226
22.2k
       || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
3227
#else
3228
       || (word0(&rv) & Exp_mask) <= Exp_msk1
3229
#endif
3230
450k
#endif /*}*/
3231
450k
        ) {
3232
#ifdef SET_INEXACT
3233
        if (!delta->x[0] && delta->wds <= 1)
3234
          bc.inexact = 0;
3235
#endif
3236
430k
        break;
3237
430k
        }
3238
20.0k
      if (!delta->x[0] && delta->wds <= 1) {
3239
        /* exact result */
3240
#ifdef SET_INEXACT
3241
        bc.inexact = 0;
3242
#endif
3243
5.96k
        break;
3244
5.96k
        }
3245
14.1k
      delta = lshift(delta,Log2P);
3246
14.1k
      if (cmp(delta, bs) > 0)
3247
6.46k
        goto drop_down;
3248
7.65k
      break;
3249
14.1k
      }
3250
375k
    if (i == 0) {
3251
      /* exactly half-way between */
3252
19.1k
      if (bc.dsign) {
3253
8.16k
        if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
3254
5.01k
         &&  word1(&rv) == (
3255
5.01k
#ifdef Avoid_Underflow
3256
5.01k
      (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
3257
5.01k
    ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
3258
5.01k
#endif
3259
5.01k
               0xffffffff)) {
3260
          /*boundary case -- increment exponent*/
3261
1.91k
          if (word0(&rv) == Big0 && word1(&rv) == Big1)
3262
0
            goto ovfl;
3263
1.91k
          word0(&rv) = (word0(&rv) & Exp_mask)
3264
1.91k
            + Exp_msk1
3265
#ifdef IBM
3266
            | Exp_msk1 >> 4
3267
#endif
3268
1.91k
            ;
3269
1.91k
          word1(&rv) = 0;
3270
1.91k
#ifdef Avoid_Underflow
3271
1.91k
          bc.dsign = 0;
3272
1.91k
#endif
3273
1.91k
          break;
3274
1.91k
          }
3275
8.16k
        }
3276
10.9k
      else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
3277
6.46k
 drop_down:
3278
        /* boundary case -- decrement exponent */
3279
#ifdef Sudden_Underflow /*{{*/
3280
        L = word0(&rv) & Exp_mask;
3281
#ifdef IBM
3282
        if (L <  Exp_msk1)
3283
#else
3284
#ifdef Avoid_Underflow
3285
        if (L <= (bc.scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
3286
#else
3287
        if (L <= Exp_msk1)
3288
#endif /*Avoid_Underflow*/
3289
#endif /*IBM*/
3290
          {
3291
          if (bc.nd >nd) {
3292
            bc.uflchk = 1;
3293
            break;
3294
            }
3295
          goto undfl;
3296
          }
3297
        L -= Exp_msk1;
3298
#else /*Sudden_Underflow}{*/
3299
6.46k
#ifdef Avoid_Underflow
3300
6.46k
        if (bc.scale) {
3301
0
          L = word0(&rv) & Exp_mask;
3302
0
          if (L <= (2*P+1)*Exp_msk1) {
3303
0
            if (L > (P+2)*Exp_msk1)
3304
              /* round even ==> */
3305
              /* accept rv */
3306
0
              break;
3307
            /* rv = smallest denormal */
3308
0
            if (bc.nd >nd) {
3309
0
              bc.uflchk = 1;
3310
0
              break;
3311
0
              }
3312
0
            goto undfl;
3313
0
            }
3314
0
          }
3315
6.46k
#endif /*Avoid_Underflow*/
3316
6.46k
        L = (word0(&rv) & Exp_mask) - Exp_msk1;
3317
6.46k
#endif /*Sudden_Underflow}}*/
3318
6.46k
        word0(&rv) = L | Bndry_mask1;
3319
6.46k
        word1(&rv) = 0xffffffff;
3320
#ifdef IBM
3321
        goto cont;
3322
#else
3323
6.46k
#ifndef NO_STRTOD_BIGCOMP
3324
6.46k
        if (bc.nd > nd)
3325
2.25k
          goto cont;
3326
4.20k
#endif
3327
4.20k
        break;
3328
6.46k
#endif
3329
6.46k
        }
3330
17.2k
#ifndef ROUND_BIASED
3331
17.2k
#ifdef Avoid_Underflow
3332
17.2k
      if (Lsb1) {
3333
0
        if (!(word0(&rv) & Lsb1))
3334
0
          break;
3335
0
        }
3336
17.2k
      else if (!(word1(&rv) & Lsb))
3337
10.6k
        break;
3338
#else
3339
      if (!(word1(&rv) & LSB))
3340
        break;
3341
#endif
3342
6.58k
#endif
3343
6.58k
      if (bc.dsign)
3344
2.22k
#ifdef Avoid_Underflow
3345
2.22k
        dval(&rv) += sulp(&rv, &bc);
3346
#else
3347
        dval(&rv) += ulp(&rv);
3348
#endif
3349
4.35k
#ifndef ROUND_BIASED
3350
4.35k
      else {
3351
4.35k
#ifdef Avoid_Underflow
3352
4.35k
        dval(&rv) -= sulp(&rv, &bc);
3353
#else
3354
        dval(&rv) -= ulp(&rv);
3355
#endif
3356
4.35k
#ifndef Sudden_Underflow
3357
4.35k
        if (!dval(&rv)) {
3358
0
          if (bc.nd >nd) {
3359
0
            bc.uflchk = 1;
3360
0
            break;
3361
0
            }
3362
0
          goto undfl;
3363
0
          }
3364
4.35k
#endif
3365
4.35k
        }
3366
6.58k
#ifdef Avoid_Underflow
3367
6.58k
      bc.dsign = 1 - bc.dsign;
3368
6.58k
#endif
3369
6.58k
#endif
3370
6.58k
      break;
3371
6.58k
      }
3372
356k
    if ((aadj = ratio(delta, bs)) <= 2.) {
3373
301k
      if (bc.dsign)
3374
182k
        aadj = aadj1 = 1.;
3375
118k
      else if (word1(&rv) || word0(&rv) & Bndry_mask) {
3376
109k
#ifndef Sudden_Underflow
3377
109k
        if (word1(&rv) == Tiny1 && !word0(&rv)) {
3378
0
          if (bc.nd >nd) {
3379
0
            bc.uflchk = 1;
3380
0
            break;
3381
0
            }
3382
0
          goto undfl;
3383
0
          }
3384
109k
#endif
3385
109k
        aadj = 1.;
3386
109k
        aadj1 = -1.;
3387
109k
        }
3388
8.91k
      else {
3389
        /* special case -- power of FLT_RADIX to be */
3390
        /* rounded down... */
3391
3392
8.91k
        if (aadj < 2./FLT_RADIX)
3393
0
          aadj = 1./FLT_RADIX;
3394
8.91k
        else
3395
8.91k
          aadj *= 0.5;
3396
8.91k
        aadj1 = -aadj;
3397
8.91k
        }
3398
301k
      }
3399
55.2k
    else {
3400
55.2k
      aadj *= 0.5;
3401
55.2k
      aadj1 = bc.dsign ? aadj : -aadj;
3402
#ifdef Check_FLT_ROUNDS
3403
      switch(bc.rounding) {
3404
        case 2: /* towards +infinity */
3405
          aadj1 -= 0.5;
3406
          break;
3407
        case 0: /* towards 0 */
3408
        case 3: /* towards -infinity */
3409
          aadj1 += 0.5;
3410
        }
3411
#else
3412
55.2k
      if (Flt_Rounds == 0)
3413
0
        aadj1 += 0.5;
3414
55.2k
#endif /*Check_FLT_ROUNDS*/
3415
55.2k
      }
3416
356k
    y = word0(&rv) & Exp_mask;
3417
3418
    /* Check for overflow */
3419
3420
356k
    if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
3421
7.54k
      dval(&rv0) = dval(&rv);
3422
7.54k
      word0(&rv) -= P*Exp_msk1;
3423
7.54k
      adj.d = aadj1 * ulp(&rv);
3424
7.54k
      dval(&rv) += adj.d;
3425
7.54k
      if ((word0(&rv) & Exp_mask) >=
3426
7.54k
          Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
3427
2.28k
        if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
3428
2.28k
          goto ovfl;
3429
0
        word0(&rv) = Big0;
3430
0
        word1(&rv) = Big1;
3431
0
        goto cont;
3432
2.28k
        }
3433
5.25k
      else
3434
5.25k
        word0(&rv) += P*Exp_msk1;
3435
7.54k
      }
3436
348k
    else {
3437
348k
#ifdef Avoid_Underflow
3438
348k
      if (bc.scale && y <= 2*P*Exp_msk1) {
3439
166k
        if (aadj <= 0x7fffffff) {
3440
166k
          if ((z = aadj) <= 0)
3441
8.90k
            z = 1;
3442
166k
          aadj = z;
3443
166k
          aadj1 = bc.dsign ? aadj : -aadj;
3444
166k
          }
3445
166k
        dval(&aadj2) = aadj1;
3446
166k
        word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
3447
166k
        aadj1 = dval(&aadj2);
3448
166k
        adj.d = aadj1 * ulp(&rv);
3449
166k
        dval(&rv) += adj.d;
3450
166k
        if (rv.d == 0.)
3451
#ifdef NO_STRTOD_BIGCOMP
3452
          goto undfl;
3453
#else
3454
8.90k
          {
3455
8.90k
          req_bigcomp = 1;
3456
8.90k
          break;
3457
8.90k
          }
3458
166k
#endif
3459
166k
        }
3460
182k
      else {
3461
182k
        adj.d = aadj1 * ulp(&rv);
3462
182k
        dval(&rv) += adj.d;
3463
182k
        }
3464
#else
3465
#ifdef Sudden_Underflow
3466
      if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
3467
        dval(&rv0) = dval(&rv);
3468
        word0(&rv) += P*Exp_msk1;
3469
        adj.d = aadj1 * ulp(&rv);
3470
        dval(&rv) += adj.d;
3471
#ifdef IBM
3472
        if ((word0(&rv) & Exp_mask) <  P*Exp_msk1)
3473
#else
3474
        if ((word0(&rv) & Exp_mask) <= P*Exp_msk1)
3475
#endif
3476
          {
3477
          if (word0(&rv0) == Tiny0
3478
           && word1(&rv0) == Tiny1) {
3479
            if (bc.nd >nd) {
3480
              bc.uflchk = 1;
3481
              break;
3482
              }
3483
            goto undfl;
3484
            }
3485
          word0(&rv) = Tiny0;
3486
          word1(&rv) = Tiny1;
3487
          goto cont;
3488
          }
3489
        else
3490
          word0(&rv) -= P*Exp_msk1;
3491
        }
3492
      else {
3493
        adj.d = aadj1 * ulp(&rv);
3494
        dval(&rv) += adj.d;
3495
        }
3496
#else /*Sudden_Underflow*/
3497
      /* Compute adj so that the IEEE rounding rules will
3498
       * correctly round rv + adj in some half-way cases.
3499
       * If rv * ulp(rv) is denormalized (i.e.,
3500
       * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
3501
       * trouble from bits lost to denormalization;
3502
       * example: 1.2e-307 .
3503
       */
3504
      if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
3505
        aadj1 = (double)(int)(aadj + 0.5);
3506
        if (!bc.dsign)
3507
          aadj1 = -aadj1;
3508
        }
3509
      adj.d = aadj1 * ulp(&rv);
3510
      dval(&rv) += adj.d;
3511
#endif /*Sudden_Underflow*/
3512
#endif /*Avoid_Underflow*/
3513
348k
      }
3514
345k
    z = word0(&rv) & Exp_mask;
3515
345k
#ifndef SET_INEXACT
3516
345k
    if (bc.nd == nd) {
3517
286k
#ifdef Avoid_Underflow
3518
286k
    if (!bc.scale)
3519
125k
#endif
3520
125k
    if (y == z) {
3521
      /* Can we stop now? */
3522
123k
      L = (Long)aadj;
3523
123k
      aadj -= L;
3524
      /* The tolerances below are conservative. */
3525
123k
      if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
3526
79.9k
        if (aadj < .4999999 || aadj > .5000001)
3527
75.8k
          break;
3528
79.9k
        }
3529
43.1k
      else if (aadj < .4999999/FLT_RADIX)
3530
43.1k
        break;
3531
123k
      }
3532
286k
    }
3533
226k
#endif
3534
228k
 cont:
3535
228k
    Bfree(bb);
3536
228k
    Bfree(bd);
3537
228k
    Bfree(bs);
3538
228k
    Bfree(delta);
3539
228k
    }
3540
664k
  Bfree(bb);
3541
664k
  Bfree(bd);
3542
664k
  Bfree(bs);
3543
664k
  Bfree(bd0);
3544
664k
  Bfree(delta);
3545
664k
#ifndef NO_STRTOD_BIGCOMP
3546
664k
  if (req_bigcomp) {
3547
77.9k
    bd0 = 0;
3548
77.9k
    bc.e0 += nz1;
3549
77.9k
    bigcomp(&rv, s0, &bc);
3550
77.9k
    y = word0(&rv) & Exp_mask;
3551
77.9k
    if (y == Exp_mask)
3552
740
      goto ovfl;
3553
77.2k
    if (y == 0 && rv.d == 0.)
3554
8.17k
      goto undfl;
3555
77.2k
    }
3556
655k
#endif
3557
#ifdef SET_INEXACT
3558
  if (bc.inexact) {
3559
    if (!oldinexact) {
3560
      word0(&rv0) = Exp_1 + (70 << Exp_shift);
3561
      word1(&rv0) = 0;
3562
      dval(&rv0) += 1.;
3563
      }
3564
    }
3565
  else if (!oldinexact)
3566
    clear_inexact();
3567
#endif
3568
655k
#ifdef Avoid_Underflow
3569
655k
  if (bc.scale) {
3570
209k
    word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
3571
209k
    word1(&rv0) = 0;
3572
209k
    dval(&rv) *= dval(&rv0);
3573
#ifndef NO_ERRNO
3574
    /* try to avoid the bug of testing an 8087 register value */
3575
#ifdef IEEE_Arith
3576
    if (!(word0(&rv) & Exp_mask))
3577
#else
3578
    if (word0(&rv) == 0 && word1(&rv) == 0)
3579
#endif
3580
      errno = ERANGE;
3581
#endif
3582
209k
    }
3583
655k
#endif /* Avoid_Underflow */
3584
#ifdef SET_INEXACT
3585
  if (bc.inexact && !(word0(&rv) & Exp_mask)) {
3586
    /* set underflow bit */
3587
    dval(&rv0) = 1e-300;
3588
    dval(&rv0) *= dval(&rv0);
3589
    }
3590
#endif
3591
1.12M
 ret:
3592
1.12M
  if (se)
3593
117k
    *se = (char *)s;
3594
1.12M
  return sign ? -dval(&rv) : dval(&rv);
3595
655k
  }
3596
3597
#if !defined(MULTIPLE_THREADS) && !defined(dtoa_result)
3598
 ZEND_TLS char *dtoa_result;
3599
#endif
3600
3601
 static char *
3602
#ifdef KR_headers
3603
rv_alloc(i) int i;
3604
#else
3605
rv_alloc(int i)
3606
#endif
3607
2.21M
{
3608
3609
2.21M
  int j, k, *r;
3610
2.21M
  size_t rem;
3611
3612
2.21M
  rem = sizeof(Bigint) - sizeof(ULong) - sizeof(int);
3613
3614
3615
2.21M
  j = sizeof(ULong);
3616
2.21M
  if (i > ((INT_MAX >> 2) + rem))
3617
0
    i = (INT_MAX >> 2) + rem;
3618
2.21M
  for(k = 0;
3619
2.21M
    rem + j <= (size_t)i; j <<= 1)
3620
70
      k++;
3621
3622
2.21M
  r = (int*)Balloc(k);
3623
2.21M
  *r = k;
3624
2.21M
  return
3625
2.21M
#ifndef MULTIPLE_THREADS
3626
2.21M
  dtoa_result =
3627
2.21M
#endif
3628
2.21M
    (char *)(r+1);
3629
2.21M
  }
3630
3631
 static char *
3632
#ifdef KR_headers
3633
nrv_alloc(s, rve, n) char *s, **rve; int n;
3634
#else
3635
nrv_alloc(const char *s, char **rve, int n)
3636
#endif
3637
142k
{
3638
142k
  char *rv, *t;
3639
3640
142k
  t = rv = rv_alloc(n);
3641
453k
  while((*t = *s++)) t++;
3642
142k
  if (rve)
3643
0
    *rve = t;
3644
142k
  return rv;
3645
142k
  }
3646
3647
/* freedtoa(s) must be used to free values s returned by dtoa
3648
 * when MULTIPLE_THREADS is #defined.  It should be used in all cases,
3649
 * but for consistency with earlier versions of dtoa, it is optional
3650
 * when MULTIPLE_THREADS is not defined.
3651
 */
3652
3653
ZEND_API void
3654
#ifdef KR_headers
3655
zend_freedtoa(s) char *s;
3656
#else
3657
zend_freedtoa(char *s)
3658
#endif
3659
2.21M
{
3660
2.21M
  Bigint *b = (Bigint *)((int *)s - 1);
3661
2.21M
  b->maxwds = 1 << (b->k = *(int*)b);
3662
2.21M
  Bfree(b);
3663
2.21M
#ifndef MULTIPLE_THREADS
3664
2.21M
  if (s == dtoa_result)
3665
2.21M
    dtoa_result = 0;
3666
2.21M
#endif
3667
2.21M
  }
3668
3669
/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
3670
 *
3671
 * Inspired by "How to Print Floating-Point Numbers Accurately" by
3672
 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
3673
 *
3674
 * Modifications:
3675
 *  1. Rather than iterating, we use a simple numeric overestimate
3676
 *     to determine k = floor(log10(d)).  We scale relevant
3677
 *     quantities using O(log2(k)) rather than O(k) multiplications.
3678
 *  2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
3679
 *     try to generate digits strictly left to right.  Instead, we
3680
 *     compute with fewer bits and propagate the carry if necessary
3681
 *     when rounding the final digit up.  This is often faster.
3682
 *  3. Under the assumption that input will be rounded nearest,
3683
 *     mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
3684
 *     That is, we allow equality in stopping tests when the
3685
 *     round-nearest rule will give the same floating-point value
3686
 *     as would satisfaction of the stopping test with strict
3687
 *     inequality.
3688
 *  4. We remove common factors of powers of 2 from relevant
3689
 *     quantities.
3690
 *  5. When converting floating-point integers less than 1e16,
3691
 *     we use floating-point arithmetic rather than resorting
3692
 *     to multiple-precision integers.
3693
 *  6. When asked to produce fewer than 15 digits, we first try
3694
 *     to get by with floating-point arithmetic; we resort to
3695
 *     multiple-precision integer arithmetic only if we cannot
3696
 *     guarantee that the floating-point calculation has given
3697
 *     the correctly rounded result.  For k requested digits and
3698
 *     "uniformly" distributed input, the probability is
3699
 *     something like 10^(k-15) that we must resort to the Long
3700
 *     calculation.
3701
 */
3702
3703
/* trunc(dval(&u)) keeps the per-digit remainder chain in the FP unit instead of a
3704
 * FP->int->FP round-trip, cutting the digit loop's critical-path latency and
3705
 * speeding up fixed-precision float->string ((string)/echo/printf/number_format).
3706
 * Only a win where trunc() lowers to one instruction (aarch64 frintz, x86
3707
 * roundsd), elsewhere it's a call, so keep (Long)(dval(&u)). */
3708
#if defined(__SSE4_1__) || defined(__aarch64__)
3709
# define FAST_TRUNC
3710
#endif
3711
3712
ZEND_API char *zend_dtoa(double dd, int mode, int ndigits, int *decpt, bool *sign, char **rve)
3713
2.21M
{
3714
 /* Arguments ndigits, decpt, sign are similar to those
3715
  of ecvt and fcvt; trailing zeros are suppressed from
3716
  the returned string.  If not null, *rve is set to point
3717
  to the end of the return value.  If d is +-Infinity or NaN,
3718
  then *decpt is set to 9999.
3719
3720
  mode:
3721
    0 ==> shortest string that yields d when read in
3722
      and rounded to nearest.
3723
    1 ==> like 0, but with Steele & White stopping rule;
3724
      e.g. with IEEE P754 arithmetic , mode 0 gives
3725
      1e23 whereas mode 1 gives 9.999999999999999e22.
3726
    2 ==> max(1,ndigits) significant digits.  This gives a
3727
      return value similar to that of ecvt, except
3728
      that trailing zeros are suppressed.
3729
    3 ==> through ndigits past the decimal point.  This
3730
      gives a return value similar to that from fcvt,
3731
      except that trailing zeros are suppressed, and
3732
      ndigits can be negative.
3733
    4,5 ==> similar to 2 and 3, respectively, but (in
3734
      round-nearest mode) with the tests of mode 0 to
3735
      possibly return a shorter string that rounds to d.
3736
      With IEEE arithmetic and compilation with
3737
      -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
3738
      as modes 2 and 3 when FLT_ROUNDS != 1.
3739
    6-9 ==> Debugging modes similar to mode - 4:  don't try
3740
      fast floating-point estimate (if applicable).
3741
3742
    Values of mode other than 0-9 are treated as mode 0.
3743
3744
    Sufficient space is allocated to the return value
3745
    to hold the suppressed trailing zeros.
3746
  */
3747
3748
2.21M
  int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1,
3749
2.21M
    j, j1 = 0, k, k0, k_check, leftright, m2, m5, s2, s5,
3750
2.21M
    spec_case = 0, try_quick;
3751
2.21M
  Long L;
3752
2.21M
#ifndef Sudden_Underflow
3753
2.21M
  int denorm;
3754
2.21M
  ULong x;
3755
2.21M
#endif
3756
2.21M
  Bigint *b, *b1, *delta, *mlo, *mhi, *S;
3757
2.21M
  U d2, eps, u;
3758
2.21M
  double ds;
3759
#ifdef FAST_TRUNC
3760
  double tr;
3761
#endif
3762
2.21M
  char *s, *s0;
3763
2.21M
#ifndef No_leftright
3764
2.21M
#ifdef IEEE_Arith
3765
2.21M
  U eps1;
3766
2.21M
#endif
3767
2.21M
#endif
3768
#ifdef SET_INEXACT
3769
  int inexact, oldinexact;
3770
#endif
3771
#ifdef Honor_FLT_ROUNDS /*{*/
3772
  int Rounding;
3773
#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
3774
  Rounding = Flt_Rounds;
3775
#else /*}{*/
3776
  Rounding = 1;
3777
  switch(fegetround()) {
3778
    case FE_TOWARDZERO: Rounding = 0; break;
3779
    case FE_UPWARD: Rounding = 2; break;
3780
    case FE_DOWNWARD: Rounding = 3;
3781
    }
3782
#endif /*}}*/
3783
#endif /*}*/
3784
3785
2.21M
#ifndef MULTIPLE_THREADS
3786
2.21M
  if (dtoa_result) {
3787
0
    zend_freedtoa(dtoa_result);
3788
0
    dtoa_result = 0;
3789
0
    }
3790
2.21M
#endif
3791
3792
2.21M
  u.d = dd;
3793
2.21M
  if (word0(&u) & Sign_bit) {
3794
    /* set sign for everything, including 0's and NaNs */
3795
79.6k
    *sign = 1;
3796
79.6k
    word0(&u) &= ~Sign_bit; /* clear sign bit */
3797
79.6k
    }
3798
2.13M
  else
3799
2.13M
    *sign = 0;
3800
3801
2.21M
#if defined(IEEE_Arith) + defined(VAX)
3802
2.21M
#ifdef IEEE_Arith
3803
2.21M
  if ((word0(&u) & Exp_mask) == Exp_mask)
3804
#else
3805
  if (word0(&u)  == 0x8000)
3806
#endif
3807
24.3k
    {
3808
    /* Infinity or NaN */
3809
24.3k
    *decpt = 9999;
3810
24.3k
#ifdef IEEE_Arith
3811
24.3k
    if (!word1(&u) && !(word0(&u) & 0xfffff))
3812
24.1k
      return nrv_alloc("Infinity", rve, 8);
3813
198
#endif
3814
198
    return nrv_alloc("NaN", rve, 3);
3815
24.3k
    }
3816
2.19M
#endif
3817
#ifdef IBM
3818
  dval(&u) += 0; /* normalize */
3819
#endif
3820
2.19M
  if (!dval(&u)) {
3821
117k
    *decpt = 1;
3822
117k
    return nrv_alloc("0", rve, 1);
3823
117k
    }
3824
3825
#ifdef SET_INEXACT
3826
  try_quick = oldinexact = get_inexact();
3827
  inexact = 1;
3828
#endif
3829
#ifdef Honor_FLT_ROUNDS
3830
  if (Rounding >= 2) {
3831
    if (*sign)
3832
      Rounding = Rounding == 2 ? 0 : 2;
3833
    else
3834
      if (Rounding != 2)
3835
        Rounding = 0;
3836
    }
3837
#endif
3838
3839
2.07M
  b = d2b(&u, &be, &bbits);
3840
#ifdef Sudden_Underflow
3841
  i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
3842
#else
3843
2.07M
  if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
3844
2.03M
#endif
3845
2.03M
    dval(&d2) = dval(&u);
3846
2.03M
    word0(&d2) &= Frac_mask1;
3847
2.03M
    word0(&d2) |= Exp_11;
3848
#ifdef IBM
3849
    if (j = 11 - hi0bits(word0(&d2) & Frac_mask))
3850
      dval(&d2) /= 1 << j;
3851
#endif
3852
3853
    /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
3854
     * log10(x)  =  log(x) / log(10)
3855
     *    ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
3856
     * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
3857
     *
3858
     * This suggests computing an approximation k to log10(d) by
3859
     *
3860
     * k = (i - Bias)*0.301029995663981
3861
     *  + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
3862
     *
3863
     * We want k to be too large rather than too small.
3864
     * The error in the first-order Taylor series approximation
3865
     * is in our favor, so we just round up the constant enough
3866
     * to compensate for any error in the multiplication of
3867
     * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
3868
     * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
3869
     * adding 1e-13 to the constant term more than suffices.
3870
     * Hence we adjust the constant term to 0.1760912590558.
3871
     * (We could get a more accurate k by invoking log10,
3872
     *  but this is probably not worthwhile.)
3873
     */
3874
3875
2.03M
    i -= Bias;
3876
#ifdef IBM
3877
    i <<= 2;
3878
    i += j;
3879
#endif
3880
2.03M
#ifndef Sudden_Underflow
3881
2.03M
    denorm = 0;
3882
2.03M
    }
3883
42.9k
  else {
3884
    /* d is denormalized */
3885
3886
42.9k
    i = bbits + be + (Bias + (P-1) - 1);
3887
42.9k
    x = i > 32  ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
3888
42.9k
          : word1(&u) << (32 - i);
3889
42.9k
    dval(&d2) = x;
3890
42.9k
    word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
3891
42.9k
    i -= (Bias + (P-1) - 1) + 1;
3892
42.9k
    denorm = 1;
3893
42.9k
    }
3894
2.07M
#endif
3895
2.07M
  ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
3896
2.07M
  k = (int)ds;
3897
2.07M
  if (ds < 0. && ds != k)
3898
198k
    k--; /* want k = floor(ds) */
3899
2.07M
  k_check = 1;
3900
2.07M
  if (k >= 0 && k <= Ten_pmax) {
3901
1.77M
    if (dval(&u) < tens[k])
3902
6.90k
      k--;
3903
1.77M
    k_check = 0;
3904
1.77M
    }
3905
2.07M
  j = bbits - i - 1;
3906
2.07M
  if (j >= 0) {
3907
1.85M
    b2 = 0;
3908
1.85M
    s2 = j;
3909
1.85M
    }
3910
222k
  else {
3911
222k
    b2 = -j;
3912
222k
    s2 = 0;
3913
222k
    }
3914
2.07M
  if (k >= 0) {
3915
1.87M
    b5 = 0;
3916
1.87M
    s5 = k;
3917
1.87M
    s2 += k;
3918
1.87M
    }
3919
199k
  else {
3920
199k
    b2 -= k;
3921
199k
    b5 = -k;
3922
199k
    s5 = 0;
3923
199k
    }
3924
2.07M
  if (mode < 0 || mode > 9)
3925
0
    mode = 0;
3926
3927
2.07M
#ifndef SET_INEXACT
3928
#ifdef Check_FLT_ROUNDS
3929
  try_quick = Rounding == 1;
3930
#else
3931
2.07M
  try_quick = 1;
3932
2.07M
#endif
3933
2.07M
#endif /*SET_INEXACT*/
3934
3935
2.07M
  if (mode > 5) {
3936
0
    mode -= 4;
3937
0
    try_quick = 0;
3938
0
    }
3939
2.07M
  leftright = 1;
3940
2.07M
  ilim = ilim1 = -1;  /* Values for cases 0 and 1; done here to */
3941
        /* silence erroneous "gcc -Wall" warning. */
3942
2.07M
  switch(mode) {
3943
1.77M
    case 0:
3944
1.77M
    case 1:
3945
1.77M
      i = 18;
3946
1.77M
      ndigits = 0;
3947
1.77M
      break;
3948
302k
    case 2:
3949
302k
      leftright = 0;
3950
302k
      ZEND_FALLTHROUGH;
3951
302k
    case 4:
3952
302k
      if (ndigits <= 0)
3953
0
        ndigits = 1;
3954
302k
      ilim = ilim1 = i = ndigits;
3955
302k
      break;
3956
445
    case 3:
3957
445
      leftright = 0;
3958
445
      ZEND_FALLTHROUGH;
3959
445
    case 5:
3960
445
      i = ndigits + k + 1;
3961
445
      ilim = i;
3962
445
      ilim1 = i - 1;
3963
445
      if (i <= 0)
3964
143
        i = 1;
3965
2.07M
    }
3966
2.07M
  s = s0 = rv_alloc(i);
3967
3968
#ifdef Honor_FLT_ROUNDS
3969
  if (mode > 1 && Rounding != 1)
3970
    leftright = 0;
3971
#endif
3972
3973
2.07M
  if (ilim >= 0 && ilim <= Quick_max && try_quick) {
3974
3975
    /* Try to get by with floating-point arithmetic. */
3976
3977
302k
    i = 0;
3978
302k
    dval(&d2) = dval(&u);
3979
302k
    k0 = k;
3980
302k
    ilim0 = ilim;
3981
302k
    ieps = 2; /* conservative */
3982
302k
    if (k > 0) {
3983
202k
      ds = tens[k&0xf];
3984
202k
      j = k >> 4;
3985
202k
      if (j & Bletch) {
3986
        /* prevent overflows */
3987
439
        j &= Bletch - 1;
3988
439
        dval(&u) /= bigtens[n_bigtens-1];
3989
439
        ieps++;
3990
439
        }
3991
242k
      for(; j; j >>= 1, i++)
3992
40.3k
        if (j & 1) {
3993
27.9k
          ieps++;
3994
27.9k
          ds *= bigtens[i];
3995
27.9k
          }
3996
202k
      dval(&u) /= ds;
3997
202k
      }
3998
100k
    else if ((j1 = -k)) {
3999
85.1k
      dval(&u) *= tens[j1 & 0xf];
4000
344k
      for(j = j1 >> 4; j; j >>= 1, i++)
4001
259k
        if (j & 1) {
4002
139k
          ieps++;
4003
139k
          dval(&u) *= bigtens[i];
4004
139k
          }
4005
85.1k
      }
4006
302k
    if (k_check && dval(&u) < 1. && ilim > 0) {
4007
23.2k
      if (ilim1 <= 0)
4008
23
        goto fast_failed;
4009
23.2k
      ilim = ilim1;
4010
23.2k
      k--;
4011
23.2k
      dval(&u) *= 10.;
4012
23.2k
      ieps++;
4013
23.2k
      }
4014
302k
    dval(&eps) = ieps*dval(&u) + 7.;
4015
302k
    word0(&eps) -= (P-1)*Exp_msk1;
4016
302k
    if (ilim == 0) {
4017
23
      S = mhi = 0;
4018
23
      dval(&u) -= 5.;
4019
23
      if (dval(&u) > dval(&eps))
4020
10
        goto one_digit;
4021
13
      if (dval(&u) < -dval(&eps))
4022
13
        goto no_digits;
4023
0
      goto fast_failed;
4024
13
      }
4025
302k
#ifndef No_leftright
4026
302k
    if (leftright) {
4027
      /* Use Steele & White method of only
4028
       * generating digits needed.
4029
       */
4030
0
      dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
4031
0
#ifdef IEEE_Arith
4032
0
      if (k0 < 0 && j1 >= 307) {
4033
0
        eps1.d = 1.01e256; /* 1.01 allows roundoff in the next few lines */
4034
0
        word0(&eps1) -= Exp_msk1 * (Bias+P-1);
4035
0
        dval(&eps1) *= tens[j1 & 0xf];
4036
0
        for(i = 0, j = (j1-256) >> 4; j; j >>= 1, i++)
4037
0
          if (j & 1)
4038
0
            dval(&eps1) *= bigtens[i];
4039
0
        if (eps.d < eps1.d)
4040
0
          eps.d = eps1.d;
4041
0
        }
4042
0
#endif
4043
0
      for(i = 0;;) {
4044
0
        L = dval(&u);
4045
0
        dval(&u) -= L;
4046
0
        *s++ = '0' + (int)L;
4047
0
        if (1. - dval(&u) < dval(&eps))
4048
0
          goto bump_up;
4049
0
        if (dval(&u) < dval(&eps))
4050
0
          goto ret1;
4051
0
        if (++i >= ilim)
4052
0
          break;
4053
0
        dval(&eps) *= 10.;
4054
0
        dval(&u) *= 10.;
4055
0
        }
4056
0
      }
4057
302k
    else {
4058
302k
#endif
4059
      /* Generate ilim digits, then fix them up. */
4060
302k
      dval(&eps) *= tens[ilim-1];
4061
3.79M
      for(i = 1;; i++, dval(&u) *= 10.) {
4062
#ifdef FAST_TRUNC
4063
        tr = trunc(dval(&u));
4064
        L = (Long)tr;
4065
        if (!(dval(&u) -= tr))
4066
          ilim = i;
4067
#else
4068
3.79M
        L = (Long)(dval(&u));
4069
3.79M
        if (!(dval(&u) -= L))
4070
34.1k
          ilim = i;
4071
3.79M
#endif
4072
3.79M
        *s++ = '0' + (int)L;
4073
3.79M
        if (i == ilim) {
4074
302k
          if (dval(&u) > 0.5 + dval(&eps))
4075
52.7k
            goto bump_up;
4076
249k
          else if (dval(&u) < 0.5 - dval(&eps)) {
4077
743k
            while(*--s == '0');
4078
132k
            s++;
4079
132k
            goto ret1;
4080
132k
            }
4081
117k
          break;
4082
302k
          }
4083
3.79M
        }
4084
302k
#ifndef No_leftright
4085
302k
      }
4086
117k
#endif
4087
117k
 fast_failed:
4088
117k
    s = s0;
4089
117k
    dval(&u) = dval(&d2);
4090
117k
    k = k0;
4091
117k
    ilim = ilim0;
4092
117k
    }
4093
4094
  /* Do we have a "small" integer? */
4095
4096
1.89M
  if (be >= 0 && k <= Int_max) {
4097
    /* Yes. */
4098
108k
    ds = tens[k];
4099
108k
    if (ndigits < 0 && ilim <= 0) {
4100
0
      S = mhi = 0;
4101
0
      if (ilim < 0 || dval(&u) <= 5*ds)
4102
0
        goto no_digits;
4103
0
      goto one_digit;
4104
0
      }
4105
499k
    for(i = 1;; i++, dval(&u) *= 10.) {
4106
#ifdef FAST_TRUNC
4107
      tr = trunc(dval(&u) / ds);
4108
      L = (Long)tr;
4109
      dval(&u) -= tr*ds;
4110
#else
4111
499k
      L = (Long)(dval(&u) / ds);
4112
499k
      dval(&u) -= L*ds;
4113
499k
#endif
4114
#ifdef Check_FLT_ROUNDS
4115
      /* If FLT_ROUNDS == 2, L will usually be high by 1 */
4116
      if (dval(&u) < 0) {
4117
        L--;
4118
        dval(&u) += ds;
4119
        }
4120
#endif
4121
499k
      *s++ = '0' + (int)L;
4122
499k
      if (!dval(&u)) {
4123
#ifdef SET_INEXACT
4124
        inexact = 0;
4125
#endif
4126
107k
        break;
4127
107k
        }
4128
391k
      if (i == ilim) {
4129
#ifdef Honor_FLT_ROUNDS
4130
        if (mode > 1)
4131
        switch(Rounding) {
4132
          case 0: goto ret1;
4133
          case 2: goto bump_up;
4134
          }
4135
#endif
4136
1.05k
        dval(&u) += dval(&u);
4137
#ifdef ROUND_BIASED
4138
        if (dval(&u) >= ds)
4139
#else
4140
1.05k
        if (dval(&u) > ds || (dval(&u) == ds && L & 1))
4141
327
#endif
4142
327
          {
4143
53.0k
 bump_up:
4144
250k
          while(*--s == '9')
4145
197k
            if (s == s0) {
4146
479
              k++;
4147
479
              *s = '0';
4148
479
              break;
4149
479
              }
4150
53.0k
          ++*s++;
4151
53.0k
          }
4152
53.7k
        break;
4153
1.05k
        }
4154
391k
      }
4155
160k
    goto ret1;
4156
108k
    }
4157
4158
1.78M
  m2 = b2;
4159
1.78M
  m5 = b5;
4160
1.78M
  mhi = mlo = 0;
4161
1.78M
  if (leftright) {
4162
1.66M
    i =
4163
1.66M
#ifndef Sudden_Underflow
4164
1.66M
      denorm ? be + (Bias + (P-1) - 1 + 1) :
4165
1.66M
#endif
4166
#ifdef IBM
4167
      1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
4168
#else
4169
1.66M
      1 + P - bbits;
4170
1.66M
#endif
4171
1.66M
    b2 += i;
4172
1.66M
    s2 += i;
4173
1.66M
    mhi = i2b(1);
4174
1.66M
    }
4175
1.78M
  if (m2 > 0 && s2 > 0) {
4176
246k
    i = m2 < s2 ? m2 : s2;
4177
246k
    b2 -= i;
4178
246k
    m2 -= i;
4179
246k
    s2 -= i;
4180
246k
    }
4181
1.78M
  if (b5 > 0) {
4182
138k
    if (leftright) {
4183
114k
      if (m5 > 0) {
4184
114k
        mhi = pow5mult(mhi, m5);
4185
114k
        b1 = mult(mhi, b);
4186
114k
        Bfree(b);
4187
114k
        b = b1;
4188
114k
        }
4189
114k
      if ((j = b5 - m5))
4190
0
        b = pow5mult(b, j);
4191
114k
      }
4192
23.9k
    else
4193
23.9k
      b = pow5mult(b, b5);
4194
138k
    }
4195
1.78M
  S = i2b(1);
4196
1.78M
  if (s5 > 0)
4197
1.63M
    S = pow5mult(S, s5);
4198
4199
  /* Check for special case that d is a normalized power of 2. */
4200
4201
1.78M
  spec_case = 0;
4202
1.78M
  if ((mode < 2 || leftright)
4203
#ifdef Honor_FLT_ROUNDS
4204
      && Rounding == 1
4205
#endif
4206
1.78M
        ) {
4207
1.66M
    if (!word1(&u) && !(word0(&u) & Bndry_mask)
4208
6.70k
#ifndef Sudden_Underflow
4209
6.70k
     && word0(&u) & (Exp_mask & ~Exp_msk1)
4210
1.66M
#endif
4211
1.66M
        ) {
4212
      /* The special case */
4213
6.48k
      b2 += Log2P;
4214
6.48k
      s2 += Log2P;
4215
6.48k
      spec_case = 1;
4216
6.48k
      }
4217
1.66M
    }
4218
4219
  /* Arrange for convenient computation of quotients:
4220
   * shift left if necessary so divisor has 4 leading 0 bits.
4221
   *
4222
   * Perhaps we should just compute leading 28 bits of S once
4223
   * and for all and pass them and a shift to quorem, so it
4224
   * can do shifts and ORs to compute the numerator for q.
4225
   */
4226
1.78M
  i = dshift(S, s2);
4227
1.78M
  b2 += i;
4228
1.78M
  m2 += i;
4229
1.78M
  s2 += i;
4230
1.78M
  if (b2 > 0)
4231
1.76M
    b = lshift(b, b2);
4232
1.78M
  if (s2 > 0)
4233
1.76M
    S = lshift(S, s2);
4234
1.78M
  if (k_check) {
4235
230k
    if (cmp(b,S) < 0) {
4236
27.7k
      k--;
4237
27.7k
      b = multadd(b, 10, 0);  /* we botched the k estimate */
4238
27.7k
      if (leftright)
4239
5.38k
        mhi = multadd(mhi, 10, 0);
4240
27.7k
      ilim = ilim1;
4241
27.7k
      }
4242
230k
    }
4243
1.78M
  if (ilim <= 0 && (mode == 3 || mode == 5)) {
4244
143
    if (ilim < 0 || cmp(b,S = multadd(S,5,0)) <= 0) {
4245
      /* no digits, fcvt style */
4246
133
 no_digits:
4247
133
      k = -1 - ndigits;
4248
133
      goto ret;
4249
120
      }
4250
33
 one_digit:
4251
33
    *s++ = '1';
4252
33
    k++;
4253
33
    goto ret;
4254
143
    }
4255
1.78M
  if (leftright) {
4256
1.66M
    if (m2 > 0)
4257
1.66M
      mhi = lshift(mhi, m2);
4258
4259
    /* Compute mlo -- check for special case
4260
     * that d is a normalized power of 2.
4261
     */
4262
4263
1.66M
    mlo = mhi;
4264
1.66M
    if (spec_case) {
4265
6.48k
      mhi = Balloc(mhi->k);
4266
6.48k
      Bcopy(mhi, mlo);
4267
6.48k
      mhi = lshift(mhi, Log2P);
4268
6.48k
      }
4269
4270
16.6M
    for(i = 1;;i++) {
4271
16.6M
      dig = quorem(b,S) + '0';
4272
      /* Do we yet have the shortest decimal string
4273
       * that will round to d?
4274
       */
4275
16.6M
      j = cmp(b, mlo);
4276
16.6M
      delta = diff(S, mhi);
4277
16.6M
      j1 = delta->sign ? 1 : cmp(b, delta);
4278
16.6M
      Bfree(delta);
4279
16.6M
#ifndef ROUND_BIASED
4280
16.6M
      if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
4281
#ifdef Honor_FLT_ROUNDS
4282
        && Rounding >= 1
4283
#endif
4284
16.6M
                   ) {
4285
2.00k
        if (dig == '9')
4286
275
          goto round_9_up;
4287
1.73k
        if (j > 0)
4288
1.12k
          dig++;
4289
#ifdef SET_INEXACT
4290
        else if (!b->x[0] && b->wds <= 1)
4291
          inexact = 0;
4292
#endif
4293
1.73k
        *s++ = dig;
4294
1.73k
        goto ret;
4295
2.00k
        }
4296
16.6M
#endif
4297
16.6M
      if (j < 0 || (j == 0 && mode != 1
4298
7.53k
#ifndef ROUND_BIASED
4299
7.53k
              && !(word1(&u) & 1)
4300
15.5M
#endif
4301
15.5M
          )) {
4302
1.16M
        if (!b->x[0] && b->wds <= 1) {
4303
#ifdef SET_INEXACT
4304
          inexact = 0;
4305
#endif
4306
498k
          goto accept_dig;
4307
498k
          }
4308
#ifdef Honor_FLT_ROUNDS
4309
        if (mode > 1)
4310
         switch(Rounding) {
4311
          case 0: goto accept_dig;
4312
          case 2: goto keep_dig;
4313
          }
4314
#endif /*Honor_FLT_ROUNDS*/
4315
670k
        if (j1 > 0) {
4316
229k
          b = lshift(b, 1);
4317
229k
          j1 = cmp(b, S);
4318
#ifdef ROUND_BIASED
4319
          if (j1 >= 0 /*)*/
4320
#else
4321
229k
          if ((j1 > 0 || (j1 == 0 && dig & 1))
4322
172k
#endif
4323
172k
          && dig++ == '9')
4324
268
            goto round_9_up;
4325
229k
          }
4326
1.16M
 accept_dig:
4327
1.16M
        *s++ = dig;
4328
1.16M
        goto ret;
4329
670k
        }
4330
15.5M
      if (j1 > 0) {
4331
#ifdef Honor_FLT_ROUNDS
4332
        if (!Rounding)
4333
          goto accept_dig;
4334
#endif
4335
495k
        if (dig == '9') { /* possible if i == 1 */
4336
3.06k
 round_9_up:
4337
3.06k
          *s++ = '9';
4338
3.06k
          goto roundoff;
4339
2.52k
          }
4340
492k
        *s++ = dig + 1;
4341
492k
        goto ret;
4342
495k
        }
4343
#ifdef Honor_FLT_ROUNDS
4344
 keep_dig:
4345
#endif
4346
15.0M
      *s++ = dig;
4347
15.0M
      if (i == ilim)
4348
0
        break;
4349
15.0M
      b = multadd(b, 10, 0);
4350
15.0M
      if (mlo == mhi)
4351
14.9M
        mlo = mhi = multadd(mhi, 10, 0);
4352
93.9k
      else {
4353
93.9k
        mlo = multadd(mlo, 10, 0);
4354
93.9k
        mhi = multadd(mhi, 10, 0);
4355
93.9k
        }
4356
15.0M
      }
4357
1.66M
    }
4358
116k
  else
4359
1.62M
    for(i = 1;; i++) {
4360
1.62M
      *s++ = dig = quorem(b,S) + '0';
4361
1.62M
      if (!b->x[0] && b->wds <= 1) {
4362
#ifdef SET_INEXACT
4363
        inexact = 0;
4364
#endif
4365
59
        goto ret;
4366
59
        }
4367
1.62M
      if (i >= ilim)
4368
116k
        break;
4369
1.51M
      b = multadd(b, 10, 0);
4370
1.51M
      }
4371
4372
  /* Round off last digit */
4373
4374
#ifdef Honor_FLT_ROUNDS
4375
  switch(Rounding) {
4376
    case 0: goto trimzeros;
4377
    case 2: goto roundoff;
4378
    }
4379
#endif
4380
116k
  b = lshift(b, 1);
4381
116k
  j = cmp(b, S);
4382
#ifdef ROUND_BIASED
4383
  if (j >= 0)
4384
#else
4385
116k
  if (j > 0 || (j == 0 && dig & 1))
4386
43.1k
#endif
4387
43.1k
    {
4388
46.1k
 roundoff:
4389
91.1k
    while(*--s == '9')
4390
48.4k
      if (s == s0) {
4391
3.54k
        k++;
4392
3.54k
        *s++ = '1';
4393
3.54k
        goto ret;
4394
3.54k
        }
4395
42.6k
    ++*s++;
4396
42.6k
    }
4397
73.0k
  else {
4398
#ifdef Honor_FLT_ROUNDS
4399
 trimzeros:
4400
#endif
4401
145k
    while(*--s == '0');
4402
73.0k
    s++;
4403
73.0k
    }
4404
1.78M
 ret:
4405
1.78M
  Bfree(S);
4406
1.78M
  if (mhi) {
4407
1.66M
    if (mlo && mlo != mhi)
4408
6.48k
      Bfree(mlo);
4409
1.66M
    Bfree(mhi);
4410
1.66M
    }
4411
2.07M
 ret1:
4412
#ifdef SET_INEXACT
4413
  if (inexact) {
4414
    if (!oldinexact) {
4415
      word0(&u) = Exp_1 + (70 << Exp_shift);
4416
      word1(&u) = 0;
4417
      dval(&u) += 1.;
4418
      }
4419
    }
4420
  else if (!oldinexact)
4421
    clear_inexact();
4422
#endif
4423
2.07M
  Bfree(b);
4424
2.07M
  *s = 0;
4425
2.07M
  *decpt = k + 1;
4426
2.07M
  if (rve)
4427
445
    *rve = s;
4428
2.07M
  return s0;
4429
1.78M
  }
4430
4431
ZEND_API double zend_hex_strtod(const char *str, const char **endptr)
4432
923
{
4433
923
  const char *s = str;
4434
923
  char c;
4435
923
  int any = 0;
4436
923
  double value = 0;
4437
4438
923
  if (*s == '0' && (s[1] == 'x' || s[1] == 'X')) {
4439
0
    s += 2;
4440
0
  }
4441
4442
19.9k
  while ((c = *s++)) {
4443
19.4k
    if (c >= '0' && c <= '9') {
4444
15.4k
      c -= '0';
4445
15.4k
    } else if (c >= 'A' && c <= 'F') {
4446
1.41k
      c -= 'A' - 10;
4447
2.50k
    } else if (c >= 'a' && c <= 'f') {
4448
2.15k
      c -= 'a' - 10;
4449
2.15k
    } else {
4450
351
      break;
4451
351
    }
4452
4453
19.0k
    any = 1;
4454
19.0k
    value = value * 16 + c;
4455
19.0k
  }
4456
4457
923
  if (endptr != NULL) {
4458
923
    *endptr = any ? s - 1 : str;
4459
923
  }
4460
4461
923
  return value;
4462
923
}
4463
4464
ZEND_API double zend_oct_strtod(const char *str, const char **endptr)
4465
781
{
4466
781
  const char *s = str;
4467
781
  char c;
4468
781
  double value = 0;
4469
781
  int any = 0;
4470
4471
781
  if (str[0] == '\0') {
4472
0
    if (endptr != NULL) {
4473
0
      *endptr = str;
4474
0
    }
4475
0
    return 0.0;
4476
0
  }
4477
4478
22.9k
  while ((c = *s++)) {
4479
22.6k
    if (c < '0' || c > '7') {
4480
      /* break and return the current value if the number is not well-formed
4481
       * that's what Linux strtol() does
4482
       */
4483
390
      break;
4484
390
    }
4485
22.2k
    value = value * 8 + c - '0';
4486
22.2k
    any = 1;
4487
22.2k
  }
4488
4489
781
  if (endptr != NULL) {
4490
781
    *endptr = any ? s - 1 : str;
4491
781
  }
4492
4493
781
  return value;
4494
781
}
4495
4496
ZEND_API double zend_bin_strtod(const char *str, const char **endptr)
4497
419
{
4498
419
  const char *s = str;
4499
419
  char    c;
4500
419
  double    value = 0;
4501
419
  int     any = 0;
4502
4503
419
  if ('0' == *s && ('b' == s[1] || 'B' == s[1])) {
4504
0
    s += 2;
4505
0
  }
4506
4507
29.3k
  while ((c = *s++)) {
4508
    /*
4509
     * Verify the validity of the current character as a base-2 digit.  In
4510
     * the event that an invalid digit is found, halt the conversion and
4511
     * return the portion which has been converted thus far.
4512
     */
4513
29.1k
    if ('0' == c || '1' == c)
4514
28.9k
      value = value * 2 + c - '0';
4515
219
    else
4516
219
      break;
4517
4518
28.9k
    any = 1;
4519
28.9k
  }
4520
4521
  /*
4522
   * As with many strtoX implementations, should the subject sequence be
4523
   * empty or not well-formed, no conversion is performed and the original
4524
   * value of str is stored in *endptr, provided that endptr is not a null
4525
   * pointer.
4526
   */
4527
419
  if (NULL != endptr) {
4528
419
    *endptr = (char *)(any ? s - 1 : str);
4529
419
  }
4530
4531
419
  return value;
4532
419
}
4533
4534
ZEND_API char *zend_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf)
4535
2.21M
{
4536
2.21M
  char *digits, *dst, *src;
4537
2.21M
  int i, decpt;
4538
2.21M
  bool sign;
4539
2.21M
  int mode = ndigit >= 0 ? 2 : 0;
4540
4541
2.21M
  if (mode == 0) {
4542
1.78M
    ndigit = 17;
4543
1.78M
  }
4544
2.21M
  digits = zend_dtoa(value, mode, ndigit, &decpt, &sign, NULL);
4545
2.21M
  if (decpt == 9999) {
4546
    /*
4547
     * Infinity or NaN, convert to inf or nan with sign.
4548
     * We assume the buffer is at least ndigit long.
4549
     */
4550
24.3k
    snprintf(buf, ndigit + 1, "%s%s", (sign && *digits == 'I') ? "-" : "", *digits == 'I' ? "INF" : "NAN");
4551
24.3k
    zend_freedtoa(digits);
4552
24.3k
    return (buf);
4553
24.3k
  }
4554
4555
2.19M
  dst = buf;
4556
2.19M
  if (sign) {
4557
79.2k
    *dst++ = '-';
4558
79.2k
  }
4559
4560
2.19M
  if ((decpt >= 0 && decpt > ndigit) || decpt < -3) { /* use E-style */
4561
    /* exponential format (e.g. 1.2345e+13) */
4562
302k
    if (--decpt < 0) {
4563
168k
      sign = true;
4564
168k
      decpt = -decpt;
4565
168k
    } else {
4566
134k
      sign = false;
4567
134k
    }
4568
302k
    src = digits;
4569
302k
    *dst++ = *src++;
4570
302k
    *dst++ = dec_point;
4571
302k
    if (*src == '\0') {
4572
10.2k
      *dst++ = '0';
4573
292k
    } else {
4574
3.76M
      do {
4575
3.76M
        *dst++ = *src++;
4576
3.76M
      } while (*src != '\0');
4577
292k
    }
4578
302k
    *dst++ = exponent;
4579
302k
    if (sign) {
4580
168k
      *dst++ = '-';
4581
168k
    } else {
4582
134k
      *dst++ = '+';
4583
134k
    }
4584
302k
    if (decpt < 10) {
4585
4.16k
      *dst++ = '0' + decpt;
4586
4.16k
      *dst = '\0';
4587
298k
    } else {
4588
      /* XXX - optimize */
4589
298k
      int n;
4590
728k
      for (n = decpt, i = 0; (n /= 10) != 0; i++);
4591
298k
      dst[i + 1] = '\0';
4592
1.02M
      while (decpt != 0) {
4593
728k
        dst[i--] = '0' + decpt % 10;
4594
728k
        decpt /= 10;
4595
728k
      }
4596
298k
    }
4597
1.89M
  } else if (decpt < 0) {
4598
    /* standard format 0. */
4599
8.60k
    *dst++ = '0';   /* zero before decimal point */
4600
8.60k
    *dst++ = dec_point;
4601
14.6k
    do {
4602
14.6k
      *dst++ = '0';
4603
14.6k
    } while (++decpt < 0);
4604
8.60k
    src = digits;
4605
74.7k
    while (*src != '\0') {
4606
66.1k
      *dst++ = *src++;
4607
66.1k
    }
4608
8.60k
    *dst = '\0';
4609
1.88M
  } else {
4610
    /* standard format */
4611
12.1M
    for (i = 0, src = digits; i < decpt; i++) {
4612
10.3M
      if (*src != '\0') {
4613
10.1M
        *dst++ = *src++;
4614
10.1M
      } else {
4615
189k
        *dst++ = '0';
4616
189k
      }
4617
10.3M
    }
4618
1.88M
    if (*src != '\0') {
4619
1.49M
      if (src == digits) {
4620
22.5k
        *dst++ = '0';   /* zero before decimal point */
4621
22.5k
      }
4622
1.49M
      *dst++ = dec_point;
4623
7.40M
      for (i = decpt; digits[i] != '\0'; i++) {
4624
5.90M
        *dst++ = digits[i];
4625
5.90M
      }
4626
1.49M
    }
4627
1.88M
    *dst = '\0';
4628
1.88M
  }
4629
2.19M
  zend_freedtoa(digits);
4630
2.19M
  return (buf);
4631
2.21M
}
4632
4633
static void destroy_freelist(void)
4634
0
{
4635
0
  int i;
4636
0
  Bigint *tmp;
4637
4638
0
  ACQUIRE_DTOA_LOCK(0)
4639
0
  for (i = 0; i <= Kmax; i++) {
4640
0
    Bigint **listp = &freelist[i];
4641
0
    while ((tmp = *listp) != NULL) {
4642
0
      *listp = tmp->next;
4643
0
      FREE(tmp);
4644
0
    }
4645
0
    freelist[i] = NULL;
4646
0
  }
4647
0
  FREE_DTOA_LOCK(0)
4648
0
}
4649
4650
static void free_p5s(void)
4651
0
{
4652
0
  Bigint **listp, *tmp;
4653
4654
0
  ACQUIRE_DTOA_LOCK(1)
4655
0
  listp = &p5s;
4656
0
  while ((tmp = *listp) != NULL) {
4657
0
    *listp = tmp->next;
4658
0
    FREE(tmp);
4659
0
  }
4660
0
  p5s = NULL;
4661
0
  FREE_DTOA_LOCK(1)
4662
0
}