Coverage Report

Created: 2022-10-06 21:30

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