Coverage Report

Created: 2026-05-30 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/moddable/xs/sources/xsdtoa.c
Line
Count
Source
1
/*
2
 * Copyright (c) 2016-2026  Moddable Tech, Inc.
3
 *
4
 *   This file is part of the Moddable SDK Runtime.
5
 * 
6
 *   The Moddable SDK Runtime is free software: you can redistribute it and/or modify
7
 *   it under the terms of the GNU Lesser General Public License as published by
8
 *   the Free Software Foundation, either version 3 of the License, or
9
 *   (at your option) any later version.
10
 * 
11
 *   The Moddable SDK Runtime is distributed in the hope that it will be useful,
12
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *   GNU Lesser General Public License for more details.
15
 * 
16
 *   You should have received a copy of the GNU Lesser General Public License
17
 *   along with the Moddable SDK Runtime.  If not, see <http://www.gnu.org/licenses/>.
18
 *
19
 * This file incorporates work covered by the following copyright and  
20
 * permission notice:  
21
 *
22
 *       Copyright (C) 2010-2016 Marvell International Ltd.
23
 *       Copyright (C) 2002-2010 Kinoma, Inc.
24
 *
25
 *       Licensed under the Apache License, Version 2.0 (the "License");
26
 *       you may not use this file except in compliance with the License.
27
 *       You may obtain a copy of the License at
28
 *
29
 *        http://www.apache.org/licenses/LICENSE-2.0
30
 *
31
 *       Unless required by applicable law or agreed to in writing, software
32
 *       distributed under the License is distributed on an "AS IS" BASIS,
33
 *       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34
 *       See the License for the specific language governing permissions and
35
 *       limitations under the License.
36
 */
37
38
#include "xsAll.h"
39
40
#if mxNoChunks
41
#define mxUseChunkHeap 0
42
#else
43
#define mxUseChunkHeap 1
44
#endif
45
46
#define __XS__ 1
47
627M
#define __XS__a , DTOA
48
#define __XS__d , ThInfo* DTOA
49
50
0
#define FREE fxDTOAFree
51
#define INFNAN_CHECK
52
83.7M
#define MALLOC fxDTOAMalloc
53
#define NO_BF96
54
#define NO_HEX_FP
55
#define Omit_Private_Memory
56
#define ROUND_BIASED
57
58
#if mxLittleEndian
59
#define IEEE_8087
60
#endif
61
#if mxBigEndian
62
#define IEEE_MC68k
63
#endif
64
#if ((defined(__GNUC__) && defined(__LP64__)) || (defined(_MSC_VER) && defined(_M_X64)))
65
#else
66
#define NO_LONG_LONG
67
#endif
68
69
static void* fxDTOAMalloc(size_t size, void* it);
70
static void fxDTOAFree(void* block, void* it);
71
72
#undef assert
73
74
/****************************************************************
75
 *
76
 * The author of this software is David M. Gay.
77
 *
78
 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
79
 *
80
 * Permission to use, copy, modify, and distribute this software for any
81
 * purpose without fee is hereby granted, provided that this entire notice
82
 * is included in all copies of any software which is or includes a copy
83
 * or modification of this software and in all copies of the supporting
84
 * documentation for such software.
85
 *
86
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
87
 * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
88
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
89
 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
90
 *
91
 ***************************************************************/
92
93
/* Please send bug reports to David M. Gay (dmg at acm dot org,
94
 * with " at " changed at "@" and " dot " changed to ".").  */
95
96
/* On a machine with IEEE extended-precision registers, it is
97
 * necessary to specify double-precision (53-bit) rounding precision
98
 * before invoking strtod or dtoa.  If the machine uses (the equivalent
99
 * of) Intel 80x87 arithmetic, the call
100
 *  _control87(PC_53, MCW_PC);
101
 * does this with many compilers.  Whether this or another call is
102
 * appropriate depends on the compiler; for this to work, it may be
103
 * necessary to #include "float.h" or another system-dependent header
104
 * file.
105
 */
106
107
/* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
108
 * (Note that IEEE arithmetic is disabled by gcc's -ffast-math flag.)
109
 *
110
 * This strtod returns a nearest machine number to the input decimal
111
 * string (or sets errno to ERANGE).  With IEEE arithmetic, ties are
112
 * broken by the IEEE round-even rule.  Otherwise ties are broken by
113
 * biased rounding (add half and chop).
114
 *
115
 * Inspired loosely by William D. Clinger's paper "How to Read Floating
116
 * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
117
 *
118
 * Modifications:
119
 *
120
 *  1. We only require IEEE, IBM, or VAX double-precision
121
 *    arithmetic (not IEEE double-extended).
122
 *  2. We get by with floating-point arithmetic in a case that
123
 *    Clinger missed -- when we're computing d * 10^n
124
 *    for a small integer d and the integer n is not too
125
 *    much larger than 22 (the maximum integer k for which
126
 *    we can represent 10^k exactly), we may be able to
127
 *    compute (d*10^k) * 10^(e-k) with just one roundoff.
128
 *  3. Rather than a bit-at-a-time adjustment of the binary
129
 *    result in the hard case, we use floating-point
130
 *    arithmetic to determine the adjustment to within
131
 *    one bit; only in really hard cases do we need to
132
 *    compute a second residual.
133
 *  4. Because of 3., we don't need a large table of powers of 10
134
 *    for ten-to-e (just some small tables, e.g. of 10^k
135
 *    for 0 <= k <= 22).
136
 */
137
138
/*
139
 * #define IEEE_8087 for IEEE-arithmetic machines where the least
140
 *  significant byte has the lowest address.
141
 * #define IEEE_MC68k for IEEE-arithmetic machines where the most
142
 *  significant byte has the lowest address.
143
 * #define Long int on machines with 32-bit ints and 64-bit longs.
144
 * #define IBM for IBM mainframe-style floating-point arithmetic.
145
 * #define VAX for VAX-style floating-point arithmetic (D_floating).
146
 * #define No_leftright to omit left-right logic in fast floating-point
147
 *  computation of dtoa.  This will cause dtoa modes 4 and 5 to be
148
 *  treated the same as modes 2 and 3 for some inputs.
149
 * #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
150
 *  and strtod and dtoa should round accordingly.  Unless Trust_FLT_ROUNDS
151
 *  is also #defined, fegetround() will be queried for the rounding mode.
152
 *  Note that both FLT_ROUNDS and fegetround() are specified by the C99
153
 *  standard (and are specified to be consistent, with fesetround()
154
 *  affecting the value of FLT_ROUNDS), but that some (Linux) systems
155
 *  do not work correctly in this regard, so using fegetround() is more
156
 *  portable than using FLT_ROUNDS directly.
157
 * #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
158
 *  and Honor_FLT_ROUNDS is not #defined.
159
 * #define RND_PRODQUOT to use rnd_prod and rnd_quot (assembly routines
160
 *  that use extended-precision instructions to compute rounded
161
 *  products and quotients) with IBM.
162
 * #define ROUND_BIASED for IEEE-format with biased rounding and arithmetic
163
 *  that rounds toward +Infinity.
164
 * #define ROUND_BIASED_without_Round_Up for IEEE-format with biased
165
 *  rounding when the underlying floating-point arithmetic uses
166
 *  unbiased rounding.  This prevent using ordinary floating-point
167
 *  arithmetic when the result could be computed with one rounding error.
168
 * #define Inaccurate_Divide for IEEE-format with correctly rounded
169
 *  products but inaccurate quotients, e.g., for Intel i860.
170
 * #define NO_LONG_LONG on machines that do not have a "long long"
171
 *  integer type (of >= 64 bits).  On such machines, you can
172
 *  #define Just_16 to store 16 bits per 32-bit Long when doing
173
 *  high-precision integer arithmetic.  Whether this speeds things
174
 *  up or slows things down depends on the machine and the number
175
 *  being converted.  If long long is available and the name is
176
 *  something other than "long long", #define Llong to be the name,
177
 *  and if "unsigned Llong" does not work as an unsigned version of
178
 *  Llong, #define #ULLong to be the corresponding unsigned type.
179
 * #define Bad_float_h if your system lacks a float.h or if it does not
180
 *  define some or all of DBL_DIG, DBL_MAX_10_EXP, DBL_MAX_EXP,
181
 *  FLT_RADIX, FLT_ROUNDS, and DBL_MAX.
182
 * #define MALLOC your_malloc, where your_malloc(n) acts like malloc(n)
183
 *  if memory is available and otherwise does something you deem
184
 *  appropriate.  If MALLOC is undefined, malloc will be invoked
185
 *  directly -- and assumed always to succeed.  Similarly, if you
186
 *  want something other than the system's free() to be called to
187
 *  recycle memory acquired from MALLOC, #define FREE to be the
188
 *  name of the alternate routine.  (FREE or free is only called in
189
 *  pathological cases, e.g., in a dtoa call after a dtoa return in
190
 *  mode 3 with thousands of digits requested.)
191
 * #define Omit_Private_Memory to omit logic (added Jan. 1998) for making
192
 *  memory allocations from a private pool of memory when possible.
193
 *  When used, the private pool is PRIVATE_MEM bytes long:  2304 bytes,
194
 *  unless #defined to be a different length.  This default length
195
 *  suffices to get rid of MALLOC calls except for unusual cases,
196
 *  such as decimal-to-binary conversion of a very long string of
197
 *  digits.  The longest string dtoa can return is about 751 bytes
198
 *  long.  For conversions by strtod of strings of 800 digits and
199
 *  all dtoa conversions in single-threaded executions with 8-byte
200
 *  pointers, PRIVATE_MEM >= 7400 appears to suffice; with 4-byte
201
 *  pointers, PRIVATE_MEM >= 7112 appears adequate.
202
 * #define NO_INFNAN_CHECK if you do not wish to have INFNAN_CHECK
203
 *  #defined automatically on IEEE systems.  On such systems,
204
 *  when INFNAN_CHECK is #defined, strtod checks
205
 *  for Infinity and NaN (case insensitively).  On some systems
206
 *  (e.g., some HP systems), it may be necessary to #define NAN_WORD0
207
 *  appropriately -- to the most significant word of a quiet NaN.
208
 *  (On HP Series 700/800 machines, -DNAN_WORD0=0x7ff40000 works.)
209
 *  When INFNAN_CHECK is #defined and No_Hex_NaN is not #defined,
210
 *  strtod also accepts (case insensitively) strings of the form
211
 *  NaN(x), where x is a string of hexadecimal digits and spaces;
212
 *  if there is only one string of hexadecimal digits, it is taken
213
 *  for the 52 fraction bits of the resulting NaN; if there are two
214
 *  or more strings of hex digits, the first is for the high 20 bits,
215
 *  the second and subsequent for the low 32 bits, with intervening
216
 *  white space ignored; but if this results in none of the 52
217
 *  fraction bits being on (an IEEE Infinity symbol), then NAN_WORD0
218
 *  and NAN_WORD1 are used instead.
219
 * #define MULTIPLE_THREADS if the system offers preemptively scheduled
220
 *  multiple threads.  In this case, you must provide (or suitably
221
 *  #define) two locks, acquired by ACQUIRE_DTOA_LOCK(n) and freed
222
 *  by FREE_DTOA_LOCK(n) for n = 0 or 1.  (The second lock, accessed
223
 *  in pow5mult, ensures lazy evaluation of only one copy of high
224
 *  powers of 5; omitting this lock would introduce a small
225
 *  probability of wasting memory, but would otherwise be harmless.)
226
 *  You must also invoke freedtoa(s) to free the value s returned by
227
 *  dtoa.  You may do so whether or not MULTIPLE_THREADS is #defined.
228
229
 *  When MULTIPLE_THREADS is #defined, this source file provides
230
 *    void set_max_dtoa_threads(unsigned int n);
231
 *  and expects
232
 *    unsigned int dtoa_get_threadno(void);
233
 *  to be available (possibly provided by
234
 *    #define dtoa_get_threadno omp_get_thread_num
235
 *  if OpenMP is in use or by
236
 *    #define dtoa_get_threadno pthread_self
237
 *  if Pthreads is in use), to return the current thread number.
238
 *  If set_max_dtoa_threads(n) was called and the current thread
239
 *  number is k with k < n, then calls on ACQUIRE_DTOA_LOCK(...) and
240
 *  FREE_DTOA_LOCK(...) are avoided; instead each thread with thread
241
 *  number < n has a separate copy of relevant data structures.
242
 *  After set_max_dtoa_threads(n), a call set_max_dtoa_threads(m)
243
 *  with m <= n has has no effect, but a call with m > n is honored.
244
 *  Such a call invokes REALLOC (assumed to be "realloc" if REALLOC
245
 *  is not #defined) to extend the size of the relevant array.
246
247
 * #define NO_IEEE_Scale to disable new (Feb. 1997) logic in strtod that
248
 *  avoids underflows on inputs whose result does not underflow.
249
 *  If you #define NO_IEEE_Scale on a machine that uses IEEE-format
250
 *  floating-point numbers and flushes underflows to zero rather
251
 *  than implementing gradual underflow, then you must also #define
252
 *  Sudden_Underflow.
253
 * #define USE_LOCALE to use the current locale's decimal_point value.
254
 * #define SET_INEXACT if IEEE arithmetic is being used and extra
255
 *  computation should be done to set the inexact flag when the
256
 *  result is inexact and avoid setting inexact when the result
257
 *  is exact.  In this case, dtoa.c must be compiled in
258
 *  an environment, perhaps provided by #include "dtoa.c" in a
259
 *  suitable wrapper, that defines two functions,
260
 *    int get_inexact(void);
261
 *    void clear_inexact(void);
262
 *  such that get_inexact() returns a nonzero value if the
263
 *  inexact bit is already set, and clear_inexact() sets the
264
 *  inexact bit to 0.  When SET_INEXACT is #defined, strtod
265
 *  also does extra computations to set the underflow and overflow
266
 *  flags when appropriate (i.e., when the result is tiny and
267
 *  inexact or when it is a numeric value rounded to +-infinity).
268
 * #define NO_ERRNO if strtod should not assign errno = ERANGE when
269
 *  the result overflows to +-Infinity or underflows to 0.
270
 *  When errno should be assigned, under seemingly rare conditions
271
 *  it may be necessary to define Set_errno(x) suitably, e.g., in
272
 *  a local errno.h, such as
273
 *    #include <errno.h>
274
 *    #define Set_errno(x) _set_errno(x)
275
 * #define NO_HEX_FP to omit recognition of hexadecimal floating-point
276
 *  values by strtod.
277
 * #define NO_STRTOD_BIGCOMP (on IEEE-arithmetic systems only for now)
278
 *  to disable logic for "fast" testing of very long input strings
279
 *  to strtod.  This testing proceeds by initially truncating the
280
 *  input string, then if necessary comparing the whole string with
281
 *  a decimal expansion to decide close cases. This logic is only
282
 *  used for input more than STRTOD_DIGLIM digits long (default 40).
283
 */
284
285
#ifndef Long
286
32.9M
#define Long int
287
#endif
288
#ifndef ULong
289
typedef unsigned Long ULong;
290
#endif
291
292
#ifdef DEBUG
293
#include <assert.h>
294
#include "stdio.h"
295
#define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
296
#define Debug(x) x
297
int dtoa_stats[7]; /* strtod_{64,96,bigcomp},dtoa_{exact,64,96,bigcomp} */
298
#else
299
#define assert(x) /*nothing*/
300
#define Debug(x) /*nothing*/
301
#endif
302
303
#include "stdlib.h"
304
#include "string.h"
305
306
#ifdef USE_LOCALE
307
#include "locale.h"
308
#endif
309
310
#ifdef Honor_FLT_ROUNDS
311
#ifndef Trust_FLT_ROUNDS
312
#include <fenv.h>
313
#endif
314
#endif
315
316
#ifdef __cplusplus
317
extern "C" {
318
#endif
319
#ifdef MALLOC
320
extern void *MALLOC(size_t, void*);
321
#else
322
#define MALLOC malloc
323
#endif
324
325
#ifdef REALLOC
326
extern void *REALLOC(void*,size_t);
327
#else
328
#define REALLOC realloc
329
#endif
330
331
#ifndef FREE
332
#define FREE free
333
#endif
334
335
#ifdef __cplusplus
336
  }
337
#endif
338
339
#ifndef Omit_Private_Memory
340
#ifndef PRIVATE_MEM
341
#define PRIVATE_MEM 2304
342
#endif
343
#define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
344
static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
345
#endif
346
347
#undef IEEE_Arith
348
#undef Avoid_Underflow
349
#ifdef IEEE_MC68k
350
#define IEEE_Arith
351
#endif
352
#ifdef IEEE_8087
353
#define IEEE_Arith
354
#endif
355
356
#ifdef IEEE_Arith
357
#ifndef NO_INFNAN_CHECK
358
#undef INFNAN_CHECK
359
#define INFNAN_CHECK
360
#endif
361
#else
362
#undef INFNAN_CHECK
363
#define NO_STRTOD_BIGCOMP
364
#endif
365
366
#include "errno.h"
367
368
#ifdef NO_ERRNO /*{*/
369
#undef Set_errno
370
#define Set_errno(x)
371
#else
372
#ifndef Set_errno
373
122k
#define Set_errno(x) errno = x
374
#endif
375
#endif /*}*/
376
377
#ifdef Bad_float_h
378
379
#ifdef IEEE_Arith
380
#define DBL_DIG 15
381
#define DBL_MAX_10_EXP 308
382
#define DBL_MAX_EXP 1024
383
#define FLT_RADIX 2
384
#endif /*IEEE_Arith*/
385
386
#ifdef IBM
387
#define DBL_DIG 16
388
#define DBL_MAX_10_EXP 75
389
#define DBL_MAX_EXP 63
390
#define FLT_RADIX 16
391
#define DBL_MAX 7.2370055773322621e+75
392
#endif
393
394
#ifdef VAX
395
#define DBL_DIG 16
396
#define DBL_MAX_10_EXP 38
397
#define DBL_MAX_EXP 127
398
#define FLT_RADIX 2
399
#define DBL_MAX 1.7014118346046923e+38
400
#endif
401
402
#ifndef LONG_MAX
403
#define LONG_MAX 2147483647
404
#endif
405
406
#else /* ifndef Bad_float_h */
407
#include "float.h"
408
#endif /* Bad_float_h */
409
410
#ifndef __MATH_H__
411
#include "math.h"
412
#endif
413
414
#ifdef __cplusplus
415
extern "C" {
416
#endif
417
418
#if defined(IEEE_8087) + defined(IEEE_MC68k) + defined(VAX) + defined(IBM) != 1
419
Exactly one of IEEE_8087, IEEE_MC68k, VAX, or IBM should be defined.
420
#endif
421
422
#undef USE_BF96
423
424
#ifdef NO_LONG_LONG /*{{*/
425
#undef ULLong
426
#ifdef Just_16
427
#undef Pack_32
428
/* When Pack_32 is not defined, we store 16 bits per 32-bit Long.
429
 * This makes some inner loops simpler and sometimes saves work
430
 * during multiplications, but it often seems to make things slightly
431
 * slower.  Hence the default is now to store 32 bits per Long.
432
 */
433
#endif
434
#else /*}{ long long available */
435
#ifndef Llong
436
#define Llong long long
437
#endif
438
#ifndef ULLong
439
244M
#define ULLong unsigned Llong
440
#endif
441
#ifndef NO_BF96 /*{*/
442
#define USE_BF96
443
444
#ifdef SET_INEXACT
445
#define dtoa_divmax 27
446
#else
447
int dtoa_divmax = 2;  /* Permit experimenting: on some systems, 64-bit integer */
448
      /* division is slow enough that we may sometimes want to */
449
      /* avoid using it.   We assume (but do not check) that   */
450
      /* dtoa_divmax <= 27.*/
451
#endif
452
453
typedef struct BF96 {   /* Normalized 96-bit software floating point numbers */
454
  unsigned int b0,b1,b2;  /* b0 = most significant, binary point just to its left */
455
  int e;      /* number represented = b * 2^e, with .5 <= b < 1 */
456
  } BF96;
457
458
 static BF96 pten[667] = {
459
  { 0xeef453d6, 0x923bd65a, 0x113faa29, -1136 },
460
  { 0x9558b466, 0x1b6565f8, 0x4ac7ca59, -1132 },
461
  { 0xbaaee17f, 0xa23ebf76, 0x5d79bcf0, -1129 },
462
  { 0xe95a99df, 0x8ace6f53, 0xf4d82c2c, -1126 },
463
  { 0x91d8a02b, 0xb6c10594, 0x79071b9b, -1122 },
464
  { 0xb64ec836, 0xa47146f9, 0x9748e282, -1119 },
465
  { 0xe3e27a44, 0x4d8d98b7, 0xfd1b1b23, -1116 },
466
  { 0x8e6d8c6a, 0xb0787f72, 0xfe30f0f5, -1112 },
467
  { 0xb208ef85, 0x5c969f4f, 0xbdbd2d33, -1109 },
468
  { 0xde8b2b66, 0xb3bc4723, 0xad2c7880, -1106 },
469
  { 0x8b16fb20, 0x3055ac76, 0x4c3bcb50, -1102 },
470
  { 0xaddcb9e8, 0x3c6b1793, 0xdf4abe24, -1099 },
471
  { 0xd953e862, 0x4b85dd78, 0xd71d6dad, -1096 },
472
  { 0x87d4713d, 0x6f33aa6b, 0x8672648c, -1092 },
473
  { 0xa9c98d8c, 0xcb009506, 0x680efdaf, -1089 },
474
  { 0xd43bf0ef, 0xfdc0ba48, 0x0212bd1b, -1086 },
475
  { 0x84a57695, 0xfe98746d, 0x014bb630, -1082 },
476
  { 0xa5ced43b, 0x7e3e9188, 0x419ea3bd, -1079 },
477
  { 0xcf42894a, 0x5dce35ea, 0x52064cac, -1076 },
478
  { 0x818995ce, 0x7aa0e1b2, 0x7343efeb, -1072 },
479
  { 0xa1ebfb42, 0x19491a1f, 0x1014ebe6, -1069 },
480
  { 0xca66fa12, 0x9f9b60a6, 0xd41a26e0, -1066 },
481
  { 0xfd00b897, 0x478238d0, 0x8920b098, -1063 },
482
  { 0x9e20735e, 0x8cb16382, 0x55b46e5f, -1059 },
483
  { 0xc5a89036, 0x2fddbc62, 0xeb2189f7, -1056 },
484
  { 0xf712b443, 0xbbd52b7b, 0xa5e9ec75, -1053 },
485
  { 0x9a6bb0aa, 0x55653b2d, 0x47b233c9, -1049 },
486
  { 0xc1069cd4, 0xeabe89f8, 0x999ec0bb, -1046 },
487
  { 0xf148440a, 0x256e2c76, 0xc00670ea, -1043 },
488
  { 0x96cd2a86, 0x5764dbca, 0x38040692, -1039 },
489
  { 0xbc807527, 0xed3e12bc, 0xc6050837, -1036 },
490
  { 0xeba09271, 0xe88d976b, 0xf7864a44, -1033 },
491
  { 0x93445b87, 0x31587ea3, 0x7ab3ee6a, -1029 },
492
  { 0xb8157268, 0xfdae9e4c, 0x5960ea05, -1026 },
493
  { 0xe61acf03, 0x3d1a45df, 0x6fb92487, -1023 },
494
  { 0x8fd0c162, 0x06306bab, 0xa5d3b6d4, -1019 },
495
  { 0xb3c4f1ba, 0x87bc8696, 0x8f48a489, -1016 },
496
  { 0xe0b62e29, 0x29aba83c, 0x331acdab, -1013 },
497
  { 0x8c71dcd9, 0xba0b4925, 0x9ff0c08b, -1009 },
498
  { 0xaf8e5410, 0x288e1b6f, 0x07ecf0ae, -1006 },
499
  { 0xdb71e914, 0x32b1a24a, 0xc9e82cd9, -1003 },
500
  { 0x892731ac, 0x9faf056e, 0xbe311c08,  -999 },
501
  { 0xab70fe17, 0xc79ac6ca, 0x6dbd630a,  -996 },
502
  { 0xd64d3d9d, 0xb981787d, 0x092cbbcc,  -993 },
503
  { 0x85f04682, 0x93f0eb4e, 0x25bbf560,  -989 },
504
  { 0xa76c5823, 0x38ed2621, 0xaf2af2b8,  -986 },
505
  { 0xd1476e2c, 0x07286faa, 0x1af5af66,  -983 },
506
  { 0x82cca4db, 0x847945ca, 0x50d98d9f,  -979 },
507
  { 0xa37fce12, 0x6597973c, 0xe50ff107,  -976 },
508
  { 0xcc5fc196, 0xfefd7d0c, 0x1e53ed49,  -973 },
509
  { 0xff77b1fc, 0xbebcdc4f, 0x25e8e89c,  -970 },
510
  { 0x9faacf3d, 0xf73609b1, 0x77b19161,  -966 },
511
  { 0xc795830d, 0x75038c1d, 0xd59df5b9,  -963 },
512
  { 0xf97ae3d0, 0xd2446f25, 0x4b057328,  -960 },
513
  { 0x9becce62, 0x836ac577, 0x4ee367f9,  -956 },
514
  { 0xc2e801fb, 0x244576d5, 0x229c41f7,  -953 },
515
  { 0xf3a20279, 0xed56d48a, 0x6b435275,  -950 },
516
  { 0x9845418c, 0x345644d6, 0x830a1389,  -946 },
517
  { 0xbe5691ef, 0x416bd60c, 0x23cc986b,  -943 },
518
  { 0xedec366b, 0x11c6cb8f, 0x2cbfbe86,  -940 },
519
  { 0x94b3a202, 0xeb1c3f39, 0x7bf7d714,  -936 },
520
  { 0xb9e08a83, 0xa5e34f07, 0xdaf5ccd9,  -933 },
521
  { 0xe858ad24, 0x8f5c22c9, 0xd1b3400f,  -930 },
522
  { 0x91376c36, 0xd99995be, 0x23100809,  -926 },
523
  { 0xb5854744, 0x8ffffb2d, 0xabd40a0c,  -923 },
524
  { 0xe2e69915, 0xb3fff9f9, 0x16c90c8f,  -920 },
525
  { 0x8dd01fad, 0x907ffc3b, 0xae3da7d9,  -916 },
526
  { 0xb1442798, 0xf49ffb4a, 0x99cd11cf,  -913 },
527
  { 0xdd95317f, 0x31c7fa1d, 0x40405643,  -910 },
528
  { 0x8a7d3eef, 0x7f1cfc52, 0x482835ea,  -906 },
529
  { 0xad1c8eab, 0x5ee43b66, 0xda324365,  -903 },
530
  { 0xd863b256, 0x369d4a40, 0x90bed43e,  -900 },
531
  { 0x873e4f75, 0xe2224e68, 0x5a7744a6,  -896 },
532
  { 0xa90de353, 0x5aaae202, 0x711515d0,  -893 },
533
  { 0xd3515c28, 0x31559a83, 0x0d5a5b44,  -890 },
534
  { 0x8412d999, 0x1ed58091, 0xe858790a,  -886 },
535
  { 0xa5178fff, 0x668ae0b6, 0x626e974d,  -883 },
536
  { 0xce5d73ff, 0x402d98e3, 0xfb0a3d21,  -880 },
537
  { 0x80fa687f, 0x881c7f8e, 0x7ce66634,  -876 },
538
  { 0xa139029f, 0x6a239f72, 0x1c1fffc1,  -873 },
539
  { 0xc9874347, 0x44ac874e, 0xa327ffb2,  -870 },
540
  { 0xfbe91419, 0x15d7a922, 0x4bf1ff9f,  -867 },
541
  { 0x9d71ac8f, 0xada6c9b5, 0x6f773fc3,  -863 },
542
  { 0xc4ce17b3, 0x99107c22, 0xcb550fb4,  -860 },
543
  { 0xf6019da0, 0x7f549b2b, 0x7e2a53a1,  -857 },
544
  { 0x99c10284, 0x4f94e0fb, 0x2eda7444,  -853 },
545
  { 0xc0314325, 0x637a1939, 0xfa911155,  -850 },
546
  { 0xf03d93ee, 0xbc589f88, 0x793555ab,  -847 },
547
  { 0x96267c75, 0x35b763b5, 0x4bc1558b,  -843 },
548
  { 0xbbb01b92, 0x83253ca2, 0x9eb1aaed,  -840 },
549
  { 0xea9c2277, 0x23ee8bcb, 0x465e15a9,  -837 },
550
  { 0x92a1958a, 0x7675175f, 0x0bfacd89,  -833 },
551
  { 0xb749faed, 0x14125d36, 0xcef980ec,  -830 },
552
  { 0xe51c79a8, 0x5916f484, 0x82b7e127,  -827 },
553
  { 0x8f31cc09, 0x37ae58d2, 0xd1b2ecb8,  -823 },
554
  { 0xb2fe3f0b, 0x8599ef07, 0x861fa7e6,  -820 },
555
  { 0xdfbdcece, 0x67006ac9, 0x67a791e0,  -817 },
556
  { 0x8bd6a141, 0x006042bd, 0xe0c8bb2c,  -813 },
557
  { 0xaecc4991, 0x4078536d, 0x58fae9f7,  -810 },
558
  { 0xda7f5bf5, 0x90966848, 0xaf39a475,  -807 },
559
  { 0x888f9979, 0x7a5e012d, 0x6d8406c9,  -803 },
560
  { 0xaab37fd7, 0xd8f58178, 0xc8e5087b,  -800 },
561
  { 0xd5605fcd, 0xcf32e1d6, 0xfb1e4a9a,  -797 },
562
  { 0x855c3be0, 0xa17fcd26, 0x5cf2eea0,  -793 },
563
  { 0xa6b34ad8, 0xc9dfc06f, 0xf42faa48,  -790 },
564
  { 0xd0601d8e, 0xfc57b08b, 0xf13b94da,  -787 },
565
  { 0x823c1279, 0x5db6ce57, 0x76c53d08,  -783 },
566
  { 0xa2cb1717, 0xb52481ed, 0x54768c4b,  -780 },
567
  { 0xcb7ddcdd, 0xa26da268, 0xa9942f5d,  -777 },
568
  { 0xfe5d5415, 0x0b090b02, 0xd3f93b35,  -774 },
569
  { 0x9efa548d, 0x26e5a6e1, 0xc47bc501,  -770 },
570
  { 0xc6b8e9b0, 0x709f109a, 0x359ab641,  -767 },
571
  { 0xf867241c, 0x8cc6d4c0, 0xc30163d2,  -764 },
572
  { 0x9b407691, 0xd7fc44f8, 0x79e0de63,  -760 },
573
  { 0xc2109436, 0x4dfb5636, 0x985915fc,  -757 },
574
  { 0xf294b943, 0xe17a2bc4, 0x3e6f5b7b,  -754 },
575
  { 0x979cf3ca, 0x6cec5b5a, 0xa705992c,  -750 },
576
  { 0xbd8430bd, 0x08277231, 0x50c6ff78,  -747 },
577
  { 0xece53cec, 0x4a314ebd, 0xa4f8bf56,  -744 },
578
  { 0x940f4613, 0xae5ed136, 0x871b7795,  -740 },
579
  { 0xb9131798, 0x99f68584, 0x28e2557b,  -737 },
580
  { 0xe757dd7e, 0xc07426e5, 0x331aeada,  -734 },
581
  { 0x9096ea6f, 0x3848984f, 0x3ff0d2c8,  -730 },
582
  { 0xb4bca50b, 0x065abe63, 0x0fed077a,  -727 },
583
  { 0xe1ebce4d, 0xc7f16dfb, 0xd3e84959,  -724 },
584
  { 0x8d3360f0, 0x9cf6e4bd, 0x64712dd7,  -720 },
585
  { 0xb080392c, 0xc4349dec, 0xbd8d794d,  -717 },
586
  { 0xdca04777, 0xf541c567, 0xecf0d7a0,  -714 },
587
  { 0x89e42caa, 0xf9491b60, 0xf41686c4,  -710 },
588
  { 0xac5d37d5, 0xb79b6239, 0x311c2875,  -707 },
589
  { 0xd77485cb, 0x25823ac7, 0x7d633293,  -704 },
590
  { 0x86a8d39e, 0xf77164bc, 0xae5dff9c,  -700 },
591
  { 0xa8530886, 0xb54dbdeb, 0xd9f57f83,  -697 },
592
  { 0xd267caa8, 0x62a12d66, 0xd072df63,  -694 },
593
  { 0x8380dea9, 0x3da4bc60, 0x4247cb9e,  -690 },
594
  { 0xa4611653, 0x8d0deb78, 0x52d9be85,  -687 },
595
  { 0xcd795be8, 0x70516656, 0x67902e27,  -684 },
596
  { 0x806bd971, 0x4632dff6, 0x00ba1cd8,  -680 },
597
  { 0xa086cfcd, 0x97bf97f3, 0x80e8a40e,  -677 },
598
  { 0xc8a883c0, 0xfdaf7df0, 0x6122cd12,  -674 },
599
  { 0xfad2a4b1, 0x3d1b5d6c, 0x796b8057,  -671 },
600
  { 0x9cc3a6ee, 0xc6311a63, 0xcbe33036,  -667 },
601
  { 0xc3f490aa, 0x77bd60fc, 0xbedbfc44,  -664 },
602
  { 0xf4f1b4d5, 0x15acb93b, 0xee92fb55,  -661 },
603
  { 0x99171105, 0x2d8bf3c5, 0x751bdd15,  -657 },
604
  { 0xbf5cd546, 0x78eef0b6, 0xd262d45a,  -654 },
605
  { 0xef340a98, 0x172aace4, 0x86fb8971,  -651 },
606
  { 0x9580869f, 0x0e7aac0e, 0xd45d35e6,  -647 },
607
  { 0xbae0a846, 0xd2195712, 0x89748360,  -644 },
608
  { 0xe998d258, 0x869facd7, 0x2bd1a438,  -641 },
609
  { 0x91ff8377, 0x5423cc06, 0x7b6306a3,  -637 },
610
  { 0xb67f6455, 0x292cbf08, 0x1a3bc84c,  -634 },
611
  { 0xe41f3d6a, 0x7377eeca, 0x20caba5f,  -631 },
612
  { 0x8e938662, 0x882af53e, 0x547eb47b,  -627 },
613
  { 0xb23867fb, 0x2a35b28d, 0xe99e619a,  -624 },
614
  { 0xdec681f9, 0xf4c31f31, 0x6405fa00,  -621 },
615
  { 0x8b3c113c, 0x38f9f37e, 0xde83bc40,  -617 },
616
  { 0xae0b158b, 0x4738705e, 0x9624ab50,  -614 },
617
  { 0xd98ddaee, 0x19068c76, 0x3badd624,  -611 },
618
  { 0x87f8a8d4, 0xcfa417c9, 0xe54ca5d7,  -607 },
619
  { 0xa9f6d30a, 0x038d1dbc, 0x5e9fcf4c,  -604 },
620
  { 0xd47487cc, 0x8470652b, 0x7647c320,  -601 },
621
  { 0x84c8d4df, 0xd2c63f3b, 0x29ecd9f4,  -597 },
622
  { 0xa5fb0a17, 0xc777cf09, 0xf4681071,  -594 },
623
  { 0xcf79cc9d, 0xb955c2cc, 0x7182148d,  -591 },
624
  { 0x81ac1fe2, 0x93d599bf, 0xc6f14cd8,  -587 },
625
  { 0xa21727db, 0x38cb002f, 0xb8ada00e,  -584 },
626
  { 0xca9cf1d2, 0x06fdc03b, 0xa6d90811,  -581 },
627
  { 0xfd442e46, 0x88bd304a, 0x908f4a16,  -578 },
628
  { 0x9e4a9cec, 0x15763e2e, 0x9a598e4e,  -574 },
629
  { 0xc5dd4427, 0x1ad3cdba, 0x40eff1e1,  -571 },
630
  { 0xf7549530, 0xe188c128, 0xd12bee59,  -568 },
631
  { 0x9a94dd3e, 0x8cf578b9, 0x82bb74f8,  -564 },
632
  { 0xc13a148e, 0x3032d6e7, 0xe36a5236,  -561 },
633
  { 0xf18899b1, 0xbc3f8ca1, 0xdc44e6c3,  -558 },
634
  { 0x96f5600f, 0x15a7b7e5, 0x29ab103a,  -554 },
635
  { 0xbcb2b812, 0xdb11a5de, 0x7415d448,  -551 },
636
  { 0xebdf6617, 0x91d60f56, 0x111b495b,  -548 },
637
  { 0x936b9fce, 0xbb25c995, 0xcab10dd9,  -544 },
638
  { 0xb84687c2, 0x69ef3bfb, 0x3d5d514f,  -541 },
639
  { 0xe65829b3, 0x046b0afa, 0x0cb4a5a3,  -538 },
640
  { 0x8ff71a0f, 0xe2c2e6dc, 0x47f0e785,  -534 },
641
  { 0xb3f4e093, 0xdb73a093, 0x59ed2167,  -531 },
642
  { 0xe0f218b8, 0xd25088b8, 0x306869c1,  -528 },
643
  { 0x8c974f73, 0x83725573, 0x1e414218,  -524 },
644
  { 0xafbd2350, 0x644eeacf, 0xe5d1929e,  -521 },
645
  { 0xdbac6c24, 0x7d62a583, 0xdf45f746,  -518 },
646
  { 0x894bc396, 0xce5da772, 0x6b8bba8c,  -514 },
647
  { 0xab9eb47c, 0x81f5114f, 0x066ea92f,  -511 },
648
  { 0xd686619b, 0xa27255a2, 0xc80a537b,  -508 },
649
  { 0x8613fd01, 0x45877585, 0xbd06742c,  -504 },
650
  { 0xa798fc41, 0x96e952e7, 0x2c481138,  -501 },
651
  { 0xd17f3b51, 0xfca3a7a0, 0xf75a1586,  -498 },
652
  { 0x82ef8513, 0x3de648c4, 0x9a984d73,  -494 },
653
  { 0xa3ab6658, 0x0d5fdaf5, 0xc13e60d0,  -491 },
654
  { 0xcc963fee, 0x10b7d1b3, 0x318df905,  -488 },
655
  { 0xffbbcfe9, 0x94e5c61f, 0xfdf17746,  -485 },
656
  { 0x9fd561f1, 0xfd0f9bd3, 0xfeb6ea8b,  -481 },
657
  { 0xc7caba6e, 0x7c5382c8, 0xfe64a52e,  -478 },
658
  { 0xf9bd690a, 0x1b68637b, 0x3dfdce7a,  -475 },
659
  { 0x9c1661a6, 0x51213e2d, 0x06bea10c,  -471 },
660
  { 0xc31bfa0f, 0xe5698db8, 0x486e494f,  -468 },
661
  { 0xf3e2f893, 0xdec3f126, 0x5a89dba3,  -465 },
662
  { 0x986ddb5c, 0x6b3a76b7, 0xf8962946,  -461 },
663
  { 0xbe895233, 0x86091465, 0xf6bbb397,  -458 },
664
  { 0xee2ba6c0, 0x678b597f, 0x746aa07d,  -455 },
665
  { 0x94db4838, 0x40b717ef, 0xa8c2a44e,  -451 },
666
  { 0xba121a46, 0x50e4ddeb, 0x92f34d62,  -448 },
667
  { 0xe896a0d7, 0xe51e1566, 0x77b020ba,  -445 },
668
  { 0x915e2486, 0xef32cd60, 0x0ace1474,  -441 },
669
  { 0xb5b5ada8, 0xaaff80b8, 0x0d819992,  -438 },
670
  { 0xe3231912, 0xd5bf60e6, 0x10e1fff6,  -435 },
671
  { 0x8df5efab, 0xc5979c8f, 0xca8d3ffa,  -431 },
672
  { 0xb1736b96, 0xb6fd83b3, 0xbd308ff8,  -428 },
673
  { 0xddd0467c, 0x64bce4a0, 0xac7cb3f6,  -425 },
674
  { 0x8aa22c0d, 0xbef60ee4, 0x6bcdf07a,  -421 },
675
  { 0xad4ab711, 0x2eb3929d, 0x86c16c98,  -418 },
676
  { 0xd89d64d5, 0x7a607744, 0xe871c7bf,  -415 },
677
  { 0x87625f05, 0x6c7c4a8b, 0x11471cd7,  -411 },
678
  { 0xa93af6c6, 0xc79b5d2d, 0xd598e40d,  -408 },
679
  { 0xd389b478, 0x79823479, 0x4aff1d10,  -405 },
680
  { 0x843610cb, 0x4bf160cb, 0xcedf722a,  -401 },
681
  { 0xa54394fe, 0x1eedb8fe, 0xc2974eb4,  -398 },
682
  { 0xce947a3d, 0xa6a9273e, 0x733d2262,  -395 },
683
  { 0x811ccc66, 0x8829b887, 0x0806357d,  -391 },
684
  { 0xa163ff80, 0x2a3426a8, 0xca07c2dc,  -388 },
685
  { 0xc9bcff60, 0x34c13052, 0xfc89b393,  -385 },
686
  { 0xfc2c3f38, 0x41f17c67, 0xbbac2078,  -382 },
687
  { 0x9d9ba783, 0x2936edc0, 0xd54b944b,  -378 },
688
  { 0xc5029163, 0xf384a931, 0x0a9e795e,  -375 },
689
  { 0xf64335bc, 0xf065d37d, 0x4d4617b5,  -372 },
690
  { 0x99ea0196, 0x163fa42e, 0x504bced1,  -368 },
691
  { 0xc06481fb, 0x9bcf8d39, 0xe45ec286,  -365 },
692
  { 0xf07da27a, 0x82c37088, 0x5d767327,  -362 },
693
  { 0x964e858c, 0x91ba2655, 0x3a6a07f8,  -358 },
694
  { 0xbbe226ef, 0xb628afea, 0x890489f7,  -355 },
695
  { 0xeadab0ab, 0xa3b2dbe5, 0x2b45ac74,  -352 },
696
  { 0x92c8ae6b, 0x464fc96f, 0x3b0b8bc9,  -348 },
697
  { 0xb77ada06, 0x17e3bbcb, 0x09ce6ebb,  -345 },
698
  { 0xe5599087, 0x9ddcaabd, 0xcc420a6a,  -342 },
699
  { 0x8f57fa54, 0xc2a9eab6, 0x9fa94682,  -338 },
700
  { 0xb32df8e9, 0xf3546564, 0x47939822,  -335 },
701
  { 0xdff97724, 0x70297ebd, 0x59787e2b,  -332 },
702
  { 0x8bfbea76, 0xc619ef36, 0x57eb4edb,  -328 },
703
  { 0xaefae514, 0x77a06b03, 0xede62292,  -325 },
704
  { 0xdab99e59, 0x958885c4, 0xe95fab36,  -322 },
705
  { 0x88b402f7, 0xfd75539b, 0x11dbcb02,  -318 },
706
  { 0xaae103b5, 0xfcd2a881, 0xd652bdc2,  -315 },
707
  { 0xd59944a3, 0x7c0752a2, 0x4be76d33,  -312 },
708
  { 0x857fcae6, 0x2d8493a5, 0x6f70a440,  -308 },
709
  { 0xa6dfbd9f, 0xb8e5b88e, 0xcb4ccd50,  -305 },
710
  { 0xd097ad07, 0xa71f26b2, 0x7e2000a4,  -302 },
711
  { 0x825ecc24, 0xc873782f, 0x8ed40066,  -298 },
712
  { 0xa2f67f2d, 0xfa90563b, 0x72890080,  -295 },
713
  { 0xcbb41ef9, 0x79346bca, 0x4f2b40a0,  -292 },
714
  { 0xfea126b7, 0xd78186bc, 0xe2f610c8,  -289 },
715
  { 0x9f24b832, 0xe6b0f436, 0x0dd9ca7d,  -285 },
716
  { 0xc6ede63f, 0xa05d3143, 0x91503d1c,  -282 },
717
  { 0xf8a95fcf, 0x88747d94, 0x75a44c63,  -279 },
718
  { 0x9b69dbe1, 0xb548ce7c, 0xc986afbe,  -275 },
719
  { 0xc24452da, 0x229b021b, 0xfbe85bad,  -272 },
720
  { 0xf2d56790, 0xab41c2a2, 0xfae27299,  -269 },
721
  { 0x97c560ba, 0x6b0919a5, 0xdccd879f,  -265 },
722
  { 0xbdb6b8e9, 0x05cb600f, 0x5400e987,  -262 },
723
  { 0xed246723, 0x473e3813, 0x290123e9,  -259 },
724
  { 0x9436c076, 0x0c86e30b, 0xf9a0b672,  -255 },
725
  { 0xb9447093, 0x8fa89bce, 0xf808e40e,  -252 },
726
  { 0xe7958cb8, 0x7392c2c2, 0xb60b1d12,  -249 },
727
  { 0x90bd77f3, 0x483bb9b9, 0xb1c6f22b,  -245 },
728
  { 0xb4ecd5f0, 0x1a4aa828, 0x1e38aeb6,  -242 },
729
  { 0xe2280b6c, 0x20dd5232, 0x25c6da63,  -239 },
730
  { 0x8d590723, 0x948a535f, 0x579c487e,  -235 },
731
  { 0xb0af48ec, 0x79ace837, 0x2d835a9d,  -232 },
732
  { 0xdcdb1b27, 0x98182244, 0xf8e43145,  -229 },
733
  { 0x8a08f0f8, 0xbf0f156b, 0x1b8e9ecb,  -225 },
734
  { 0xac8b2d36, 0xeed2dac5, 0xe272467e,  -222 },
735
  { 0xd7adf884, 0xaa879177, 0x5b0ed81d,  -219 },
736
  { 0x86ccbb52, 0xea94baea, 0x98e94712,  -215 },
737
  { 0xa87fea27, 0xa539e9a5, 0x3f2398d7,  -212 },
738
  { 0xd29fe4b1, 0x8e88640e, 0x8eec7f0d,  -209 },
739
  { 0x83a3eeee, 0xf9153e89, 0x1953cf68,  -205 },
740
  { 0xa48ceaaa, 0xb75a8e2b, 0x5fa8c342,  -202 },
741
  { 0xcdb02555, 0x653131b6, 0x3792f412,  -199 },
742
  { 0x808e1755, 0x5f3ebf11, 0xe2bbd88b,  -195 },
743
  { 0xa0b19d2a, 0xb70e6ed6, 0x5b6aceae,  -192 },
744
  { 0xc8de0475, 0x64d20a8b, 0xf245825a,  -189 },
745
  { 0xfb158592, 0xbe068d2e, 0xeed6e2f0,  -186 },
746
  { 0x9ced737b, 0xb6c4183d, 0x55464dd6,  -182 },
747
  { 0xc428d05a, 0xa4751e4c, 0xaa97e14c,  -179 },
748
  { 0xf5330471, 0x4d9265df, 0xd53dd99f,  -176 },
749
  { 0x993fe2c6, 0xd07b7fab, 0xe546a803,  -172 },
750
  { 0xbf8fdb78, 0x849a5f96, 0xde985204,  -169 },
751
  { 0xef73d256, 0xa5c0f77c, 0x963e6685,  -166 },
752
  { 0x95a86376, 0x27989aad, 0xdde70013,  -162 },
753
  { 0xbb127c53, 0xb17ec159, 0x5560c018,  -159 },
754
  { 0xe9d71b68, 0x9dde71af, 0xaab8f01e,  -156 },
755
  { 0x92267121, 0x62ab070d, 0xcab39613,  -152 },
756
  { 0xb6b00d69, 0xbb55c8d1, 0x3d607b97,  -149 },
757
  { 0xe45c10c4, 0x2a2b3b05, 0x8cb89a7d,  -146 },
758
  { 0x8eb98a7a, 0x9a5b04e3, 0x77f3608e,  -142 },
759
  { 0xb267ed19, 0x40f1c61c, 0x55f038b2,  -139 },
760
  { 0xdf01e85f, 0x912e37a3, 0x6b6c46de,  -136 },
761
  { 0x8b61313b, 0xbabce2c6, 0x2323ac4b,  -132 },
762
  { 0xae397d8a, 0xa96c1b77, 0xabec975e,  -129 },
763
  { 0xd9c7dced, 0x53c72255, 0x96e7bd35,  -126 },
764
  { 0x881cea14, 0x545c7575, 0x7e50d641,  -122 },
765
  { 0xaa242499, 0x697392d2, 0xdde50bd1,  -119 },
766
  { 0xd4ad2dbf, 0xc3d07787, 0x955e4ec6,  -116 },
767
  { 0x84ec3c97, 0xda624ab4, 0xbd5af13b,  -112 },
768
  { 0xa6274bbd, 0xd0fadd61, 0xecb1ad8a,  -109 },
769
  { 0xcfb11ead, 0x453994ba, 0x67de18ed,  -106 },
770
  { 0x81ceb32c, 0x4b43fcf4, 0x80eacf94,  -102 },
771
  { 0xa2425ff7, 0x5e14fc31, 0xa1258379,   -99 },
772
  { 0xcad2f7f5, 0x359a3b3e, 0x096ee458,   -96 },
773
  { 0xfd87b5f2, 0x8300ca0d, 0x8bca9d6e,   -93 },
774
  { 0x9e74d1b7, 0x91e07e48, 0x775ea264,   -89 },
775
  { 0xc6120625, 0x76589dda, 0x95364afe,   -86 },
776
  { 0xf79687ae, 0xd3eec551, 0x3a83ddbd,   -83 },
777
  { 0x9abe14cd, 0x44753b52, 0xc4926a96,   -79 },
778
  { 0xc16d9a00, 0x95928a27, 0x75b7053c,   -76 },
779
  { 0xf1c90080, 0xbaf72cb1, 0x5324c68b,   -73 },
780
  { 0x971da050, 0x74da7bee, 0xd3f6fc16,   -69 },
781
  { 0xbce50864, 0x92111aea, 0x88f4bb1c,   -66 },
782
  { 0xec1e4a7d, 0xb69561a5, 0x2b31e9e3,   -63 },
783
  { 0x9392ee8e, 0x921d5d07, 0x3aff322e,   -59 },
784
  { 0xb877aa32, 0x36a4b449, 0x09befeb9,   -56 },
785
  { 0xe69594be, 0xc44de15b, 0x4c2ebe68,   -53 },
786
  { 0x901d7cf7, 0x3ab0acd9, 0x0f9d3701,   -49 },
787
  { 0xb424dc35, 0x095cd80f, 0x538484c1,   -46 },
788
  { 0xe12e1342, 0x4bb40e13, 0x2865a5f2,   -43 },
789
  { 0x8cbccc09, 0x6f5088cb, 0xf93f87b7,   -39 },
790
  { 0xafebff0b, 0xcb24aafe, 0xf78f69a5,   -36 },
791
  { 0xdbe6fece, 0xbdedd5be, 0xb573440e,   -33 },
792
  { 0x89705f41, 0x36b4a597, 0x31680a88,   -29 },
793
  { 0xabcc7711, 0x8461cefc, 0xfdc20d2b,   -26 },
794
  { 0xd6bf94d5, 0xe57a42bc, 0x3d329076,   -23 },
795
  { 0x8637bd05, 0xaf6c69b5, 0xa63f9a49,   -19 },
796
  { 0xa7c5ac47, 0x1b478423, 0x0fcf80dc,   -16 },
797
  { 0xd1b71758, 0xe219652b, 0xd3c36113,   -13 },
798
  { 0x83126e97, 0x8d4fdf3b, 0x645a1cac,    -9 },
799
  { 0xa3d70a3d, 0x70a3d70a, 0x3d70a3d7,    -6 },
800
  { 0xcccccccc, 0xcccccccc, 0xcccccccc,    -3 },
801
  { 0x80000000, 0x00000000, 0x00000000,    1 },
802
  { 0xa0000000, 0x00000000, 0x00000000,    4 },
803
  { 0xc8000000, 0x00000000, 0x00000000,    7 },
804
  { 0xfa000000, 0x00000000, 0x00000000,   10 },
805
  { 0x9c400000, 0x00000000, 0x00000000,   14 },
806
  { 0xc3500000, 0x00000000, 0x00000000,   17 },
807
  { 0xf4240000, 0x00000000, 0x00000000,   20 },
808
  { 0x98968000, 0x00000000, 0x00000000,   24 },
809
  { 0xbebc2000, 0x00000000, 0x00000000,   27 },
810
  { 0xee6b2800, 0x00000000, 0x00000000,   30 },
811
  { 0x9502f900, 0x00000000, 0x00000000,   34 },
812
  { 0xba43b740, 0x00000000, 0x00000000,   37 },
813
  { 0xe8d4a510, 0x00000000, 0x00000000,   40 },
814
  { 0x9184e72a, 0x00000000, 0x00000000,   44 },
815
  { 0xb5e620f4, 0x80000000, 0x00000000,   47 },
816
  { 0xe35fa931, 0xa0000000, 0x00000000,   50 },
817
  { 0x8e1bc9bf, 0x04000000, 0x00000000,   54 },
818
  { 0xb1a2bc2e, 0xc5000000, 0x00000000,   57 },
819
  { 0xde0b6b3a, 0x76400000, 0x00000000,   60 },
820
  { 0x8ac72304, 0x89e80000, 0x00000000,   64 },
821
  { 0xad78ebc5, 0xac620000, 0x00000000,   67 },
822
  { 0xd8d726b7, 0x177a8000, 0x00000000,   70 },
823
  { 0x87867832, 0x6eac9000, 0x00000000,   74 },
824
  { 0xa968163f, 0x0a57b400, 0x00000000,   77 },
825
  { 0xd3c21bce, 0xcceda100, 0x00000000,   80 },
826
  { 0x84595161, 0x401484a0, 0x00000000,   84 },
827
  { 0xa56fa5b9, 0x9019a5c8, 0x00000000,   87 },
828
  { 0xcecb8f27, 0xf4200f3a, 0x00000000,   90 },
829
  { 0x813f3978, 0xf8940984, 0x40000000,   94 },
830
  { 0xa18f07d7, 0x36b90be5, 0x50000000,   97 },
831
  { 0xc9f2c9cd, 0x04674ede, 0xa4000000,  100 },
832
  { 0xfc6f7c40, 0x45812296, 0x4d000000,  103 },
833
  { 0x9dc5ada8, 0x2b70b59d, 0xf0200000,  107 },
834
  { 0xc5371912, 0x364ce305, 0x6c280000,  110 },
835
  { 0xf684df56, 0xc3e01bc6, 0xc7320000,  113 },
836
  { 0x9a130b96, 0x3a6c115c, 0x3c7f4000,  117 },
837
  { 0xc097ce7b, 0xc90715b3, 0x4b9f1000,  120 },
838
  { 0xf0bdc21a, 0xbb48db20, 0x1e86d400,  123 },
839
  { 0x96769950, 0xb50d88f4, 0x13144480,  127 },
840
  { 0xbc143fa4, 0xe250eb31, 0x17d955a0,  130 },
841
  { 0xeb194f8e, 0x1ae525fd, 0x5dcfab08,  133 },
842
  { 0x92efd1b8, 0xd0cf37be, 0x5aa1cae5,  137 },
843
  { 0xb7abc627, 0x050305ad, 0xf14a3d9e,  140 },
844
  { 0xe596b7b0, 0xc643c719, 0x6d9ccd05,  143 },
845
  { 0x8f7e32ce, 0x7bea5c6f, 0xe4820023,  147 },
846
  { 0xb35dbf82, 0x1ae4f38b, 0xdda2802c,  150 },
847
  { 0xe0352f62, 0xa19e306e, 0xd50b2037,  153 },
848
  { 0x8c213d9d, 0xa502de45, 0x4526f422,  157 },
849
  { 0xaf298d05, 0x0e4395d6, 0x9670b12b,  160 },
850
  { 0xdaf3f046, 0x51d47b4c, 0x3c0cdd76,  163 },
851
  { 0x88d8762b, 0xf324cd0f, 0xa5880a69,  167 },
852
  { 0xab0e93b6, 0xefee0053, 0x8eea0d04,  170 },
853
  { 0xd5d238a4, 0xabe98068, 0x72a49045,  173 },
854
  { 0x85a36366, 0xeb71f041, 0x47a6da2b,  177 },
855
  { 0xa70c3c40, 0xa64e6c51, 0x999090b6,  180 },
856
  { 0xd0cf4b50, 0xcfe20765, 0xfff4b4e3,  183 },
857
  { 0x82818f12, 0x81ed449f, 0xbff8f10e,  187 },
858
  { 0xa321f2d7, 0x226895c7, 0xaff72d52,  190 },
859
  { 0xcbea6f8c, 0xeb02bb39, 0x9bf4f8a6,  193 },
860
  { 0xfee50b70, 0x25c36a08, 0x02f236d0,  196 },
861
  { 0x9f4f2726, 0x179a2245, 0x01d76242,  200 },
862
  { 0xc722f0ef, 0x9d80aad6, 0x424d3ad2,  203 },
863
  { 0xf8ebad2b, 0x84e0d58b, 0xd2e08987,  206 },
864
  { 0x9b934c3b, 0x330c8577, 0x63cc55f4,  210 },
865
  { 0xc2781f49, 0xffcfa6d5, 0x3cbf6b71,  213 },
866
  { 0xf316271c, 0x7fc3908a, 0x8bef464e,  216 },
867
  { 0x97edd871, 0xcfda3a56, 0x97758bf0,  220 },
868
  { 0xbde94e8e, 0x43d0c8ec, 0x3d52eeed,  223 },
869
  { 0xed63a231, 0xd4c4fb27, 0x4ca7aaa8,  226 },
870
  { 0x945e455f, 0x24fb1cf8, 0x8fe8caa9,  230 },
871
  { 0xb975d6b6, 0xee39e436, 0xb3e2fd53,  233 },
872
  { 0xe7d34c64, 0xa9c85d44, 0x60dbbca8,  236 },
873
  { 0x90e40fbe, 0xea1d3a4a, 0xbc8955e9,  240 },
874
  { 0xb51d13ae, 0xa4a488dd, 0x6babab63,  243 },
875
  { 0xe264589a, 0x4dcdab14, 0xc696963c,  246 },
876
  { 0x8d7eb760, 0x70a08aec, 0xfc1e1de5,  250 },
877
  { 0xb0de6538, 0x8cc8ada8, 0x3b25a55f,  253 },
878
  { 0xdd15fe86, 0xaffad912, 0x49ef0eb7,  256 },
879
  { 0x8a2dbf14, 0x2dfcc7ab, 0x6e356932,  260 },
880
  { 0xacb92ed9, 0x397bf996, 0x49c2c37f,  263 },
881
  { 0xd7e77a8f, 0x87daf7fb, 0xdc33745e,  266 },
882
  { 0x86f0ac99, 0xb4e8dafd, 0x69a028bb,  270 },
883
  { 0xa8acd7c0, 0x222311bc, 0xc40832ea,  273 },
884
  { 0xd2d80db0, 0x2aabd62b, 0xf50a3fa4,  276 },
885
  { 0x83c7088e, 0x1aab65db, 0x792667c6,  280 },
886
  { 0xa4b8cab1, 0xa1563f52, 0x577001b8,  283 },
887
  { 0xcde6fd5e, 0x09abcf26, 0xed4c0226,  286 },
888
  { 0x80b05e5a, 0xc60b6178, 0x544f8158,  290 },
889
  { 0xa0dc75f1, 0x778e39d6, 0x696361ae,  293 },
890
  { 0xc913936d, 0xd571c84c, 0x03bc3a19,  296 },
891
  { 0xfb587849, 0x4ace3a5f, 0x04ab48a0,  299 },
892
  { 0x9d174b2d, 0xcec0e47b, 0x62eb0d64,  303 },
893
  { 0xc45d1df9, 0x42711d9a, 0x3ba5d0bd,  306 },
894
  { 0xf5746577, 0x930d6500, 0xca8f44ec,  309 },
895
  { 0x9968bf6a, 0xbbe85f20, 0x7e998b13,  313 },
896
  { 0xbfc2ef45, 0x6ae276e8, 0x9e3fedd8,  316 },
897
  { 0xefb3ab16, 0xc59b14a2, 0xc5cfe94e,  319 },
898
  { 0x95d04aee, 0x3b80ece5, 0xbba1f1d1,  323 },
899
  { 0xbb445da9, 0xca61281f, 0x2a8a6e45,  326 },
900
  { 0xea157514, 0x3cf97226, 0xf52d09d7,  329 },
901
  { 0x924d692c, 0xa61be758, 0x593c2626,  333 },
902
  { 0xb6e0c377, 0xcfa2e12e, 0x6f8b2fb0,  336 },
903
  { 0xe498f455, 0xc38b997a, 0x0b6dfb9c,  339 },
904
  { 0x8edf98b5, 0x9a373fec, 0x4724bd41,  343 },
905
  { 0xb2977ee3, 0x00c50fe7, 0x58edec91,  346 },
906
  { 0xdf3d5e9b, 0xc0f653e1, 0x2f2967b6,  349 },
907
  { 0x8b865b21, 0x5899f46c, 0xbd79e0d2,  353 },
908
  { 0xae67f1e9, 0xaec07187, 0xecd85906,  356 },
909
  { 0xda01ee64, 0x1a708de9, 0xe80e6f48,  359 },
910
  { 0x884134fe, 0x908658b2, 0x3109058d,  363 },
911
  { 0xaa51823e, 0x34a7eede, 0xbd4b46f0,  366 },
912
  { 0xd4e5e2cd, 0xc1d1ea96, 0x6c9e18ac,  369 },
913
  { 0x850fadc0, 0x9923329e, 0x03e2cf6b,  373 },
914
  { 0xa6539930, 0xbf6bff45, 0x84db8346,  376 },
915
  { 0xcfe87f7c, 0xef46ff16, 0xe6126418,  379 },
916
  { 0x81f14fae, 0x158c5f6e, 0x4fcb7e8f,  383 },
917
  { 0xa26da399, 0x9aef7749, 0xe3be5e33,  386 },
918
  { 0xcb090c80, 0x01ab551c, 0x5cadf5bf,  389 },
919
  { 0xfdcb4fa0, 0x02162a63, 0x73d9732f,  392 },
920
  { 0x9e9f11c4, 0x014dda7e, 0x2867e7fd,  396 },
921
  { 0xc646d635, 0x01a1511d, 0xb281e1fd,  399 },
922
  { 0xf7d88bc2, 0x4209a565, 0x1f225a7c,  402 },
923
  { 0x9ae75759, 0x6946075f, 0x3375788d,  406 },
924
  { 0xc1a12d2f, 0xc3978937, 0x0052d6b1,  409 },
925
  { 0xf209787b, 0xb47d6b84, 0xc0678c5d,  412 },
926
  { 0x9745eb4d, 0x50ce6332, 0xf840b7ba,  416 },
927
  { 0xbd176620, 0xa501fbff, 0xb650e5a9,  419 },
928
  { 0xec5d3fa8, 0xce427aff, 0xa3e51f13,  422 },
929
  { 0x93ba47c9, 0x80e98cdf, 0xc66f336c,  426 },
930
  { 0xb8a8d9bb, 0xe123f017, 0xb80b0047,  429 },
931
  { 0xe6d3102a, 0xd96cec1d, 0xa60dc059,  432 },
932
  { 0x9043ea1a, 0xc7e41392, 0x87c89837,  436 },
933
  { 0xb454e4a1, 0x79dd1877, 0x29babe45,  439 },
934
  { 0xe16a1dc9, 0xd8545e94, 0xf4296dd6,  442 },
935
  { 0x8ce2529e, 0x2734bb1d, 0x1899e4a6,  446 },
936
  { 0xb01ae745, 0xb101e9e4, 0x5ec05dcf,  449 },
937
  { 0xdc21a117, 0x1d42645d, 0x76707543,  452 },
938
  { 0x899504ae, 0x72497eba, 0x6a06494a,  456 },
939
  { 0xabfa45da, 0x0edbde69, 0x0487db9d,  459 },
940
  { 0xd6f8d750, 0x9292d603, 0x45a9d284,  462 },
941
  { 0x865b8692, 0x5b9bc5c2, 0x0b8a2392,  466 },
942
  { 0xa7f26836, 0xf282b732, 0x8e6cac77,  469 },
943
  { 0xd1ef0244, 0xaf2364ff, 0x3207d795,  472 },
944
  { 0x8335616a, 0xed761f1f, 0x7f44e6bd,  476 },
945
  { 0xa402b9c5, 0xa8d3a6e7, 0x5f16206c,  479 },
946
  { 0xcd036837, 0x130890a1, 0x36dba887,  482 },
947
  { 0x80222122, 0x6be55a64, 0xc2494954,  486 },
948
  { 0xa02aa96b, 0x06deb0fd, 0xf2db9baa,  489 },
949
  { 0xc83553c5, 0xc8965d3d, 0x6f928294,  492 },
950
  { 0xfa42a8b7, 0x3abbf48c, 0xcb772339,  495 },
951
  { 0x9c69a972, 0x84b578d7, 0xff2a7604,  499 },
952
  { 0xc38413cf, 0x25e2d70d, 0xfef51385,  502 },
953
  { 0xf46518c2, 0xef5b8cd1, 0x7eb25866,  505 },
954
  { 0x98bf2f79, 0xd5993802, 0xef2f773f,  509 },
955
  { 0xbeeefb58, 0x4aff8603, 0xaafb550f,  512 },
956
  { 0xeeaaba2e, 0x5dbf6784, 0x95ba2a53,  515 },
957
  { 0x952ab45c, 0xfa97a0b2, 0xdd945a74,  519 },
958
  { 0xba756174, 0x393d88df, 0x94f97111,  522 },
959
  { 0xe912b9d1, 0x478ceb17, 0x7a37cd56,  525 },
960
  { 0x91abb422, 0xccb812ee, 0xac62e055,  529 },
961
  { 0xb616a12b, 0x7fe617aa, 0x577b986b,  532 },
962
  { 0xe39c4976, 0x5fdf9d94, 0xed5a7e85,  535 },
963
  { 0x8e41ade9, 0xfbebc27d, 0x14588f13,  539 },
964
  { 0xb1d21964, 0x7ae6b31c, 0x596eb2d8,  542 },
965
  { 0xde469fbd, 0x99a05fe3, 0x6fca5f8e,  545 },
966
  { 0x8aec23d6, 0x80043bee, 0x25de7bb9,  549 },
967
  { 0xada72ccc, 0x20054ae9, 0xaf561aa7,  552 },
968
  { 0xd910f7ff, 0x28069da4, 0x1b2ba151,  555 },
969
  { 0x87aa9aff, 0x79042286, 0x90fb44d2,  559 },
970
  { 0xa99541bf, 0x57452b28, 0x353a1607,  562 },
971
  { 0xd3fa922f, 0x2d1675f2, 0x42889b89,  565 },
972
  { 0x847c9b5d, 0x7c2e09b7, 0x69956135,  569 },
973
  { 0xa59bc234, 0xdb398c25, 0x43fab983,  572 },
974
  { 0xcf02b2c2, 0x1207ef2e, 0x94f967e4,  575 },
975
  { 0x8161afb9, 0x4b44f57d, 0x1d1be0ee,  579 },
976
  { 0xa1ba1ba7, 0x9e1632dc, 0x6462d92a,  582 },
977
  { 0xca28a291, 0x859bbf93, 0x7d7b8f75,  585 },
978
  { 0xfcb2cb35, 0xe702af78, 0x5cda7352,  588 },
979
  { 0x9defbf01, 0xb061adab, 0x3a088813,  592 },
980
  { 0xc56baec2, 0x1c7a1916, 0x088aaa18,  595 },
981
  { 0xf6c69a72, 0xa3989f5b, 0x8aad549e,  598 },
982
  { 0x9a3c2087, 0xa63f6399, 0x36ac54e2,  602 },
983
  { 0xc0cb28a9, 0x8fcf3c7f, 0x84576a1b,  605 },
984
  { 0xf0fdf2d3, 0xf3c30b9f, 0x656d44a2,  608 },
985
  { 0x969eb7c4, 0x7859e743, 0x9f644ae5,  612 },
986
  { 0xbc4665b5, 0x96706114, 0x873d5d9f,  615 },
987
  { 0xeb57ff22, 0xfc0c7959, 0xa90cb506,  618 },
988
  { 0x9316ff75, 0xdd87cbd8, 0x09a7f124,  622 },
989
  { 0xb7dcbf53, 0x54e9bece, 0x0c11ed6d,  625 },
990
  { 0xe5d3ef28, 0x2a242e81, 0x8f1668c8,  628 },
991
  { 0x8fa47579, 0x1a569d10, 0xf96e017d,  632 },
992
  { 0xb38d92d7, 0x60ec4455, 0x37c981dc,  635 },
993
  { 0xe070f78d, 0x3927556a, 0x85bbe253,  638 },
994
  { 0x8c469ab8, 0x43b89562, 0x93956d74,  642 },
995
  { 0xaf584166, 0x54a6babb, 0x387ac8d1,  645 },
996
  { 0xdb2e51bf, 0xe9d0696a, 0x06997b05,  648 },
997
  { 0x88fcf317, 0xf22241e2, 0x441fece3,  652 },
998
  { 0xab3c2fdd, 0xeeaad25a, 0xd527e81c,  655 },
999
  { 0xd60b3bd5, 0x6a5586f1, 0x8a71e223,  658 },
1000
  { 0x85c70565, 0x62757456, 0xf6872d56,  662 },
1001
  { 0xa738c6be, 0xbb12d16c, 0xb428f8ac,  665 },
1002
  { 0xd106f86e, 0x69d785c7, 0xe13336d7,  668 },
1003
  { 0x82a45b45, 0x0226b39c, 0xecc00246,  672 },
1004
  { 0xa34d7216, 0x42b06084, 0x27f002d7,  675 },
1005
  { 0xcc20ce9b, 0xd35c78a5, 0x31ec038d,  678 },
1006
  { 0xff290242, 0xc83396ce, 0x7e670471,  681 },
1007
  { 0x9f79a169, 0xbd203e41, 0x0f0062c6,  685 },
1008
  { 0xc75809c4, 0x2c684dd1, 0x52c07b78,  688 },
1009
  { 0xf92e0c35, 0x37826145, 0xa7709a56,  691 },
1010
  { 0x9bbcc7a1, 0x42b17ccb, 0x88a66076,  695 },
1011
  { 0xc2abf989, 0x935ddbfe, 0x6acff893,  698 },
1012
  { 0xf356f7eb, 0xf83552fe, 0x0583f6b8,  701 },
1013
  { 0x98165af3, 0x7b2153de, 0xc3727a33,  705 },
1014
  { 0xbe1bf1b0, 0x59e9a8d6, 0x744f18c0,  708 },
1015
  { 0xeda2ee1c, 0x7064130c, 0x1162def0,  711 },
1016
  { 0x9485d4d1, 0xc63e8be7, 0x8addcb56,  715 },
1017
  { 0xb9a74a06, 0x37ce2ee1, 0x6d953e2b,  718 },
1018
  { 0xe8111c87, 0xc5c1ba99, 0xc8fa8db6,  721 },
1019
  { 0x910ab1d4, 0xdb9914a0, 0x1d9c9892,  725 },
1020
  { 0xb54d5e4a, 0x127f59c8, 0x2503beb6,  728 },
1021
  { 0xe2a0b5dc, 0x971f303a, 0x2e44ae64,  731 },
1022
  { 0x8da471a9, 0xde737e24, 0x5ceaecfe,  735 },
1023
  { 0xb10d8e14, 0x56105dad, 0x7425a83e,  738 },
1024
  { 0xdd50f199, 0x6b947518, 0xd12f124e,  741 },
1025
  { 0x8a5296ff, 0xe33cc92f, 0x82bd6b70,  745 },
1026
  { 0xace73cbf, 0xdc0bfb7b, 0x636cc64d,  748 },
1027
  { 0xd8210bef, 0xd30efa5a, 0x3c47f7e0,  751 },
1028
  { 0x8714a775, 0xe3e95c78, 0x65acfaec,  755 },
1029
  { 0xa8d9d153, 0x5ce3b396, 0x7f1839a7,  758 },
1030
  { 0xd31045a8, 0x341ca07c, 0x1ede4811,  761 },
1031
  { 0x83ea2b89, 0x2091e44d, 0x934aed0a,  765 },
1032
  { 0xa4e4b66b, 0x68b65d60, 0xf81da84d,  768 },
1033
  { 0xce1de406, 0x42e3f4b9, 0x36251260,  771 },
1034
  { 0x80d2ae83, 0xe9ce78f3, 0xc1d72b7c,  775 },
1035
  { 0xa1075a24, 0xe4421730, 0xb24cf65b,  778 },
1036
  { 0xc94930ae, 0x1d529cfc, 0xdee033f2,  781 },
1037
  { 0xfb9b7cd9, 0xa4a7443c, 0x169840ef,  784 },
1038
  { 0x9d412e08, 0x06e88aa5, 0x8e1f2895,  788 },
1039
  { 0xc491798a, 0x08a2ad4e, 0xf1a6f2ba,  791 },
1040
  { 0xf5b5d7ec, 0x8acb58a2, 0xae10af69,  794 },
1041
  { 0x9991a6f3, 0xd6bf1765, 0xacca6da1,  798 },
1042
  { 0xbff610b0, 0xcc6edd3f, 0x17fd090a,  801 },
1043
  { 0xeff394dc, 0xff8a948e, 0xddfc4b4c,  804 },
1044
  { 0x95f83d0a, 0x1fb69cd9, 0x4abdaf10,  808 },
1045
  { 0xbb764c4c, 0xa7a4440f, 0x9d6d1ad4,  811 },
1046
  { 0xea53df5f, 0xd18d5513, 0x84c86189,  814 },
1047
  { 0x92746b9b, 0xe2f8552c, 0x32fd3cf5,  818 },
1048
  { 0xb7118682, 0xdbb66a77, 0x3fbc8c33,  821 },
1049
  { 0xe4d5e823, 0x92a40515, 0x0fabaf3f,  824 },
1050
  { 0x8f05b116, 0x3ba6832d, 0x29cb4d87,  828 },
1051
  { 0xb2c71d5b, 0xca9023f8, 0x743e20e9,  831 },
1052
  { 0xdf78e4b2, 0xbd342cf6, 0x914da924,  834 },
1053
  { 0x8bab8eef, 0xb6409c1a, 0x1ad089b6,  838 },
1054
  { 0xae9672ab, 0xa3d0c320, 0xa184ac24,  841 },
1055
  { 0xda3c0f56, 0x8cc4f3e8, 0xc9e5d72d,  844 },
1056
  { 0x88658996, 0x17fb1871, 0x7e2fa67c,  848 },
1057
  { 0xaa7eebfb, 0x9df9de8d, 0xddbb901b,  851 },
1058
  { 0xd51ea6fa, 0x85785631, 0x552a7422,  854 },
1059
  { 0x8533285c, 0x936b35de, 0xd53a8895,  858 },
1060
  { 0xa67ff273, 0xb8460356, 0x8a892aba,  861 },
1061
  { 0xd01fef10, 0xa657842c, 0x2d2b7569,  864 },
1062
  { 0x8213f56a, 0x67f6b29b, 0x9c3b2962,  868 },
1063
  { 0xa298f2c5, 0x01f45f42, 0x8349f3ba,  871 },
1064
  { 0xcb3f2f76, 0x42717713, 0x241c70a9,  874 },
1065
  { 0xfe0efb53, 0xd30dd4d7, 0xed238cd3,  877 },
1066
  { 0x9ec95d14, 0x63e8a506, 0xf4363804,  881 },
1067
  { 0xc67bb459, 0x7ce2ce48, 0xb143c605,  884 },
1068
  { 0xf81aa16f, 0xdc1b81da, 0xdd94b786,  887 },
1069
  { 0x9b10a4e5, 0xe9913128, 0xca7cf2b4,  891 },
1070
  { 0xc1d4ce1f, 0x63f57d72, 0xfd1c2f61,  894 },
1071
  { 0xf24a01a7, 0x3cf2dccf, 0xbc633b39,  897 },
1072
  { 0x976e4108, 0x8617ca01, 0xd5be0503,  901 },
1073
  { 0xbd49d14a, 0xa79dbc82, 0x4b2d8644,  904 },
1074
  { 0xec9c459d, 0x51852ba2, 0xddf8e7d6,  907 },
1075
  { 0x93e1ab82, 0x52f33b45, 0xcabb90e5,  911 },
1076
  { 0xb8da1662, 0xe7b00a17, 0x3d6a751f,  914 },
1077
  { 0xe7109bfb, 0xa19c0c9d, 0x0cc51267,  917 },
1078
  { 0x906a617d, 0x450187e2, 0x27fb2b80,  921 },
1079
  { 0xb484f9dc, 0x9641e9da, 0xb1f9f660,  924 },
1080
  { 0xe1a63853, 0xbbd26451, 0x5e7873f8,  927 },
1081
  { 0x8d07e334, 0x55637eb2, 0xdb0b487b,  931 },
1082
  { 0xb049dc01, 0x6abc5e5f, 0x91ce1a9a,  934 },
1083
  { 0xdc5c5301, 0xc56b75f7, 0x7641a140,  937 },
1084
  { 0x89b9b3e1, 0x1b6329ba, 0xa9e904c8,  941 },
1085
  { 0xac2820d9, 0x623bf429, 0x546345fa,  944 },
1086
  { 0xd732290f, 0xbacaf133, 0xa97c1779,  947 },
1087
  { 0x867f59a9, 0xd4bed6c0, 0x49ed8eab,  951 },
1088
  { 0xa81f3014, 0x49ee8c70, 0x5c68f256,  954 },
1089
  { 0xd226fc19, 0x5c6a2f8c, 0x73832eec,  957 },
1090
  { 0x83585d8f, 0xd9c25db7, 0xc831fd53,  961 },
1091
  { 0xa42e74f3, 0xd032f525, 0xba3e7ca8,  964 },
1092
  { 0xcd3a1230, 0xc43fb26f, 0x28ce1bd2,  967 },
1093
  { 0x80444b5e, 0x7aa7cf85, 0x7980d163,  971 },
1094
  { 0xa0555e36, 0x1951c366, 0xd7e105bc,  974 },
1095
  { 0xc86ab5c3, 0x9fa63440, 0x8dd9472b,  977 },
1096
  { 0xfa856334, 0x878fc150, 0xb14f98f6,  980 },
1097
  { 0x9c935e00, 0xd4b9d8d2, 0x6ed1bf9a,  984 },
1098
  { 0xc3b83581, 0x09e84f07, 0x0a862f80,  987 },
1099
  { 0xf4a642e1, 0x4c6262c8, 0xcd27bb61,  990 },
1100
  { 0x98e7e9cc, 0xcfbd7dbd, 0x8038d51c,  994 },
1101
  { 0xbf21e440, 0x03acdd2c, 0xe0470a63,  997 },
1102
  { 0xeeea5d50, 0x04981478, 0x1858ccfc, 1000 },
1103
  { 0x95527a52, 0x02df0ccb, 0x0f37801e, 1004 },
1104
  { 0xbaa718e6, 0x8396cffd, 0xd3056025, 1007 },
1105
  { 0xe950df20, 0x247c83fd, 0x47c6b82e, 1010 },
1106
  { 0x91d28b74, 0x16cdd27e, 0x4cdc331d, 1014 },
1107
  { 0xb6472e51, 0x1c81471d, 0xe0133fe4, 1017 },
1108
  { 0xe3d8f9e5, 0x63a198e5, 0x58180fdd, 1020 },
1109
  { 0x8e679c2f, 0x5e44ff8f, 0x570f09ea, 1024 },
1110
  { 0xb201833b, 0x35d63f73, 0x2cd2cc65, 1027 },
1111
  { 0xde81e40a, 0x034bcf4f, 0xf8077f7e, 1030 },
1112
  { 0x8b112e86, 0x420f6191, 0xfb04afaf, 1034 },
1113
  { 0xadd57a27, 0xd29339f6, 0x79c5db9a, 1037 },
1114
  { 0xd94ad8b1, 0xc7380874, 0x18375281, 1040 },
1115
  { 0x87cec76f, 0x1c830548, 0x8f229391, 1044 },
1116
  { 0xa9c2794a, 0xe3a3c69a, 0xb2eb3875, 1047 },
1117
  { 0xd433179d, 0x9c8cb841, 0x5fa60692, 1050 },
1118
  { 0x849feec2, 0x81d7f328, 0xdbc7c41b, 1054 },
1119
  { 0xa5c7ea73, 0x224deff3, 0x12b9b522, 1057 },
1120
  { 0xcf39e50f, 0xeae16bef, 0xd768226b, 1060 },
1121
  { 0x81842f29, 0xf2cce375, 0xe6a11583, 1064 },
1122
  { 0xa1e53af4, 0x6f801c53, 0x60495ae3, 1067 },
1123
  { 0xca5e89b1, 0x8b602368, 0x385bb19c, 1070 },
1124
  { 0xfcf62c1d, 0xee382c42, 0x46729e03, 1073 },
1125
  { 0x9e19db92, 0xb4e31ba9, 0x6c07a2c2, 1077 }
1126
  };
1127
 static short int Lhint[2098] = {
1128
     /*18,*/19,    19,    19,    19,    20,    20,    20,    21,    21,
1129
     21,    22,    22,    22,    23,    23,    23,    23,    24,    24,
1130
     24,    25,    25,    25,    26,    26,    26,    26,    27,    27,
1131
     27,    28,    28,    28,    29,    29,    29,    29,    30,    30,
1132
     30,    31,    31,    31,    32,    32,    32,    32,    33,    33,
1133
     33,    34,    34,    34,    35,    35,    35,    35,    36,    36,
1134
     36,    37,    37,    37,    38,    38,    38,    38,    39,    39,
1135
     39,    40,    40,    40,    41,    41,    41,    41,    42,    42,
1136
     42,    43,    43,    43,    44,    44,    44,    44,    45,    45,
1137
     45,    46,    46,    46,    47,    47,    47,    47,    48,    48,
1138
     48,    49,    49,    49,    50,    50,    50,    51,    51,    51,
1139
     51,    52,    52,    52,    53,    53,    53,    54,    54,    54,
1140
     54,    55,    55,    55,    56,    56,    56,    57,    57,    57,
1141
     57,    58,    58,    58,    59,    59,    59,    60,    60,    60,
1142
     60,    61,    61,    61,    62,    62,    62,    63,    63,    63,
1143
     63,    64,    64,    64,    65,    65,    65,    66,    66,    66,
1144
     66,    67,    67,    67,    68,    68,    68,    69,    69,    69,
1145
     69,    70,    70,    70,    71,    71,    71,    72,    72,    72,
1146
     72,    73,    73,    73,    74,    74,    74,    75,    75,    75,
1147
     75,    76,    76,    76,    77,    77,    77,    78,    78,    78,
1148
     78,    79,    79,    79,    80,    80,    80,    81,    81,    81,
1149
     82,    82,    82,    82,    83,    83,    83,    84,    84,    84,
1150
     85,    85,    85,    85,    86,    86,    86,    87,    87,    87,
1151
     88,    88,    88,    88,    89,    89,    89,    90,    90,    90,
1152
     91,    91,    91,    91,    92,    92,    92,    93,    93,    93,
1153
     94,    94,    94,    94,    95,    95,    95,    96,    96,    96,
1154
     97,    97,    97,    97,    98,    98,    98,    99,    99,    99,
1155
    100,   100,   100,   100,   101,   101,   101,   102,   102,   102,
1156
    103,   103,   103,   103,   104,   104,   104,   105,   105,   105,
1157
    106,   106,   106,   106,   107,   107,   107,   108,   108,   108,
1158
    109,   109,   109,   110,   110,   110,   110,   111,   111,   111,
1159
    112,   112,   112,   113,   113,   113,   113,   114,   114,   114,
1160
    115,   115,   115,   116,   116,   116,   116,   117,   117,   117,
1161
    118,   118,   118,   119,   119,   119,   119,   120,   120,   120,
1162
    121,   121,   121,   122,   122,   122,   122,   123,   123,   123,
1163
    124,   124,   124,   125,   125,   125,   125,   126,   126,   126,
1164
    127,   127,   127,   128,   128,   128,   128,   129,   129,   129,
1165
    130,   130,   130,   131,   131,   131,   131,   132,   132,   132,
1166
    133,   133,   133,   134,   134,   134,   134,   135,   135,   135,
1167
    136,   136,   136,   137,   137,   137,   137,   138,   138,   138,
1168
    139,   139,   139,   140,   140,   140,   141,   141,   141,   141,
1169
    142,   142,   142,   143,   143,   143,   144,   144,   144,   144,
1170
    145,   145,   145,   146,   146,   146,   147,   147,   147,   147,
1171
    148,   148,   148,   149,   149,   149,   150,   150,   150,   150,
1172
    151,   151,   151,   152,   152,   152,   153,   153,   153,   153,
1173
    154,   154,   154,   155,   155,   155,   156,   156,   156,   156,
1174
    157,   157,   157,   158,   158,   158,   159,   159,   159,   159,
1175
    160,   160,   160,   161,   161,   161,   162,   162,   162,   162,
1176
    163,   163,   163,   164,   164,   164,   165,   165,   165,   165,
1177
    166,   166,   166,   167,   167,   167,   168,   168,   168,   169,
1178
    169,   169,   169,   170,   170,   170,   171,   171,   171,   172,
1179
    172,   172,   172,   173,   173,   173,   174,   174,   174,   175,
1180
    175,   175,   175,   176,   176,   176,   177,   177,   177,   178,
1181
    178,   178,   178,   179,   179,   179,   180,   180,   180,   181,
1182
    181,   181,   181,   182,   182,   182,   183,   183,   183,   184,
1183
    184,   184,   184,   185,   185,   185,   186,   186,   186,   187,
1184
    187,   187,   187,   188,   188,   188,   189,   189,   189,   190,
1185
    190,   190,   190,   191,   191,   191,   192,   192,   192,   193,
1186
    193,   193,   193,   194,   194,   194,   195,   195,   195,   196,
1187
    196,   196,   197,   197,   197,   197,   198,   198,   198,   199,
1188
    199,   199,   200,   200,   200,   200,   201,   201,   201,   202,
1189
    202,   202,   203,   203,   203,   203,   204,   204,   204,   205,
1190
    205,   205,   206,   206,   206,   206,   207,   207,   207,   208,
1191
    208,   208,   209,   209,   209,   209,   210,   210,   210,   211,
1192
    211,   211,   212,   212,   212,   212,   213,   213,   213,   214,
1193
    214,   214,   215,   215,   215,   215,   216,   216,   216,   217,
1194
    217,   217,   218,   218,   218,   218,   219,   219,   219,   220,
1195
    220,   220,   221,   221,   221,   221,   222,   222,   222,   223,
1196
    223,   223,   224,   224,   224,   224,   225,   225,   225,   226,
1197
    226,   226,   227,   227,   227,   228,   228,   228,   228,   229,
1198
    229,   229,   230,   230,   230,   231,   231,   231,   231,   232,
1199
    232,   232,   233,   233,   233,   234,   234,   234,   234,   235,
1200
    235,   235,   236,   236,   236,   237,   237,   237,   237,   238,
1201
    238,   238,   239,   239,   239,   240,   240,   240,   240,   241,
1202
    241,   241,   242,   242,   242,   243,   243,   243,   243,   244,
1203
    244,   244,   245,   245,   245,   246,   246,   246,   246,   247,
1204
    247,   247,   248,   248,   248,   249,   249,   249,   249,   250,
1205
    250,   250,   251,   251,   251,   252,   252,   252,   252,   253,
1206
    253,   253,   254,   254,   254,   255,   255,   255,   256,   256,
1207
    256,   256,   257,   257,   257,   258,   258,   258,   259,   259,
1208
    259,   259,   260,   260,   260,   261,   261,   261,   262,   262,
1209
    262,   262,   263,   263,   263,   264,   264,   264,   265,   265,
1210
    265,   265,   266,   266,   266,   267,   267,   267,   268,   268,
1211
    268,   268,   269,   269,   269,   270,   270,   270,   271,   271,
1212
    271,   271,   272,   272,   272,   273,   273,   273,   274,   274,
1213
    274,   274,   275,   275,   275,   276,   276,   276,   277,   277,
1214
    277,   277,   278,   278,   278,   279,   279,   279,   280,   280,
1215
    280,   280,   281,   281,   281,   282,   282,   282,   283,   283,
1216
    283,   283,   284,   284,   284,   285,   285,   285,   286,   286,
1217
    286,   287,   287,   287,   287,   288,   288,   288,   289,   289,
1218
    289,   290,   290,   290,   290,   291,   291,   291,   292,   292,
1219
    292,   293,   293,   293,   293,   294,   294,   294,   295,   295,
1220
    295,   296,   296,   296,   296,   297,   297,   297,   298,   298,
1221
    298,   299,   299,   299,   299,   300,   300,   300,   301,   301,
1222
    301,   302,   302,   302,   302,   303,   303,   303,   304,   304,
1223
    304,   305,   305,   305,   305,   306,   306,   306,   307,   307,
1224
    307,   308,   308,   308,   308,   309,   309,   309,   310,   310,
1225
    310,   311,   311,   311,   311,   312,   312,   312,   313,   313,
1226
    313,   314,   314,   314,   315,   315,   315,   315,   316,   316,
1227
    316,   317,   317,   317,   318,   318,   318,   318,   319,   319,
1228
    319,   320,   320,   320,   321,   321,   321,   321,   322,   322,
1229
    322,   323,   323,   323,   324,   324,   324,   324,   325,   325,
1230
    325,   326,   326,   326,   327,   327,   327,   327,   328,   328,
1231
    328,   329,   329,   329,   330,   330,   330,   330,   331,   331,
1232
    331,   332,   332,   332,   333,   333,   333,   333,   334,   334,
1233
    334,   335,   335,   335,   336,   336,   336,   336,   337,   337,
1234
    337,   338,   338,   338,   339,   339,   339,   339,   340,   340,
1235
    340,   341,   341,   341,   342,   342,   342,   342,   343,   343,
1236
    343,   344,   344,   344,   345,   345,   345,   346,   346,   346,
1237
    346,   347,   347,   347,   348,   348,   348,   349,   349,   349,
1238
    349,   350,   350,   350,   351,   351,   351,   352,   352,   352,
1239
    352,   353,   353,   353,   354,   354,   354,   355,   355,   355,
1240
    355,   356,   356,   356,   357,   357,   357,   358,   358,   358,
1241
    358,   359,   359,   359,   360,   360,   360,   361,   361,   361,
1242
    361,   362,   362,   362,   363,   363,   363,   364,   364,   364,
1243
    364,   365,   365,   365,   366,   366,   366,   367,   367,   367,
1244
    367,   368,   368,   368,   369,   369,   369,   370,   370,   370,
1245
    370,   371,   371,   371,   372,   372,   372,   373,   373,   373,
1246
    374,   374,   374,   374,   375,   375,   375,   376,   376,   376,
1247
    377,   377,   377,   377,   378,   378,   378,   379,   379,   379,
1248
    380,   380,   380,   380,   381,   381,   381,   382,   382,   382,
1249
    383,   383,   383,   383,   384,   384,   384,   385,   385,   385,
1250
    386,   386,   386,   386,   387,   387,   387,   388,   388,   388,
1251
    389,   389,   389,   389,   390,   390,   390,   391,   391,   391,
1252
    392,   392,   392,   392,   393,   393,   393,   394,   394,   394,
1253
    395,   395,   395,   395,   396,   396,   396,   397,   397,   397,
1254
    398,   398,   398,   398,   399,   399,   399,   400,   400,   400,
1255
    401,   401,   401,   402,   402,   402,   402,   403,   403,   403,
1256
    404,   404,   404,   405,   405,   405,   405,   406,   406,   406,
1257
    407,   407,   407,   408,   408,   408,   408,   409,   409,   409,
1258
    410,   410,   410,   411,   411,   411,   411,   412,   412,   412,
1259
    413,   413,   413,   414,   414,   414,   414,   415,   415,   415,
1260
    416,   416,   416,   417,   417,   417,   417,   418,   418,   418,
1261
    419,   419,   419,   420,   420,   420,   420,   421,   421,   421,
1262
    422,   422,   422,   423,   423,   423,   423,   424,   424,   424,
1263
    425,   425,   425,   426,   426,   426,   426,   427,   427,   427,
1264
    428,   428,   428,   429,   429,   429,   429,   430,   430,   430,
1265
    431,   431,   431,   432,   432,   432,   433,   433,   433,   433,
1266
    434,   434,   434,   435,   435,   435,   436,   436,   436,   436,
1267
    437,   437,   437,   438,   438,   438,   439,   439,   439,   439,
1268
    440,   440,   440,   441,   441,   441,   442,   442,   442,   442,
1269
    443,   443,   443,   444,   444,   444,   445,   445,   445,   445,
1270
    446,   446,   446,   447,   447,   447,   448,   448,   448,   448,
1271
    449,   449,   449,   450,   450,   450,   451,   451,   451,   451,
1272
    452,   452,   452,   453,   453,   453,   454,   454,   454,   454,
1273
    455,   455,   455,   456,   456,   456,   457,   457,   457,   457,
1274
    458,   458,   458,   459,   459,   459,   460,   460,   460,   461,
1275
    461,   461,   461,   462,   462,   462,   463,   463,   463,   464,
1276
    464,   464,   464,   465,   465,   465,   466,   466,   466,   467,
1277
    467,   467,   467,   468,   468,   468,   469,   469,   469,   470,
1278
    470,   470,   470,   471,   471,   471,   472,   472,   472,   473,
1279
    473,   473,   473,   474,   474,   474,   475,   475,   475,   476,
1280
    476,   476,   476,   477,   477,   477,   478,   478,   478,   479,
1281
    479,   479,   479,   480,   480,   480,   481,   481,   481,   482,
1282
    482,   482,   482,   483,   483,   483,   484,   484,   484,   485,
1283
    485,   485,   485,   486,   486,   486,   487,   487,   487,   488,
1284
    488,   488,   488,   489,   489,   489,   490,   490,   490,   491,
1285
    491,   491,   492,   492,   492,   492,   493,   493,   493,   494,
1286
    494,   494,   495,   495,   495,   495,   496,   496,   496,   497,
1287
    497,   497,   498,   498,   498,   498,   499,   499,   499,   500,
1288
    500,   500,   501,   501,   501,   501,   502,   502,   502,   503,
1289
    503,   503,   504,   504,   504,   504,   505,   505,   505,   506,
1290
    506,   506,   507,   507,   507,   507,   508,   508,   508,   509,
1291
    509,   509,   510,   510,   510,   510,   511,   511,   511,   512,
1292
    512,   512,   513,   513,   513,   513,   514,   514,   514,   515,
1293
    515,   515,   516,   516,   516,   516,   517,   517,   517,   518,
1294
    518,   518,   519,   519,   519,   520,   520,   520,   520,   521,
1295
    521,   521,   522,   522,   522,   523,   523,   523,   523,   524,
1296
    524,   524,   525,   525,   525,   526,   526,   526,   526,   527,
1297
    527,   527,   528,   528,   528,   529,   529,   529,   529,   530,
1298
    530,   530,   531,   531,   531,   532,   532,   532,   532,   533,
1299
    533,   533,   534,   534,   534,   535,   535,   535,   535,   536,
1300
    536,   536,   537,   537,   537,   538,   538,   538,   538,   539,
1301
    539,   539,   540,   540,   540,   541,   541,   541,   541,   542,
1302
    542,   542,   543,   543,   543,   544,   544,   544,   544,   545,
1303
    545,   545,   546,   546,   546,   547,   547,   547,   548,   548,
1304
    548,   548,   549,   549,   549,   550,   550,   550,   551,   551,
1305
    551,   551,   552,   552,   552,   553,   553,   553,   554,   554,
1306
    554,   554,   555,   555,   555,   556,   556,   556,   557,   557,
1307
    557,   557,   558,   558,   558,   559,   559,   559,   560,   560,
1308
    560,   560,   561,   561,   561,   562,   562,   562,   563,   563,
1309
    563,   563,   564,   564,   564,   565,   565,   565,   566,   566,
1310
    566,   566,   567,   567,   567,   568,   568,   568,   569,   569,
1311
    569,   569,   570,   570,   570,   571,   571,   571,   572,   572,
1312
    572,   572,   573,   573,   573,   574,   574,   574,   575,   575,
1313
    575,   575,   576,   576,   576,   577,   577,   577,   578,   578,
1314
    578,   579,   579,   579,   579,   580,   580,   580,   581,   581,
1315
    581,   582,   582,   582,   582,   583,   583,   583,   584,   584,
1316
    584,   585,   585,   585,   585,   586,   586,   586,   587,   587,
1317
    587,   588,   588,   588,   588,   589,   589,   589,   590,   590,
1318
    590,   591,   591,   591,   591,   592,   592,   592,   593,   593,
1319
    593,   594,   594,   594,   594,   595,   595,   595,   596,   596,
1320
    596,   597,   597,   597,   597,   598,   598,   598,   599,   599,
1321
    599,   600,   600,   600,   600,   601,   601,   601,   602,   602,
1322
    602,   603,   603,   603,   603,   604,   604,   604,   605,   605,
1323
    605,   606,   606,   606,   607,   607,   607,   607,   608,   608,
1324
    608,   609,   609,   609,   610,   610,   610,   610,   611,   611,
1325
    611,   612,   612,   612,   613,   613,   613,   613,   614,   614,
1326
    614,   615,   615,   615,   616,   616,   616,   616,   617,   617,
1327
    617,   618,   618,   618,   619,   619,   619,   619,   620,   620,
1328
    620,   621,   621,   621,   622,   622,   622,   622,   623,   623,
1329
    623,   624,   624,   624,   625,   625,   625,   625,   626,   626,
1330
    626,   627,   627,   627,   628,   628,   628,   628,   629,   629,
1331
    629,   630,   630,   630,   631,   631,   631,   631,   632,   632,
1332
    632,   633,   633,   633,   634,   634,   634,   634,   635,   635,
1333
    635,   636,   636,   636,   637,   637,   637,   638,   638,   638,
1334
    638,   639,   639,   639,   640,   640,   640,   641,   641,   641,
1335
    641,   642,   642,   642,   643,   643,   643,   644,   644,   644,
1336
    644,   645,   645,   645,   646,   646,   646,   647,   647,   647,
1337
    647,   648,   648,   648,   649,   649,   649,   650,   650 };
1338
 static ULLong pfive[27] = {
1339
    5ll,
1340
    25ll,
1341
    125ll,
1342
    625ll,
1343
    3125ll,
1344
    15625ll,
1345
    78125ll,
1346
    390625ll,
1347
    1953125ll,
1348
    9765625ll,
1349
    48828125ll,
1350
    244140625ll,
1351
    1220703125ll,
1352
    6103515625ll,
1353
    30517578125ll,
1354
    152587890625ll,
1355
    762939453125ll,
1356
    3814697265625ll,
1357
    19073486328125ll,
1358
    95367431640625ll,
1359
    476837158203125ll,
1360
    2384185791015625ll,
1361
    11920928955078125ll,
1362
    59604644775390625ll,
1363
    298023223876953125ll,
1364
    1490116119384765625ll,
1365
    7450580596923828125ll
1366
    };
1367
1368
 static int pfivebits[25] = {3, 5, 7, 10, 12, 14, 17, 19, 21, 24, 26, 28, 31,
1369
           33, 35, 38, 40, 42, 45, 47, 49, 52, 54, 56, 59};
1370
#endif /*}*/
1371
#endif /*}} NO_LONG_LONG */
1372
1373
typedef union { double d; ULong L[2];
1374
#ifdef USE_BF96
1375
  ULLong LL;
1376
#endif
1377
  } U;
1378
1379
#ifdef IEEE_8087
1380
126M
#define word0(x) (x)->L[1]
1381
47.3M
#define word1(x) (x)->L[0]
1382
#else
1383
#define word0(x) (x)->L[0]
1384
#define word1(x) (x)->L[1]
1385
#endif
1386
198M
#define dval(x) (x)->d
1387
#define LLval(x) (x)->LL
1388
1389
#ifndef STRTOD_DIGLIM
1390
2.81M
#define STRTOD_DIGLIM 40
1391
#endif
1392
1393
#ifdef DIGLIM_DEBUG
1394
extern int strtod_diglim;
1395
#else
1396
2.81M
#define strtod_diglim STRTOD_DIGLIM
1397
#endif
1398
1399
/* The following definition of Storeinc is appropriate for MIPS processors.
1400
 * An alternative that might be better on some machines is
1401
 * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
1402
 */
1403
#if defined(IEEE_8087) + defined(VAX)
1404
#define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
1405
((unsigned short *)a)[0] = (unsigned short)c, a++)
1406
#else
1407
#define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
1408
((unsigned short *)a)[1] = (unsigned short)c, a++)
1409
#endif
1410
1411
/* #define P DBL_MANT_DIG */
1412
/* Ten_pmax = floor(P*log(2)/log(5)) */
1413
/* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
1414
/* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
1415
/* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
1416
1417
#ifdef IEEE_Arith
1418
14.4M
#define Exp_shift  20
1419
22.7M
#define Exp_shift1 20
1420
22.2M
#define Exp_msk1    0x100000
1421
#define Exp_msk11   0x100000
1422
43.2M
#define Exp_mask  0x7ff00000
1423
40.0M
#define P 53
1424
#define Nbits 53
1425
28.2M
#define Bias 1023
1426
#define Emax 1023
1427
2.97M
#define Emin (-1022)
1428
1.61M
#define Exp_1  0x3ff00000
1429
11.3M
#define Exp_11 0x3ff00000
1430
3.91M
#define Ebits 11
1431
14.3M
#define Frac_mask  0xfffff
1432
11.3M
#define Frac_mask1 0xfffff
1433
11.0M
#define Ten_pmax 22
1434
443k
#define Bletch 0x10
1435
10.8M
#define Bndry_mask  0xfffff
1436
23.2k
#define Bndry_mask1 0xfffff
1437
#define LSB 1
1438
14.8M
#define Sign_bit 0x80000000
1439
2.26M
#define Log2P 1
1440
#define Tiny0 0
1441
651k
#define Tiny1 1
1442
11.8M
#define Quick_max 14
1443
9.51M
#define Int_max 14
1444
#ifndef NO_IEEE_Scale
1445
#define Avoid_Underflow
1446
#ifdef Flush_Denorm /* debugging option */
1447
#undef Sudden_Underflow
1448
#endif
1449
#endif
1450
1451
#ifndef Flt_Rounds
1452
#ifdef FLT_ROUNDS
1453
6.27M
#define Flt_Rounds FLT_ROUNDS
1454
#else
1455
#define Flt_Rounds 1
1456
#endif
1457
#endif /*Flt_Rounds*/
1458
1459
#ifdef Honor_FLT_ROUNDS
1460
#undef Check_FLT_ROUNDS
1461
#define Check_FLT_ROUNDS
1462
#else
1463
#define Rounding Flt_Rounds
1464
#endif
1465
1466
#else /* ifndef IEEE_Arith */
1467
#undef Check_FLT_ROUNDS
1468
#undef Honor_FLT_ROUNDS
1469
#undef SET_INEXACT
1470
#undef  Sudden_Underflow
1471
#define Sudden_Underflow
1472
#ifdef IBM
1473
#undef Flt_Rounds
1474
#define Flt_Rounds 0
1475
#define Exp_shift  24
1476
#define Exp_shift1 24
1477
#define Exp_msk1   0x1000000
1478
#define Exp_msk11  0x1000000
1479
#define Exp_mask  0x7f000000
1480
#define P 14
1481
#define Nbits 56
1482
#define Bias 65
1483
#define Emax 248
1484
#define Emin (-260)
1485
#define Exp_1  0x41000000
1486
#define Exp_11 0x41000000
1487
#define Ebits 8 /* exponent has 7 bits, but 8 is the right value in b2d */
1488
#define Frac_mask  0xffffff
1489
#define Frac_mask1 0xffffff
1490
#define Bletch 4
1491
#define Ten_pmax 22
1492
#define Bndry_mask  0xefffff
1493
#define Bndry_mask1 0xffffff
1494
#define LSB 1
1495
#define Sign_bit 0x80000000
1496
#define Log2P 4
1497
#define Tiny0 0x100000
1498
#define Tiny1 0
1499
#define Quick_max 14
1500
#define Int_max 15
1501
#else /* VAX */
1502
#undef Flt_Rounds
1503
#define Flt_Rounds 1
1504
#define Exp_shift  23
1505
#define Exp_shift1 7
1506
#define Exp_msk1    0x80
1507
#define Exp_msk11   0x800000
1508
#define Exp_mask  0x7f80
1509
#define P 56
1510
#define Nbits 56
1511
#define Bias 129
1512
#define Emax 126
1513
#define Emin (-129)
1514
#define Exp_1  0x40800000
1515
#define Exp_11 0x4080
1516
#define Ebits 8
1517
#define Frac_mask  0x7fffff
1518
#define Frac_mask1 0xffff007f
1519
#define Ten_pmax 24
1520
#define Bletch 2
1521
#define Bndry_mask  0xffff007f
1522
#define Bndry_mask1 0xffff007f
1523
#define LSB 0x10000
1524
#define Sign_bit 0x8000
1525
#define Log2P 1
1526
#define Tiny0 0x80
1527
#define Tiny1 0
1528
#define Quick_max 15
1529
#define Int_max 15
1530
#endif /* IBM, VAX */
1531
#endif /* IEEE_Arith */
1532
1533
#ifndef IEEE_Arith
1534
#define ROUND_BIASED
1535
#else
1536
#ifdef ROUND_BIASED_without_Round_Up
1537
#undef  ROUND_BIASED
1538
#define ROUND_BIASED
1539
#endif
1540
#endif
1541
1542
#ifdef RND_PRODQUOT
1543
#define rounded_product(a,b) a = rnd_prod(a, b)
1544
#define rounded_quotient(a,b) a = rnd_quot(a, b)
1545
extern double rnd_prod(double, double), rnd_quot(double, double);
1546
#else
1547
14.2k
#define rounded_product(a,b) a *= b
1548
139k
#define rounded_quotient(a,b) a /= b
1549
#endif
1550
1551
1.15k
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
1552
830
#define Big1 0xffffffff
1553
1554
#ifndef Pack_32
1555
#define Pack_32
1556
#endif
1557
1558
typedef struct BCinfo BCinfo;
1559
 struct
1560
BCinfo { int dp0, dp1, dplen, dsign, e0, inexact, nd, nd0, rounding, scale, uflchk; };
1561
1562
1.47G
#define FFFFFFFF 0xffffffffUL
1563
1564
#ifdef MULTIPLE_THREADS
1565
#define MTa , PTI
1566
#define MTb , &TI
1567
#define MTd , ThInfo **PTI
1568
static unsigned int maxthreads = 0;
1569
#elif __XS__
1570
208M
#define MTa __XS__a
1571
308M
#define MTb __XS__a
1572
#define MTd __XS__d
1573
#else
1574
#define MTa /*nothing*/
1575
#define MTb /*nothing*/
1576
#define MTd /*nothing*/
1577
#endif
1578
1579
408M
#define Kmax 7
1580
1581
#ifdef __cplusplus
1582
extern "C" double strtod2(const char *s00, char **se __XS__d);
1583
extern "C" char *dtoa(double d, int mode, int ndigits,
1584
      int *decpt, int *sign, char **rve __XS__d);
1585
#endif
1586
1587
 struct
1588
Bigint {
1589
  struct Bigint *next;
1590
  int k, maxwds, sign, wds;
1591
  ULong x[1];
1592
  };
1593
1594
 typedef struct Bigint Bigint;
1595
 typedef struct
1596
ThInfo {
1597
  Bigint *Freelist[Kmax+1];
1598
  Bigint *P5s;
1599
#ifdef __XS__
1600
  txMachine* the;
1601
  #if mxUseChunkHeap
1602
    txByte* current;
1603
    int dirty;
1604
  #endif
1605
#endif
1606
  } ThInfo;
1607
1608
#ifdef __XS__
1609
452M
#define TI0 (*DTOA)
1610
#else
1611
 static ThInfo TI0;
1612
#endif
1613
1614
#ifdef MULTIPLE_THREADS
1615
 static ThInfo *TI1;
1616
 static int TI0_used;
1617
1618
 void
1619
set_max_dtoa_threads(unsigned int n)
1620
{
1621
  size_t L;
1622
1623
  if (n > maxthreads) {
1624
    L = n*sizeof(ThInfo);
1625
    if (TI1) {
1626
      TI1 = (ThInfo*)REALLOC(TI1, L);
1627
      memset(TI1 + maxthreads, 0, (n-maxthreads)*sizeof(ThInfo));
1628
      }
1629
    else {
1630
      TI1 = (ThInfo*)MALLOC(L);
1631
      if (TI0_used) {
1632
        memcpy(TI1, &TI0, sizeof(ThInfo));
1633
        if (n > 1)
1634
          memset(TI1 + 1, 0, L - sizeof(ThInfo));
1635
        memset(&TI0, 0, sizeof(ThInfo));
1636
        }
1637
      else
1638
        memset(TI1, 0, L);
1639
      }
1640
    maxthreads = n;
1641
    }
1642
  }
1643
1644
 static ThInfo*
1645
get_TI(void)
1646
{
1647
  unsigned int thno = dtoa_get_threadno();
1648
  if (thno < maxthreads)
1649
    return TI1 + thno;
1650
  if (thno == 0)
1651
    TI0_used = 1;
1652
  return &TI0;
1653
  }
1654
#define freelist TI->Freelist
1655
#define p5s TI->P5s
1656
#else
1657
442M
#define freelist TI0.Freelist
1658
9.73M
#define p5s TI0.P5s
1659
#endif
1660
1661
 static Bigint *
1662
Balloc(int k MTd)
1663
140M
{
1664
140M
  int x;
1665
140M
  Bigint *rv;
1666
#ifndef Omit_Private_Memory
1667
  unsigned int len;
1668
#endif
1669
#ifdef MULTIPLE_THREADS
1670
  ThInfo *TI;
1671
1672
  if (!(TI = *PTI))
1673
    *PTI = TI = get_TI();
1674
  if (TI == &TI0)
1675
    ACQUIRE_DTOA_LOCK(0);
1676
#endif
1677
  /* The k > Kmax case does not need ACQUIRE_DTOA_LOCK(0), */
1678
  /* but this case seems very unlikely. */
1679
140M
  if (k <= Kmax && (rv = freelist[k]))
1680
140M
    freelist[k] = rv->next;
1681
83.7M
  else {
1682
83.7M
    x = 1 << k;
1683
83.7M
#ifdef Omit_Private_Memory
1684
83.7M
    rv = (Bigint *)MALLOC(sizeof(Bigint) + (x-1)*sizeof(ULong) __XS__a);
1685
#else
1686
    len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
1687
      /sizeof(double);
1688
    if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem
1689
#ifdef MULTIPLE_THREADS
1690
      && TI == TI1
1691
#endif
1692
      ) {
1693
      rv = (Bigint*)pmem_next;
1694
      pmem_next += len;
1695
      }
1696
    else
1697
      rv = (Bigint*)MALLOC(len*sizeof(double) __XS__a);
1698
#endif
1699
83.7M
    rv->k = k;
1700
83.7M
    rv->maxwds = x;
1701
83.7M
    }
1702
#ifdef MULTIPLE_THREADS
1703
  if (TI == &TI0)
1704
    FREE_DTOA_LOCK(0);
1705
#endif
1706
140M
  rv->sign = rv->wds = 0;
1707
140M
  return rv;
1708
140M
  }
1709
1710
 static void
1711
Bfree(Bigint *v MTd)
1712
122M
{
1713
#ifdef MULTIPLE_THREADS
1714
  ThInfo *TI;
1715
#endif
1716
122M
  if (v) {
1717
122M
    if (v->k > Kmax)
1718
0
      FREE((void*)v __XS__a);
1719
122M
    else {
1720
#ifdef MULTIPLE_THREADS
1721
      if (!(TI = *PTI))
1722
        *PTI = TI = get_TI();
1723
      if (TI == &TI0)
1724
        ACQUIRE_DTOA_LOCK(0);
1725
#endif
1726
122M
      v->next = freelist[v->k];
1727
122M
      freelist[v->k] = v;
1728
#ifdef MULTIPLE_THREADS
1729
      if (TI == &TI0)
1730
        FREE_DTOA_LOCK(0);
1731
#endif
1732
122M
      }
1733
122M
    }
1734
122M
  }
1735
1736
3.85M
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
1737
3.85M
y->wds*sizeof(Long) + 2*sizeof(int))
1738
1739
 static Bigint *
1740
multadd(Bigint *b, int m, int a MTd)  /* multiply by m and add a */
1741
122M
{
1742
122M
  int i, wds;
1743
122M
#ifdef ULLong
1744
122M
  ULong *x;
1745
122M
  ULLong carry, y;
1746
#else
1747
  ULong carry, *x, y;
1748
#ifdef Pack_32
1749
  ULong xi, z;
1750
#endif
1751
#endif
1752
122M
  Bigint *b1;
1753
1754
122M
  wds = b->wds;
1755
122M
  x = b->x;
1756
122M
  i = 0;
1757
122M
  carry = a;
1758
557M
  do {
1759
557M
#ifdef ULLong
1760
557M
    y = *x * (ULLong)m + carry;
1761
557M
    carry = y >> 32;
1762
557M
    *x++ = y & FFFFFFFF;
1763
#else
1764
#ifdef Pack_32
1765
    xi = *x;
1766
    y = (xi & 0xffff) * m + carry;
1767
    z = (xi >> 16) * m + (y >> 16);
1768
    carry = z >> 16;
1769
    *x++ = (z << 16) + (y & 0xffff);
1770
#else
1771
    y = *x * m + carry;
1772
    carry = y >> 16;
1773
    *x++ = y & 0xffff;
1774
#endif
1775
#endif
1776
557M
    }
1777
557M
    while(++i < wds);
1778
122M
  if (carry) {
1779
6.41M
    if (wds >= b->maxwds) {
1780
179k
      b1 = Balloc(b->k+1 MTa);
1781
179k
      Bcopy(b1, b);
1782
179k
      Bfree(b MTa);
1783
179k
      b = b1;
1784
179k
      }
1785
6.41M
    b->x[wds++] = (ULong)carry;
1786
6.41M
    b->wds = wds;
1787
6.41M
    }
1788
122M
  return b;
1789
122M
  }
1790
1791
 static Bigint *
1792
s2b(const char *s, int nd0, int nd, ULong y9, int dplen MTd)
1793
2.81M
{
1794
2.81M
  Bigint *b;
1795
2.81M
  int i, k;
1796
2.81M
  Long x, y;
1797
1798
2.81M
  x = (nd + 8) / 9;
1799
5.92M
  for(k = 0, y = 1; x > y; y <<= 1, k++) ;
1800
2.81M
#ifdef Pack_32
1801
2.81M
  b = Balloc(k MTa);
1802
2.81M
  b->x[0] = y9;
1803
2.81M
  b->wds = 1;
1804
#else
1805
  b = Balloc(k+1 MTa);
1806
  b->x[0] = y9 & 0xffff;
1807
  b->wds = (b->x[1] = y9 >> 16) ? 2 : 1;
1808
#endif
1809
1810
2.81M
  i = 9;
1811
2.81M
  if (9 < nd0) {
1812
987k
    s += 9;
1813
7.04M
    do b = multadd(b, 10, *s++ - '0' MTa);
1814
7.04M
      while(++i < nd0);
1815
987k
    s += dplen;
1816
987k
    }
1817
1.82M
  else
1818
1.82M
    s += dplen + 9;
1819
17.3M
  for(; i < nd; i++)
1820
14.5M
    b = multadd(b, 10, *s++ - '0' MTa);
1821
2.81M
  return b;
1822
2.81M
  }
1823
1824
 static int
1825
hi0bits(ULong x)
1826
4.69M
{
1827
4.69M
  int k = 0;
1828
1829
4.69M
  if (!(x & 0xffff0000)) {
1830
3.02M
    k = 16;
1831
3.02M
    x <<= 16;
1832
3.02M
    }
1833
4.69M
  if (!(x & 0xff000000)) {
1834
2.76M
    k += 8;
1835
2.76M
    x <<= 8;
1836
2.76M
    }
1837
4.69M
  if (!(x & 0xf0000000)) {
1838
2.67M
    k += 4;
1839
2.67M
    x <<= 4;
1840
2.67M
    }
1841
4.69M
  if (!(x & 0xc0000000)) {
1842
2.70M
    k += 2;
1843
2.70M
    x <<= 2;
1844
2.70M
    }
1845
4.69M
  if (!(x & 0x80000000)) {
1846
2.77M
    k++;
1847
2.77M
    if (!(x & 0x40000000))
1848
0
      return 32;
1849
2.77M
    }
1850
4.69M
  return k;
1851
4.69M
  }
1852
1853
 static int
1854
lo0bits(ULong *y)
1855
14.3M
{
1856
14.3M
  int k;
1857
14.3M
  ULong x = *y;
1858
1859
14.3M
  if (x & 7) {
1860
4.55M
    if (x & 1)
1861
2.42M
      return 0;
1862
2.13M
    if (x & 2) {
1863
1.50M
      *y = x >> 1;
1864
1.50M
      return 1;
1865
1.50M
      }
1866
625k
    *y = x >> 2;
1867
625k
    return 2;
1868
2.13M
    }
1869
9.76M
  k = 0;
1870
9.76M
  if (!(x & 0xffff)) {
1871
4.88M
    k = 16;
1872
4.88M
    x >>= 16;
1873
4.88M
    }
1874
9.76M
  if (!(x & 0xff)) {
1875
4.15M
    k += 8;
1876
4.15M
    x >>= 8;
1877
4.15M
    }
1878
9.76M
  if (!(x & 0xf)) {
1879
5.92M
    k += 4;
1880
5.92M
    x >>= 4;
1881
5.92M
    }
1882
9.76M
  if (!(x & 0x3)) {
1883
4.32M
    k += 2;
1884
4.32M
    x >>= 2;
1885
4.32M
    }
1886
9.76M
  if (!(x & 1)) {
1887
4.23M
    k++;
1888
4.23M
    x >>= 1;
1889
4.23M
    if (!x)
1890
0
      return 32;
1891
4.23M
    }
1892
9.76M
  *y = x;
1893
9.76M
  return k;
1894
9.76M
  }
1895
1896
 static Bigint *
1897
i2b(int i MTd)
1898
14.1M
{
1899
14.1M
  Bigint *b;
1900
1901
14.1M
  b = Balloc(1 MTa);
1902
14.1M
  b->x[0] = i;
1903
14.1M
  b->wds = 1;
1904
14.1M
  return b;
1905
14.1M
  }
1906
1907
 static Bigint *
1908
mult(Bigint *a, Bigint *b MTd)
1909
26.4M
{
1910
26.4M
  Bigint *c;
1911
26.4M
  int k, wa, wb, wc;
1912
26.4M
  ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
1913
26.4M
  ULong y;
1914
26.4M
#ifdef ULLong
1915
26.4M
  ULLong carry, z;
1916
#else
1917
  ULong carry, z;
1918
#ifdef Pack_32
1919
  ULong z2;
1920
#endif
1921
#endif
1922
1923
26.4M
  if (a->wds < b->wds) {
1924
5.34M
    c = a;
1925
5.34M
    a = b;
1926
5.34M
    b = c;
1927
5.34M
    }
1928
26.4M
  k = a->k;
1929
26.4M
  wa = a->wds;
1930
26.4M
  wb = b->wds;
1931
26.4M
  wc = wa + wb;
1932
26.4M
  if (wc > a->maxwds)
1933
10.7M
    k++;
1934
26.4M
  c = Balloc(k MTa);
1935
148M
  for(x = c->x, xa = x + wc; x < xa; x++)
1936
121M
    *x = 0;
1937
26.4M
  xa = a->x;
1938
26.4M
  xae = xa + wa;
1939
26.4M
  xb = b->x;
1940
26.4M
  xbe = xb + wb;
1941
26.4M
  xc0 = c->x;
1942
26.4M
#ifdef ULLong
1943
73.7M
  for(; xb < xbe; xc0++) {
1944
47.3M
    if ((y = *xb++)) {
1945
47.3M
      x = xa;
1946
47.3M
      xc = xc0;
1947
47.3M
      carry = 0;
1948
199M
      do {
1949
199M
        z = *x++ * (ULLong)y + *xc + carry;
1950
199M
        carry = z >> 32;
1951
199M
        *xc++ = z & FFFFFFFF;
1952
199M
        }
1953
199M
        while(x < xae);
1954
47.3M
      *xc = (ULong)carry;
1955
47.3M
      }
1956
47.3M
    }
1957
#else
1958
#ifdef Pack_32
1959
  for(; xb < xbe; xb++, xc0++) {
1960
    if ((y = *xb & 0xffff)) {
1961
      x = xa;
1962
      xc = xc0;
1963
      carry = 0;
1964
      do {
1965
        z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
1966
        carry = z >> 16;
1967
        z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
1968
        carry = z2 >> 16;
1969
        Storeinc(xc, z2, z);
1970
        }
1971
        while(x < xae);
1972
      *xc = carry;
1973
      }
1974
    if ((y = *xb >> 16)) {
1975
      x = xa;
1976
      xc = xc0;
1977
      carry = 0;
1978
      z2 = *xc;
1979
      do {
1980
        z = (*x & 0xffff) * y + (*xc >> 16) + carry;
1981
        carry = z >> 16;
1982
        Storeinc(xc, z, z2);
1983
        z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
1984
        carry = z2 >> 16;
1985
        }
1986
        while(x < xae);
1987
      *xc = z2;
1988
      }
1989
    }
1990
#else
1991
  for(; xb < xbe; xc0++) {
1992
    if (y = *xb++) {
1993
      x = xa;
1994
      xc = xc0;
1995
      carry = 0;
1996
      do {
1997
        z = *x++ * y + *xc + carry;
1998
        carry = z >> 16;
1999
        *xc++ = z & 0xffff;
2000
        }
2001
        while(x < xae);
2002
      *xc = carry;
2003
      }
2004
    }
2005
#endif
2006
#endif
2007
44.6M
  for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
2008
26.4M
  c->wds = wc;
2009
26.4M
  return c;
2010
26.4M
  }
2011
2012
 static Bigint *
2013
pow5mult(Bigint *b, int k MTd)
2014
5.38M
{
2015
5.38M
  Bigint *b1, *p5, *p51;
2016
#ifdef MULTIPLE_THREADS
2017
  ThInfo *TI;
2018
#endif
2019
5.38M
  int i;
2020
5.38M
  static int p05[3] = { 5, 25, 125 };
2021
2022
5.38M
  if ((i = k & 3))
2023
4.14M
    b = multadd(b, p05[i-1], 0 MTa);
2024
2025
5.38M
  if (!(k >>= 2))
2026
488k
    return b;
2027
#ifdef  MULTIPLE_THREADS
2028
  if (!(TI = *PTI))
2029
    *PTI = TI = get_TI();
2030
#endif
2031
4.89M
  if (!(p5 = p5s)) {
2032
    /* first time */
2033
#ifdef MULTIPLE_THREADS
2034
    if (!(TI = *PTI))
2035
      *PTI = TI = get_TI();
2036
    if (TI == &TI0)
2037
      ACQUIRE_DTOA_LOCK(1);
2038
    if (!(p5 = p5s)) {
2039
      p5 = p5s = i2b(625 MTa);
2040
      p5->next = 0;
2041
      }
2042
    if (TI == &TI0)
2043
      FREE_DTOA_LOCK(1);
2044
#else
2045
4.83M
    p5 = p5s = i2b(625 MTa);
2046
4.83M
    p5->next = 0;
2047
4.83M
#endif
2048
4.83M
    }
2049
19.0M
  for(;;) {
2050
19.0M
    if (k & 1) {
2051
10.7M
      b1 = mult(b, p5 MTa);
2052
10.7M
      Bfree(b MTa);
2053
10.7M
      b = b1;
2054
10.7M
      }
2055
19.0M
    if (!(k >>= 1))
2056
4.89M
      break;
2057
14.1M
    if (!(p51 = p5->next)) {
2058
#ifdef MULTIPLE_THREADS
2059
      if (!TI && !(TI = *PTI))
2060
        *PTI = TI = get_TI();
2061
      if (TI == &TI0)
2062
        ACQUIRE_DTOA_LOCK(1);
2063
      if (!(p51 = p5->next)) {
2064
        p51 = p5->next = mult(p5,p5 MTa);
2065
        p51->next = 0;
2066
        }
2067
      if (TI == &TI0)
2068
        FREE_DTOA_LOCK(1);
2069
#else
2070
13.8M
      p51 = p5->next = mult(p5,p5 __XS__a);
2071
13.8M
      p51->next = 0;
2072
13.8M
#endif
2073
13.8M
      }
2074
14.1M
    p5 = p51;
2075
14.1M
    }
2076
4.89M
  return b;
2077
5.38M
  }
2078
2079
 static Bigint *
2080
lshift(Bigint *b, int k MTd)
2081
17.0M
{
2082
17.0M
  int i, k1, n, n1;
2083
17.0M
  Bigint *b1;
2084
17.0M
  ULong *x, *x1, *xe, z;
2085
2086
17.0M
#ifdef Pack_32
2087
17.0M
  n = k >> 5;
2088
#else
2089
  n = k >> 4;
2090
#endif
2091
17.0M
  k1 = b->k;
2092
17.0M
  n1 = n + b->wds + 1;
2093
33.2M
  for(i = b->maxwds; n1 > i; i <<= 1)
2094
16.2M
    k1++;
2095
17.0M
  b1 = Balloc(k1 MTa);
2096
17.0M
  x1 = b1->x;
2097
54.4M
  for(i = 0; i < n; i++)
2098
37.4M
    *x1++ = 0;
2099
17.0M
  x = b->x;
2100
17.0M
  xe = x + b->wds;
2101
17.0M
#ifdef Pack_32
2102
17.0M
  if (k &= 0x1f) {
2103
16.9M
    k1 = 32 - k;
2104
16.9M
    z = 0;
2105
55.1M
    do {
2106
55.1M
      *x1++ = *x << k | z;
2107
55.1M
      z = *x++ >> k1;
2108
55.1M
      }
2109
55.1M
      while(x < xe);
2110
16.9M
    if ((*x1 = z))
2111
2.34M
      ++n1;
2112
16.9M
    }
2113
#else
2114
  if (k &= 0xf) {
2115
    k1 = 16 - k;
2116
    z = 0;
2117
    do {
2118
      *x1++ = *x << k  & 0xffff | z;
2119
      z = *x++ >> k1;
2120
      }
2121
      while(x < xe);
2122
    if (*x1 = z)
2123
      ++n1;
2124
    }
2125
#endif
2126
137k
  else do
2127
433k
    *x1++ = *x++;
2128
433k
    while(x < xe);
2129
17.0M
  b1->wds = n1 - 1;
2130
17.0M
  Bfree(b MTa);
2131
17.0M
  return b1;
2132
17.0M
  }
2133
2134
 static int
2135
cmp(Bigint *a, Bigint *b)
2136
191M
{
2137
191M
  ULong *xa, *xa0, *xb, *xb0;
2138
191M
  int i, j;
2139
2140
191M
  i = a->wds;
2141
191M
  j = b->wds;
2142
#ifdef DEBUG
2143
  if (i > 1 && !a->x[i-1])
2144
    Bug("cmp called with a->x[a->wds-1] == 0");
2145
  if (j > 1 && !b->x[j-1])
2146
    Bug("cmp called with b->x[b->wds-1] == 0");
2147
#endif
2148
191M
  if (i -= j)
2149
45.1M
    return i;
2150
146M
  xa0 = a->x;
2151
146M
  xa = xa0 + j;
2152
146M
  xb0 = b->x;
2153
146M
  xb = xb0 + j;
2154
151M
  for(;;) {
2155
151M
    if (*--xa != *--xb)
2156
145M
      return *xa < *xb ? -1 : 1;
2157
6.42M
    if (xa <= xa0)
2158
1.03M
      break;
2159
6.42M
    }
2160
1.03M
  return 0;
2161
146M
  }
2162
2163
 static Bigint *
2164
diff(Bigint *a, Bigint *b MTd)
2165
48.9M
{
2166
48.9M
  Bigint *c;
2167
48.9M
  int i, wa, wb;
2168
48.9M
  ULong *xa, *xae, *xb, *xbe, *xc;
2169
48.9M
#ifdef ULLong
2170
48.9M
  ULLong borrow, y;
2171
#else
2172
  ULong borrow, y;
2173
#ifdef Pack_32
2174
  ULong z;
2175
#endif
2176
#endif
2177
2178
48.9M
  i = cmp(a,b);
2179
48.9M
  if (!i) {
2180
418k
    c = Balloc(0 MTa);
2181
418k
    c->wds = 1;
2182
418k
    c->x[0] = 0;
2183
418k
    return c;
2184
418k
    }
2185
48.4M
  if (i < 0) {
2186
2.20M
    c = a;
2187
2.20M
    a = b;
2188
2.20M
    b = c;
2189
2.20M
    i = 1;
2190
2.20M
    }
2191
46.2M
  else
2192
46.2M
    i = 0;
2193
48.4M
  c = Balloc(a->k MTa);
2194
48.4M
  c->sign = i;
2195
48.4M
  wa = a->wds;
2196
48.4M
  xa = a->x;
2197
48.4M
  xae = xa + wa;
2198
48.4M
  wb = b->wds;
2199
48.4M
  xb = b->x;
2200
48.4M
  xbe = xb + wb;
2201
48.4M
  xc = c->x;
2202
48.4M
  borrow = 0;
2203
48.4M
#ifdef ULLong
2204
246M
  do {
2205
246M
    y = (ULLong)*xa++ - *xb++ - borrow;
2206
246M
    borrow = y >> 32 & (ULong)1;
2207
246M
    *xc++ = y & FFFFFFFF;
2208
246M
    }
2209
246M
    while(xb < xbe);
2210
70.6M
  while(xa < xae) {
2211
22.1M
    y = *xa++ - borrow;
2212
22.1M
    borrow = y >> 32 & (ULong)1;
2213
22.1M
    *xc++ = y & FFFFFFFF;
2214
22.1M
    }
2215
#else
2216
#ifdef Pack_32
2217
  do {
2218
    y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
2219
    borrow = (y & 0x10000) >> 16;
2220
    z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
2221
    borrow = (z & 0x10000) >> 16;
2222
    Storeinc(xc, z, y);
2223
    }
2224
    while(xb < xbe);
2225
  while(xa < xae) {
2226
    y = (*xa & 0xffff) - borrow;
2227
    borrow = (y & 0x10000) >> 16;
2228
    z = (*xa++ >> 16) - borrow;
2229
    borrow = (z & 0x10000) >> 16;
2230
    Storeinc(xc, z, y);
2231
    }
2232
#else
2233
  do {
2234
    y = *xa++ - *xb++ - borrow;
2235
    borrow = (y & 0x10000) >> 16;
2236
    *xc++ = y & 0xffff;
2237
    }
2238
    while(xb < xbe);
2239
  while(xa < xae) {
2240
    y = *xa++ - borrow;
2241
    borrow = (y & 0x10000) >> 16;
2242
    *xc++ = y & 0xffff;
2243
    }
2244
#endif
2245
#endif
2246
52.6M
  while(!*--xc)
2247
4.13M
    wa--;
2248
48.4M
  c->wds = wa;
2249
48.4M
  return c;
2250
48.9M
  }
2251
2252
 static double
2253
ulp(U *x)
2254
764k
{
2255
764k
  Long L;
2256
764k
  U u;
2257
2258
764k
  L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
2259
#ifndef Avoid_Underflow
2260
#ifndef Sudden_Underflow
2261
  if (L > 0) {
2262
#endif
2263
#endif
2264
#ifdef IBM
2265
    L |= Exp_msk1 >> 4;
2266
#endif
2267
764k
    word0(&u) = L;
2268
764k
    word1(&u) = 0;
2269
#ifndef Avoid_Underflow
2270
#ifndef Sudden_Underflow
2271
    }
2272
  else {
2273
    L = -L >> Exp_shift;
2274
    if (L < Exp_shift) {
2275
      word0(&u) = 0x80000 >> L;
2276
      word1(&u) = 0;
2277
      }
2278
    else {
2279
      word0(&u) = 0;
2280
      L -= Exp_shift;
2281
      word1(&u) = L >= 31 ? 1 : 1 << 31 - L;
2282
      }
2283
    }
2284
#endif
2285
#endif
2286
764k
  return dval(&u);
2287
764k
  }
2288
2289
 static double
2290
b2d(Bigint *a, int *e)
2291
1.51M
{
2292
1.51M
  ULong *xa, *xa0, w, y, z;
2293
1.51M
  int k;
2294
1.51M
  U d;
2295
#ifdef VAX
2296
  ULong d0, d1;
2297
#else
2298
1.51M
#define d0 word0(&d)
2299
1.51M
#define d1 word1(&d)
2300
1.51M
#endif
2301
2302
1.51M
  xa0 = a->x;
2303
1.51M
  xa = xa0 + a->wds;
2304
1.51M
  y = *--xa;
2305
#ifdef DEBUG
2306
  if (!y) Bug("zero y in b2d");
2307
#endif
2308
1.51M
  k = hi0bits(y);
2309
1.51M
  *e = 32 - k;
2310
1.51M
#ifdef Pack_32
2311
1.51M
  if (k < Ebits) {
2312
442k
    d0 = Exp_1 | y >> (Ebits - k);
2313
442k
    w = xa > xa0 ? *--xa : 0;
2314
442k
    d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
2315
442k
    goto ret_d;
2316
442k
    }
2317
1.07M
  z = xa > xa0 ? *--xa : 0;
2318
1.07M
  if (k -= Ebits) {
2319
1.02M
    d0 = Exp_1 | y << k | z >> (32 - k);
2320
1.02M
    y = xa > xa0 ? *--xa : 0;
2321
1.02M
    d1 = z << k | y >> (32 - k);
2322
1.02M
    }
2323
44.5k
  else {
2324
44.5k
    d0 = Exp_1 | y;
2325
44.5k
    d1 = z;
2326
44.5k
    }
2327
#else
2328
  if (k < Ebits + 16) {
2329
    z = xa > xa0 ? *--xa : 0;
2330
    d0 = Exp_1 | y << k - Ebits | z >> Ebits + 16 - k;
2331
    w = xa > xa0 ? *--xa : 0;
2332
    y = xa > xa0 ? *--xa : 0;
2333
    d1 = z << k + 16 - Ebits | w << k - Ebits | y >> 16 + Ebits - k;
2334
    goto ret_d;
2335
    }
2336
  z = xa > xa0 ? *--xa : 0;
2337
  w = xa > xa0 ? *--xa : 0;
2338
  k -= Ebits + 16;
2339
  d0 = Exp_1 | y << k + 16 | z << k | w >> 16 - k;
2340
  y = xa > xa0 ? *--xa : 0;
2341
  d1 = w << k + 16 | y << k;
2342
#endif
2343
1.51M
 ret_d:
2344
#ifdef VAX
2345
  word0(&d) = d0 >> 16 | d0 << 16;
2346
  word1(&d) = d1 >> 16 | d1 << 16;
2347
#else
2348
1.51M
#undef d0
2349
1.51M
#undef d1
2350
1.51M
#endif
2351
1.51M
  return dval(&d);
2352
1.07M
  }
2353
2354
 static Bigint *
2355
d2b(U *d, int *e, int *bits MTd)
2356
14.3M
{
2357
14.3M
  Bigint *b;
2358
14.3M
  int de, k;
2359
14.3M
  ULong *x, y, z;
2360
14.3M
#ifndef Sudden_Underflow
2361
14.3M
  int i;
2362
14.3M
#endif
2363
#ifdef VAX
2364
  ULong d0, d1;
2365
  d0 = word0(d) >> 16 | word0(d) << 16;
2366
  d1 = word1(d) >> 16 | word1(d) << 16;
2367
#else
2368
42.9M
#define d0 word0(d)
2369
14.3M
#define d1 word1(d)
2370
14.3M
#endif
2371
2372
14.3M
#ifdef Pack_32
2373
14.3M
  b = Balloc(1 MTa);
2374
#else
2375
  b = Balloc(2 MTa);
2376
#endif
2377
14.3M
  x = b->x;
2378
2379
14.3M
  z = d0 & Frac_mask;
2380
14.3M
  d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
2381
#ifdef Sudden_Underflow
2382
  de = (int)(d0 >> Exp_shift);
2383
#ifndef IBM
2384
  z |= Exp_msk11;
2385
#endif
2386
#else
2387
14.3M
  if ((de = (int)(d0 >> Exp_shift)))
2388
14.3M
    z |= Exp_msk1;
2389
14.3M
#endif
2390
14.3M
#ifdef Pack_32
2391
14.3M
  if ((y = d1)) {
2392
5.45M
    if ((k = lo0bits(&y))) {
2393
3.04M
      x[0] = y | z << (32 - k);
2394
3.04M
      z >>= k;
2395
3.04M
      }
2396
2.41M
    else
2397
2.41M
      x[0] = y;
2398
5.45M
#ifndef Sudden_Underflow
2399
5.45M
    i =
2400
5.45M
#endif
2401
5.45M
        b->wds = (x[1] = z) ? 2 : 1;
2402
5.45M
    }
2403
8.86M
  else {
2404
8.86M
    k = lo0bits(&z);
2405
8.86M
    x[0] = z;
2406
8.86M
#ifndef Sudden_Underflow
2407
8.86M
    i =
2408
8.86M
#endif
2409
8.86M
        b->wds = 1;
2410
8.86M
    k += 32;
2411
8.86M
    }
2412
#else
2413
  if (y = d1) {
2414
    if (k = lo0bits(&y))
2415
      if (k >= 16) {
2416
        x[0] = y | z << 32 - k & 0xffff;
2417
        x[1] = z >> k - 16 & 0xffff;
2418
        x[2] = z >> k;
2419
        i = 2;
2420
        }
2421
      else {
2422
        x[0] = y & 0xffff;
2423
        x[1] = y >> 16 | z << 16 - k & 0xffff;
2424
        x[2] = z >> k & 0xffff;
2425
        x[3] = z >> k+16;
2426
        i = 3;
2427
        }
2428
    else {
2429
      x[0] = y & 0xffff;
2430
      x[1] = y >> 16;
2431
      x[2] = z & 0xffff;
2432
      x[3] = z >> 16;
2433
      i = 3;
2434
      }
2435
    }
2436
  else {
2437
#ifdef DEBUG
2438
    if (!z)
2439
      Bug("Zero passed to d2b");
2440
#endif
2441
    k = lo0bits(&z);
2442
    if (k >= 16) {
2443
      x[0] = z;
2444
      i = 0;
2445
      }
2446
    else {
2447
      x[0] = z & 0xffff;
2448
      x[1] = z >> 16;
2449
      i = 1;
2450
      }
2451
    k += 32;
2452
    }
2453
  while(!x[i])
2454
    --i;
2455
  b->wds = i + 1;
2456
#endif
2457
14.3M
#ifndef Sudden_Underflow
2458
14.3M
  if (de) {
2459
14.3M
#endif
2460
#ifdef IBM
2461
    *e = (de - Bias - (P-1) << 2) + k;
2462
    *bits = 4*P + 8 - k - hi0bits(word0(d) & Frac_mask);
2463
#else
2464
14.3M
    *e = de - Bias - (P-1) + k;
2465
14.3M
    *bits = P - k;
2466
14.3M
#endif
2467
14.3M
#ifndef Sudden_Underflow
2468
14.3M
    }
2469
14.5k
  else {
2470
14.5k
    *e = de - Bias - (P-1) + 1 + k;
2471
14.5k
#ifdef Pack_32
2472
14.5k
    *bits = 32*i - hi0bits(x[i-1]);
2473
#else
2474
    *bits = (i+2)*16 - hi0bits(x[i]);
2475
#endif
2476
14.5k
    }
2477
14.3M
#endif
2478
14.3M
  return b;
2479
14.3M
  }
2480
#undef d0
2481
#undef d1
2482
2483
 static double
2484
ratio(Bigint *a, Bigint *b)
2485
756k
{
2486
756k
  U da, db;
2487
756k
  int k, ka, kb;
2488
2489
756k
  dval(&da) = b2d(a, &ka);
2490
756k
  dval(&db) = b2d(b, &kb);
2491
756k
#ifdef Pack_32
2492
756k
  k = ka - kb + 32*(a->wds - b->wds);
2493
#else
2494
  k = ka - kb + 16*(a->wds - b->wds);
2495
#endif
2496
#ifdef IBM
2497
  if (k > 0) {
2498
    word0(&da) += (k >> 2)*Exp_msk1;
2499
    if (k &= 3)
2500
      dval(&da) *= 1 << k;
2501
    }
2502
  else {
2503
    k = -k;
2504
    word0(&db) += (k >> 2)*Exp_msk1;
2505
    if (k &= 3)
2506
      dval(&db) *= 1 << k;
2507
    }
2508
#else
2509
756k
  if (k > 0)
2510
363k
    word0(&da) += k*Exp_msk1;
2511
393k
  else {
2512
393k
    k = -k;
2513
393k
    word0(&db) += k*Exp_msk1;
2514
393k
    }
2515
756k
#endif
2516
756k
  return dval(&da) / dval(&db);
2517
756k
  }
2518
2519
 static const double
2520
tens[] ICACHE_FLASH_ATTR = {
2521
    1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
2522
    1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
2523
    1e20, 1e21, 1e22
2524
#ifdef VAX
2525
    , 1e23, 1e24
2526
#endif
2527
    };
2528
2529
 static const double
2530
#ifdef IEEE_Arith
2531
bigtens[] ICACHE_FLASH_ATTR = { 1e16, 1e32, 1e64, 1e128, 1e256 };
2532
static const double tinytens[] ICACHE_FLASH_ATTR = { 1e-16, 1e-32, 1e-64, 1e-128,
2533
#ifdef Avoid_Underflow
2534
    9007199254740992.*9007199254740992.e-256
2535
    /* = 2^106 * 1e-256 */
2536
#else
2537
    1e-256
2538
#endif
2539
    };
2540
/* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
2541
/* flag unnecessarily.  It leads to a song and dance at the end of strtod. */
2542
574k
#define Scale_Bit 0x10
2543
730k
#define n_bigtens 5
2544
#else
2545
#ifdef IBM
2546
bigtens[] = { 1e16, 1e32, 1e64 };
2547
static const double tinytens[] = { 1e-16, 1e-32, 1e-64 };
2548
#define n_bigtens 3
2549
#else
2550
bigtens[] ICACHE_FLASH_ATTR = { 1e16, 1e32 };
2551
static const double tinytens[] ICACHE_FLASH_ATTR = { 1e-16, 1e-32 };
2552
#define n_bigtens 2
2553
#endif
2554
#endif
2555
2556
#undef Need_Hexdig
2557
#ifdef INFNAN_CHECK
2558
#ifndef No_Hex_NaN
2559
#define Need_Hexdig
2560
#endif
2561
#endif
2562
2563
#ifndef Need_Hexdig
2564
#ifndef NO_HEX_FP
2565
#define Need_Hexdig
2566
#endif
2567
#endif
2568
2569
#ifdef Need_Hexdig /*{*/
2570
#if 0
2571
static unsigned char hexdig[256];
2572
2573
 static void
2574
htinit(unsigned char *h, unsigned char *s, int inc)
2575
{
2576
  int i, j;
2577
  for(i = 0; (j = s[i]) !=0; i++)
2578
    h[j] = i + inc;
2579
  }
2580
2581
 static void
2582
hexdig_init(void) /* Use of hexdig_init omitted 20121220 to avoid a */
2583
      /* race condition when multiple threads are used. */
2584
{
2585
#define USC (unsigned char *)
2586
  htinit(hexdig, USC "0123456789", 0x10);
2587
  htinit(hexdig, USC "abcdef", 0x10 + 10);
2588
  htinit(hexdig, USC "ABCDEF", 0x10 + 10);
2589
  }
2590
#else
2591
static unsigned char const hexdig[256] ICACHE_RODATA_ATTR = {
2592
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2593
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2594
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2595
  16,17,18,19,20,21,22,23,24,25,0,0,0,0,0,0,
2596
  0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
2597
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2598
  0,26,27,28,29,30,31,0,0,0,0,0,0,0,0,0,
2599
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2600
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2601
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2602
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2603
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2604
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2605
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2606
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
2607
  0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
2608
  };
2609
#endif
2610
#endif /* } Need_Hexdig */
2611
2612
#ifdef INFNAN_CHECK
2613
2614
#ifndef NAN_WORD0
2615
139k
#define NAN_WORD0 0x7ff80000
2616
#endif
2617
2618
#ifndef NAN_WORD1
2619
139k
#define NAN_WORD1 0
2620
#endif
2621
2622
 static int
2623
match(const char **sp, const char *t)
2624
337k
{
2625
337k
  int c, d;
2626
337k
  const char *s = *sp;
2627
2628
1.95M
  while((d = *t++)) {
2629
#ifndef __XS__
2630
    if ((c = *++s) >= 'A' && c <= 'Z')
2631
      c += 'a' - 'A';
2632
#else
2633
1.62M
        c = *++s;
2634
1.62M
#endif
2635
1.62M
    if (c != d)
2636
7.19k
      return 0;
2637
1.62M
    }
2638
330k
  *sp = s + 1;
2639
330k
  return 1;
2640
337k
  }
2641
2642
#ifndef No_Hex_NaN
2643
 static void
2644
hexnan(U *rvp, const char **sp)
2645
1.74k
{
2646
1.74k
  ULong c, x[2];
2647
1.74k
  const char *s;
2648
1.74k
  int c1, havedig, udx0, xshift;
2649
2650
  /**** if (!hexdig['0']) hexdig_init(); ****/
2651
1.74k
  x[0] = x[1] = 0;
2652
1.74k
  havedig = xshift = 0;
2653
1.74k
  udx0 = 1;
2654
1.74k
  s = *sp;
2655
  /* allow optional initial 0x or 0X */
2656
2.33k
  while((c = *(const unsigned char*)(s+1)) && c <= ' ')
2657
589
    ++s;
2658
1.74k
  if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X'))
2659
467
    s += 2;
2660
7.34k
  while((c = *(const unsigned char*)++s)) {
2661
6.20k
    if ((c1 = hexdig[c]))
2662
4.90k
      c  = c1 & 0xf;
2663
1.30k
    else if (c <= ' ') {
2664
697
      if (udx0 && havedig) {
2665
212
        udx0 = 0;
2666
212
        xshift = 1;
2667
212
        }
2668
697
      continue;
2669
697
      }
2670
#ifdef GDTOA_NON_PEDANTIC_NANCHECK
2671
    else if (/*(*/ c == ')' && havedig) {
2672
      *sp = s + 1;
2673
      break;
2674
      }
2675
    else
2676
      return; /* invalid form: don't change *sp */
2677
#else
2678
603
    else {
2679
112k
      do {
2680
112k
        if (/*(*/ c == ')') {
2681
90
          *sp = s + 1;
2682
90
          break;
2683
90
          }
2684
112k
        } while((c = *++s));
2685
603
      break;
2686
603
      }
2687
4.90k
#endif
2688
4.90k
    havedig = 1;
2689
4.90k
    if (xshift) {
2690
202
      xshift = 0;
2691
202
      x[0] = x[1];
2692
202
      x[1] = 0;
2693
202
      }
2694
4.90k
    if (udx0)
2695
3.88k
      x[0] = (x[0] << 4) | (x[1] >> 28);
2696
4.90k
    x[1] = (x[1] << 4) | c;
2697
4.90k
    }
2698
1.74k
  if ((x[0] &= 0xfffff) || x[1]) {
2699
1.50k
    word0(rvp) = Exp_mask | x[0];
2700
1.50k
    word1(rvp) = x[1];
2701
1.50k
    }
2702
1.74k
  }
2703
#endif /*No_Hex_NaN*/
2704
#endif /* INFNAN_CHECK */
2705
2706
#ifdef Pack_32
2707
#define ULbits 32
2708
#define kshift 5
2709
3.16M
#define kmask 31
2710
#else
2711
#define ULbits 16
2712
#define kshift 4
2713
#define kmask 15
2714
#endif
2715
2716
#if !defined(NO_HEX_FP) || defined(Honor_FLT_ROUNDS) /*{*/
2717
 static Bigint *
2718
increment(Bigint *b MTd)
2719
{
2720
  ULong *x, *xe;
2721
  Bigint *b1;
2722
2723
  x = b->x;
2724
  xe = x + b->wds;
2725
  do {
2726
    if (*x < (ULong)0xffffffffL) {
2727
      ++*x;
2728
      return b;
2729
      }
2730
    *x++ = 0;
2731
    } while(x < xe);
2732
  {
2733
    if (b->wds >= b->maxwds) {
2734
      b1 = Balloc(b->k+1 MTa);
2735
      Bcopy(b1,b);
2736
      Bfree(b MTa);
2737
      b = b1;
2738
      }
2739
    b->x[b->wds++] = 1;
2740
    }
2741
  return b;
2742
  }
2743
2744
#endif /*}*/
2745
2746
#ifndef NO_HEX_FP /*{*/
2747
2748
 static void
2749
rshift(Bigint *b, int k)
2750
{
2751
  ULong *x, *x1, *xe, y;
2752
  int n;
2753
2754
  x = x1 = b->x;
2755
  n = k >> kshift;
2756
  if (n < b->wds) {
2757
    xe = x + b->wds;
2758
    x += n;
2759
    if (k &= kmask) {
2760
      n = 32 - k;
2761
      y = *x++ >> k;
2762
      while(x < xe) {
2763
        *x1++ = (y | (*x << n)) & 0xffffffff;
2764
        y = *x++ >> k;
2765
        }
2766
      if ((*x1 = y) !=0)
2767
        x1++;
2768
      }
2769
    else
2770
      while(x < xe)
2771
        *x1++ = *x++;
2772
    }
2773
  if ((b->wds = x1 - b->x) == 0)
2774
    b->x[0] = 0;
2775
  }
2776
2777
 static ULong
2778
any_on(Bigint *b, int k)
2779
{
2780
  int n, nwds;
2781
  ULong *x, *x0, x1, x2;
2782
2783
  x = b->x;
2784
  nwds = b->wds;
2785
  n = k >> kshift;
2786
  if (n > nwds)
2787
    n = nwds;
2788
  else if (n < nwds && (k &= kmask)) {
2789
    x1 = x2 = x[n];
2790
    x1 >>= k;
2791
    x1 <<= k;
2792
    if (x1 != x2)
2793
      return 1;
2794
    }
2795
  x0 = x;
2796
  x += n;
2797
  while(x > x0)
2798
    if (*--x)
2799
      return 1;
2800
  return 0;
2801
  }
2802
2803
enum {  /* rounding values: same as FLT_ROUNDS */
2804
  Round_zero = 0,
2805
  Round_near = 1,
2806
  Round_up = 2,
2807
  Round_down = 3
2808
  };
2809
2810
 void
2811
gethex( const char **sp, U *rvp, int rounding, int sign MTd)
2812
{
2813
  Bigint *b;
2814
  const unsigned char *decpt, *s0, *s, *s1;
2815
  Long e, e1;
2816
  ULong L, lostbits, *x;
2817
  int big, denorm, esign, havedig, k, n, nbits, up, zret;
2818
#ifdef IBM
2819
  int j;
2820
#endif
2821
  enum {
2822
#ifdef IEEE_Arith /*{{*/
2823
    emax = 0x7fe - Bias - P + 1,
2824
    emin = Emin - P + 1
2825
#else /*}{*/
2826
    emin = Emin - P,
2827
#ifdef VAX
2828
    emax = 0x7ff - Bias - P + 1
2829
#endif
2830
#ifdef IBM
2831
    emax = 0x7f - Bias - P
2832
#endif
2833
#endif /*}}*/
2834
    };
2835
#ifdef USE_LOCALE
2836
  int i;
2837
#ifdef NO_LOCALE_CACHE
2838
  const unsigned char *decimalpoint = (unsigned char*)
2839
    localeconv()->decimal_point;
2840
#else
2841
  const unsigned char *decimalpoint;
2842
  static unsigned char *decimalpoint_cache;
2843
  if (!(s0 = decimalpoint_cache)) {
2844
    s0 = (unsigned char*)localeconv()->decimal_point;
2845
    if ((decimalpoint_cache = (unsigned char*)
2846
        MALLOC(strlen((const char*)s0) + 1))) {
2847
      strcpy((char*)decimalpoint_cache, (const char*)s0);
2848
      s0 = decimalpoint_cache;
2849
      }
2850
    }
2851
  decimalpoint = s0;
2852
#endif
2853
#endif
2854
2855
  /**** if (!hexdig['0']) hexdig_init(); ****/
2856
  havedig = 0;
2857
  s0 = *(const unsigned char **)sp + 2;
2858
  while(s0[havedig] == '0')
2859
    havedig++;
2860
  s0 += havedig;
2861
  s = s0;
2862
  decpt = 0;
2863
  zret = 0;
2864
  e = 0;
2865
  if (hexdig[*s])
2866
    havedig++;
2867
  else {
2868
    zret = 1;
2869
#ifdef USE_LOCALE
2870
    for(i = 0; decimalpoint[i]; ++i) {
2871
      if (s[i] != decimalpoint[i])
2872
        goto pcheck;
2873
      }
2874
    decpt = s += i;
2875
#else
2876
    if (*s != '.')
2877
      goto pcheck;
2878
    decpt = ++s;
2879
#endif
2880
    if (!hexdig[*s])
2881
      goto pcheck;
2882
    while(*s == '0')
2883
      s++;
2884
    if (hexdig[*s])
2885
      zret = 0;
2886
    havedig = 1;
2887
    s0 = s;
2888
    }
2889
  while(hexdig[*s])
2890
    s++;
2891
#ifdef USE_LOCALE
2892
  if (*s == *decimalpoint && !decpt) {
2893
    for(i = 1; decimalpoint[i]; ++i) {
2894
      if (s[i] != decimalpoint[i])
2895
        goto pcheck;
2896
      }
2897
    decpt = s += i;
2898
#else
2899
  if (*s == '.' && !decpt) {
2900
    decpt = ++s;
2901
#endif
2902
    while(hexdig[*s])
2903
      s++;
2904
    }/*}*/
2905
  if (decpt)
2906
    e = -(((Long)(s-decpt)) << 2);
2907
 pcheck:
2908
  s1 = s;
2909
  big = esign = 0;
2910
  switch(*s) {
2911
    case 'p':
2912
    case 'P':
2913
    switch(*++s) {
2914
      case '-':
2915
      esign = 1;
2916
      /* no break */
2917
      mxFallThrough;
2918
      case '+':
2919
      s++;
2920
      }
2921
    if ((n = hexdig[*s]) == 0 || n > 0x19) {
2922
      s = s1;
2923
      break;
2924
      }
2925
    e1 = n - 0x10;
2926
    while((n = hexdig[*++s]) !=0 && n <= 0x19) {
2927
      if (e1 & 0xf8000000)
2928
        big = 1;
2929
      e1 = 10*e1 + n - 0x10;
2930
      }
2931
    if (esign)
2932
      e1 = -e1;
2933
    e += e1;
2934
    }
2935
  *sp = (char*)s;
2936
  if (!havedig)
2937
    *sp = (char*)s0 - 1;
2938
  if (zret)
2939
    goto retz1;
2940
  if (big) {
2941
    if (esign) {
2942
#ifdef IEEE_Arith
2943
      switch(rounding) {
2944
        case Round_up:
2945
        if (sign)
2946
          break;
2947
        goto ret_tiny;
2948
        case Round_down:
2949
        if (!sign)
2950
          break;
2951
        goto ret_tiny;
2952
        }
2953
#endif
2954
      goto retz;
2955
#ifdef IEEE_Arith
2956
 ret_tinyf:
2957
      Bfree(b MTa);
2958
 ret_tiny:
2959
      Set_errno(ERANGE);
2960
      word0(rvp) = 0;
2961
      word1(rvp) = 1;
2962
      return;
2963
#endif /* IEEE_Arith */
2964
      }
2965
    switch(rounding) {
2966
      case Round_near:
2967
      goto ovfl1;
2968
      case Round_up:
2969
      if (!sign)
2970
        goto ovfl1;
2971
      goto ret_big;
2972
      case Round_down:
2973
      if (sign)
2974
        goto ovfl1;
2975
      goto ret_big;
2976
      }
2977
 ret_big:
2978
    word0(rvp) = Big0;
2979
    word1(rvp) = Big1;
2980
    return;
2981
    }
2982
  n = s1 - s0 - 1;
2983
  for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1)
2984
    k++;
2985
  b = Balloc(k MTa);
2986
  x = b->x;
2987
  n = 0;
2988
  L = 0;
2989
#ifdef USE_LOCALE
2990
  for(i = 0; decimalpoint[i+1]; ++i);
2991
#endif
2992
  while(s1 > s0) {
2993
#ifdef USE_LOCALE
2994
    if (*--s1 == decimalpoint[i]) {
2995
      s1 -= i;
2996
      continue;
2997
      }
2998
#else
2999
    if (*--s1 == '.')
3000
      continue;
3001
#endif
3002
    if (n == ULbits) {
3003
      *x++ = L;
3004
      L = 0;
3005
      n = 0;
3006
      }
3007
    L |= (hexdig[*s1] & 0x0f) << n;
3008
    n += 4;
3009
    }
3010
  *x++ = L;
3011
  b->wds = n = x - b->x;
3012
  n = ULbits*n - hi0bits(L);
3013
  nbits = Nbits;
3014
  lostbits = 0;
3015
  x = b->x;
3016
  if (n > nbits) {
3017
    n -= nbits;
3018
    if (any_on(b,n)) {
3019
      lostbits = 1;
3020
      k = n - 1;
3021
      if (x[k>>kshift] & 1 << (k & kmask)) {
3022
        lostbits = 2;
3023
        if (k > 0 && any_on(b,k))
3024
          lostbits = 3;
3025
        }
3026
      }
3027
    rshift(b, n);
3028
    e += n;
3029
    }
3030
  else if (n < nbits) {
3031
    n = nbits - n;
3032
    b = lshift(b, n MTa);
3033
    e -= n;
3034
    x = b->x;
3035
    }
3036
  if (e > emax) {
3037
 ovfl:
3038
    Bfree(b MTa);
3039
 ovfl1:
3040
    Set_errno(ERANGE);
3041
#ifdef Honor_FLT_ROUNDS
3042
    switch (rounding) {
3043
      case Round_zero:
3044
      goto ret_big;
3045
      case Round_down:
3046
      if (!sign)
3047
        goto ret_big;
3048
      break;
3049
      case Round_up:
3050
      if (sign)
3051
        goto ret_big;
3052
      }
3053
#endif
3054
    word0(rvp) = Exp_mask;
3055
    word1(rvp) = 0;
3056
    return;
3057
    }
3058
  denorm = 0;
3059
  if (e < emin) {
3060
    denorm = 1;
3061
    n = emin - e;
3062
    if (n >= nbits) {
3063
#ifdef IEEE_Arith /*{*/
3064
      switch (rounding) {
3065
        case Round_near:
3066
        if (n == nbits && (n < 2 || lostbits || any_on(b,n-1)))
3067
          goto ret_tinyf;
3068
        break;
3069
        case Round_up:
3070
        if (!sign)
3071
          goto ret_tinyf;
3072
        break;
3073
        case Round_down:
3074
        if (sign)
3075
          goto ret_tinyf;
3076
        }
3077
#endif /* } IEEE_Arith */
3078
      Bfree(b MTa);
3079
 retz:
3080
      Set_errno(ERANGE);
3081
 retz1:
3082
      rvp->d = 0.;
3083
      return;
3084
      }
3085
    k = n - 1;
3086
    if (lostbits)
3087
      lostbits = 1;
3088
    else if (k > 0)
3089
      lostbits = any_on(b,k);
3090
    if (x[k>>kshift] & 1 << (k & kmask))
3091
      lostbits |= 2;
3092
    nbits -= n;
3093
    rshift(b,n);
3094
    e = emin;
3095
    }
3096
  if (lostbits) {
3097
    up = 0;
3098
    switch(rounding) {
3099
      case Round_zero:
3100
      break;
3101
      case Round_near:
3102
      if (lostbits & 2
3103
       && (lostbits & 1) | (x[0] & 1))
3104
        up = 1;
3105
      break;
3106
      case Round_up:
3107
      up = 1 - sign;
3108
      break;
3109
      case Round_down:
3110
      up = sign;
3111
      }
3112
    if (up) {
3113
      k = b->wds;
3114
      b = increment(b MTa);
3115
      x = b->x;
3116
      if (denorm) {
3117
#if 0
3118
        if (nbits == Nbits - 1
3119
         && x[nbits >> kshift] & 1 << (nbits & kmask))
3120
          denorm = 0; /* not currently used */
3121
#endif
3122
        }
3123
      else if (b->wds > k
3124
       || ((n = nbits & kmask) !=0
3125
           && hi0bits(x[k-1]) < 32-n)) {
3126
        rshift(b,1);
3127
        if (++e > Emax)
3128
          goto ovfl;
3129
        }
3130
      }
3131
    }
3132
#ifdef IEEE_Arith
3133
  if (denorm)
3134
    word0(rvp) = b->wds > 1 ? b->x[1] & ~0x100000 : 0;
3135
  else
3136
    word0(rvp) = (b->x[1] & ~0x100000) | ((e + 0x3ff + 52) << 20);
3137
  word1(rvp) = b->x[0];
3138
#endif
3139
#ifdef IBM
3140
  if ((j = e & 3)) {
3141
    k = b->x[0] & ((1 << j) - 1);
3142
    rshift(b,j);
3143
    if (k) {
3144
      switch(rounding) {
3145
        case Round_up:
3146
        if (!sign)
3147
          increment(b);
3148
        break;
3149
        case Round_down:
3150
        if (sign)
3151
          increment(b);
3152
        break;
3153
        case Round_near:
3154
        j = 1 << (j-1);
3155
        if (k & j && ((k & (j-1)) | lostbits))
3156
          increment(b);
3157
        }
3158
      }
3159
    }
3160
  e >>= 2;
3161
  word0(rvp) = b->x[1] | ((e + 65 + 13) << 24);
3162
  word1(rvp) = b->x[0];
3163
#endif
3164
#ifdef VAX
3165
  /* The next two lines ignore swap of low- and high-order 2 bytes. */
3166
  /* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */
3167
  /* word1(rvp) = b->x[0]; */
3168
  word0(rvp) = ((b->x[1] & ~0x800000) >> 16) | ((e + 129 + 55) << 7) | (b->x[1] << 16);
3169
  word1(rvp) = (b->x[0] >> 16) | (b->x[0] << 16);
3170
#endif
3171
  Bfree(b MTa);
3172
  }
3173
#endif /*!NO_HEX_FP}*/
3174
3175
 static int
3176
dshift(Bigint *b, int p2)
3177
3.16M
{
3178
3.16M
  int rv = hi0bits(b->x[b->wds-1]) - 4;
3179
3.16M
  if (p2 > 0)
3180
2.56M
    rv -= p2;
3181
3.16M
  return rv & kmask;
3182
3.16M
  }
3183
3184
 static int
3185
quorem(Bigint *b, Bigint *S)
3186
46.1M
{
3187
46.1M
  int n;
3188
46.1M
  ULong *bx, *bxe, q, *sx, *sxe;
3189
46.1M
#ifdef ULLong
3190
46.1M
  ULLong borrow, carry, y, ys;
3191
#else
3192
  ULong borrow, carry, y, ys;
3193
#ifdef Pack_32
3194
  ULong si, z, zs;
3195
#endif
3196
#endif
3197
3198
46.1M
  n = S->wds;
3199
#ifdef DEBUG
3200
  /*debug*/ if (b->wds > n)
3201
  /*debug*/ Bug("oversize b in quorem");
3202
#endif
3203
46.1M
  if (b->wds < n)
3204
54.1k
    return 0;
3205
46.0M
  sx = S->x;
3206
46.0M
  sxe = sx + --n;
3207
46.0M
  bx = b->x;
3208
46.0M
  bxe = bx + n;
3209
46.0M
  q = *bxe / (*sxe + 1);  /* ensure q <= true quotient */
3210
#ifdef DEBUG
3211
#ifdef NO_STRTOD_BIGCOMP
3212
  /*debug*/ if (q > 9)
3213
#else
3214
  /* An oversized q is possible when quorem is called from bigcomp and */
3215
  /* the input is near, e.g., twice the smallest denormalized number. */
3216
  /*debug*/ if (q > 15)
3217
#endif
3218
  /*debug*/ Bug("oversized quotient in quorem");
3219
#endif
3220
46.0M
  if (q) {
3221
40.1M
    borrow = 0;
3222
40.1M
    carry = 0;
3223
222M
    do {
3224
222M
#ifdef ULLong
3225
222M
      ys = *sx++ * (ULLong)q + carry;
3226
222M
      carry = ys >> 32;
3227
222M
      y = *bx - (ys & FFFFFFFF) - borrow;
3228
222M
      borrow = y >> 32 & (ULong)1;
3229
222M
      *bx++ = y & FFFFFFFF;
3230
#else
3231
#ifdef Pack_32
3232
      si = *sx++;
3233
      ys = (si & 0xffff) * q + carry;
3234
      zs = (si >> 16) * q + (ys >> 16);
3235
      carry = zs >> 16;
3236
      y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
3237
      borrow = (y & 0x10000) >> 16;
3238
      z = (*bx >> 16) - (zs & 0xffff) - borrow;
3239
      borrow = (z & 0x10000) >> 16;
3240
      Storeinc(bx, z, y);
3241
#else
3242
      ys = *sx++ * q + carry;
3243
      carry = ys >> 16;
3244
      y = *bx - (ys & 0xffff) - borrow;
3245
      borrow = (y & 0x10000) >> 16;
3246
      *bx++ = y & 0xffff;
3247
#endif
3248
#endif
3249
222M
      }
3250
222M
      while(sx <= sxe);
3251
40.1M
    if (!*bxe) {
3252
1.74k
      bx = b->x;
3253
1.74k
      while(--bxe > bx && !*bxe)
3254
0
        --n;
3255
1.74k
      b->wds = n;
3256
1.74k
      }
3257
40.1M
    }
3258
46.0M
  if (cmp(b, S) >= 0) {
3259
572k
    q++;
3260
572k
    borrow = 0;
3261
572k
    carry = 0;
3262
572k
    bx = b->x;
3263
572k
    sx = S->x;
3264
1.65M
    do {
3265
1.65M
#ifdef ULLong
3266
1.65M
      ys = *sx++ + carry;
3267
1.65M
      carry = ys >> 32;
3268
1.65M
      y = *bx - (ys & FFFFFFFF) - borrow;
3269
1.65M
      borrow = y >> 32 & (ULong)1;
3270
1.65M
      *bx++ = y & FFFFFFFF;
3271
#else
3272
#ifdef Pack_32
3273
      si = *sx++;
3274
      ys = (si & 0xffff) + carry;
3275
      zs = (si >> 16) + (ys >> 16);
3276
      carry = zs >> 16;
3277
      y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
3278
      borrow = (y & 0x10000) >> 16;
3279
      z = (*bx >> 16) - (zs & 0xffff) - borrow;
3280
      borrow = (z & 0x10000) >> 16;
3281
      Storeinc(bx, z, y);
3282
#else
3283
      ys = *sx++ + carry;
3284
      carry = ys >> 16;
3285
      y = *bx - (ys & 0xffff) - borrow;
3286
      borrow = (y & 0x10000) >> 16;
3287
      *bx++ = y & 0xffff;
3288
#endif
3289
#endif
3290
1.65M
      }
3291
1.65M
      while(sx <= sxe);
3292
572k
    bx = b->x;
3293
572k
    bxe = bx + n;
3294
572k
    if (!*bxe) {
3295
578k
      while(--bxe > bx && !*bxe)
3296
12.8k
        --n;
3297
565k
      b->wds = n;
3298
565k
      }
3299
572k
    }
3300
46.0M
  return q;
3301
46.1M
  }
3302
3303
#if defined(Avoid_Underflow) || !defined(NO_STRTOD_BIGCOMP) /*{*/
3304
 static double
3305
sulp(U *x, BCinfo *bc)
3306
7.77k
{
3307
7.77k
  U u;
3308
7.77k
  double rv;
3309
7.77k
  int i;
3310
3311
7.77k
  rv = ulp(x);
3312
7.77k
  if (!bc->scale || (i = 2*P + 1 - ((word0(x) & Exp_mask) >> Exp_shift)) <= 0)
3313
7.76k
    return rv; /* Is there an example where i <= 0 ? */
3314
5
  word0(&u) = Exp_1 + (i << Exp_shift);
3315
5
  word1(&u) = 0;
3316
5
  return rv * u.d;
3317
7.77k
  }
3318
#endif /*}*/
3319
3320
#ifndef NO_STRTOD_BIGCOMP
3321
 static void
3322
bigcomp(U *rv, const char *s0, BCinfo *bc MTd)
3323
2.91k
{
3324
2.91k
  Bigint *b, *d;
3325
2.91k
  int b2, bbits, d2, dd = 0, dig, dsign, i, j, nd, nd0, p2, p5, speccase;
3326
3327
2.91k
  dsign = bc->dsign;
3328
2.91k
  nd = bc->nd;
3329
2.91k
  nd0 = bc->nd0;
3330
2.91k
  p5 = nd + bc->e0 - 1;
3331
2.91k
  speccase = 0;
3332
2.91k
#ifndef Sudden_Underflow
3333
2.91k
  if (rv->d == 0.) { /* special case: value near underflow-to-zero */
3334
        /* threshold was rounded to zero */
3335
166
    b = i2b(1 MTa);
3336
166
    p2 = Emin - P + 1;
3337
166
    bbits = 1;
3338
166
#ifdef Avoid_Underflow
3339
166
    word0(rv) = (P+2) << Exp_shift;
3340
#else
3341
    word1(rv) = 1;
3342
#endif
3343
166
    i = 0;
3344
#ifdef Honor_FLT_ROUNDS
3345
    if (bc->rounding == 1)
3346
#endif
3347
166
      {
3348
166
      speccase = 1;
3349
166
      --p2;
3350
166
      dsign = 0;
3351
166
      goto have_i;
3352
166
      }
3353
166
    }
3354
2.75k
  else
3355
2.75k
#endif
3356
2.75k
    b = d2b(rv, &p2, &bbits MTa);
3357
2.75k
#ifdef Avoid_Underflow
3358
2.75k
  p2 -= bc->scale;
3359
2.75k
#endif
3360
  /* floor(log2(rv)) == bbits - 1 + p2 */
3361
  /* Check for denormal case. */
3362
2.75k
  i = P - bbits;
3363
2.75k
  if (i > (j = P - Emin - 1 + p2)) {
3364
#ifdef Sudden_Underflow
3365
    Bfree(b MTa);
3366
    b = i2b(1);
3367
    p2 = Emin;
3368
    i = P - 1;
3369
#ifdef Avoid_Underflow
3370
    word0(rv) = (1 + bc->scale) << Exp_shift;
3371
#else
3372
    word0(rv) = Exp_msk1;
3373
#endif
3374
    word1(rv) = 0;
3375
#else
3376
5
    i = j;
3377
5
#endif
3378
5
    }
3379
#ifdef Honor_FLT_ROUNDS
3380
  if (bc->rounding != 1) {
3381
    if (i > 0)
3382
      b = lshift(b, i MTa);
3383
    if (dsign)
3384
      b = increment(b MTa);
3385
    }
3386
  else
3387
#endif
3388
2.75k
    {
3389
2.75k
    b = lshift(b, ++i MTa);
3390
2.75k
    b->x[0] |= 1;
3391
2.75k
    }
3392
2.75k
#ifndef Sudden_Underflow
3393
2.91k
 have_i:
3394
2.91k
#endif
3395
2.91k
  p2 -= p5 + i;
3396
2.91k
  d = i2b(1 MTa);
3397
  /* Arrange for convenient computation of quotients:
3398
   * shift left if necessary so divisor has 4 leading 0 bits.
3399
   */
3400
2.91k
  if (p5 > 0)
3401
1.54k
    d = pow5mult(d, p5 MTa);
3402
1.37k
  else if (p5 < 0)
3403
1.09k
    b = pow5mult(b, -p5 MTa);
3404
2.91k
  if (p2 > 0) {
3405
1.39k
    b2 = p2;
3406
1.39k
    d2 = 0;
3407
1.39k
    }
3408
1.52k
  else {
3409
1.52k
    b2 = 0;
3410
1.52k
    d2 = -p2;
3411
1.52k
    }
3412
2.91k
  i = dshift(d, d2);
3413
2.91k
  if ((b2 += i) > 0)
3414
2.82k
    b = lshift(b, b2 MTa);
3415
2.91k
  if ((d2 += i) > 0)
3416
2.79k
    d = lshift(d, d2 MTa);
3417
3418
  /* Now b/d = exactly half-way between the two floating-point values */
3419
  /* on either side of the input string.  Compute first digit of b/d. */
3420
3421
2.91k
  if (!(dig = quorem(b,d))) {
3422
0
    b = multadd(b, 10, 0 MTa);  /* very unlikely */
3423
0
    dig = quorem(b,d);
3424
0
    }
3425
3426
  /* Compare b/d with s0 */
3427
3428
43.2k
  for(i = 0; i < nd0; ) {
3429
42.6k
    if ((dd = s0[i++] - '0' - dig))
3430
2.26k
      goto ret;
3431
40.4k
    if (!b->x[0] && b->wds == 1) {
3432
23
      if (i < nd)
3433
14
        dd = 1;
3434
23
      goto ret;
3435
23
      }
3436
40.3k
    b = multadd(b, 10, 0 MTa);
3437
40.3k
    dig = quorem(b,d);
3438
40.3k
    }
3439
8.38k
  for(j = bc->dp1; i++ < nd;) {
3440
8.22k
    if ((dd = s0[j++] - '0' - dig))
3441
464
      goto ret;
3442
7.75k
    if (!b->x[0] && b->wds == 1) {
3443
0
      if (i < nd)
3444
0
        dd = 1;
3445
0
      goto ret;
3446
0
      }
3447
7.75k
    b = multadd(b, 10, 0 MTa);
3448
7.75k
    dig = quorem(b,d);
3449
7.75k
    }
3450
165
  if (dig > 0 || b->x[0] || b->wds > 1)
3451
165
    dd = -1;
3452
2.91k
 ret:
3453
2.91k
  Bfree(b MTa);
3454
2.91k
  Bfree(d MTa);
3455
#ifdef Honor_FLT_ROUNDS
3456
  if (bc->rounding != 1) {
3457
    if (dd < 0) {
3458
      if (bc->rounding == 0) {
3459
        if (!dsign)
3460
          goto retlow1;
3461
        }
3462
      else if (dsign)
3463
        goto rethi1;
3464
      }
3465
    else if (dd > 0) {
3466
      if (bc->rounding == 0) {
3467
        if (dsign)
3468
          goto rethi1;
3469
        goto ret1;
3470
        }
3471
      if (!dsign)
3472
        goto rethi1;
3473
      dval(rv) += 2.*sulp(rv,bc);
3474
      }
3475
    else {
3476
      bc->inexact = 0;
3477
      if (dsign)
3478
        goto rethi1;
3479
      }
3480
    }
3481
  else
3482
#endif
3483
2.91k
  if (speccase) {
3484
166
    if (dd <= 0)
3485
166
      rv->d = 0.;
3486
166
    }
3487
2.75k
  else if (dd < 0) {
3488
2.65k
    if (!dsign)  /* does not happen for round-near */
3489
0
retlow1:
3490
0
      dval(rv) -= sulp(rv,bc);
3491
2.65k
    }
3492
102
  else if (dd > 0) {
3493
93
    if (dsign) {
3494
102
 rethi1:
3495
102
      dval(rv) += sulp(rv,bc);
3496
102
      }
3497
93
    }
3498
9
  else {
3499
    /* Exact half-way case:  apply round-even rule. */
3500
9
    if ((j = ((word0(rv) & Exp_mask) >> Exp_shift) - bc->scale) <= 0) {
3501
0
      i = 1 - j;
3502
0
      if (i <= 31) {
3503
0
        if (word1(rv) & (0x1 << i))
3504
0
          goto odd;
3505
0
        }
3506
0
      else if (word0(rv) & (0x1 << (i-32)))
3507
0
        goto odd;
3508
0
      }
3509
9
    else if (word1(rv) & 1) {
3510
9
 odd:
3511
9
      if (dsign)
3512
9
        goto rethi1;
3513
0
      goto retlow1;
3514
9
      }
3515
9
    }
3516
3517
#ifdef Honor_FLT_ROUNDS
3518
 ret1:
3519
#endif
3520
2.91k
  return;
3521
2.91k
  }
3522
#endif /* NO_STRTOD_BIGCOMP */
3523
3524
 double
3525
strtod2(const char *s00, char **se __XS__d)
3526
15.8M
{
3527
15.8M
  int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1;
3528
15.8M
  int esign, i, j, k, nd, nd0, nf, nz, nz0, nz1, sign;
3529
15.8M
  const char *s, *s0, *s1;
3530
15.8M
  double aadj, aadj1;
3531
15.8M
  Long L;
3532
15.8M
  U aadj2, adj, rv, rv0;
3533
15.8M
  ULong y, z;
3534
15.8M
  BCinfo bc;
3535
15.8M
  Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
3536
#ifdef USE_BF96
3537
  ULLong bhi, blo, brv, t00, t01, t02, t10, t11, terv, tg, tlo, yz;
3538
  const BF96 *p10;
3539
  int bexact, erv;
3540
#endif
3541
15.8M
#ifdef Avoid_Underflow
3542
#ifndef ROUND_BIASED
3543
  ULong Lsb;
3544
  ULong Lsb1;
3545
#endif
3546
15.8M
#endif
3547
#ifdef SET_INEXACT
3548
  int oldinexact;
3549
#endif
3550
15.8M
#ifndef NO_STRTOD_BIGCOMP
3551
15.8M
  int req_bigcomp = 0;
3552
15.8M
#endif
3553
#ifdef MULTIPLE_THREADS
3554
  ThInfo *TI = 0;
3555
#endif
3556
#ifdef Honor_FLT_ROUNDS /*{*/
3557
#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
3558
  bc.rounding = Flt_Rounds;
3559
#else /*}{*/
3560
  bc.rounding = 1;
3561
  switch(fegetround()) {
3562
    case FE_TOWARDZERO: bc.rounding = 0; break;
3563
    case FE_UPWARD: bc.rounding = 2; break;
3564
    case FE_DOWNWARD: bc.rounding = 3;
3565
    }
3566
#endif /*}}*/
3567
#endif /*}*/
3568
#ifdef USE_LOCALE
3569
  const char *s2;
3570
#endif
3571
3572
15.8M
  sign = nz0 = nz1 = nz = bc.dplen = bc.uflchk = 0;
3573
15.8M
  dval(&rv) = 0.;
3574
15.8M
  for(s = s00;;s++) switch(c_read8(s)) {
3575
1.30M
    case '-':
3576
1.30M
      sign = 1;
3577
      /* no break */
3578
1.30M
      mxFallThrough;
3579
1.31M
    case '+':
3580
1.31M
      if (c_read8(++s))
3581
1.30M
        goto break2;
3582
      /* no break */
3583
6.63k
      mxFallThrough;
3584
6.63k
    case 0:
3585
6.63k
      goto ret0;
3586
0
    case '\t':
3587
0
    case '\n':
3588
0
    case '\v':
3589
0
    case '\f':
3590
0
    case '\r':
3591
0
    case ' ':
3592
0
      continue;
3593
14.5M
    default:
3594
14.5M
      goto break2;
3595
15.8M
    }
3596
15.8M
 break2:
3597
15.8M
  if (c_read8(s) == '0') {
3598
#ifndef NO_HEX_FP /*{*/
3599
    switch(s[1]) {
3600
      case 'x':
3601
      case 'X':
3602
#ifdef Honor_FLT_ROUNDS
3603
      gethex(&s, &rv, bc.rounding, sign MTb);
3604
#else
3605
      gethex(&s, &rv, 1, sign MTb);
3606
#endif
3607
      goto ret;
3608
      }
3609
#endif /*}*/
3610
470k
    nz0 = 1;
3611
471k
    while(c_read8(++s) == '0') ;
3612
470k
    if (!c_read8(s))
3613
205k
      goto ret;
3614
470k
    }
3615
15.6M
  s0 = s;
3616
15.6M
  nd = nf = 0;
3617
#ifdef USE_BF96
3618
  yz = 0;
3619
  for(; (c = c_read8(s)) >= '0' && c <= '9'; nd++, s++)
3620
    if (nd < 19)
3621
      yz = 10*yz + c - '0';
3622
#else
3623
15.6M
  y = z = 0;
3624
50.8M
  for(; (c = c_read8(s)) >= '0' && c <= '9'; nd++, s++)
3625
35.1M
    if (nd < 9)
3626
27.5M
      y = 10*y + c - '0';
3627
7.63M
    else if (nd < DBL_DIG + 2)
3628
6.86M
      z = 10*z + c - '0';
3629
15.6M
#endif
3630
15.6M
  nd0 = nd;
3631
15.6M
  bc.dp0 = bc.dp1 = (int)(s - s0);
3632
16.9M
  for(s1 = s; s1 > s0 && *--s1 == '0'; )
3633
1.31M
    ++nz1;
3634
#ifdef USE_LOCALE
3635
  s1 = localeconv()->decimal_point;
3636
  if (c == *s1) {
3637
    c = '.';
3638
    if (*++s1) {
3639
      s2 = s;
3640
      for(;;) {
3641
        if (*++s2 != *s1) {
3642
          c = 0;
3643
          break;
3644
          }
3645
        if (!*++s1) {
3646
          s = s2;
3647
          break;
3648
          }
3649
        }
3650
      }
3651
    }
3652
#endif
3653
15.6M
  if (c == '.') {
3654
2.38M
    c = c_read8(++s);
3655
2.38M
    bc.dp1 = (int)(s - s0);
3656
2.38M
    bc.dplen = bc.dp1 - bc.dp0;
3657
2.38M
    if (!nd) {
3658
268k
      for(; c == '0'; c = c_read8(++s))
3659
99.4k
        nz++;
3660
168k
      if (c > '0' && c <= '9') {
3661
106k
        bc.dp0 = (int)(s0 - s);
3662
106k
        bc.dp1 = bc.dp0 + bc.dplen;
3663
106k
        s0 = s;
3664
106k
        nf += nz;
3665
106k
        nz = 0;
3666
106k
        goto have_dig;
3667
106k
        }
3668
62.4k
      goto dig_done;
3669
168k
      }
3670
29.7M
    for(; c >= '0' && c <= '9'; c = c_read8(++s)) {
3671
27.5M
 have_dig:
3672
27.5M
      nz++;
3673
27.5M
      if (c -= '0') {
3674
24.5M
        nf += nz;
3675
24.5M
        i = 1;
3676
#ifdef USE_BF96
3677
        for(; i < nz; ++i) {
3678
          if (++nd <= 19)
3679
            yz *= 10;
3680
          }
3681
        if (++nd <= 19)
3682
          yz = 10*yz + c;
3683
#else
3684
27.4M
        for(; i < nz; ++i) {
3685
2.95M
          if (nd++ < 9)
3686
1.40M
            y *= 10;
3687
1.55M
          else if (nd <= DBL_DIG + 2)
3688
1.47M
            z *= 10;
3689
2.95M
          }
3690
24.5M
        if (nd++ < 9)
3691
11.0M
          y = 10*y + c;
3692
13.4M
        else if (nd <= DBL_DIG + 2)
3693
12.6M
          z = 10*z + c;
3694
24.5M
#endif
3695
24.5M
        nz = nz1 = 0;
3696
24.5M
        }
3697
27.5M
      }
3698
2.21M
    }
3699
15.6M
 dig_done:
3700
15.6M
  e = 0;
3701
15.6M
  if (c == 'e' || c == 'E') {
3702
1.94M
    if (!nd && !nz && !nz0) {
3703
187k
      goto ret0;
3704
187k
      }
3705
1.75M
    s00 = s;
3706
1.75M
    esign = 0;
3707
1.75M
    switch(c = c_read8(++s)) {
3708
569k
      case '-':
3709
569k
        esign = 1;
3710
569k
        mxFallThrough;
3711
1.69M
      case '+':
3712
1.69M
        c = c_read8(++s);
3713
1.75M
      }
3714
1.75M
    if (c >= '0' && c <= '9') {
3715
1.76M
      while(c == '0')
3716
15.8k
        c = c_read8(++s);
3717
1.74M
      if (c > '0' && c <= '9') {
3718
1.74M
        L = c - '0';
3719
1.74M
        s1 = s;
3720
4.13M
        while((c = *++s) >= '0' && c <= '9') {
3721
2.39M
          if (L <= 19999)
3722
2.34M
            L = 10*L + c - '0';
3723
2.39M
          }
3724
1.74M
        if (L > 19999)
3725
          /* Avoid confusion from exponents
3726
           * so large that e might overflow.
3727
           */
3728
1.92k
          e = 19999; /* safe for 16 bit ints */
3729
1.74M
        else
3730
1.74M
          e = (int)L;
3731
1.74M
        if (esign)
3732
567k
          e = -e;
3733
1.74M
        }
3734
3.38k
      else
3735
3.38k
        e = 0;
3736
1.74M
      }
3737
5.99k
    else
3738
5.99k
      s = s00;
3739
1.75M
    }
3740
15.4M
  if (!nd) {
3741
6.83M
    if (!nz && !nz0) {
3742
6.73M
#ifdef INFNAN_CHECK /*{*/
3743
      /* Check for Nan and Infinity */
3744
6.73M
      if (!bc.dplen)
3745
6.67M
       switch(c) {
3746
#ifndef __XS__
3747
        case 'i':
3748
        case 'I':
3749
        if (match(&s,"nf")) {
3750
          --s;
3751
          if (!match(&s,"inity"))
3752
            ++s;
3753
#else
3754
195k
        case 'I':
3755
195k
        if (match(&s,"nfinity")) {
3756
190k
#endif
3757
190k
          word0(&rv) = 0x7ff00000;
3758
190k
          word1(&rv) = 0;
3759
190k
          goto ret;
3760
190k
          }
3761
4.52k
        break;
3762
#ifndef __XS__
3763
        case 'n':
3764
        case 'N':
3765
        if (match(&s, "an")) {
3766
#else
3767
142k
        case 'N':
3768
142k
        if (match(&s, "aN")) {
3769
139k
#endif
3770
139k
          word0(&rv) = NAN_WORD0;
3771
139k
          word1(&rv) = NAN_WORD1;
3772
139k
#ifndef No_Hex_NaN
3773
139k
          if (c_read8(s) == '(') /*)*/
3774
1.74k
            hexnan(&rv, &s);
3775
139k
#endif
3776
139k
          goto ret;
3777
139k
          }
3778
6.67M
        }
3779
6.40M
#endif /*} INFNAN_CHECK */
3780
6.59M
 ret0:
3781
6.59M
      s = s00;
3782
6.59M
      sign = 0;
3783
6.59M
      }
3784
6.69M
    goto ret;
3785
6.83M
    }
3786
8.65M
  bc.e0 = e1 = e -= nf;
3787
3788
  /* Now we have nd0 digits, starting at s0, followed by a
3789
   * decimal point, followed by nd-nd0 digits.  The number we're
3790
   * after is the integer represented by those digits times
3791
   * 10**e */
3792
3793
8.65M
  if (!nd0)
3794
106k
    nd0 = nd;
3795
8.65M
#ifndef USE_BF96
3796
8.65M
  k = nd < DBL_DIG + 2 ? nd : DBL_DIG + 2;
3797
8.65M
  dval(&rv) = y;
3798
8.65M
  if (k > 9) {
3799
#ifdef SET_INEXACT
3800
    if (k > DBL_DIG)
3801
      oldinexact = get_inexact();
3802
#endif
3803
2.95M
    dval(&rv) = tens[k - 9] * dval(&rv) + z;
3804
2.95M
    }
3805
8.65M
#endif
3806
8.65M
  bd0 = 0;
3807
8.65M
  if (nd <= DBL_DIG
3808
6.05M
#ifndef RND_PRODQUOT
3809
6.05M
#ifndef Honor_FLT_ROUNDS
3810
6.05M
    && Flt_Rounds == 1
3811
8.65M
#endif
3812
8.65M
#endif
3813
8.65M
      ) {
3814
#ifdef USE_BF96
3815
    dval(&rv) = yz;
3816
#endif
3817
6.05M
    if (!e)
3818
5.66M
      goto ret;
3819
393k
#ifndef ROUND_BIASED_without_Round_Up
3820
393k
    if (e > 0) {
3821
140k
      if (e <= Ten_pmax) {
3822
#ifdef SET_INEXACT
3823
        bc.inexact = 0;
3824
        oldinexact = 1;
3825
#endif
3826
#ifdef VAX
3827
        goto vax_ovfl_check;
3828
#else
3829
#ifdef Honor_FLT_ROUNDS
3830
        /* round correctly FLT_ROUNDS = 2 or 3 */
3831
        if (sign) {
3832
          rv.d = -rv.d;
3833
          sign = 0;
3834
          }
3835
#endif
3836
12.6k
        /* rv = */ rounded_product(dval(&rv), tens[e]);
3837
12.6k
        goto ret;
3838
12.6k
#endif
3839
12.6k
        }
3840
128k
      i = DBL_DIG - nd;
3841
128k
      if (e <= Ten_pmax + i) {
3842
        /* A fancier test would sometimes let us do
3843
         * this for larger i values.
3844
         */
3845
#ifdef SET_INEXACT
3846
        bc.inexact = 0;
3847
        oldinexact = 1;
3848
#endif
3849
#ifdef Honor_FLT_ROUNDS
3850
        /* round correctly FLT_ROUNDS = 2 or 3 */
3851
        if (sign) {
3852
          rv.d = -rv.d;
3853
          sign = 0;
3854
          }
3855
#endif
3856
1.55k
        e -= i;
3857
1.55k
        dval(&rv) *= tens[i];
3858
#ifdef VAX
3859
        /* VAX exponent range is so narrow we must
3860
         * worry about overflow here...
3861
         */
3862
 vax_ovfl_check:
3863
        word0(&rv) -= P*Exp_msk1;
3864
        /* rv = */ rounded_product(dval(&rv), tens[e]);
3865
        if ((word0(&rv) & Exp_mask)
3866
         > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
3867
          goto ovfl;
3868
        word0(&rv) += P*Exp_msk1;
3869
#else
3870
1.55k
        /* rv = */ rounded_product(dval(&rv), tens[e]);
3871
1.55k
#endif
3872
1.55k
        goto ret;
3873
1.55k
        }
3874
128k
      }
3875
252k
#ifndef Inaccurate_Divide
3876
252k
    else if (e >= -Ten_pmax) {
3877
#ifdef SET_INEXACT
3878
        bc.inexact = 0;
3879
        oldinexact = 1;
3880
#endif
3881
#ifdef Honor_FLT_ROUNDS
3882
      /* round correctly FLT_ROUNDS = 2 or 3 */
3883
      if (sign) {
3884
        rv.d = -rv.d;
3885
        sign = 0;
3886
        }
3887
#endif
3888
139k
      /* rv = */ rounded_quotient(dval(&rv), tens[-e]);
3889
139k
      goto ret;
3890
139k
      }
3891
393k
#endif
3892
393k
#endif /* ROUND_BIASED_without_Round_Up */
3893
393k
    }
3894
#ifdef USE_BF96
3895
  k = nd < 19 ? nd : 19;
3896
#endif
3897
2.83M
  e1 += nd - k; /* scale factor = 10^e1 */
3898
3899
2.83M
#ifdef IEEE_Arith
3900
#ifdef SET_INEXACT
3901
  bc.inexact = 1;
3902
#ifndef USE_BF96
3903
  if (k <= DBL_DIG)
3904
#endif
3905
    oldinexact = get_inexact();
3906
#endif
3907
#ifdef Honor_FLT_ROUNDS
3908
  if (bc.rounding >= 2) {
3909
    if (sign)
3910
      bc.rounding = bc.rounding == 2 ? 0 : 2;
3911
    else
3912
      if (bc.rounding != 2)
3913
        bc.rounding = 0;
3914
    }
3915
#endif
3916
2.83M
#endif /*IEEE_Arith*/
3917
3918
#ifdef USE_BF96 /*{*/
3919
  Debug(++dtoa_stats[0]);
3920
  i = e1 + 342;
3921
  if (i < 0)
3922
    goto undfl;
3923
  if (i > 650)
3924
    goto ovfl;
3925
  p10 = &pten[i];
3926
  brv = yz;
3927
  /* shift brv left, with i =  number of bits shifted */
3928
  i = 0;
3929
  if (!(brv & 0xffffffff00000000ull)) {
3930
    i = 32;
3931
    brv <<= 32;
3932
    }
3933
  if (!(brv & 0xffff000000000000ull)) {
3934
    i += 16;
3935
    brv <<= 16;
3936
    }
3937
  if (!(brv & 0xff00000000000000ull)) {
3938
    i += 8;
3939
    brv <<= 8;
3940
    }
3941
  if (!(brv & 0xf000000000000000ull)) {
3942
    i += 4;
3943
    brv <<= 4;
3944
    }
3945
  if (!(brv & 0xc000000000000000ull)) {
3946
    i += 2;
3947
    brv <<= 2;
3948
    }
3949
  if (!(brv & 0x8000000000000000ull)) {
3950
    i += 1;
3951
    brv <<= 1;
3952
    }
3953
  erv = (64 + 0x3fe) + p10->e - i;
3954
  if (erv <= 0 && nd > 19)
3955
    goto many_digits; /* denormal: may need to look at all digits */
3956
  bhi = brv >> 32;
3957
  blo = brv & 0xffffffffull;
3958
  /* Unsigned 32-bit ints lie in [0,2^32-1] and */
3959
  /* unsigned 64-bit ints lie in [0, 2^64-1].  The product of two unsigned */
3960
  /* 32-bit ints is <= 2^64 - 2*2^32-1 + 1 = 2^64 - 1 - 2*(2^32 - 1), so */
3961
  /* we can add two unsigned 32-bit ints to the product of two such ints, */
3962
  /* and 64 bits suffice to contain the result. */
3963
  t01 = bhi * p10->b1;
3964
  t10 = blo * p10->b0 + (t01 & 0xffffffffull);
3965
  t00 = bhi * p10->b0 + (t01 >> 32) + (t10 >> 32);
3966
  if (t00 & 0x8000000000000000ull) {
3967
    if ((t00 & 0x3ff) && (~t00 & 0x3fe)) { /* unambiguous result? */
3968
      if (nd > 19 && ((t00 + (1<<i) + 2) & 0x400) ^ (t00 & 0x400))
3969
        goto many_digits;
3970
      if (erv <= 0)
3971
        goto denormal;
3972
#ifdef Honor_FLT_ROUNDS
3973
      switch(bc.rounding) {
3974
        case 0: goto noround;
3975
        case 2: goto roundup;
3976
        }
3977
#endif
3978
      if (t00 & 0x400 && t00 & 0xbff)
3979
        goto roundup;
3980
      goto noround;
3981
      }
3982
    }
3983
  else {
3984
    if ((t00 & 0x1ff) && (~t00 & 0x1fe)) { /* unambiguous result? */
3985
      if (nd > 19 && ((t00 + (1<<i) + 2) & 0x200) ^ (t00 & 0x200))
3986
        goto many_digits;
3987
      if (erv <= 1)
3988
        goto denormal1;
3989
#ifdef Honor_FLT_ROUNDS
3990
      switch(bc.rounding) {
3991
        case 0: goto noround1;
3992
        case 2: goto roundup1;
3993
        }
3994
#endif
3995
      if (t00 & 0x200)
3996
        goto roundup1;
3997
      goto noround1;
3998
      }
3999
    }
4000
  /* 3 multiplies did not suffice; try a 96-bit approximation */
4001
  Debug(++dtoa_stats[1]);
4002
  t02 = bhi * p10->b2;
4003
  t11 = blo * p10->b1 + (t02 & 0xffffffffull);
4004
  bexact = 1;
4005
  if (e1 < 0 || e1 > 41 || (t10 | t11) & 0xffffffffull || nd > 19)
4006
    bexact = 0;
4007
  tlo = (t10 & 0xffffffffull) + (t02 >> 32) + (t11 >> 32);
4008
  if (!bexact && (tlo + 0x10) >> 32 > tlo >> 32)
4009
    goto many_digits;
4010
  t00 += tlo >> 32;
4011
  if (t00 & 0x8000000000000000ull) {
4012
    if (erv <= 0) { /* denormal result */
4013
      if (nd >= 20 || !((tlo & 0xfffffff0) | (t00 & 0x3ff)))
4014
        goto many_digits;
4015
 denormal:
4016
      if (erv <= -52) {
4017
#ifdef Honor_FLT_ROUNDS
4018
        switch(bc.rounding) {
4019
          case 0: goto undfl;
4020
          case 2: goto tiniest;
4021
          }
4022
#endif
4023
        if (erv < -52 || !(t00 & 0x7fffffffffffffffull))
4024
          goto undfl;
4025
        goto tiniest;
4026
        }
4027
      tg = 1ull << (11 - erv);
4028
      t00 &= ~(tg - 1); /* clear low bits */
4029
#ifdef Honor_FLT_ROUNDS
4030
      switch(bc.rounding) {
4031
        case 0: goto noround_den;
4032
        case 2: goto roundup_den;
4033
        }
4034
#endif
4035
      if (t00 & tg) {
4036
#ifdef Honor_FLT_ROUNDS
4037
 roundup_den:
4038
#endif
4039
        t00 += tg << 1;
4040
        if (!(t00 & 0x8000000000000000ull)) {
4041
          if (++erv > 0)
4042
            goto smallest_normal;
4043
          t00 = 0x8000000000000000ull;
4044
          }
4045
        }
4046
#ifdef Honor_FLT_ROUNDS
4047
 noround_den:
4048
#endif
4049
      LLval(&rv) = t00 >> (12 - erv);
4050
      Set_errno(ERANGE);
4051
      goto ret;
4052
      }
4053
    if (bexact) {
4054
#ifdef SET_INEXACT
4055
      if (!(t00 & 0x7ff) && !(tlo & 0xffffffffull)) {
4056
        bc.inexact = 0;
4057
        goto noround;
4058
        }
4059
#endif
4060
#ifdef Honor_FLT_ROUNDS
4061
      switch(bc.rounding) {
4062
        case 2:
4063
        if (t00 & 0x7ff)
4064
          goto roundup;
4065
        case 0: goto noround;
4066
        }
4067
#endif
4068
      if (t00 & 0x400 && (tlo & 0xffffffff) | (t00 & 0xbff))
4069
        goto roundup;
4070
      goto noround;
4071
      }
4072
    if ((tlo & 0xfffffff0) | (t00 & 0x3ff)
4073
     && (nd <= 19 ||  ((t00 + (1ull << i)) & 0xfffffffffffffc00ull)
4074
        == (t00 & 0xfffffffffffffc00ull))) {
4075
      /* Unambiguous result. */
4076
      /* If nd > 19, then incrementing the 19th digit */
4077
      /* does not affect rv. */
4078
#ifdef Honor_FLT_ROUNDS
4079
      switch(bc.rounding) {
4080
        case 0: goto noround;
4081
        case 2: goto roundup;
4082
        }
4083
#endif
4084
      if (t00 & 0x400) { /* round up */
4085
 roundup:
4086
        t00 += 0x800;
4087
        if (!(t00 & 0x8000000000000000ull)) {
4088
          /* rounded up to a power of 2 */
4089
          if (erv >= 0x7fe)
4090
            goto ovfl;
4091
          terv = erv + 1;
4092
          LLval(&rv) = terv << 52;
4093
          goto ret;
4094
          }
4095
        }
4096
 noround:
4097
      if (erv >= 0x7ff)
4098
        goto ovfl;
4099
      terv = erv;
4100
      LLval(&rv) = (terv << 52) | ((t00 & 0x7ffffffffffff800ull) >> 11);
4101
      goto ret;
4102
      }
4103
    }
4104
  else {
4105
    if (erv <= 1) { /* denormal result */
4106
      if (nd >= 20 || !((tlo & 0xfffffff0) | (t00 & 0x1ff)))
4107
        goto many_digits;
4108
 denormal1:
4109
      if (erv <= -51) {
4110
#ifdef Honor_FLT_ROUNDS
4111
        switch(bc.rounding) {
4112
          case 0: goto undfl;
4113
          case 2: goto tiniest;
4114
          }
4115
#endif
4116
        if (erv < -51 || !(t00 & 0x3fffffffffffffffull))
4117
          goto undfl;
4118
 tiniest:
4119
        LLval(&rv) = 1;
4120
        Set_errno(ERANGE);
4121
        goto ret;
4122
        }
4123
      tg = 1ull << (11 - erv);
4124
#ifdef Honor_FLT_ROUNDS
4125
      switch(bc.rounding) {
4126
        case 0: goto noround1_den;
4127
        case 2: goto roundup1_den;
4128
        }
4129
#endif
4130
      if (t00 & tg) {
4131
#ifdef Honor_FLT_ROUNDS
4132
 roundup1_den:
4133
#endif
4134
        if (0x8000000000000000ull & (t00 += (tg<<1)) && erv == 1) {
4135
4136
 smallest_normal:
4137
          LLval(&rv) = 0x0010000000000000ull;
4138
          goto ret;
4139
          }
4140
        }
4141
#ifdef Honor_FLT_ROUNDS
4142
 noround1_den:
4143
#endif
4144
      if (erv <= -52)
4145
        goto undfl;
4146
      LLval(&rv) = t00 >> (12 - erv);
4147
      Set_errno(ERANGE);
4148
      goto ret;
4149
      }
4150
    if (bexact) {
4151
#ifdef SET_INEXACT
4152
      if (!(t00 & 0x3ff) && !(tlo & 0xffffffffull)) {
4153
        bc.inexact = 0;
4154
        goto noround1;
4155
        }
4156
#endif
4157
#ifdef Honor_FLT_ROUNDS
4158
      switch(bc.rounding) {
4159
        case 2:
4160
        if (t00 & 0x3ff)
4161
          goto roundup1;
4162
        case 0: goto noround1;
4163
        }
4164
#endif
4165
      if (t00 & 0x200 && (t00 & 0x5ff || tlo))
4166
        goto roundup1;
4167
      goto noround1;
4168
      }
4169
    if ((tlo & 0xfffffff0) | (t00 & 0x1ff)
4170
     && (nd <= 19 ||  ((t00 + (1ull << i)) & 0x7ffffffffffffe00ull)
4171
        == (t00 & 0x7ffffffffffffe00ull))) {
4172
      /* Unambiguous result. */
4173
#ifdef Honor_FLT_ROUNDS
4174
      switch(bc.rounding) {
4175
        case 0: goto noround1;
4176
        case 2: goto roundup1;
4177
        }
4178
#endif
4179
      if (t00 & 0x200) { /* round up */
4180
 roundup1:
4181
        t00 += 0x400;
4182
        if (!(t00 & 0x4000000000000000ull)) {
4183
          /* rounded up to a power of 2 */
4184
          if (erv >= 0x7ff)
4185
            goto ovfl;
4186
          terv = erv;
4187
          LLval(&rv) = terv << 52;
4188
          goto ret;
4189
          }
4190
        }
4191
 noround1:
4192
      if (erv >= 0x800)
4193
        goto ovfl;
4194
      terv = erv - 1;
4195
      LLval(&rv) = (terv << 52) | ((t00 & 0x3ffffffffffffc00ull) >> 10);
4196
      goto ret;
4197
      }
4198
    }
4199
 many_digits:
4200
  Debug(++dtoa_stats[2]);
4201
  if (nd > 17) {
4202
    if (nd > 18) {
4203
      yz /= 100;
4204
      e1 += 2;
4205
      }
4206
    else {
4207
      yz /= 10;
4208
      e1 += 1;
4209
      }
4210
    y = yz / 100000000;
4211
    }
4212
  else if (nd > 9) {
4213
    i = nd - 9;
4214
    y = (yz >> i) / pfive[i-1];
4215
    }
4216
  else
4217
    y = yz;
4218
  dval(&rv) = yz;
4219
#endif /*}*/
4220
4221
2.83M
#ifdef IEEE_Arith
4222
2.83M
#ifdef Avoid_Underflow
4223
2.83M
  bc.scale = 0;
4224
2.83M
#endif
4225
2.83M
#endif /*IEEE_Arith*/
4226
4227
  /* Get starting approximation = rv * 10**e1 */
4228
4229
2.83M
  if (e1 > 0) {
4230
1.35M
    if ((i = e1 & 15))
4231
1.29M
      dval(&rv) *= tens[i];
4232
1.35M
    if (e1 &= ~15) {
4233
913k
      if (e1 > DBL_MAX_10_EXP) {
4234
19.0k
 ovfl:
4235
        /* Can't trust HUGE_VAL */
4236
19.0k
#ifdef IEEE_Arith
4237
#ifdef Honor_FLT_ROUNDS
4238
        switch(bc.rounding) {
4239
          case 0: /* toward 0 */
4240
          case 3: /* toward -infinity */
4241
          word0(&rv) = Big0;
4242
          word1(&rv) = Big1;
4243
          break;
4244
          default:
4245
          word0(&rv) = Exp_mask;
4246
          word1(&rv) = 0;
4247
          }
4248
#else /*Honor_FLT_ROUNDS*/
4249
19.0k
        word0(&rv) = Exp_mask;
4250
19.0k
        word1(&rv) = 0;
4251
19.0k
#endif /*Honor_FLT_ROUNDS*/
4252
#ifdef SET_INEXACT
4253
        /* set overflow bit */
4254
        dval(&rv0) = 1e300;
4255
        dval(&rv0) *= dval(&rv0);
4256
#endif
4257
#else /*IEEE_Arith*/
4258
        word0(&rv) = Big0;
4259
        word1(&rv) = Big1;
4260
#endif /*IEEE_Arith*/
4261
22.2k
 range_err:
4262
22.2k
        if (bd0) {
4263
306
          Bfree(bb MTb);
4264
306
          Bfree(bd MTb);
4265
306
          Bfree(bs MTb);
4266
306
          Bfree(bd0 MTb);
4267
306
          Bfree(delta MTb);
4268
306
          }
4269
22.2k
        Set_errno(ERANGE);
4270
22.2k
        goto ret;
4271
19.0k
        }
4272
908k
      e1 >>= 4;
4273
2.45M
      for(j = 0; e1 > 1; j++, e1 >>= 1)
4274
1.54M
        if (e1 & 1)
4275
637k
          dval(&rv) *= bigtens[j];
4276
    /* The last multiplication could overflow. */
4277
908k
      word0(&rv) -= P*Exp_msk1;
4278
908k
      dval(&rv) *= bigtens[j];
4279
908k
      if ((z = word0(&rv) & Exp_mask)
4280
908k
       > Exp_msk1*(DBL_MAX_EXP+Bias-P))
4281
13.6k
        goto ovfl;
4282
895k
      if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
4283
        /* set to largest number */
4284
        /* (Can't trust DBL_MAX) */
4285
524
        word0(&rv) = Big0;
4286
524
        word1(&rv) = Big1;
4287
524
        }
4288
894k
      else
4289
894k
        word0(&rv) += P*Exp_msk1;
4290
895k
      }
4291
1.35M
    }
4292
1.47M
  else if (e1 < 0) {
4293
1.14M
    e1 = -e1;
4294
1.14M
    if ((i = e1 & 15))
4295
1.10M
      dval(&rv) /= tens[i];
4296
1.14M
    if (e1 >>= 4) {
4297
577k
      if (e1 >= 1 << n_bigtens)
4298
3.00k
        goto undfl;
4299
574k
#ifdef Avoid_Underflow
4300
574k
      if (e1 & Scale_Bit)
4301
100k
        bc.scale = 2*P;
4302
2.40M
      for(j = 0; e1 > 0; j++, e1 >>= 1)
4303
1.83M
        if (e1 & 1)
4304
1.05M
          dval(&rv) *= tinytens[j];
4305
574k
      if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
4306
100k
            >> Exp_shift)) > 0) {
4307
        /* scaled rv is denormal; clear j low bits */
4308
14.8k
        if (j >= 32) {
4309
5.99k
          if (j > 54)
4310
70
            goto undfl;
4311
5.92k
          word1(&rv) = 0;
4312
5.92k
          if (j >= 53)
4313
166
           word0(&rv) = (P+2)*Exp_msk1;
4314
5.76k
          else
4315
5.76k
           word0(&rv) &= 0xffffffff << (j-32);
4316
5.92k
          }
4317
8.81k
        else
4318
8.81k
          word1(&rv) &= 0xffffffff << j;
4319
14.8k
        }
4320
#else
4321
      for(j = 0; e1 > 1; j++, e1 >>= 1)
4322
        if (e1 & 1)
4323
          dval(&rv) *= tinytens[j];
4324
      /* The last multiplication could underflow. */
4325
      dval(&rv0) = dval(&rv);
4326
      dval(&rv) *= tinytens[j];
4327
      if (!dval(&rv)) {
4328
        dval(&rv) = 2.*dval(&rv0);
4329
        dval(&rv) *= tinytens[j];
4330
#endif
4331
574k
        if (!dval(&rv)) {
4332
3.23k
 undfl:
4333
3.23k
          dval(&rv) = 0.;
4334
#ifdef Honor_FLT_ROUNDS
4335
          if (bc.rounding == 2)
4336
            word1(&rv) = 1;
4337
#endif
4338
3.23k
          goto range_err;
4339
0
          }
4340
#ifndef Avoid_Underflow
4341
        word0(&rv) = Tiny0;
4342
        word1(&rv) = Tiny1;
4343
        /* The refinement below will clean
4344
         * this approximation up.
4345
         */
4346
        }
4347
#endif
4348
574k
      }
4349
1.14M
    }
4350
4351
  /* Now the hard part -- adjusting rv to the correct value.*/
4352
4353
  /* Put digits into bd: true value = bd * 10^e */
4354
4355
2.81M
  bc.nd = nd - nz1;
4356
2.81M
#ifndef NO_STRTOD_BIGCOMP
4357
2.81M
  bc.nd0 = nd0; /* Only needed if nd > strtod_diglim, but done here */
4358
      /* to silence an erroneous warning about bc.nd0 */
4359
      /* possibly not being initialized. */
4360
2.81M
  if (nd > strtod_diglim) {
4361
    /* ASSERT(strtod_diglim >= 18); 18 == one more than the */
4362
    /* minimum number of decimal digits to distinguish double values */
4363
    /* in IEEE arithmetic. */
4364
4.43k
    i = j = 18;
4365
4.43k
    if (i > nd0)
4366
984
      j += bc.dplen;
4367
18.9k
    for(;;) {
4368
18.9k
      if (--j < bc.dp1 && j >= bc.dp0)
4369
510
        j = bc.dp0 - 1;
4370
18.9k
      if (s0[j] != '0')
4371
4.43k
        break;
4372
14.4k
      --i;
4373
14.4k
      }
4374
4.43k
    e += nd - i;
4375
4.43k
    nd = i;
4376
4.43k
    if (nd0 > nd)
4377
3.48k
      nd0 = nd;
4378
4.43k
    if (nd < 9) { /* must recompute y */
4379
835
      y = 0;
4380
3.42k
      for(i = 0; i < nd0; ++i)
4381
2.58k
        y = 10*y + s0[i] - '0';
4382
1.06k
      for(j = bc.dp1; i < nd; ++i)
4383
233
        y = 10*y + s0[j++] - '0';
4384
835
      }
4385
4.43k
    }
4386
2.81M
#endif
4387
2.81M
  bd0 = s2b(s0, nd0, nd, y, bc.dplen MTb);
4388
4389
2.94M
  for(;;) {
4390
2.94M
    bd = Balloc(bd0->k MTb);
4391
2.94M
    Bcopy(bd, bd0);
4392
2.94M
    bb = d2b(&rv, &bbe, &bbbits MTb);  /* rv = bb * 2^bbe */
4393
2.94M
    bs = i2b(1 MTb);
4394
4395
2.94M
    if (e >= 0) {
4396
1.74M
      bb2 = bb5 = 0;
4397
1.74M
      bd2 = bd5 = e;
4398
1.74M
      }
4399
1.20M
    else {
4400
1.20M
      bb2 = bb5 = -e;
4401
1.20M
      bd2 = bd5 = 0;
4402
1.20M
      }
4403
2.94M
    if (bbe >= 0)
4404
1.75M
      bb2 += bbe;
4405
1.19M
    else
4406
1.19M
      bd2 -= bbe;
4407
2.94M
    bs2 = bb2;
4408
#ifdef Honor_FLT_ROUNDS
4409
    if (bc.rounding != 1)
4410
      bs2++;
4411
#endif
4412
2.94M
#ifdef Avoid_Underflow
4413
#ifndef ROUND_BIASED
4414
    Lsb = LSB;
4415
    Lsb1 = 0;
4416
#endif
4417
2.94M
    j = bbe - bc.scale;
4418
2.94M
    i = j + bbbits - 1; /* logb(rv) */
4419
2.94M
    j = P + 1 - bbbits;
4420
2.94M
    if (i < Emin) { /* denormal */
4421
20.4k
      i = Emin - i;
4422
20.4k
      j -= i;
4423
#ifndef ROUND_BIASED
4424
      if (i < 32)
4425
        Lsb <<= i;
4426
      else if (i < 52)
4427
        Lsb1 = Lsb << (i-32);
4428
      else
4429
        Lsb1 = Exp_mask;
4430
#endif
4431
20.4k
      }
4432
#else /*Avoid_Underflow*/
4433
#ifdef Sudden_Underflow
4434
#ifdef IBM
4435
    j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
4436
#else
4437
    j = P + 1 - bbbits;
4438
#endif
4439
#else /*Sudden_Underflow*/
4440
    j = bbe;
4441
    i = j + bbbits - 1; /* logb(rv) */
4442
    if (i < Emin) /* denormal */
4443
      j += P - Emin;
4444
    else
4445
      j = P + 1 - bbbits;
4446
#endif /*Sudden_Underflow*/
4447
#endif /*Avoid_Underflow*/
4448
2.94M
    bb2 += j;
4449
2.94M
    bd2 += j;
4450
2.94M
#ifdef Avoid_Underflow
4451
2.94M
    bd2 += bc.scale;
4452
2.94M
#endif
4453
2.94M
    i = bb2 < bd2 ? bb2 : bd2;
4454
2.94M
    if (i > bs2)
4455
1.27M
      i = bs2;
4456
2.94M
    if (i > 0) {
4457
2.91M
      bb2 -= i;
4458
2.91M
      bd2 -= i;
4459
2.91M
      bs2 -= i;
4460
2.91M
      }
4461
2.94M
    if (bb5 > 0) {
4462
1.20M
      bs = pow5mult(bs, bb5 MTb);
4463
1.20M
      bb1 = mult(bs, bb MTb);
4464
1.20M
      Bfree(bb MTb);
4465
1.20M
      bb = bb1;
4466
1.20M
      }
4467
2.94M
    if (bb2 > 0)
4468
2.94M
      bb = lshift(bb, bb2 MTb);
4469
2.94M
    if (bd5 > 0)
4470
1.10M
      bd = pow5mult(bd, bd5 MTb);
4471
2.94M
    if (bd2 > 0)
4472
1.27M
      bd = lshift(bd, bd2 MTb);
4473
2.94M
    if (bs2 > 0)
4474
1.58M
      bs = lshift(bs, bs2 MTb);
4475
2.94M
    delta = diff(bb, bd MTb);
4476
2.94M
    bc.dsign = delta->sign;
4477
2.94M
    delta->sign = 0;
4478
2.94M
    i = cmp(delta, bs);
4479
2.94M
#ifndef NO_STRTOD_BIGCOMP /*{*/
4480
2.94M
    if (bc.nd > nd && i <= 0) {
4481
4.46k
      if (bc.dsign) {
4482
        /* Must use bigcomp(). */
4483
2.75k
        req_bigcomp = 1;
4484
2.75k
        break;
4485
2.75k
        }
4486
#ifdef Honor_FLT_ROUNDS
4487
      if (bc.rounding != 1) {
4488
        if (i < 0) {
4489
          req_bigcomp = 1;
4490
          break;
4491
          }
4492
        }
4493
      else
4494
#endif
4495
1.70k
        i = -1; /* Discarded digits make delta smaller. */
4496
1.70k
      }
4497
2.94M
#endif /*}*/
4498
#ifdef Honor_FLT_ROUNDS /*{*/
4499
    if (bc.rounding != 1) {
4500
      if (i < 0) {
4501
        /* Error is less than an ulp */
4502
        if (!delta->x[0] && delta->wds <= 1) {
4503
          /* exact */
4504
#ifdef SET_INEXACT
4505
          bc.inexact = 0;
4506
#endif
4507
          break;
4508
          }
4509
        if (bc.rounding) {
4510
          if (bc.dsign) {
4511
            adj.d = 1.;
4512
            goto apply_adj;
4513
            }
4514
          }
4515
        else if (!bc.dsign) {
4516
          adj.d = -1.;
4517
          if (!word1(&rv)
4518
           && !(word0(&rv) & Frac_mask)) {
4519
            y = word0(&rv) & Exp_mask;
4520
#ifdef Avoid_Underflow
4521
            if (!bc.scale || y > 2*P*Exp_msk1)
4522
#else
4523
            if (y)
4524
#endif
4525
              {
4526
              delta = lshift(delta,Log2P MTb);
4527
              if (cmp(delta, bs) <= 0)
4528
              adj.d = -0.5;
4529
              }
4530
            }
4531
 apply_adj:
4532
#ifdef Avoid_Underflow /*{*/
4533
          if (bc.scale && (y = word0(&rv) & Exp_mask)
4534
            <= 2*P*Exp_msk1)
4535
            word0(&adj) += (2*P+1)*Exp_msk1 - y;
4536
#else
4537
#ifdef Sudden_Underflow
4538
          if ((word0(&rv) & Exp_mask) <=
4539
              P*Exp_msk1) {
4540
            word0(&rv) += P*Exp_msk1;
4541
            dval(&rv) += adj.d*ulp(dval(&rv));
4542
            word0(&rv) -= P*Exp_msk1;
4543
            }
4544
          else
4545
#endif /*Sudden_Underflow*/
4546
#endif /*Avoid_Underflow}*/
4547
          dval(&rv) += adj.d*ulp(&rv);
4548
          }
4549
        break;
4550
        }
4551
      adj.d = ratio(delta, bs);
4552
      if (adj.d < 1.)
4553
        adj.d = 1.;
4554
      if (adj.d <= 0x7ffffffe) {
4555
        /* adj = rounding ? ceil(adj) : floor(adj); */
4556
        y = adj.d;
4557
        if (y != adj.d) {
4558
          if (!((bc.rounding>>1) ^ bc.dsign))
4559
            y++;
4560
          adj.d = y;
4561
          }
4562
        }
4563
#ifdef Avoid_Underflow /*{*/
4564
      if (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
4565
        word0(&adj) += (2*P+1)*Exp_msk1 - y;
4566
#else
4567
#ifdef Sudden_Underflow
4568
      if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
4569
        word0(&rv) += P*Exp_msk1;
4570
        adj.d *= ulp(dval(&rv));
4571
        if (bc.dsign)
4572
          dval(&rv) += adj.d;
4573
        else
4574
          dval(&rv) -= adj.d;
4575
        word0(&rv) -= P*Exp_msk1;
4576
        goto cont;
4577
        }
4578
#endif /*Sudden_Underflow*/
4579
#endif /*Avoid_Underflow}*/
4580
      adj.d *= ulp(&rv);
4581
      if (bc.dsign) {
4582
        if (word0(&rv) == Big0 && word1(&rv) == Big1)
4583
          goto ovfl;
4584
        dval(&rv) += adj.d;
4585
        }
4586
      else
4587
        dval(&rv) -= adj.d;
4588
      goto cont;
4589
      }
4590
#endif /*}Honor_FLT_ROUNDS*/
4591
4592
2.94M
    if (i < 0) {
4593
      /* Error is less than half an ulp -- check for
4594
       * special case of mantissa a power of two.
4595
       */
4596
2.15M
      if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask
4597
96.4k
#ifdef IEEE_Arith /*{*/
4598
96.4k
#ifdef Avoid_Underflow
4599
96.4k
       || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
4600
#else
4601
       || (word0(&rv) & Exp_mask) <= Exp_msk1
4602
#endif
4603
2.15M
#endif /*}*/
4604
2.15M
        ) {
4605
#ifdef SET_INEXACT
4606
        if (!delta->x[0] && delta->wds <= 1)
4607
          bc.inexact = 0;
4608
#endif
4609
2.05M
        break;
4610
2.05M
        }
4611
94.5k
      if (!delta->x[0] && delta->wds <= 1) {
4612
        /* exact result */
4613
#ifdef SET_INEXACT
4614
        bc.inexact = 0;
4615
#endif
4616
5.61k
        break;
4617
5.61k
        }
4618
88.9k
      delta = lshift(delta,Log2P MTb);
4619
88.9k
      if (cmp(delta, bs) > 0)
4620
233
        goto drop_down;
4621
88.6k
      break;
4622
88.9k
      }
4623
792k
    if (i == 0) {
4624
      /* exactly half-way between */
4625
36.1k
      if (bc.dsign) {
4626
7.67k
        if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
4627
324
         &&  word1(&rv) == (
4628
324
#ifdef Avoid_Underflow
4629
324
      (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
4630
324
    ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
4631
324
#endif
4632
324
               0xffffffff)) {
4633
          /*boundary case -- increment exponent*/
4634
10
          if (word0(&rv) == Big0 && word1(&rv) == Big1)
4635
0
            goto ovfl;
4636
10
          word0(&rv) = (word0(&rv) & Exp_mask)
4637
10
            + Exp_msk1
4638
#ifdef IBM
4639
            | Exp_msk1 >> 4
4640
#endif
4641
10
            ;
4642
10
          word1(&rv) = 0;
4643
10
#ifdef Avoid_Underflow
4644
10
          bc.dsign = 0;
4645
10
#endif
4646
10
          break;
4647
10
          }
4648
7.67k
        }
4649
28.4k
      else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
4650
233
 drop_down:
4651
        /* boundary case -- decrement exponent */
4652
#ifdef Sudden_Underflow /*{{*/
4653
        L = word0(&rv) & Exp_mask;
4654
#ifdef IBM
4655
        if (L <  Exp_msk1)
4656
#else
4657
#ifdef Avoid_Underflow
4658
        if (L <= (bc.scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
4659
#else
4660
        if (L <= Exp_msk1)
4661
#endif /*Avoid_Underflow*/
4662
#endif /*IBM*/
4663
          {
4664
          if (bc.nd >nd) {
4665
            bc.uflchk = 1;
4666
            break;
4667
            }
4668
          goto undfl;
4669
          }
4670
        L -= Exp_msk1;
4671
#else /*Sudden_Underflow}{*/
4672
233
#ifdef Avoid_Underflow
4673
233
        if (bc.scale) {
4674
0
          L = word0(&rv) & Exp_mask;
4675
0
          if (L <= (2*P+1)*Exp_msk1) {
4676
0
            if (L > (P+2)*Exp_msk1)
4677
              /* round even ==> */
4678
              /* accept rv */
4679
0
              break;
4680
            /* rv = smallest denormal */
4681
0
            if (bc.nd >nd) {
4682
0
              bc.uflchk = 1;
4683
0
              break;
4684
0
              }
4685
0
            goto undfl;
4686
0
            }
4687
0
          }
4688
233
#endif /*Avoid_Underflow*/
4689
233
        L = (word0(&rv) & Exp_mask) - Exp_msk1;
4690
233
#endif /*Sudden_Underflow}}*/
4691
233
        word0(&rv) = L | Bndry_mask1;
4692
233
        word1(&rv) = 0xffffffff;
4693
#ifdef IBM
4694
        goto cont;
4695
#else
4696
233
#ifndef NO_STRTOD_BIGCOMP
4697
233
        if (bc.nd > nd)
4698
31
          goto cont;
4699
202
#endif
4700
202
        break;
4701
233
#endif
4702
233
        }
4703
#ifndef ROUND_BIASED
4704
#ifdef Avoid_Underflow
4705
      if (Lsb1) {
4706
        if (!(word0(&rv) & Lsb1))
4707
          break;
4708
        }
4709
      else if (!(word1(&rv) & Lsb))
4710
        break;
4711
#else
4712
      if (!(word1(&rv) & LSB))
4713
        break;
4714
#endif
4715
#endif
4716
36.1k
      if (bc.dsign)
4717
7.66k
#ifdef Avoid_Underflow
4718
7.66k
        dval(&rv) += sulp(&rv, &bc);
4719
#else
4720
        dval(&rv) += ulp(&rv);
4721
#endif
4722
#ifndef ROUND_BIASED
4723
      else {
4724
#ifdef Avoid_Underflow
4725
        dval(&rv) -= sulp(&rv, &bc);
4726
#else
4727
        dval(&rv) -= ulp(&rv);
4728
#endif
4729
#ifndef Sudden_Underflow
4730
        if (!dval(&rv)) {
4731
          if (bc.nd >nd) {
4732
            bc.uflchk = 1;
4733
            break;
4734
            }
4735
          goto undfl;
4736
          }
4737
#endif
4738
        }
4739
#ifdef Avoid_Underflow
4740
      bc.dsign = 1 - bc.dsign;
4741
#endif
4742
#endif
4743
36.1k
      break;
4744
36.1k
      }
4745
756k
    if ((aadj = ratio(delta, bs)) <= 2.) {
4746
533k
      if (bc.dsign)
4747
207k
        aadj = aadj1 = 1.;
4748
326k
      else if (word1(&rv) || word0(&rv) & Bndry_mask) {
4749
325k
#ifndef Sudden_Underflow
4750
325k
        if (word1(&rv) == Tiny1 && !word0(&rv)) {
4751
0
          if (bc.nd >nd) {
4752
0
            bc.uflchk = 1;
4753
0
            break;
4754
0
            }
4755
0
          goto undfl;
4756
0
          }
4757
325k
#endif
4758
325k
        aadj = 1.;
4759
325k
        aadj1 = -1.;
4760
325k
        }
4761
169
      else {
4762
        /* special case -- power of FLT_RADIX to be */
4763
        /* rounded down... */
4764
4765
169
        if (aadj < 2./FLT_RADIX)
4766
0
          aadj = 1./FLT_RADIX;
4767
169
        else
4768
169
          aadj *= 0.5;
4769
169
        aadj1 = -aadj;
4770
169
        }
4771
533k
      }
4772
223k
    else {
4773
223k
      aadj *= 0.5;
4774
223k
      aadj1 = bc.dsign ? aadj : -aadj;
4775
#ifdef Check_FLT_ROUNDS
4776
      switch(bc.rounding) {
4777
        case 2: /* towards +infinity */
4778
          aadj1 -= 0.5;
4779
          break;
4780
        case 0: /* towards 0 */
4781
        case 3: /* towards -infinity */
4782
          aadj1 += 0.5;
4783
        }
4784
#else
4785
223k
      if (Flt_Rounds == 0)
4786
0
        aadj1 += 0.5;
4787
223k
#endif /*Check_FLT_ROUNDS*/
4788
223k
      }
4789
756k
    y = word0(&rv) & Exp_mask;
4790
4791
    /* Check for overflow */
4792
4793
756k
    if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
4794
1.22k
      dval(&rv0) = dval(&rv);
4795
1.22k
      word0(&rv) -= P*Exp_msk1;
4796
1.22k
      adj.d = aadj1 * ulp(&rv);
4797
1.22k
      dval(&rv) += adj.d;
4798
1.22k
      if ((word0(&rv) & Exp_mask) >=
4799
1.22k
          Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
4800
306
        if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
4801
306
          goto ovfl;
4802
0
        word0(&rv) = Big0;
4803
0
        word1(&rv) = Big1;
4804
0
        goto cont;
4805
306
        }
4806
919
      else
4807
919
        word0(&rv) += P*Exp_msk1;
4808
1.22k
      }
4809
755k
    else {
4810
755k
#ifdef Avoid_Underflow
4811
755k
      if (bc.scale && y <= 2*P*Exp_msk1) {
4812
5.89k
        if (aadj <= 0x7fffffff) {
4813
5.89k
          if ((z = (ULong)aadj) <= 0)
4814
166
            z = 1;
4815
5.89k
          aadj = z;
4816
5.89k
          aadj1 = bc.dsign ? aadj : -aadj;
4817
5.89k
          }
4818
5.89k
        dval(&aadj2) = aadj1;
4819
5.89k
        word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
4820
5.89k
        aadj1 = dval(&aadj2);
4821
5.89k
        adj.d = aadj1 * ulp(&rv);
4822
5.89k
        dval(&rv) += adj.d;
4823
5.89k
        if (rv.d == 0.)
4824
#ifdef NO_STRTOD_BIGCOMP
4825
          goto undfl;
4826
#else
4827
166
          {
4828
166
          req_bigcomp = 1;
4829
166
          break;
4830
166
          }
4831
5.89k
#endif
4832
5.89k
        }
4833
749k
      else {
4834
749k
        adj.d = aadj1 * ulp(&rv);
4835
749k
        dval(&rv) += adj.d;
4836
749k
        }
4837
#else
4838
#ifdef Sudden_Underflow
4839
      if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
4840
        dval(&rv0) = dval(&rv);
4841
        word0(&rv) += P*Exp_msk1;
4842
        adj.d = aadj1 * ulp(&rv);
4843
        dval(&rv) += adj.d;
4844
#ifdef IBM
4845
        if ((word0(&rv) & Exp_mask) <  P*Exp_msk1)
4846
#else
4847
        if ((word0(&rv) & Exp_mask) <= P*Exp_msk1)
4848
#endif
4849
          {
4850
          if (word0(&rv0) == Tiny0
4851
           && word1(&rv0) == Tiny1) {
4852
            if (bc.nd >nd) {
4853
              bc.uflchk = 1;
4854
              break;
4855
              }
4856
            goto undfl;
4857
            }
4858
          word0(&rv) = Tiny0;
4859
          word1(&rv) = Tiny1;
4860
          goto cont;
4861
          }
4862
        else
4863
          word0(&rv) -= P*Exp_msk1;
4864
        }
4865
      else {
4866
        adj.d = aadj1 * ulp(&rv);
4867
        dval(&rv) += adj.d;
4868
        }
4869
#else /*Sudden_Underflow*/
4870
      /* Compute adj so that the IEEE rounding rules will
4871
       * correctly round rv + adj in some half-way cases.
4872
       * If rv * ulp(rv) is denormalized (i.e.,
4873
       * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
4874
       * trouble from bits lost to denormalization;
4875
       * example: 1.2e-307 .
4876
       */
4877
      if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
4878
        aadj1 = (double)(int)(aadj + 0.5);
4879
        if (!bc.dsign)
4880
          aadj1 = -aadj1;
4881
        }
4882
      adj.d = aadj1 * ulp(&rv);
4883
      dval(&rv) += adj.d;
4884
#endif /*Sudden_Underflow*/
4885
#endif /*Avoid_Underflow*/
4886
755k
      }
4887
756k
    z = word0(&rv) & Exp_mask;
4888
756k
#ifndef SET_INEXACT
4889
756k
    if (bc.nd == nd) {
4890
680k
#ifdef Avoid_Underflow
4891
680k
    if (!bc.scale)
4892
638k
#endif
4893
638k
    if (y == z) {
4894
      /* Can we stop now? */
4895
623k
      L = (Long)aadj;
4896
623k
      aadj -= L;
4897
      /* The tolerances below are conservative. */
4898
623k
      if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
4899
565k
        if (aadj < .4999999 || aadj > .5000001)
4900
565k
          break;
4901
565k
        }
4902
58.1k
      else if (aadj < .4999999/FLT_RADIX)
4903
58.1k
        break;
4904
623k
      }
4905
680k
    }
4906
132k
#endif
4907
132k
 cont:
4908
132k
    Bfree(bb MTb);
4909
132k
    Bfree(bd MTb);
4910
132k
    Bfree(bs MTb);
4911
132k
    Bfree(delta MTb);
4912
132k
    }
4913
2.81M
  Bfree(bb MTb);
4914
2.81M
  Bfree(bd MTb);
4915
2.81M
  Bfree(bs MTb);
4916
2.81M
  Bfree(bd0 MTb);
4917
2.81M
  Bfree(delta MTb);
4918
2.81M
#ifndef NO_STRTOD_BIGCOMP
4919
2.81M
  if (req_bigcomp) {
4920
2.91k
    bd0 = 0;
4921
2.91k
    bc.e0 += nz1;
4922
2.91k
    bigcomp(&rv, s0, &bc MTb);
4923
2.91k
    y = word0(&rv) & Exp_mask;
4924
2.91k
    if (y == Exp_mask)
4925
16
      goto ovfl;
4926
2.90k
    if (y == 0 && rv.d == 0.)
4927
166
      goto undfl;
4928
2.90k
    }
4929
2.81M
#endif
4930
2.81M
#ifdef Avoid_Underflow
4931
2.81M
  if (bc.scale) {
4932
100k
    word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
4933
100k
    word1(&rv0) = 0;
4934
100k
    dval(&rv) *= dval(&rv0);
4935
100k
#ifndef NO_ERRNO
4936
    /* try to avoid the bug of testing an 8087 register value */
4937
100k
#ifdef IEEE_Arith
4938
100k
    if (!(word0(&rv) & Exp_mask))
4939
#else
4940
    if (word0(&rv) == 0 && word1(&rv) == 0)
4941
#endif
4942
100k
      Set_errno(ERANGE);
4943
100k
#endif
4944
100k
    }
4945
2.81M
#endif /* Avoid_Underflow */
4946
15.8M
 ret:
4947
#ifdef SET_INEXACT
4948
  if (bc.inexact) {
4949
    if (!(word0(&rv) & Exp_mask)) {
4950
      /* set underflow and inexact bits */
4951
      dval(&rv0) = 1e-300;
4952
      dval(&rv0) *= dval(&rv0);
4953
      }
4954
    else if (!oldinexact) {
4955
      word0(&rv0) = Exp_1 + (70 << Exp_shift);
4956
      word1(&rv0) = 0;
4957
      dval(&rv0) += 1.;
4958
      }
4959
    }
4960
  else if (!oldinexact)
4961
    clear_inexact();
4962
#endif
4963
15.8M
  if (se)
4964
15.8M
    *se = (char *)s;
4965
15.8M
  return sign ? -dval(&rv) : dval(&rv);
4966
2.81M
  }
4967
4968
#ifndef MULTIPLE_THREADS
4969
#ifndef __XS__
4970
  static char *dtoa_result;
4971
#endif
4972
#endif
4973
4974
 static char *
4975
rv_alloc(int i MTd)
4976
13.5M
{
4977
13.5M
  int j, k, *r;
4978
4979
13.5M
  j = sizeof(ULong);
4980
13.5M
  for(k = 0;
4981
13.5M
    sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (size_t)i;
4982
13.5M
    j <<= 1)
4983
3.88k
      k++;
4984
13.5M
  r = (int*)Balloc(k MTa);
4985
13.5M
  *r = k;
4986
13.5M
  return
4987
13.5M
#ifndef MULTIPLE_THREADS
4988
#ifndef __XS__
4989
    dtoa_result =
4990
#endif
4991
13.5M
#endif
4992
13.5M
    (char *)(r+1);
4993
13.5M
  }
4994
4995
 static char *
4996
nrv_alloc(const char *s, char *s0, size_t s0len, char **rve, int n MTd)
4997
2.13M
{
4998
2.13M
  char *rv, *t;
4999
5000
2.13M
  if (!s0)
5001
2.13M
    s0 = rv_alloc(n MTa);
5002
0
  else if (s0len <= (size_t)n) {
5003
0
    rv = 0;
5004
0
    t = rv + n;
5005
0
    goto rve_chk;
5006
0
    }
5007
2.13M
  t = rv = s0;
5008
10.0M
  while((*t = *s++))
5009
7.93M
    ++t;
5010
2.13M
 rve_chk:
5011
2.13M
  if (rve)
5012
2.13M
    *rve = t;
5013
2.13M
  return rv;
5014
2.13M
  }
5015
5016
/* freedtoa(s) must be used to free values s returned by dtoa
5017
 * when MULTIPLE_THREADS is #defined.  It should be used in all cases,
5018
 * but for consistency with earlier versions of dtoa, it is optional
5019
 * when MULTIPLE_THREADS is not defined.
5020
 */
5021
5022
 void
5023
freedtoa(char *s __XS__d)
5024
13.5M
{
5025
#ifdef MULTIPLE_THREADS
5026
  ThInfo *TI = 0;
5027
#endif
5028
13.5M
  Bigint *b = (Bigint *)((int *)s - 1);
5029
13.5M
  b->maxwds = 1 << (b->k = *(int*)b);
5030
13.5M
  Bfree(b MTb);
5031
13.5M
#ifndef MULTIPLE_THREADS
5032
#ifndef __XS__
5033
  if (s == dtoa_result)
5034
    dtoa_result = 0;
5035
#endif
5036
13.5M
#endif
5037
13.5M
  }
5038
5039
/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
5040
 *
5041
 * Inspired by "How to Print Floating-Point Numbers Accurately" by
5042
 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
5043
 *
5044
 * Modifications:
5045
 *  1. Rather than iterating, we use a simple numeric overestimate
5046
 *     to determine k = floor(log10(d)).  We scale relevant
5047
 *     quantities using O(log2(k)) rather than O(k) multiplications.
5048
 *  2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
5049
 *     try to generate digits strictly left to right.  Instead, we
5050
 *     compute with fewer bits and propagate the carry if necessary
5051
 *     when rounding the final digit up.  This is often faster.
5052
 *  3. Under the assumption that input will be rounded nearest,
5053
 *     mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
5054
 *     That is, we allow equality in stopping tests when the
5055
 *     round-nearest rule will give the same floating-point value
5056
 *     as would satisfaction of the stopping test with strict
5057
 *     inequality.
5058
 *  4. We remove common factors of powers of 2 from relevant
5059
 *     quantities.
5060
 *  5. When converting floating-point integers less than 1e16,
5061
 *     we use floating-point arithmetic rather than resorting
5062
 *     to multiple-precision integers.
5063
 *  6. When asked to produce fewer than 15 digits, we first try
5064
 *     to get by with floating-point arithmetic; we resort to
5065
 *     multiple-precision integer arithmetic only if we cannot
5066
 *     guarantee that the floating-point calculation has given
5067
 *     the correctly rounded result.  For k requested digits and
5068
 *     "uniformly" distributed input, the probability is
5069
 *     something like 10^(k-15) that we must resort to the Long
5070
 *     calculation.
5071
 */
5072
5073
 char *
5074
dtoa_r(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve, char *buf, size_t blen __XS__d)
5075
13.5M
{
5076
 /* Arguments ndigits, decpt, sign are similar to those
5077
  of ecvt and fcvt; trailing zeros are suppressed from
5078
  the returned string.  If not null, *rve is set to point
5079
  to the end of the return value.  If d is +-Infinity or NaN,
5080
  then *decpt is set to 9999.
5081
5082
  mode:
5083
    0 ==> shortest string that yields d when read in
5084
      and rounded to nearest.
5085
    1 ==> like 0, but with Steele & White stopping rule;
5086
      e.g. with IEEE P754 arithmetic , mode 0 gives
5087
      1e23 whereas mode 1 gives 9.999999999999999e22.
5088
    2 ==> max(1,ndigits) significant digits.  This gives a
5089
      return value similar to that of ecvt, except
5090
      that trailing zeros are suppressed.
5091
    3 ==> through ndigits past the decimal point.  This
5092
      gives a return value similar to that from fcvt,
5093
      except that trailing zeros are suppressed, and
5094
      ndigits can be negative.
5095
    4,5 ==> similar to 2 and 3, respectively, but (in
5096
      round-nearest mode) with the tests of mode 0 to
5097
      possibly return a shorter string that rounds to d.
5098
      With IEEE arithmetic and compilation with
5099
      -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
5100
      as modes 2 and 3 when FLT_ROUNDS != 1.
5101
    6-9 ==> Debugging modes similar to mode - 4:  don't try
5102
      fast floating-point estimate (if applicable).
5103
5104
    Values of mode other than 0-9 are treated as mode 0.
5105
5106
  When not NULL, buf is an output buffer of length blen, which must
5107
  be large enough to accommodate suppressed trailing zeros and a trailing
5108
  null byte.  If blen is too small, rv = NULL is returned, in which case
5109
  if rve is not NULL, a subsequent call with blen >= (*rve - rv) + 1
5110
  should succeed in returning buf.
5111
5112
  When buf is NULL, sufficient space is allocated for the return value,
5113
  which, when done using, the caller should pass to freedtoa().
5114
5115
  USE_BF is automatically defined when neither NO_LONG_LONG nor NO_BF96
5116
  is defined.
5117
  */
5118
5119
#ifdef MULTIPLE_THREADS
5120
  ThInfo *TI = 0;
5121
#endif
5122
13.5M
  int bbits, b2, b5, be, dig, i, ilim, ilim1,
5123
13.5M
    j, j1, k, leftright, m2, m5, s2, s5, spec_case;
5124
13.5M
#ifndef Sudden_Underflow
5125
13.5M
  int denorm;
5126
13.5M
#endif
5127
13.5M
  Bigint *b, *b1, *delta, *mlo, *mhi, *S;
5128
13.5M
  U u;
5129
13.5M
  char *s;
5130
#ifdef SET_INEXACT
5131
  int inexact, oldinexact;
5132
#endif
5133
#ifdef USE_BF96 /*{{*/
5134
  BF96 *p10;
5135
  ULLong dbhi, dbits, dblo, den, hb, rb, rblo, res, res0, res3, reslo, sres,
5136
    sulp, tv0, tv1, tv2, tv3, ulp, ulplo, ulpmask, ures, ureslo, zb;
5137
  int eulp, k1, n2, ulpadj, ulpshift;
5138
#else /*}{*/
5139
13.5M
#ifndef Sudden_Underflow
5140
13.5M
  ULong x;
5141
13.5M
#endif
5142
13.5M
  Long L;
5143
13.5M
  U d2, eps;
5144
13.5M
  double ds;
5145
13.5M
  int ieps, ilim0, k0, k_check, try_quick;
5146
13.5M
#ifndef No_leftright
5147
13.5M
#ifdef IEEE_Arith
5148
13.5M
  U eps1;
5149
13.5M
#endif
5150
13.5M
#endif
5151
13.5M
#endif /*}}*/
5152
#ifdef Honor_FLT_ROUNDS /*{*/
5153
  int Rounding;
5154
#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
5155
  Rounding = Flt_Rounds;
5156
#else /*}{*/
5157
  Rounding = 1;
5158
  switch(fegetround()) {
5159
    case FE_TOWARDZERO: Rounding = 0; break;
5160
    case FE_UPWARD: Rounding = 2; break;
5161
    case FE_DOWNWARD: Rounding = 3;
5162
    }
5163
#endif /*}}*/
5164
#endif /*}*/
5165
5166
13.5M
  u.d = dd;
5167
13.5M
  if (word0(&u) & Sign_bit) {
5168
    /* set sign for everything, including 0's and NaNs */
5169
1.35M
    *sign = 1;
5170
1.35M
    word0(&u) &= ~Sign_bit; /* clear sign bit */
5171
1.35M
    }
5172
12.1M
  else
5173
12.1M
    *sign = 0;
5174
5175
13.5M
#if defined(IEEE_Arith) + defined(VAX)
5176
13.5M
#ifdef IEEE_Arith
5177
13.5M
  if ((word0(&u) & Exp_mask) == Exp_mask)
5178
#else
5179
  if (word0(&u)  == 0x8000)
5180
#endif
5181
1.56M
    {
5182
    /* Infinity or NaN */
5183
1.56M
    *decpt = 9999;
5184
1.56M
#ifdef IEEE_Arith
5185
1.56M
    if (!word1(&u) && !(word0(&u) & 0xfffff))
5186
537k
      return nrv_alloc("Infinity", buf, blen, rve, 8 MTb);
5187
1.02M
#endif
5188
1.02M
    *sign = 0; //NaN doesn't have a sign
5189
1.02M
    return nrv_alloc("NaN", buf, blen, rve, 3 MTb);
5190
1.56M
    }
5191
11.9M
#endif
5192
#ifdef IBM
5193
  dval(&u) += 0; /* normalize */
5194
#endif
5195
11.9M
  if (!dval(&u)) {
5196
571k
    *decpt = 1;
5197
571k
    return nrv_alloc("0", buf, blen, rve, 1 MTb);
5198
571k
    }
5199
5200
#ifdef SET_INEXACT
5201
#ifndef USE_BF96
5202
  try_quick =
5203
#endif
5204
  oldinexact = get_inexact();
5205
  inexact = 1;
5206
#endif
5207
#ifdef Honor_FLT_ROUNDS
5208
  if (Rounding >= 2) {
5209
    if (*sign)
5210
      Rounding = Rounding == 2 ? 0 : 2;
5211
    else
5212
      if (Rounding != 2)
5213
        Rounding = 0;
5214
    }
5215
#endif
5216
#ifdef USE_BF96 /*{{*/
5217
  dbits = (u.LL & 0xfffffffffffffull) << 11;  /* fraction bits */
5218
  if ((be = u.LL >> 52)) /* biased exponent; nonzero ==> normal */ {
5219
    dbits |= 0x8000000000000000ull;
5220
    denorm = ulpadj = 0;
5221
    }
5222
  else {
5223
    denorm = 1;
5224
    ulpadj = be + 1;
5225
    dbits <<= 1;
5226
    if (!(dbits & 0xffffffff00000000ull)) {
5227
      dbits <<= 32;
5228
      be -= 32;
5229
      }
5230
    if (!(dbits & 0xffff000000000000ull)) {
5231
      dbits <<= 16;
5232
      be -= 16;
5233
      }
5234
    if (!(dbits & 0xff00000000000000ull)) {
5235
      dbits <<= 8;
5236
      be -= 8;
5237
      }
5238
    if (!(dbits & 0xf000000000000000ull)) {
5239
      dbits <<= 4;
5240
      be -= 4;
5241
      }
5242
    if (!(dbits & 0xc000000000000000ull)) {
5243
      dbits <<= 2;
5244
      be -= 2;
5245
      }
5246
    if (!(dbits & 0x8000000000000000ull)) {
5247
      dbits <<= 1;
5248
      be -= 1;
5249
      }
5250
    assert(be >= -51);
5251
    ulpadj -= be;
5252
    }
5253
  j = Lhint[be + 51];
5254
  p10 = &pten[j];
5255
  dbhi = dbits >> 32;
5256
  dblo = dbits & 0xffffffffull;
5257
  i = be - 0x3fe;
5258
  if (i < p10->e
5259
  || (i == p10->e && (dbhi < p10->b0 || (dbhi == p10->b0 && dblo < p10->b1))))
5260
    --j;
5261
  k = j - 342;
5262
5263
  /* now 10^k <= dd < 10^(k+1) */
5264
5265
#else /*}{*/
5266
5267
11.3M
  b = d2b(&u, &be, &bbits MTb);
5268
#ifdef Sudden_Underflow
5269
  i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
5270
#else
5271
11.3M
  if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
5272
11.3M
#endif
5273
11.3M
    dval(&d2) = dval(&u);
5274
11.3M
    word0(&d2) &= Frac_mask1;
5275
11.3M
    word0(&d2) |= Exp_11;
5276
#ifdef IBM
5277
    if (j = 11 - hi0bits(word0(&d2) & Frac_mask))
5278
      dval(&d2) /= 1 << j;
5279
#endif
5280
5281
    /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
5282
     * log10(x)  =  log(x) / log(10)
5283
     *    ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
5284
     * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
5285
     *
5286
     * This suggests computing an approximation k to log10(d) by
5287
     *
5288
     * k = (i - Bias)*0.301029995663981
5289
     *  + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
5290
     *
5291
     * We want k to be too large rather than too small.
5292
     * The error in the first-order Taylor series approximation
5293
     * is in our favor, so we just round up the constant enough
5294
     * to compensate for any error in the multiplication of
5295
     * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
5296
     * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
5297
     * adding 1e-13 to the constant term more than suffices.
5298
     * Hence we adjust the constant term to 0.1760912590558.
5299
     * (We could get a more accurate k by invoking log10,
5300
     *  but this is probably not worthwhile.)
5301
     */
5302
5303
11.3M
    i -= Bias;
5304
#ifdef IBM
5305
    i <<= 2;
5306
    i += j;
5307
#endif
5308
11.3M
#ifndef Sudden_Underflow
5309
11.3M
    denorm = 0;
5310
11.3M
    }
5311
14.5k
  else {
5312
    /* d is denormalized */
5313
5314
14.5k
    i = bbits + be + (Bias + (P-1) - 1);
5315
14.5k
    x = i > 32  ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
5316
14.5k
          : word1(&u) << (32 - i);
5317
14.5k
    dval(&d2) = x;
5318
14.5k
    word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
5319
14.5k
    i -= (Bias + (P-1) - 1) + 1;
5320
14.5k
    denorm = 1;
5321
14.5k
    }
5322
11.3M
#endif
5323
11.3M
  ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
5324
11.3M
  k = (int)ds;
5325
11.3M
  if (ds < 0. && ds != k)
5326
805k
    k--; /* want k = floor(ds) */
5327
11.3M
  k_check = 1;
5328
11.3M
  if (k >= 0 && k <= Ten_pmax) {
5329
9.23M
    if (dval(&u) < tens[k])
5330
24.1k
      k--;
5331
9.23M
    k_check = 0;
5332
9.23M
    }
5333
11.3M
  j = bbits - i - 1;
5334
11.3M
  if (j >= 0) {
5335
5.37M
    b2 = 0;
5336
5.37M
    s2 = j;
5337
5.37M
    }
5338
5.99M
  else {
5339
5.99M
    b2 = -j;
5340
5.99M
    s2 = 0;
5341
5.99M
    }
5342
11.3M
  if (k >= 0) {
5343
10.5M
    b5 = 0;
5344
10.5M
    s5 = k;
5345
10.5M
    s2 += k;
5346
10.5M
    }
5347
805k
  else {
5348
805k
    b2 -= k;
5349
805k
    b5 = -k;
5350
805k
    s5 = 0;
5351
805k
    }
5352
11.3M
#endif /*}}*/
5353
11.3M
  if (mode < 0 || mode > 9)
5354
0
    mode = 0;
5355
5356
11.3M
#ifndef USE_BF96
5357
11.3M
#ifndef SET_INEXACT
5358
#ifdef Check_FLT_ROUNDS
5359
  try_quick = Rounding == 1;
5360
#else
5361
11.3M
  try_quick = 1;
5362
11.3M
#endif
5363
11.3M
#endif /*SET_INEXACT*/
5364
11.3M
#endif
5365
5366
11.3M
  if (mode > 5) {
5367
0
    mode -= 4;
5368
0
#ifndef USE_BF96
5369
0
    try_quick = 0;
5370
0
#endif
5371
0
    }
5372
11.3M
  leftright = 1;
5373
11.3M
  ilim = ilim1 = -1;  /* Values for cases 0 and 1; done here to */
5374
        /* silence erroneous "gcc -Wall" warning. */
5375
11.3M
  switch(mode) {
5376
10.9M
    case 0:
5377
10.9M
    case 1:
5378
10.9M
      i = 18;
5379
10.9M
      ndigits = 0;
5380
10.9M
      break;
5381
451k
    case 2:
5382
451k
      leftright = 0;
5383
      /* no break */
5384
451k
      mxFallThrough;
5385
451k
    case 4:
5386
451k
      if (ndigits <= 0)
5387
0
        ndigits = 1;
5388
451k
      ilim = ilim1 = i = ndigits;
5389
451k
      break;
5390
12.9k
    case 3:
5391
12.9k
      leftright = 0;
5392
      /* no break */
5393
12.9k
      mxFallThrough;
5394
12.9k
    case 5:
5395
12.9k
      i = ndigits + k + 1;
5396
12.9k
      ilim = i;
5397
12.9k
      ilim1 = i - 1;
5398
12.9k
      if (i <= 0)
5399
7.94k
        i = 1;
5400
11.3M
    }
5401
11.3M
  if (!buf) {
5402
11.3M
    buf = rv_alloc(i MTb);
5403
11.3M
    blen = sizeof(Bigint) + ((1 << ((int*)buf)[-1]) - 1)*sizeof(ULong) - sizeof(int);
5404
11.3M
    }
5405
0
  else if (blen <= (size_t)i) {
5406
0
    buf = 0;
5407
0
    if (rve)
5408
0
      *rve = buf + i;
5409
0
    return buf;
5410
0
    }
5411
11.3M
  s = buf;
5412
5413
  /* Check for special case that d is a normalized power of 2. */
5414
5415
11.3M
  spec_case = 0;
5416
11.3M
  if (mode < 2 || (leftright
5417
#ifdef Honor_FLT_ROUNDS
5418
      && Rounding == 1
5419
#endif
5420
10.9M
        )) {
5421
10.9M
    if (!word1(&u) && !(word0(&u) & Bndry_mask)
5422
1.37M
#ifndef Sudden_Underflow
5423
1.37M
     && word0(&u) & (Exp_mask & ~Exp_msk1)
5424
10.9M
#endif
5425
10.9M
        ) {
5426
      /* The special case */
5427
1.37M
      spec_case = 1;
5428
1.37M
      }
5429
10.9M
    }
5430
5431
#ifdef USE_BF96 /*{*/
5432
  b = 0;
5433
  if (ilim < 0 && (mode == 3 || mode == 5)) {
5434
    S = mhi = 0;
5435
    goto no_digits;
5436
    }
5437
  i = 1;
5438
  j = 52 + 0x3ff - be;
5439
  ulpshift = 0;
5440
  ulplo = 0;
5441
  /* Can we do an exact computation with 64-bit integer arithmetic? */
5442
  if (k < 0) {
5443
    if (k < -25)
5444
      goto toobig;
5445
    res = dbits >> 11;
5446
    n2 = pfivebits[k1 = -(k + 1)] + 53;
5447
    j1 = j;
5448
    if (n2 > 61) {
5449
      ulpshift = n2 - 61;
5450
      if (res & (ulpmask = (1ull << ulpshift) - 1))
5451
        goto toobig;
5452
      j -= ulpshift;
5453
      res >>= ulpshift;
5454
      }
5455
    /* Yes. */
5456
    res *= ulp = pfive[k1];
5457
    if (ulpshift) {
5458
      ulplo = ulp;
5459
      ulp >>= ulpshift;
5460
      }
5461
    j += k;
5462
    if (ilim == 0) {
5463
      S = mhi = 0;
5464
      if (res > (5ull << j))
5465
        goto one_digit;
5466
      goto no_digits;
5467
      }
5468
    goto no_div;
5469
    }
5470
  if (ilim == 0 && j + k >= 0) {
5471
    S = mhi = 0;
5472
    if ((dbits >> 11) > (pfive[k-1] << j))
5473
      goto one_digit;
5474
    goto no_digits;
5475
    }
5476
  if (k <= dtoa_divmax && j + k >= 0) {
5477
    /* Another "yes" case -- we will use exact integer arithmetic. */
5478
 use_exact:
5479
    Debug(++dtoa_stats[3]);
5480
    res = dbits >> 11;  /* residual */
5481
    ulp = 1;
5482
    if (k <= 0)
5483
      goto no_div;
5484
    j1 = j + k + 1;
5485
    den = pfive[k-i] << (j1 - i);
5486
    for(;;) {
5487
      dig = res / den;
5488
      *s++ = '0' + dig;
5489
      if (!(res -= dig*den)) {
5490
#ifdef SET_INEXACT
5491
        inexact = 0;
5492
        oldinexact = 1;
5493
#endif
5494
        goto retc;
5495
        }
5496
      if (ilim < 0) {
5497
        ures = den - res;
5498
        if (2*res <= ulp
5499
        && (spec_case ? 4*res <= ulp : (2*res < ulp || dig & 1)))
5500
          goto ulp_reached;
5501
        if (2*ures < ulp)
5502
          goto Roundup;
5503
        }
5504
      else if (i == ilim) {
5505
        switch(Rounding) {
5506
          case 0: goto retc;
5507
          case 2: goto Roundup;
5508
          }
5509
        ures = 2*res;
5510
        if (ures > den
5511
        || (ures == den && dig & 1)
5512
        || (spec_case && res <= ulp && 2*res >= ulp))
5513
          goto Roundup;
5514
        goto retc;
5515
        }
5516
      if (j1 < ++i) {
5517
        res *= 10;
5518
        ulp *= 10;
5519
        }
5520
      else {
5521
        if (i > k)
5522
          break;
5523
        den = pfive[k-i] << (j1 - i);
5524
        }
5525
      }
5526
 no_div:
5527
    for(;;) {
5528
      dig = den = res >> j;
5529
      *s++ = '0' + dig;
5530
      if (!(res -= den << j)) {
5531
#ifdef SET_INEXACT
5532
        inexact = 0;
5533
        oldinexact = 1;
5534
#endif
5535
        goto retc;
5536
        }
5537
      if (ilim < 0) {
5538
        ures = (1ull << j) - res;
5539
        if (2*res <= ulp
5540
        && (spec_case ? 4*res <= ulp : (2*res < ulp || dig & 1))) {
5541
 ulp_reached:
5542
          if (ures < res
5543
          || (ures == res && dig & 1))
5544
            goto Roundup;
5545
          goto retc;
5546
          }
5547
        if (2*ures < ulp)
5548
          goto Roundup;
5549
        }
5550
      --j;
5551
      if (i == ilim) {
5552
#ifdef Honor_FLT_ROUNDS
5553
        switch(Rounding) {
5554
          case 0: goto retc;
5555
          case 2: goto Roundup;
5556
          }
5557
#endif
5558
        hb = 1ull << j;
5559
        if (res & hb && (dig & 1 || res & (hb-1)))
5560
          goto Roundup;
5561
        if (spec_case && res <= ulp && 2*res >= ulp) {
5562
 Roundup:
5563
          while(*--s == '9')
5564
            if (s == buf) {
5565
              ++k;
5566
              *s++ = '1';
5567
              goto ret1;
5568
              }
5569
          ++*s++;
5570
          goto ret1;
5571
          }
5572
        goto retc;
5573
        }
5574
      ++i;
5575
      res *= 5;
5576
      if (ulpshift) {
5577
        ulplo = 5*(ulplo & ulpmask);
5578
        ulp = 5*ulp + (ulplo >> ulpshift);
5579
        }
5580
      else
5581
        ulp *= 5;
5582
      }
5583
    }
5584
 toobig:
5585
  if (ilim > 28)
5586
    goto Fast_failed1;
5587
  /* Scale by 10^-k */
5588
  p10 = &pten[342-k];
5589
  tv0 = p10->b2 * dblo; /* rarely matters, but does, e.g., for 9.862818194192001e18 */
5590
  tv1 = p10->b1 * dblo + (tv0 >> 32);
5591
  tv2 = p10->b2 * dbhi + (tv1 & 0xffffffffull);
5592
  tv3 = p10->b0 * dblo + (tv1>>32) + (tv2>>32);
5593
  res3 = p10->b1 * dbhi + (tv3 & 0xffffffffull);
5594
  res = p10->b0 * dbhi + (tv3>>32) + (res3>>32);
5595
  be += p10->e - 0x3fe;
5596
  eulp = j1 = be - 54 + ulpadj;
5597
  if (!(res & 0x8000000000000000ull)) {
5598
    --be;
5599
    res3 <<= 1;
5600
    res = (res << 1) | ((res3 & 0x100000000ull) >> 32);
5601
    }
5602
  res0 = res; /* save for Fast_failed */
5603
#if !defined(SET_INEXACT) && !defined(NO_DTOA_64) /*{*/
5604
  if (ilim > 19)
5605
    goto Fast_failed;
5606
  Debug(++dtoa_stats[4]);
5607
  assert(be >= 0 && be <= 4); /* be = 0 is rare, but possible, e.g., for 1e20 */
5608
  res >>= 4 - be;
5609
  ulp = p10->b0;  /* ulp */
5610
  ulp = (ulp << 29) | (p10->b1 >> 3);
5611
  /* scaled ulp = ulp * 2^(eulp - 60) */
5612
  /* We maintain 61 bits of the scaled ulp. */
5613
  if (ilim == 0) {
5614
    if (!(res & 0x7fffffffffffffeull)
5615
     || !((~res) & 0x7fffffffffffffeull))
5616
      goto Fast_failed1;
5617
    S = mhi = 0;
5618
    if (res >= 0x5000000000000000ull)
5619
      goto one_digit;
5620
    goto no_digits;
5621
    }
5622
  rb = 1; /* upper bound on rounding error */
5623
  for(;;++i) {
5624
    dig = res >> 60;
5625
    *s++ = '0' + dig;
5626
    res &= 0xfffffffffffffffull;
5627
    if (ilim < 0) {
5628
      ures = 0x1000000000000000ull - res;
5629
      if (eulp > 0) {
5630
        assert(eulp <= 4);
5631
        sulp = ulp << (eulp - 1);
5632
        if (res <= ures) {
5633
          if (res + rb > ures - rb)
5634
            goto Fast_failed;
5635
          if (res < sulp)
5636
            goto retc;
5637
          }
5638
        else {
5639
          if (res - rb <= ures + rb)
5640
            goto Fast_failed;
5641
          if (ures < sulp)
5642
            goto Roundup;
5643
          }
5644
        }
5645
      else {
5646
        zb = -(1ull << (eulp + 63));
5647
        if (!(zb & res)) {
5648
          sres = res << (1 - eulp);
5649
          if (sres < ulp && (!spec_case || 2*sres < ulp)) {
5650
            if ((res+rb) << (1 - eulp) >= ulp)
5651
              goto Fast_failed;
5652
            if (ures < res) {
5653
              if (ures + rb >= res - rb)
5654
                goto Fast_failed;
5655
              goto Roundup;
5656
              }
5657
            if (ures - rb < res + rb)
5658
              goto Fast_failed;
5659
            goto retc;
5660
            }
5661
          }
5662
        if (!(zb & ures) && ures << -eulp < ulp) {
5663
          if (ures << (1 - eulp) < ulp)
5664
            goto  Roundup;
5665
          goto Fast_failed;
5666
          }
5667
        }
5668
      }
5669
    else if (i == ilim) {
5670
      ures = 0x1000000000000000ull - res;
5671
      if (ures < res) {
5672
        if (ures <= rb || res - rb <= ures + rb) {
5673
          if (j + k >= 0 && k >= 0 && k <= 27)
5674
            goto use_exact1;
5675
          goto Fast_failed;
5676
          }
5677
#ifdef Honor_FLT_ROUNDS
5678
        if (Rounding == 0)
5679
          goto retc;
5680
#endif
5681
        goto Roundup;
5682
        }
5683
      if (res <= rb || ures - rb <= res + rb) {
5684
        if (j + k >= 0 && k >= 0 && k <= 27) {
5685
 use_exact1:
5686
          s = buf;
5687
          i = 1;
5688
          goto use_exact;
5689
          }
5690
        goto Fast_failed;
5691
        }
5692
#ifdef Honor_FLT_ROUNDS
5693
      if (Rounding == 2)
5694
        goto Roundup;
5695
#endif
5696
      goto retc;
5697
      }
5698
    rb *= 10;
5699
    if (rb >= 0x1000000000000000ull)
5700
      goto Fast_failed;
5701
    res *= 10;
5702
    ulp *= 5;
5703
    if (ulp & 0x8000000000000000ull) {
5704
      eulp += 4;
5705
      ulp >>= 3;
5706
      }
5707
    else {
5708
      eulp += 3;
5709
      ulp >>= 2;
5710
      }
5711
    }
5712
#endif /*}*/
5713
#ifndef NO_BF96
5714
 Fast_failed:
5715
#endif
5716
  Debug(++dtoa_stats[5]);
5717
  s = buf;
5718
  i = 4 - be;
5719
  res = res0 >> i;
5720
  reslo = 0xffffffffull & res3;
5721
  if (i)
5722
    reslo = (res0 << (64 - i)) >> 32 | (reslo >> i);
5723
  rb = 0;
5724
  rblo = 4; /* roundoff bound */
5725
  ulp = p10->b0;  /* ulp */
5726
  ulp = (ulp << 29) | (p10->b1 >> 3);
5727
  eulp = j1;
5728
  for(i = 1;;++i) {
5729
    dig = res >> 60;
5730
    *s++ = '0' + dig;
5731
    res &= 0xfffffffffffffffull;
5732
#ifdef SET_INEXACT
5733
    if (!res && !reslo) {
5734
      if (!(res3 & 0xffffffffull)) {
5735
        inexact = 0;
5736
        oldinexact = 1;
5737
        }
5738
      goto retc;
5739
      }
5740
#endif
5741
    if (ilim < 0) {
5742
      ures = 0x1000000000000000ull - res;
5743
      ureslo = 0;
5744
      if (reslo) {
5745
        ureslo = 0x100000000ull - reslo;
5746
        --ures;
5747
        }
5748
      if (eulp > 0) {
5749
        assert(eulp <= 4);
5750
        sulp = (ulp << (eulp - 1)) - rb;
5751
        if (res <= ures) {
5752
          if (res < sulp) {
5753
            if (res+rb < ures-rb)
5754
              goto retc;
5755
            }
5756
          }
5757
        else if (ures < sulp) {
5758
          if (res-rb > ures+rb)
5759
            goto Roundup;
5760
          }
5761
        goto Fast_failed1;
5762
        }
5763
      else {
5764
        zb = -(1ull << (eulp + 60));
5765
        if (!(zb & (res + rb))) {
5766
          sres = (res - rb) << (1 - eulp);
5767
          if (sres < ulp && (!spec_case || 2*sres < ulp)) {
5768
            sres = res << (1 - eulp);
5769
            if ((j = eulp + 31) > 0)
5770
              sres += (rblo + reslo) >> j;
5771
            else
5772
              sres += (rblo + reslo) << -j;
5773
            if (sres + (rb << (1 - eulp)) >= ulp)
5774
              goto Fast_failed1;
5775
            if (sres >= ulp)
5776
              goto more96;
5777
            if (ures < res
5778
            || (ures == res && ureslo < reslo)) {
5779
              if (ures + rb >= res - rb)
5780
                goto Fast_failed1;
5781
              goto Roundup;
5782
              }
5783
            if (ures - rb <= res + rb)
5784
              goto Fast_failed1;
5785
            goto retc;
5786
            }
5787
          }
5788
        if (!(zb & ures) && (ures-rb) << (1 - eulp) < ulp) {
5789
          if ((ures + rb) << (1 - eulp) < ulp)
5790
            goto Roundup;
5791
          goto Fast_failed1;
5792
          }
5793
        }
5794
      }
5795
    else if (i == ilim) {
5796
      ures = 0x1000000000000000ull - res;
5797
      sres = ureslo = 0;
5798
      if (reslo) {
5799
        ureslo = 0x100000000ull - reslo;
5800
        --ures;
5801
        sres = (reslo + rblo) >> 31;
5802
        }
5803
      sres += 2*rb;
5804
      if (ures <= res) {
5805
        if (ures <=sres || res - ures <= sres)
5806
          goto Fast_failed1;
5807
#ifdef Honor_FLT_ROUNDS
5808
        if (Rounding == 0)
5809
          goto retc;
5810
#endif
5811
        goto Roundup;
5812
        }
5813
      if (res <= sres || ures - res <= sres)
5814
        goto Fast_failed1;
5815
#ifdef Honor_FLT_ROUNDS
5816
      if (Rounding == 2)
5817
        goto Roundup;
5818
#endif
5819
      goto retc;
5820
      }
5821
 more96:
5822
    rblo *= 10;
5823
    rb = 10*rb + (rblo >> 32);
5824
    rblo &= 0xffffffffull;
5825
    if (rb >= 0x1000000000000000ull)
5826
      goto Fast_failed1;
5827
    reslo *= 10;
5828
    res = 10*res + (reslo >> 32);
5829
    reslo &= 0xffffffffull;
5830
    ulp *= 5;
5831
    if (ulp & 0x8000000000000000ull) {
5832
      eulp += 4;
5833
      ulp >>= 3;
5834
      }
5835
    else {
5836
      eulp += 3;
5837
      ulp >>= 2;
5838
      }
5839
    }
5840
 Fast_failed1:
5841
  Debug(++dtoa_stats[6]);
5842
  S = mhi = mlo = 0;
5843
#ifdef USE_BF96
5844
  b = d2b(&u, &be, &bbits MTb);
5845
#endif
5846
  s = buf;
5847
  i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
5848
  i -= Bias;
5849
  if (ulpadj)
5850
    i -= ulpadj - 1;
5851
  j = bbits - i - 1;
5852
  if (j >= 0) {
5853
    b2 = 0;
5854
    s2 = j;
5855
    }
5856
  else {
5857
    b2 = -j;
5858
    s2 = 0;
5859
    }
5860
  if (k >= 0) {
5861
    b5 = 0;
5862
    s5 = k;
5863
    s2 += k;
5864
    }
5865
  else {
5866
    b2 -= k;
5867
    b5 = -k;
5868
    s5 = 0;
5869
    }
5870
#endif /*}*/
5871
5872
#ifdef Honor_FLT_ROUNDS
5873
  if (mode > 1 && Rounding != 1)
5874
    leftright = 0;
5875
#endif
5876
5877
11.3M
#ifndef USE_BF96 /*{*/
5878
11.3M
  if (ilim >= 0 && ilim <= Quick_max && try_quick) {
5879
5880
    /* Try to get by with floating-point arithmetic. */
5881
5882
457k
    i = 0;
5883
457k
    dval(&d2) = dval(&u);
5884
457k
    j1 = -(k0 = k);
5885
457k
    ilim0 = ilim;
5886
457k
    ieps = 2; /* conservative */
5887
457k
    if (k > 0) {
5888
290k
      ds = tens[k&0xf];
5889
290k
      j = k >> 4;
5890
290k
      if (j & Bletch) {
5891
        /* prevent overflows */
5892
152k
        j &= Bletch - 1;
5893
152k
        dval(&u) /= bigtens[n_bigtens-1];
5894
152k
        ieps++;
5895
152k
        }
5896
548k
      for(; j; j >>= 1, i++)
5897
257k
        if (j & 1) {
5898
195k
          ieps++;
5899
195k
          ds *= bigtens[i];
5900
195k
          }
5901
290k
      dval(&u) /= ds;
5902
290k
      }
5903
166k
    else if (j1 > 0) {
5904
124k
      dval(&u) *= tens[j1 & 0xf];
5905
294k
      for(j = j1 >> 4; j; j >>= 1, i++)
5906
169k
        if (j & 1) {
5907
169k
          ieps++;
5908
169k
          dval(&u) *= bigtens[i];
5909
169k
          }
5910
124k
      }
5911
457k
    if (k_check && dval(&u) < 1. && ilim > 0) {
5912
591
      if (ilim1 <= 0)
5913
1
        goto fast_failed;
5914
590
      ilim = ilim1;
5915
590
      k--;
5916
590
      dval(&u) *= 10.;
5917
590
      ieps++;
5918
590
      }
5919
457k
    dval(&eps) = ieps*dval(&u) + 7.;
5920
457k
    word0(&eps) -= (P-1)*Exp_msk1;
5921
457k
    if (ilim == 0) {
5922
5.74k
      S = mhi = 0;
5923
5.74k
      dval(&u) -= 5.;
5924
5.74k
      if (dval(&u) > dval(&eps))
5925
3.50k
        goto one_digit;
5926
2.24k
      if (dval(&u) < -dval(&eps))
5927
2.23k
        goto no_digits;
5928
3
      goto fast_failed;
5929
2.24k
      }
5930
451k
#ifndef No_leftright
5931
451k
    if (leftright) {
5932
      /* Use Steele & White method of only
5933
       * generating digits needed.
5934
       */
5935
0
      dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
5936
0
#ifdef IEEE_Arith
5937
0
      if (j1 >= 307) {
5938
0
        eps1.d = 1.01e256; /* 1.01 allows roundoff in the next few lines */
5939
0
        word0(&eps1) -= Exp_msk1 * (Bias+P-1);
5940
0
        dval(&eps1) *= tens[j1 & 0xf];
5941
0
        for(i = 0, j = (j1-256) >> 4; j; j >>= 1, i++)
5942
0
          if (j & 1)
5943
0
            dval(&eps1) *= bigtens[i];
5944
0
        if (eps.d < eps1.d)
5945
0
          eps.d = eps1.d;
5946
0
        if (10. - u.d < 10.*eps.d && eps.d < 1.) {
5947
          /* eps.d < 1. excludes trouble with the tiniest denormal */
5948
0
          *s++ = '1';
5949
0
          ++k;
5950
0
          goto ret1;
5951
0
          }
5952
0
        }
5953
0
#endif
5954
0
      for(i = 0;;) {
5955
0
        L = (int)dval(&u);
5956
0
        dval(&u) -= L;
5957
0
        *s++ = '0' + (int)L;
5958
0
        if (1. - dval(&u) < dval(&eps))
5959
0
          goto bump_up;
5960
0
        if (dval(&u) < dval(&eps))
5961
0
          goto retc;
5962
0
        if (++i >= ilim)
5963
0
          break;
5964
0
        dval(&eps) *= 10.;
5965
0
        dval(&u) *= 10.;
5966
0
        }
5967
0
      }
5968
451k
    else {
5969
451k
#endif
5970
      /* Generate ilim digits, then fix them up. */
5971
451k
      dval(&eps) *= tens[ilim-1];
5972
1.03M
      for(i = 1;; i++, dval(&u) *= 10.) {
5973
1.03M
        L = (Long)(dval(&u));
5974
1.03M
        if (!(dval(&u) -= L))
5975
42.5k
          ilim = i;
5976
1.03M
        *s++ = '0' + (int)L;
5977
1.03M
        if (i == ilim) {
5978
451k
          if (dval(&u) > 0.5 + dval(&eps))
5979
179k
            goto bump_up;
5980
271k
          else if (dval(&u) < 0.5 - dval(&eps))
5981
271k
            goto retc;
5982
631
          break;
5983
451k
          }
5984
1.03M
        }
5985
451k
#ifndef No_leftright
5986
451k
      }
5987
631
#endif
5988
635
 fast_failed:
5989
635
    s = buf;
5990
635
    dval(&u) = dval(&d2);
5991
635
    k = k0;
5992
635
    ilim = ilim0;
5993
635
    }
5994
5995
  /* Do we have a "small" integer? */
5996
5997
10.9M
  if (be >= 0 && k <= Int_max) {
5998
    /* Yes. */
5999
7.75M
    ds = tens[k];
6000
7.75M
    if (ndigits < 0 && ilim <= 0) {
6001
0
      S = mhi = 0;
6002
0
      if (ilim < 0 || dval(&u) <= 5*ds)
6003
0
        goto no_digits;
6004
0
      goto one_digit;
6005
0
      }
6006
21.3M
    for(i = 1;; i++, dval(&u) *= 10.) {
6007
21.3M
      L = (Long)(dval(&u) / ds);
6008
21.3M
      dval(&u) -= L*ds;
6009
#ifdef Check_FLT_ROUNDS
6010
      /* If FLT_ROUNDS == 2, L will usually be high by 1 */
6011
      if (dval(&u) < 0) {
6012
        L--;
6013
        dval(&u) += ds;
6014
        }
6015
#endif
6016
21.3M
      *s++ = '0' + (int)L;
6017
21.3M
      if (!dval(&u)) {
6018
#ifdef SET_INEXACT
6019
        inexact = 0;
6020
#endif
6021
7.75M
        break;
6022
7.75M
        }
6023
13.6M
      if (i == ilim) {
6024
#ifdef Honor_FLT_ROUNDS
6025
        if (mode > 1)
6026
        switch(Rounding) {
6027
          case 0: goto retc;
6028
          case 2: goto bump_up;
6029
          }
6030
#endif
6031
495
        dval(&u) += dval(&u);
6032
495
#ifdef ROUND_BIASED
6033
495
        if (dval(&u) >= ds)
6034
#else
6035
        if (dval(&u) > ds || (dval(&u) == ds && L & 1))
6036
#endif
6037
495
          {
6038
179k
 bump_up:
6039
195k
          while(*--s == '9')
6040
16.6k
            if (s == buf) {
6041
742
              k++;
6042
742
              *s = '0';
6043
742
              break;
6044
742
              }
6045
179k
          ++*s++;
6046
179k
          }
6047
179k
        break;
6048
495
        }
6049
13.6M
      }
6050
7.93M
    goto retc;
6051
7.75M
    }
6052
6053
3.16M
#endif /*}*/
6054
3.16M
  m2 = b2;
6055
3.16M
  m5 = b5;
6056
3.16M
  mhi = mlo = 0;
6057
3.16M
  if (leftright) {
6058
3.15M
    i =
6059
3.15M
#ifndef Sudden_Underflow
6060
3.15M
      denorm ? be + (Bias + (P-1) - 1 + 1) :
6061
3.15M
#endif
6062
#ifdef IBM
6063
      1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
6064
#else
6065
3.15M
      1 + P - bbits;
6066
3.15M
#endif
6067
3.15M
    b2 += i;
6068
3.15M
    s2 += i;
6069
3.15M
    mhi = i2b(1 MTb);
6070
3.15M
    }
6071
3.16M
  if (m2 > 0 && s2 > 0) {
6072
2.41M
    i = m2 < s2 ? m2 : s2;
6073
2.41M
    b2 -= i;
6074
2.41M
    m2 -= i;
6075
2.41M
    s2 -= i;
6076
2.41M
    }
6077
3.16M
  if (b5 > 0) {
6078
680k
    if (leftright) {
6079
676k
      if (m5 > 0) {
6080
676k
        mhi = pow5mult(mhi, m5 MTb);
6081
676k
        b1 = mult(mhi, b MTb);
6082
676k
        Bfree(b MTb);
6083
676k
        b = b1;
6084
676k
        }
6085
676k
      if ((j = b5 - m5))
6086
0
        b = pow5mult(b, j MTb);
6087
676k
      }
6088
4.78k
    else
6089
4.78k
      b = pow5mult(b, b5 MTb);
6090
680k
    }
6091
3.16M
  S = i2b(1 MTb);
6092
3.16M
  if (s5 > 0)
6093
2.39M
    S = pow5mult(S, s5 MTb);
6094
6095
3.16M
  if (spec_case) {
6096
726k
    b2 += Log2P;
6097
726k
    s2 += Log2P;
6098
726k
    }
6099
6100
  /* Arrange for convenient computation of quotients:
6101
   * shift left if necessary so divisor has 4 leading 0 bits.
6102
   *
6103
   * Perhaps we should just compute leading 28 bits of S once
6104
   * and for all and pass them and a shift to quorem, so it
6105
   * can do shifts and ors to compute the numerator for q.
6106
   */
6107
3.16M
  i = dshift(S, s2);
6108
3.16M
  b2 += i;
6109
3.16M
  m2 += i;
6110
3.16M
  s2 += i;
6111
3.16M
  if (b2 > 0)
6112
3.16M
    b = lshift(b, b2 MTb);
6113
3.16M
  if (s2 > 0)
6114
3.15M
    S = lshift(S, s2 MTb);
6115
3.16M
#ifndef USE_BF96
6116
3.16M
  if (k_check) {
6117
1.81M
    if (cmp(b,S) < 0) {
6118
85.2k
      k--;
6119
85.2k
      b = multadd(b, 10, 0 MTb);  /* we botched the k estimate */
6120
85.2k
      if (leftright)
6121
83.4k
        mhi = multadd(mhi, 10, 0 MTb);
6122
85.2k
      ilim = ilim1;
6123
85.2k
      }
6124
1.81M
    }
6125
3.16M
#endif
6126
3.16M
  if (ilim <= 0 && (mode == 3 || mode == 5)) {
6127
2.20k
    if (ilim < 0 || cmp(b,S = multadd(S,5,0 MTb)) <= 0) {
6128
      /* no digits, fcvt style */
6129
4.44k
 no_digits:
6130
4.44k
      k = -1 - ndigits;
6131
4.44k
      goto ret;
6132
2.20k
      }
6133
3.50k
 one_digit:
6134
3.50k
    *s++ = '1';
6135
3.50k
    ++k;
6136
3.50k
    goto ret;
6137
2.20k
    }
6138
3.16M
  if (leftright) {
6139
3.15M
    if (m2 > 0)
6140
3.13M
      mhi = lshift(mhi, m2 MTb);
6141
6142
    /* Compute mlo -- check for special case
6143
     * that d is a normalized power of 2.
6144
     */
6145
6146
3.15M
    mlo = mhi;
6147
3.15M
    if (spec_case) {
6148
726k
      mhi = Balloc(mhi->k MTb);
6149
726k
      Bcopy(mhi, mlo);
6150
726k
      mhi = lshift(mhi, Log2P MTb);
6151
726k
      }
6152
6153
45.9M
    for(i = 1;;i++) {
6154
45.9M
      dig = quorem(b,S) + '0';
6155
      /* Do we yet have the shortest decimal string
6156
       * that will round to d?
6157
       */
6158
45.9M
      j = cmp(b, mlo);
6159
45.9M
      delta = diff(S, mhi MTb);
6160
45.9M
      j1 = delta->sign ? 1 : cmp(b, delta);
6161
45.9M
      Bfree(delta MTb);
6162
#ifndef ROUND_BIASED
6163
      if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
6164
#ifdef Honor_FLT_ROUNDS
6165
        && (mode <= 1 || Rounding >= 1)
6166
#endif
6167
                   ) {
6168
        if (dig == '9')
6169
          goto round_9_up;
6170
        if (j > 0)
6171
          dig++;
6172
#ifdef SET_INEXACT
6173
        else if (!b->x[0] && b->wds <= 1)
6174
          inexact = 0;
6175
#endif
6176
        *s++ = dig;
6177
        goto ret;
6178
        }
6179
#endif
6180
45.9M
      if (j < 0 || (j == 0 && mode != 1
6181
#ifndef ROUND_BIASED
6182
              && !(word1(&u) & 1)
6183
#endif
6184
43.8M
          )) {
6185
2.12M
        if (!b->x[0] && b->wds <= 1) {
6186
#ifdef SET_INEXACT
6187
          inexact = 0;
6188
#endif
6189
397k
          goto accept_dig;
6190
397k
          }
6191
#ifdef Honor_FLT_ROUNDS
6192
        if (mode > 1)
6193
         switch(Rounding) {
6194
          case 0: goto accept_dig;
6195
          case 2: goto keep_dig;
6196
          }
6197
#endif /*Honor_FLT_ROUNDS*/
6198
1.73M
        if (j1 > 0) {
6199
961k
          b = lshift(b, 1 MTb);
6200
961k
          j1 = cmp(b, S);
6201
961k
#ifdef ROUND_BIASED
6202
961k
          if (j1 >= 0 /*)*/
6203
#else
6204
          if ((j1 > 0 || (j1 == 0 && dig & 1))
6205
#endif
6206
430k
          && dig++ == '9')
6207
116
            goto round_9_up;
6208
961k
          }
6209
2.12M
 accept_dig:
6210
2.12M
        *s++ = dig;
6211
2.12M
        goto ret;
6212
1.73M
        }
6213
43.8M
      if (j1 > 0) {
6214
#ifdef Honor_FLT_ROUNDS
6215
        if (!Rounding && mode > 1)
6216
          goto accept_dig;
6217
#endif
6218
1.02M
        if (dig == '9') { /* possible if i == 1 */
6219
38.2k
 round_9_up:
6220
38.2k
          *s++ = '9';
6221
38.2k
          goto roundoff;
6222
38.1k
          }
6223
988k
        *s++ = dig + 1;
6224
988k
        goto ret;
6225
1.02M
        }
6226
#ifdef Honor_FLT_ROUNDS
6227
 keep_dig:
6228
#endif
6229
42.8M
      *s++ = dig;
6230
42.8M
      if (i == ilim)
6231
0
        break;
6232
42.8M
      b = multadd(b, 10, 0 MTb);
6233
42.8M
      if (mlo == mhi)
6234
31.7M
        mlo = mhi = multadd(mhi, 10, 0 MTb);
6235
11.0M
      else {
6236
11.0M
        mlo = multadd(mlo, 10, 0 MTb);
6237
11.0M
        mhi = multadd(mhi, 10, 0 MTb);
6238
11.0M
        }
6239
42.8M
      }
6240
3.15M
    }
6241
5.33k
  else
6242
116k
    for(i = 1;; i++) {
6243
116k
      dig = quorem(b,S) + '0';
6244
116k
      *s++ = dig;
6245
116k
      if (!b->x[0] && b->wds <= 1) {
6246
#ifdef SET_INEXACT
6247
        inexact = 0;
6248
#endif
6249
1.13k
        goto ret;
6250
1.13k
        }
6251
115k
      if (i >= ilim)
6252
4.19k
        break;
6253
111k
      b = multadd(b, 10, 0 MTb);
6254
111k
      }
6255
6256
  /* Round off last digit */
6257
6258
#ifdef Honor_FLT_ROUNDS
6259
  if (mode > 1)
6260
    switch(Rounding) {
6261
      case 0: goto ret;
6262
      case 2: goto roundoff;
6263
      }
6264
#endif
6265
4.19k
  b = lshift(b, 1 MTb);
6266
4.19k
  j = cmp(b, S);
6267
4.19k
#ifdef ROUND_BIASED
6268
4.19k
  if (j >= 0)
6269
#else
6270
  if (j > 0 || (j == 0 && dig & 1))
6271
#endif
6272
2.08k
    {
6273
40.3k
 roundoff:
6274
40.6k
    while(*--s == '9')
6275
38.5k
      if (s == buf) {
6276
38.2k
        k++;
6277
38.2k
        *s++ = '1';
6278
38.2k
        goto ret;
6279
38.2k
        }
6280
2.08k
    ++*s++;
6281
2.08k
    }
6282
3.16M
 ret:
6283
3.16M
  Bfree(S MTb);
6284
3.16M
  if (mhi) {
6285
3.15M
    if (mlo && mlo != mhi)
6286
726k
      Bfree(mlo MTb);
6287
3.15M
    Bfree(mhi MTb);
6288
3.15M
    }
6289
11.3M
 retc:
6290
11.3M
  while(s > buf && s[-1] == '0')
6291
18.5k
    --s;
6292
11.3M
 ret1:
6293
11.3M
  if (b)
6294
11.3M
    Bfree(b MTb);
6295
11.3M
  *s = 0;
6296
11.3M
  *decpt = k + 1;
6297
11.3M
  if (rve)
6298
11.3M
    *rve = s;
6299
#ifdef SET_INEXACT
6300
  if (inexact) {
6301
    if (!oldinexact) {
6302
      word0(&u) = Exp_1 + (70 << Exp_shift);
6303
      word1(&u) = 0;
6304
      dval(&u) += 1.;
6305
      }
6306
    }
6307
  else if (!oldinexact)
6308
    clear_inexact();
6309
#endif
6310
11.3M
  return buf;
6311
11.3M
  }
6312
6313
 char *
6314
fx_dtoa(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve __XS__d)
6315
13.5M
{
6316
  /*  Sufficient space is allocated to the return value
6317
    to hold the suppressed trailing zeros.
6318
    See dtoa_r() above for details on the other arguments.
6319
  */
6320
13.5M
#ifndef MULTIPLE_THREADS
6321
#ifndef __XS__
6322
  if (dtoa_result)
6323
    freedtoa(dtoa_result __XS__a);
6324
#endif
6325
13.5M
#endif
6326
13.5M
  return dtoa_r(dd, mode, ndigits, decpt, sign, rve, 0, 0 __XS__a);
6327
13.5M
  }
6328
6329
#ifdef __cplusplus
6330
}
6331
#endif
6332
6333
static void fxDTOACleanup(txMachine* the, ThInfo* DTOA);
6334
static void fxDTOASetup(txMachine* the, ThInfo* DTOA);
6335
6336
void fxDTOACleanup(txMachine* the, ThInfo* DTOA)
6337
29.3M
{
6338
29.3M
#if mxUseChunkHeap
6339
29.3M
  if (DTOA->dirty)
6340
4.12M
#endif
6341
4.12M
  {
6342
4.12M
    Bigint* b;
6343
4.12M
    int i, c = Kmax +1 ;
6344
37.1M
    for (i = 0; i < c; i++) {
6345
33.0M
      b = DTOA->Freelist[i];
6346
49.4M
      while(b) {
6347
16.3M
        Bigint* next = b->next;
6348
16.3M
        fxDTOAFree(b, DTOA);
6349
16.3M
        b = next;
6350
16.3M
      }
6351
33.0M
    }
6352
6353
4.12M
    b = DTOA->P5s;
6354
9.30M
    while(b) {
6355
5.17M
      Bigint* next = b->next;
6356
5.17M
      fxDTOAFree(b, DTOA);
6357
5.17M
      b = next;
6358
5.17M
    }
6359
4.12M
  }
6360
29.3M
}
6361
6362
static void* fxDTOAMalloc(size_t size, void* it)
6363
83.7M
{
6364
83.7M
  ThInfo* DTOA = it;
6365
83.7M
  txMachine* the = DTOA->the;
6366
83.7M
  void* block = C_NULL;
6367
83.7M
#if mxUseChunkHeap
6368
83.7M
  if (the) {
6369
83.7M
    if ((DTOA->current + size) <= (txByte*)(the->firstBlock->limit)) {
6370
62.2M
      block = DTOA->current;
6371
62.2M
      DTOA->current += size;
6372
62.2M
      return block;
6373
62.2M
    }
6374
83.7M
  }
6375
21.5M
  DTOA->dirty = 1;
6376
21.5M
#endif
6377
21.5M
  block = c_malloc(size);
6378
21.5M
  if (!block)
6379
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
6380
  //fprintf(stderr, "malloc %zu %p\n", size, block);
6381
21.5M
  return block;
6382
83.7M
}
6383
6384
static void fxDTOAFree(void* block, void* it)
6385
21.5M
{
6386
  //fprintf(stderr, "free %p\n", block);
6387
21.5M
#if mxUseChunkHeap
6388
21.5M
  ThInfo* DTOA = it;
6389
21.5M
  txMachine* the = DTOA->the;
6390
21.5M
    if (the) {
6391
21.5M
        if (((txByte*)(the->firstBlock->current) <= (txByte*)block) && ((txByte*)block < DTOA->current))
6392
21.9k
          return;
6393
21.5M
    }
6394
21.5M
#endif
6395
21.5M
  c_free(block);
6396
21.5M
}
6397
6398
void fxDTOASetup(txMachine* the, ThInfo* DTOA)
6399
29.3M
{
6400
29.3M
  c_memset(DTOA, 0, sizeof(ThInfo));
6401
29.3M
  if (the) {
6402
29.3M
    DTOA->the = the;
6403
29.3M
#if mxUseChunkHeap
6404
  #if 0
6405
    if ((txByte*)(the->firstBlock->limit) - (txByte*)(the->firstBlock->current) > 192)
6406
      DTOA->current = (txByte*)(the->firstBlock->limit) - 192;
6407
    else
6408
  #endif
6409
29.3M
      DTOA->current = (txByte*)(the->firstBlock->current);
6410
29.3M
#endif
6411
29.3M
  }
6412
29.3M
}
6413
6414
txString fxIntegerToString(void* the, txInteger theValue, txString theBuffer, txSize theSize)
6415
36.2M
{
6416
36.2M
  c_snprintf(theBuffer, theSize, "%d", (int)theValue);
6417
36.2M
  return theBuffer;
6418
36.2M
}
6419
6420
txInteger fxNumberToInteger(txNumber theValue)
6421
8.01M
{
6422
#if defined(pebble)
6423
  if (c_fpclassify(theValue) == C_FP_NORMAL) { 
6424
    #define MODULO 4294967296.0
6425
    txNumber aNumber = c_fmod(c_trunc(theValue), MODULO);
6426
    if (aNumber >= MODULO / 2)
6427
      aNumber -= MODULO;
6428
    else if (aNumber < -MODULO / 2)
6429
      aNumber += MODULO;
6430
    return (txInteger)aNumber;
6431
  }
6432
  return 0;
6433
#else
6434
8.01M
  int lb = c_ilogb(theValue);
6435
8.01M
  if ((C_FP_ILOGB0 == lb) || (C_FP_ILOGBNAN == lb))
6436
2.34M
    return 0;
6437
6438
5.66M
  if (lb < 31)
6439
3.07M
    return (txInteger)theValue;
6440
6441
2.58M
  if (C_INT_MAX == lb) 
6442
482k
    return 0;
6443
6444
2.10M
  theValue = c_trunc(theValue);
6445
6.54M
  #define MODULO 4294967296.0
6446
2.10M
  theValue = c_fmod(theValue, MODULO);
6447
2.10M
  if (theValue >= MODULO / 2)
6448
1.16M
    theValue -= MODULO;
6449
940k
  else if (theValue < -MODULO / 2)
6450
219k
    theValue += MODULO;
6451
6452
2.10M
  return (txInteger)theValue;
6453
2.58M
#endif
6454
2.58M
}
6455
6456
txString fxNumberToString(void* the, txNumber theValue, txString theBuffer, txSize theSize, txByte theMode, txInteger thePrecision)
6457
13.5M
{
6458
13.5M
  ThInfo DTOA;
6459
13.5M
  char* base = C_NULL;
6460
13.5M
  int mode, precision, decpt, sign, count, exponent, pad;
6461
13.5M
  char* start;
6462
13.5M
  char* stop;
6463
13.5M
  char* result;
6464
6465
13.5M
  fxDTOASetup(the, &DTOA);
6466
13.5M
  switch (theMode) {
6467
270
  case 'e':
6468
270
    if (thePrecision > 0) {
6469
239
      mode = 2;
6470
239
      precision = thePrecision;
6471
239
    }
6472
31
    else {
6473
31
      mode = 0;
6474
31
      precision = 0;
6475
31
    }
6476
270
    break;
6477
155k
  case 'f':
6478
155k
    mode = 3;
6479
155k
    precision = thePrecision;
6480
155k
    break;
6481
451k
  case 'g':
6482
451k
    mode = 2;
6483
451k
    precision = thePrecision;
6484
451k
    break;
6485
12.8M
  default:
6486
12.8M
    mode = 0;
6487
12.8M
    precision = 0;
6488
12.8M
    break;
6489
13.5M
  }
6490
13.5M
  base = start = fx_dtoa(theValue, mode, precision, &decpt, &sign, &stop, &DTOA);
6491
13.5M
  count = mxPtrDiff(stop - start);
6492
13.5M
  result = theBuffer;
6493
13.5M
  theSize--; // C string
6494
13.5M
  if (sign && theValue) {
6495
1.31M
    *result++ = '-';
6496
1.31M
    theSize--;
6497
1.31M
  }
6498
13.5M
    if (decpt != 9999) {
6499
11.9M
    switch (theMode) {
6500
270
    case 'e':
6501
270
      exponent = 1;
6502
270
      if (thePrecision > 0)
6503
239
        pad = thePrecision;
6504
31
      else
6505
31
        pad = count;
6506
270
      break;
6507
118k
    case 'f':
6508
118k
      exponent = 0;
6509
118k
      pad = thePrecision;
6510
118k
      break;
6511
451k
    case 'g':
6512
451k
      if ((decpt < -5) || (thePrecision < decpt)) {
6513
332k
        exponent = 1;
6514
332k
        pad = thePrecision; 
6515
332k
      }
6516
118k
      else {
6517
118k
        exponent = 0;
6518
118k
                pad = thePrecision - count - decpt + 1;
6519
118k
      }
6520
451k
      break;
6521
11.3M
    default:
6522
11.3M
      if ((decpt < -5) || (21 < decpt)) {
6523
1.80M
        exponent = 1;
6524
1.80M
        pad = count;  
6525
1.80M
      }
6526
9.56M
      else {
6527
9.56M
        exponent = 0;
6528
9.56M
        pad = 0;  
6529
9.56M
      }
6530
11.3M
      break;
6531
11.9M
    }
6532
11.9M
    if (exponent) {
6533
2.13M
      theSize -= 5 - pad;
6534
2.13M
      if (theSize < 0) goto error;
6535
2.13M
      *result++ = *start++;
6536
2.13M
      pad--;
6537
2.13M
      if (pad > 0) {
6538
1.95M
        *result++ = '.';
6539
27.3M
        while ((start < stop) && (pad > 0)) {
6540
25.4M
          *result++ = *start++;
6541
25.4M
          pad--;
6542
25.4M
        }
6543
1.98M
        while  (pad > 0) {
6544
28.0k
          *result++ = '0';
6545
28.0k
          pad--;
6546
28.0k
        }
6547
1.95M
      }
6548
2.13M
      *result++ = 'e';
6549
2.13M
      decpt--;
6550
2.13M
      if (decpt < 0) {
6551
607k
        *result++ = '-';
6552
607k
        decpt = 0 - decpt;
6553
607k
      }
6554
1.52M
      else
6555
1.52M
        *result++ = '+';
6556
2.13M
      pad = 1000;
6557
5.71M
      while (pad > 1) {
6558
5.60M
        if (decpt >= pad) 
6559
2.02M
          break;
6560
3.57M
        pad /= 10;
6561
3.57M
      }
6562
7.10M
      while (pad) {
6563
4.96M
        *result++ = '0' + (decpt / pad);
6564
4.96M
        decpt %= pad;
6565
4.96M
        pad /= 10;
6566
4.96M
      }
6567
2.13M
    }
6568
9.80M
    else if (decpt <= 0) {
6569
194k
      theSize -= 2 - decpt + (mxPtrDiff(stop - start));
6570
194k
      if (theSize < 0) goto error;
6571
194k
      *result++ = '0';
6572
194k
      if ((decpt < 0) || (start < stop) || (pad > 0))
6573
190k
        *result++ = '.';
6574
499k
      while (decpt < 0) {
6575
304k
        *result++ = '0';
6576
304k
        decpt++;
6577
304k
        pad--;
6578
304k
      }
6579
967k
      while (start < stop) {
6580
772k
        *result++ = *start++;
6581
772k
        pad--;
6582
772k
      }
6583
194k
      if (pad > 0) {
6584
5.20k
        theSize -= pad;
6585
5.20k
        if (theSize < 0) goto error;
6586
16.4k
        while (pad > 0) {
6587
11.1k
          *result++ = '0';
6588
11.1k
          pad--;
6589
11.1k
        }
6590
5.20k
      }
6591
194k
    }
6592
9.61M
    else if (decpt < count) {
6593
715k
      theSize -= decpt + 1 + (mxPtrDiff(stop - start));
6594
715k
      if (theSize < 0) goto error;
6595
7.14M
      while (decpt > 0) {
6596
6.43M
        *result++ = *start++;
6597
6.43M
        decpt--;
6598
6.43M
      }
6599
715k
      *result++ = '.';
6600
4.40M
      while (start < stop) {
6601
3.69M
        *result++ = *start++;
6602
3.69M
        pad--;
6603
3.69M
      }
6604
715k
      if (pad > 0) {
6605
59
        theSize -= pad;
6606
59
        if (theSize < 0) goto error;
6607
1.07k
        while (pad > 0) {
6608
1.01k
          *result++ = '0';
6609
1.01k
          pad--;
6610
1.01k
        }
6611
59
      }
6612
715k
    }
6613
8.89M
    else {
6614
8.89M
      theSize -= (mxPtrDiff(stop - start));
6615
8.89M
      if (theSize < 0) goto error;
6616
39.4M
      while (start < stop) {
6617
30.5M
        *result++ = *start++;
6618
30.5M
        decpt--;
6619
30.5M
      }
6620
8.89M
            theSize -= decpt;
6621
8.89M
            if (theSize < 0) goto error;
6622
10.4M
      while (decpt > 0) {
6623
1.53M
        *result++ = '0';
6624
1.53M
        decpt--;
6625
1.53M
      }
6626
8.89M
      if (pad > 0) {
6627
147k
        theSize -= 1 + pad;
6628
147k
        if (theSize < 0) goto error;
6629
147k
        *result++ = '.';
6630
312k
        while (pad > 0) {
6631
165k
          *result++ = '0';
6632
165k
          pad--;
6633
165k
        }
6634
147k
      }
6635
8.89M
    }
6636
11.9M
  }
6637
1.56M
  else {
6638
1.56M
    theSize -= (mxPtrDiff(stop - start));
6639
1.56M
    if (theSize < 0) goto error;
6640
8.92M
    while (start < stop)
6641
7.36M
      *result++ = *start++;
6642
1.56M
  }
6643
13.5M
error:
6644
13.5M
  *result = 0;
6645
13.5M
  if (base)
6646
13.5M
    freedtoa(base, &DTOA);
6647
13.5M
  fxDTOACleanup(the, &DTOA);
6648
13.5M
  return theBuffer;
6649
13.5M
}
6650
6651
txNumber fxStringToNumber(void* the, txString theString, txFlag whole)
6652
16.1M
{
6653
16.1M
  txNumber result = whole ? 0 : NAN;
6654
16.1M
  txString p = fxSkipSpaces(theString), q;
6655
16.1M
  char c = *p;
6656
16.1M
  if (c) {
6657
15.8M
    txU4 d = *(p + 1);
6658
15.8M
    if (whole && (c == '0') && ((d == 'B') || (d == 'b') || (d == 'O') || (d == 'o') || (d == 'X') || (d == 'x'))) {
6659
10.8k
      p += 2;
6660
10.8k
            q = p;
6661
10.8k
      if ((d == 'B') || (d == 'b')) {
6662
3.00k
        while ((c = *p)) {
6663
2.94k
          if (('0' <= c) && (c <= '1'))
6664
1.65k
            result = (result * 2) + (c - '0');
6665
1.28k
          else
6666
1.28k
            break;
6667
1.65k
          p++;
6668
1.65k
        }
6669
1.35k
      }
6670
9.45k
      else if ((d == 'O') || (d == 'o')) {
6671
3.00k
        while ((c = *p)) {
6672
2.95k
          if (('0' <= c) && (c <= '7'))
6673
264
            result = (result * 8) + (c - '0');
6674
2.68k
          else
6675
2.68k
            break;
6676
264
          p++;
6677
264
        }
6678
2.73k
      }
6679
6.72k
      else if ((d == 'X') || (d == 'x')) {
6680
11.1k
        while ((c = *p)) {
6681
10.9k
          if (('0' <= c) && (c <= '9'))
6682
2.15k
            result = (result * 16) + (c - '0');
6683
8.78k
          else if (('a' <= c) && (c <= 'f'))
6684
2.20k
            result = (result * 16) + (10 + c - 'a');
6685
6.57k
          else if (('A' <= c) && (c <= 'F'))
6686
99
            result = (result * 16) + (10 + c - 'A');
6687
6.47k
          else
6688
6.47k
            break;
6689
4.45k
          p++;
6690
4.45k
        }
6691
6.72k
      }
6692
10.8k
      if (p == q)
6693
6.73k
        result = NAN;
6694
4.07k
      else
6695
4.07k
            q = p;
6696
10.8k
    }
6697
15.8M
    else {
6698
15.8M
      ThInfo DTOA;
6699
15.8M
      fxDTOASetup(the, &DTOA);
6700
15.8M
      result = strtod2(p, &q, &DTOA);
6701
15.8M
      fxDTOACleanup(the, &DTOA);
6702
15.8M
      if ((p == q) && !whole)
6703
71
        result = NAN;
6704
15.8M
    }
6705
15.8M
    if (whole) {
6706
15.8M
      p = fxSkipSpaces(q);
6707
15.8M
      if (*p)
6708
6.81M
        result = NAN;
6709
15.8M
    }
6710
15.8M
  }
6711
16.1M
  return result;
6712
16.1M
}