Coverage Report

Created: 2025-10-29 06:26

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-2017  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
660M
#define __XS__a , DTOA
48
#define __XS__d , ThInfo* DTOA
49
50
0
#define FREE fxDTOAFree
51
#define INFNAN_CHECK
52
87.1M
#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
47.4M
#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
85.6k
#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
279M
#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
146M
#define word0(x) (x)->L[1]
1381
53.7M
#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
274M
#define dval(x) (x)->d
1387
#define LLval(x) (x)->LL
1388
1389
#ifndef STRTOD_DIGLIM
1390
2.88M
#define STRTOD_DIGLIM 40
1391
#endif
1392
1393
#ifdef DIGLIM_DEBUG
1394
extern int strtod_diglim;
1395
#else
1396
2.88M
#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
16.8M
#define Exp_shift  20
1419
27.5M
#define Exp_shift1 20
1420
23.8M
#define Exp_msk1    0x100000
1421
#define Exp_msk11   0x100000
1422
49.7M
#define Exp_mask  0x7ff00000
1423
44.3M
#define P 53
1424
#define Nbits 53
1425
32.8M
#define Bias 1023
1426
#define Emax 1023
1427
3.03M
#define Emin (-1022)
1428
1.52M
#define Exp_1  0x3ff00000
1429
13.7M
#define Exp_11 0x3ff00000
1430
3.78M
#define Ebits 11
1431
16.8M
#define Frac_mask  0xfffff
1432
13.7M
#define Frac_mask1 0xfffff
1433
12.9M
#define Ten_pmax 22
1434
226k
#define Bletch 0x10
1435
12.2M
#define Bndry_mask  0xfffff
1436
49.4k
#define Bndry_mask1 0xfffff
1437
#define LSB 1
1438
17.7M
#define Sign_bit 0x80000000
1439
821k
#define Log2P 1
1440
#define Tiny0 0
1441
656k
#define Tiny1 1
1442
14.6M
#define Quick_max 14
1443
11.5M
#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
16.8M
#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
16.4k
#define rounded_product(a,b) a *= b
1548
227k
#define rounded_quotient(a,b) a /= b
1549
#endif
1550
1551
2.97k
#define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
1552
2.04k
#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.53G
#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
212M
#define MTa __XS__a
1571
331M
#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
420M
#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
464M
#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
455M
#define freelist TI0.Freelist
1658
9.66M
#define p5s TI0.P5s
1659
#endif
1660
1661
 static Bigint *
1662
Balloc(int k MTd)
1663
144M
{
1664
144M
  int x;
1665
144M
  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
144M
  if (k <= Kmax && (rv = freelist[k]))
1680
144M
    freelist[k] = rv->next;
1681
87.1M
  else {
1682
87.1M
    x = 1 << k;
1683
87.1M
#ifdef Omit_Private_Memory
1684
87.1M
    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
87.1M
    rv->k = k;
1700
87.1M
    rv->maxwds = x;
1701
87.1M
    }
1702
#ifdef MULTIPLE_THREADS
1703
  if (TI == &TI0)
1704
    FREE_DTOA_LOCK(0);
1705
#endif
1706
144M
  rv->sign = rv->wds = 0;
1707
144M
  return rv;
1708
144M
  }
1709
1710
 static void
1711
Bfree(Bigint *v MTd)
1712
126M
{
1713
#ifdef MULTIPLE_THREADS
1714
  ThInfo *TI;
1715
#endif
1716
126M
  if (v) {
1717
126M
    if (v->k > Kmax)
1718
0
      FREE((void*)v __XS__a);
1719
126M
    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
126M
      v->next = freelist[v->k];
1727
126M
      freelist[v->k] = v;
1728
#ifdef MULTIPLE_THREADS
1729
      if (TI == &TI0)
1730
        FREE_DTOA_LOCK(0);
1731
#endif
1732
126M
      }
1733
126M
    }
1734
126M
  }
1735
1736
3.38M
#define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
1737
3.38M
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
137M
{
1742
137M
  int i, wds;
1743
137M
#ifdef ULLong
1744
137M
  ULong *x;
1745
137M
  ULLong carry, y;
1746
#else
1747
  ULong carry, *x, y;
1748
#ifdef Pack_32
1749
  ULong xi, z;
1750
#endif
1751
#endif
1752
137M
  Bigint *b1;
1753
1754
137M
  wds = b->wds;
1755
137M
  x = b->x;
1756
137M
  i = 0;
1757
137M
  carry = a;
1758
567M
  do {
1759
567M
#ifdef ULLong
1760
567M
    y = *x * (ULLong)m + carry;
1761
567M
    carry = y >> 32;
1762
567M
    *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
567M
    }
1777
567M
    while(++i < wds);
1778
137M
  if (carry) {
1779
6.10M
    if (wds >= b->maxwds) {
1780
122k
      b1 = Balloc(b->k+1 MTa);
1781
122k
      Bcopy(b1, b);
1782
122k
      Bfree(b MTa);
1783
122k
      b = b1;
1784
122k
      }
1785
6.10M
    b->x[wds++] = (ULong)carry;
1786
6.10M
    b->wds = wds;
1787
6.10M
    }
1788
137M
  return b;
1789
137M
  }
1790
1791
 static Bigint *
1792
s2b(const char *s, int nd0, int nd, ULong y9, int dplen MTd)
1793
2.88M
{
1794
2.88M
  Bigint *b;
1795
2.88M
  int i, k;
1796
2.88M
  Long x, y;
1797
1798
2.88M
  x = (nd + 8) / 9;
1799
6.02M
  for(k = 0, y = 1; x > y; y <<= 1, k++) ;
1800
2.88M
#ifdef Pack_32
1801
2.88M
  b = Balloc(k MTa);
1802
2.88M
  b->x[0] = y9;
1803
2.88M
  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.88M
  i = 9;
1811
2.88M
  if (9 < nd0) {
1812
1.26M
    s += 9;
1813
9.82M
    do b = multadd(b, 10, *s++ - '0' MTa);
1814
9.82M
      while(++i < nd0);
1815
1.26M
    s += dplen;
1816
1.26M
    }
1817
1.62M
  else
1818
1.62M
    s += dplen + 9;
1819
15.5M
  for(; i < nd; i++)
1820
12.6M
    b = multadd(b, 10, *s++ - '0' MTa);
1821
2.88M
  return b;
1822
2.88M
  }
1823
1824
 static int
1825
hi0bits(ULong x)
1826
4.93M
{
1827
4.93M
  int k = 0;
1828
1829
4.93M
  if (!(x & 0xffff0000)) {
1830
3.50M
    k = 16;
1831
3.50M
    x <<= 16;
1832
3.50M
    }
1833
4.93M
  if (!(x & 0xff000000)) {
1834
3.20M
    k += 8;
1835
3.20M
    x <<= 8;
1836
3.20M
    }
1837
4.93M
  if (!(x & 0xf0000000)) {
1838
2.98M
    k += 4;
1839
2.98M
    x <<= 4;
1840
2.98M
    }
1841
4.93M
  if (!(x & 0xc0000000)) {
1842
3.16M
    k += 2;
1843
3.16M
    x <<= 2;
1844
3.16M
    }
1845
4.93M
  if (!(x & 0x80000000)) {
1846
2.85M
    k++;
1847
2.85M
    if (!(x & 0x40000000))
1848
0
      return 32;
1849
2.85M
    }
1850
4.93M
  return k;
1851
4.93M
  }
1852
1853
 static int
1854
lo0bits(ULong *y)
1855
16.8M
{
1856
16.8M
  int k;
1857
16.8M
  ULong x = *y;
1858
1859
16.8M
  if (x & 7) {
1860
5.37M
    if (x & 1)
1861
2.99M
      return 0;
1862
2.37M
    if (x & 2) {
1863
1.55M
      *y = x >> 1;
1864
1.55M
      return 1;
1865
1.55M
      }
1866
818k
    *y = x >> 2;
1867
818k
    return 2;
1868
2.37M
    }
1869
11.4M
  k = 0;
1870
11.4M
  if (!(x & 0xffff)) {
1871
5.64M
    k = 16;
1872
5.64M
    x >>= 16;
1873
5.64M
    }
1874
11.4M
  if (!(x & 0xff)) {
1875
4.86M
    k += 8;
1876
4.86M
    x >>= 8;
1877
4.86M
    }
1878
11.4M
  if (!(x & 0xf)) {
1879
6.03M
    k += 4;
1880
6.03M
    x >>= 4;
1881
6.03M
    }
1882
11.4M
  if (!(x & 0x3)) {
1883
5.42M
    k += 2;
1884
5.42M
    x >>= 2;
1885
5.42M
    }
1886
11.4M
  if (!(x & 1)) {
1887
5.43M
    k++;
1888
5.43M
    x >>= 1;
1889
5.43M
    if (!x)
1890
0
      return 32;
1891
5.43M
    }
1892
11.4M
  *y = x;
1893
11.4M
  return k;
1894
11.4M
  }
1895
1896
 static Bigint *
1897
i2b(int i MTd)
1898
14.3M
{
1899
14.3M
  Bigint *b;
1900
1901
14.3M
  b = Balloc(1 MTa);
1902
14.3M
  b->x[0] = i;
1903
14.3M
  b->wds = 1;
1904
14.3M
  return b;
1905
14.3M
  }
1906
1907
 static Bigint *
1908
mult(Bigint *a, Bigint *b MTd)
1909
24.6M
{
1910
24.6M
  Bigint *c;
1911
24.6M
  int k, wa, wb, wc;
1912
24.6M
  ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
1913
24.6M
  ULong y;
1914
24.6M
#ifdef ULLong
1915
24.6M
  ULLong carry, z;
1916
#else
1917
  ULong carry, z;
1918
#ifdef Pack_32
1919
  ULong z2;
1920
#endif
1921
#endif
1922
1923
24.6M
  if (a->wds < b->wds) {
1924
4.80M
    c = a;
1925
4.80M
    a = b;
1926
4.80M
    b = c;
1927
4.80M
    }
1928
24.6M
  k = a->k;
1929
24.6M
  wa = a->wds;
1930
24.6M
  wb = b->wds;
1931
24.6M
  wc = wa + wb;
1932
24.6M
  if (wc > a->maxwds)
1933
10.2M
    k++;
1934
24.6M
  c = Balloc(k MTa);
1935
134M
  for(x = c->x, xa = x + wc; x < xa; x++)
1936
110M
    *x = 0;
1937
24.6M
  xa = a->x;
1938
24.6M
  xae = xa + wa;
1939
24.6M
  xb = b->x;
1940
24.6M
  xbe = xb + wb;
1941
24.6M
  xc0 = c->x;
1942
24.6M
#ifdef ULLong
1943
68.0M
  for(; xb < xbe; xc0++) {
1944
43.3M
    if ((y = *xb++)) {
1945
43.3M
      x = xa;
1946
43.3M
      xc = xc0;
1947
43.3M
      carry = 0;
1948
175M
      do {
1949
175M
        z = *x++ * (ULLong)y + *xc + carry;
1950
175M
        carry = z >> 32;
1951
175M
        *xc++ = z & FFFFFFFF;
1952
175M
        }
1953
175M
        while(x < xae);
1954
43.3M
      *xc = (ULong)carry;
1955
43.3M
      }
1956
43.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
41.6M
  for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
2008
24.6M
  c->wds = wc;
2009
24.6M
  return c;
2010
24.6M
  }
2011
2012
 static Bigint *
2013
pow5mult(Bigint *b, int k MTd)
2014
5.51M
{
2015
5.51M
  Bigint *b1, *p5, *p51;
2016
#ifdef MULTIPLE_THREADS
2017
  ThInfo *TI;
2018
#endif
2019
5.51M
  int i;
2020
5.51M
  static int p05[3] = { 5, 25, 125 };
2021
2022
5.51M
  if ((i = k & 3))
2023
4.09M
    b = multadd(b, p05[i-1], 0 MTa);
2024
2025
5.51M
  if (!(k >>= 2))
2026
650k
    return b;
2027
#ifdef  MULTIPLE_THREADS
2028
  if (!(TI = *PTI))
2029
    *PTI = TI = get_TI();
2030
#endif
2031
4.86M
  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.80M
    p5 = p5s = i2b(625 MTa);
2046
4.80M
    p5->next = 0;
2047
4.80M
#endif
2048
4.80M
    }
2049
17.8M
  for(;;) {
2050
17.8M
    if (k & 1) {
2051
10.3M
      b1 = mult(b, p5 MTa);
2052
10.3M
      Bfree(b MTa);
2053
10.3M
      b = b1;
2054
10.3M
      }
2055
17.8M
    if (!(k >>= 1))
2056
4.86M
      break;
2057
12.9M
    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
12.6M
      p51 = p5->next = mult(p5,p5 __XS__a);
2071
12.6M
      p51->next = 0;
2072
12.6M
#endif
2073
12.6M
      }
2074
12.9M
    p5 = p51;
2075
12.9M
    }
2076
4.86M
  return b;
2077
5.51M
  }
2078
2079
 static Bigint *
2080
lshift(Bigint *b, int k MTd)
2081
17.3M
{
2082
17.3M
  int i, k1, n, n1;
2083
17.3M
  Bigint *b1;
2084
17.3M
  ULong *x, *x1, *xe, z;
2085
2086
17.3M
#ifdef Pack_32
2087
17.3M
  n = k >> 5;
2088
#else
2089
  n = k >> 4;
2090
#endif
2091
17.3M
  k1 = b->k;
2092
17.3M
  n1 = n + b->wds + 1;
2093
33.0M
  for(i = b->maxwds; n1 > i; i <<= 1)
2094
15.6M
    k1++;
2095
17.3M
  b1 = Balloc(k1 MTa);
2096
17.3M
  x1 = b1->x;
2097
51.7M
  for(i = 0; i < n; i++)
2098
34.3M
    *x1++ = 0;
2099
17.3M
  x = b->x;
2100
17.3M
  xe = x + b->wds;
2101
17.3M
#ifdef Pack_32
2102
17.3M
  if (k &= 0x1f) {
2103
17.2M
    k1 = 32 - k;
2104
17.2M
    z = 0;
2105
51.6M
    do {
2106
51.6M
      *x1++ = *x << k | z;
2107
51.6M
      z = *x++ >> k1;
2108
51.6M
      }
2109
51.6M
      while(x < xe);
2110
17.2M
    if ((*x1 = z))
2111
2.13M
      ++n1;
2112
17.2M
    }
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
135k
  else do
2127
328k
    *x1++ = *x++;
2128
328k
    while(x < xe);
2129
17.3M
  b1->wds = n1 - 1;
2130
17.3M
  Bfree(b MTa);
2131
17.3M
  return b1;
2132
17.3M
  }
2133
2134
 static int
2135
cmp(Bigint *a, Bigint *b)
2136
214M
{
2137
214M
  ULong *xa, *xa0, *xb, *xb0;
2138
214M
  int i, j;
2139
2140
214M
  i = a->wds;
2141
214M
  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
214M
  if (i -= j)
2149
45.7M
    return i;
2150
168M
  xa0 = a->x;
2151
168M
  xa = xa0 + j;
2152
168M
  xb0 = b->x;
2153
168M
  xb = xb0 + j;
2154
174M
  for(;;) {
2155
174M
    if (*--xa != *--xb)
2156
166M
      return *xa < *xb ? -1 : 1;
2157
7.31M
    if (xa <= xa0)
2158
1.77M
      break;
2159
7.31M
    }
2160
1.77M
  return 0;
2161
168M
  }
2162
2163
 static Bigint *
2164
diff(Bigint *a, Bigint *b MTd)
2165
49.0M
{
2166
49.0M
  Bigint *c;
2167
49.0M
  int i, wa, wb;
2168
49.0M
  ULong *xa, *xae, *xb, *xbe, *xc;
2169
49.0M
#ifdef ULLong
2170
49.0M
  ULLong borrow, y;
2171
#else
2172
  ULong borrow, y;
2173
#ifdef Pack_32
2174
  ULong z;
2175
#endif
2176
#endif
2177
2178
49.0M
  i = cmp(a,b);
2179
49.0M
  if (!i) {
2180
728k
    c = Balloc(0 MTa);
2181
728k
    c->wds = 1;
2182
728k
    c->x[0] = 0;
2183
728k
    return c;
2184
728k
    }
2185
48.3M
  if (i < 0) {
2186
1.91M
    c = a;
2187
1.91M
    a = b;
2188
1.91M
    b = c;
2189
1.91M
    i = 1;
2190
1.91M
    }
2191
46.4M
  else
2192
46.4M
    i = 0;
2193
48.3M
  c = Balloc(a->k MTa);
2194
48.3M
  c->sign = i;
2195
48.3M
  wa = a->wds;
2196
48.3M
  xa = a->x;
2197
48.3M
  xae = xa + wa;
2198
48.3M
  wb = b->wds;
2199
48.3M
  xb = b->x;
2200
48.3M
  xbe = xb + wb;
2201
48.3M
  xc = c->x;
2202
48.3M
  borrow = 0;
2203
48.3M
#ifdef ULLong
2204
234M
  do {
2205
234M
    y = (ULLong)*xa++ - *xb++ - borrow;
2206
234M
    borrow = y >> 32 & (ULong)1;
2207
234M
    *xc++ = y & FFFFFFFF;
2208
234M
    }
2209
234M
    while(xb < xbe);
2210
70.6M
  while(xa < xae) {
2211
22.3M
    y = *xa++ - borrow;
2212
22.3M
    borrow = y >> 32 & (ULong)1;
2213
22.3M
    *xc++ = y & FFFFFFFF;
2214
22.3M
    }
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.0M
  while(!*--xc)
2247
3.71M
    wa--;
2248
48.3M
  c->wds = wa;
2249
48.3M
  return c;
2250
49.0M
  }
2251
2252
 static double
2253
ulp(U *x)
2254
752k
{
2255
752k
  Long L;
2256
752k
  U u;
2257
2258
752k
  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
752k
    word0(&u) = L;
2268
752k
    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
752k
  return dval(&u);
2287
752k
  }
2288
2289
 static double
2290
b2d(Bigint *a, int *e)
2291
1.46M
{
2292
1.46M
  ULong *xa, *xa0, w, y, z;
2293
1.46M
  int k;
2294
1.46M
  U d;
2295
#ifdef VAX
2296
  ULong d0, d1;
2297
#else
2298
1.46M
#define d0 word0(&d)
2299
1.46M
#define d1 word1(&d)
2300
1.46M
#endif
2301
2302
1.46M
  xa0 = a->x;
2303
1.46M
  xa = xa0 + a->wds;
2304
1.46M
  y = *--xa;
2305
#ifdef DEBUG
2306
  if (!y) Bug("zero y in b2d");
2307
#endif
2308
1.46M
  k = hi0bits(y);
2309
1.46M
  *e = 32 - k;
2310
1.46M
#ifdef Pack_32
2311
1.46M
  if (k < Ebits) {
2312
429k
    d0 = Exp_1 | y >> (Ebits - k);
2313
429k
    w = xa > xa0 ? *--xa : 0;
2314
429k
    d1 = y << ((32-Ebits) + k) | w >> (Ebits - k);
2315
429k
    goto ret_d;
2316
429k
    }
2317
1.03M
  z = xa > xa0 ? *--xa : 0;
2318
1.03M
  if (k -= Ebits) {
2319
990k
    d0 = Exp_1 | y << k | z >> (32 - k);
2320
990k
    y = xa > xa0 ? *--xa : 0;
2321
990k
    d1 = z << k | y >> (32 - k);
2322
990k
    }
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.46M
 ret_d:
2344
#ifdef VAX
2345
  word0(&d) = d0 >> 16 | d0 << 16;
2346
  word1(&d) = d1 >> 16 | d1 << 16;
2347
#else
2348
1.46M
#undef d0
2349
1.46M
#undef d1
2350
1.46M
#endif
2351
1.46M
  return dval(&d);
2352
1.03M
  }
2353
2354
 static Bigint *
2355
d2b(U *d, int *e, int *bits MTd)
2356
16.8M
{
2357
16.8M
  Bigint *b;
2358
16.8M
  int de, k;
2359
16.8M
  ULong *x, y, z;
2360
16.8M
#ifndef Sudden_Underflow
2361
16.8M
  int i;
2362
16.8M
#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
50.4M
#define d0 word0(d)
2369
16.8M
#define d1 word1(d)
2370
16.8M
#endif
2371
2372
16.8M
#ifdef Pack_32
2373
16.8M
  b = Balloc(1 MTa);
2374
#else
2375
  b = Balloc(2 MTa);
2376
#endif
2377
16.8M
  x = b->x;
2378
2379
16.8M
  z = d0 & Frac_mask;
2380
16.8M
  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
16.8M
  if ((de = (int)(d0 >> Exp_shift)))
2388
16.8M
    z |= Exp_msk1;
2389
16.8M
#endif
2390
16.8M
#ifdef Pack_32
2391
16.8M
  if ((y = d1)) {
2392
6.53M
    if ((k = lo0bits(&y))) {
2393
3.56M
      x[0] = y | z << (32 - k);
2394
3.56M
      z >>= k;
2395
3.56M
      }
2396
2.96M
    else
2397
2.96M
      x[0] = y;
2398
6.53M
#ifndef Sudden_Underflow
2399
6.53M
    i =
2400
6.53M
#endif
2401
6.53M
        b->wds = (x[1] = z) ? 2 : 1;
2402
6.53M
    }
2403
10.2M
  else {
2404
10.2M
    k = lo0bits(&z);
2405
10.2M
    x[0] = z;
2406
10.2M
#ifndef Sudden_Underflow
2407
10.2M
    i =
2408
10.2M
#endif
2409
10.2M
        b->wds = 1;
2410
10.2M
    k += 32;
2411
10.2M
    }
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
16.8M
#ifndef Sudden_Underflow
2458
16.8M
  if (de) {
2459
16.8M
#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
16.8M
    *e = de - Bias - (P-1) + k;
2465
16.8M
    *bits = P - k;
2466
16.8M
#endif
2467
16.8M
#ifndef Sudden_Underflow
2468
16.8M
    }
2469
1.06k
  else {
2470
1.06k
    *e = de - Bias - (P-1) + 1 + k;
2471
1.06k
#ifdef Pack_32
2472
1.06k
    *bits = 32*i - hi0bits(x[i-1]);
2473
#else
2474
    *bits = (i+2)*16 - hi0bits(x[i]);
2475
#endif
2476
1.06k
    }
2477
16.8M
#endif
2478
16.8M
  return b;
2479
16.8M
  }
2480
#undef d0
2481
#undef d1
2482
2483
 static double
2484
ratio(Bigint *a, Bigint *b)
2485
732k
{
2486
732k
  U da, db;
2487
732k
  int k, ka, kb;
2488
2489
732k
  dval(&da) = b2d(a, &ka);
2490
732k
  dval(&db) = b2d(b, &kb);
2491
732k
#ifdef Pack_32
2492
732k
  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
732k
  if (k > 0)
2510
375k
    word0(&da) += k*Exp_msk1;
2511
356k
  else {
2512
356k
    k = -k;
2513
356k
    word0(&db) += k*Exp_msk1;
2514
356k
    }
2515
732k
#endif
2516
732k
  return dval(&da) / dval(&db);
2517
732k
  }
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
524k
#define Scale_Bit 0x10
2543
608k
#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
142k
#define NAN_WORD0 0x7ff80000
2616
#endif
2617
2618
#ifndef NAN_WORD1
2619
142k
#define NAN_WORD1 0
2620
#endif
2621
2622
 static int
2623
match(const char **sp, const char *t)
2624
185k
{
2625
185k
  int c, d;
2626
185k
  const char *s = *sp;
2627
2628
735k
  while((d = *t++)) {
2629
#ifndef __XS__
2630
    if ((c = *++s) >= 'A' && c <= 'Z')
2631
      c += 'a' - 'A';
2632
#else
2633
555k
        c = *++s;
2634
555k
#endif
2635
555k
    if (c != d)
2636
5.68k
      return 0;
2637
555k
    }
2638
180k
  *sp = s + 1;
2639
180k
  return 1;
2640
185k
  }
2641
2642
#ifndef No_Hex_NaN
2643
 static void
2644
hexnan(U *rvp, const char **sp)
2645
8.07k
{
2646
8.07k
  ULong c, x[2];
2647
8.07k
  const char *s;
2648
8.07k
  int c1, havedig, udx0, xshift;
2649
2650
  /**** if (!hexdig['0']) hexdig_init(); ****/
2651
8.07k
  x[0] = x[1] = 0;
2652
8.07k
  havedig = xshift = 0;
2653
8.07k
  udx0 = 1;
2654
8.07k
  s = *sp;
2655
  /* allow optional initial 0x or 0X */
2656
8.48k
  while((c = *(const unsigned char*)(s+1)) && c <= ' ')
2657
409
    ++s;
2658
8.07k
  if (s[1] == '0' && (s[2] == 'x' || s[2] == 'X'))
2659
1.25k
    s += 2;
2660
26.2k
  while((c = *(const unsigned char*)++s)) {
2661
21.2k
    if ((c1 = hexdig[c]))
2662
15.3k
      c  = c1 & 0xf;
2663
5.84k
    else if (c <= ' ') {
2664
2.78k
      if (udx0 && havedig) {
2665
1.46k
        udx0 = 0;
2666
1.46k
        xshift = 1;
2667
1.46k
        }
2668
2.78k
      continue;
2669
2.78k
      }
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
3.06k
    else {
2679
132k
      do {
2680
132k
        if (/*(*/ c == ')') {
2681
401
          *sp = s + 1;
2682
401
          break;
2683
401
          }
2684
132k
        } while((c = *++s));
2685
3.06k
      break;
2686
3.06k
      }
2687
15.3k
#endif
2688
15.3k
    havedig = 1;
2689
15.3k
    if (xshift) {
2690
945
      xshift = 0;
2691
945
      x[0] = x[1];
2692
945
      x[1] = 0;
2693
945
      }
2694
15.3k
    if (udx0)
2695
11.8k
      x[0] = (x[0] << 4) | (x[1] >> 28);
2696
15.3k
    x[1] = (x[1] << 4) | c;
2697
15.3k
    }
2698
8.07k
  if ((x[0] &= 0xfffff) || x[1]) {
2699
4.28k
    word0(rvp) = Exp_mask | x[0];
2700
4.28k
    word1(rvp) = x[1];
2701
4.28k
    }
2702
8.07k
  }
2703
#endif /*No_Hex_NaN*/
2704
#endif /* INFNAN_CHECK */
2705
2706
#ifdef Pack_32
2707
#define ULbits 32
2708
#define kshift 5
2709
3.47M
#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
      case '+':
2918
      s++;
2919
      }
2920
    if ((n = hexdig[*s]) == 0 || n > 0x19) {
2921
      s = s1;
2922
      break;
2923
      }
2924
    e1 = n - 0x10;
2925
    while((n = hexdig[*++s]) !=0 && n <= 0x19) {
2926
      if (e1 & 0xf8000000)
2927
        big = 1;
2928
      e1 = 10*e1 + n - 0x10;
2929
      }
2930
    if (esign)
2931
      e1 = -e1;
2932
    e += e1;
2933
    }
2934
  *sp = (char*)s;
2935
  if (!havedig)
2936
    *sp = (char*)s0 - 1;
2937
  if (zret)
2938
    goto retz1;
2939
  if (big) {
2940
    if (esign) {
2941
#ifdef IEEE_Arith
2942
      switch(rounding) {
2943
        case Round_up:
2944
        if (sign)
2945
          break;
2946
        goto ret_tiny;
2947
        case Round_down:
2948
        if (!sign)
2949
          break;
2950
        goto ret_tiny;
2951
        }
2952
#endif
2953
      goto retz;
2954
#ifdef IEEE_Arith
2955
 ret_tinyf:
2956
      Bfree(b MTa);
2957
 ret_tiny:
2958
      Set_errno(ERANGE);
2959
      word0(rvp) = 0;
2960
      word1(rvp) = 1;
2961
      return;
2962
#endif /* IEEE_Arith */
2963
      }
2964
    switch(rounding) {
2965
      case Round_near:
2966
      goto ovfl1;
2967
      case Round_up:
2968
      if (!sign)
2969
        goto ovfl1;
2970
      goto ret_big;
2971
      case Round_down:
2972
      if (sign)
2973
        goto ovfl1;
2974
      goto ret_big;
2975
      }
2976
 ret_big:
2977
    word0(rvp) = Big0;
2978
    word1(rvp) = Big1;
2979
    return;
2980
    }
2981
  n = s1 - s0 - 1;
2982
  for(k = 0; n > (1 << (kshift-2)) - 1; n >>= 1)
2983
    k++;
2984
  b = Balloc(k MTa);
2985
  x = b->x;
2986
  n = 0;
2987
  L = 0;
2988
#ifdef USE_LOCALE
2989
  for(i = 0; decimalpoint[i+1]; ++i);
2990
#endif
2991
  while(s1 > s0) {
2992
#ifdef USE_LOCALE
2993
    if (*--s1 == decimalpoint[i]) {
2994
      s1 -= i;
2995
      continue;
2996
      }
2997
#else
2998
    if (*--s1 == '.')
2999
      continue;
3000
#endif
3001
    if (n == ULbits) {
3002
      *x++ = L;
3003
      L = 0;
3004
      n = 0;
3005
      }
3006
    L |= (hexdig[*s1] & 0x0f) << n;
3007
    n += 4;
3008
    }
3009
  *x++ = L;
3010
  b->wds = n = x - b->x;
3011
  n = ULbits*n - hi0bits(L);
3012
  nbits = Nbits;
3013
  lostbits = 0;
3014
  x = b->x;
3015
  if (n > nbits) {
3016
    n -= nbits;
3017
    if (any_on(b,n)) {
3018
      lostbits = 1;
3019
      k = n - 1;
3020
      if (x[k>>kshift] & 1 << (k & kmask)) {
3021
        lostbits = 2;
3022
        if (k > 0 && any_on(b,k))
3023
          lostbits = 3;
3024
        }
3025
      }
3026
    rshift(b, n);
3027
    e += n;
3028
    }
3029
  else if (n < nbits) {
3030
    n = nbits - n;
3031
    b = lshift(b, n MTa);
3032
    e -= n;
3033
    x = b->x;
3034
    }
3035
  if (e > emax) {
3036
 ovfl:
3037
    Bfree(b MTa);
3038
 ovfl1:
3039
    Set_errno(ERANGE);
3040
#ifdef Honor_FLT_ROUNDS
3041
    switch (rounding) {
3042
      case Round_zero:
3043
      goto ret_big;
3044
      case Round_down:
3045
      if (!sign)
3046
        goto ret_big;
3047
      break;
3048
      case Round_up:
3049
      if (sign)
3050
        goto ret_big;
3051
      }
3052
#endif
3053
    word0(rvp) = Exp_mask;
3054
    word1(rvp) = 0;
3055
    return;
3056
    }
3057
  denorm = 0;
3058
  if (e < emin) {
3059
    denorm = 1;
3060
    n = emin - e;
3061
    if (n >= nbits) {
3062
#ifdef IEEE_Arith /*{*/
3063
      switch (rounding) {
3064
        case Round_near:
3065
        if (n == nbits && (n < 2 || lostbits || any_on(b,n-1)))
3066
          goto ret_tinyf;
3067
        break;
3068
        case Round_up:
3069
        if (!sign)
3070
          goto ret_tinyf;
3071
        break;
3072
        case Round_down:
3073
        if (sign)
3074
          goto ret_tinyf;
3075
        }
3076
#endif /* } IEEE_Arith */
3077
      Bfree(b MTa);
3078
 retz:
3079
      Set_errno(ERANGE);
3080
 retz1:
3081
      rvp->d = 0.;
3082
      return;
3083
      }
3084
    k = n - 1;
3085
    if (lostbits)
3086
      lostbits = 1;
3087
    else if (k > 0)
3088
      lostbits = any_on(b,k);
3089
    if (x[k>>kshift] & 1 << (k & kmask))
3090
      lostbits |= 2;
3091
    nbits -= n;
3092
    rshift(b,n);
3093
    e = emin;
3094
    }
3095
  if (lostbits) {
3096
    up = 0;
3097
    switch(rounding) {
3098
      case Round_zero:
3099
      break;
3100
      case Round_near:
3101
      if (lostbits & 2
3102
       && (lostbits & 1) | (x[0] & 1))
3103
        up = 1;
3104
      break;
3105
      case Round_up:
3106
      up = 1 - sign;
3107
      break;
3108
      case Round_down:
3109
      up = sign;
3110
      }
3111
    if (up) {
3112
      k = b->wds;
3113
      b = increment(b MTa);
3114
      x = b->x;
3115
      if (denorm) {
3116
#if 0
3117
        if (nbits == Nbits - 1
3118
         && x[nbits >> kshift] & 1 << (nbits & kmask))
3119
          denorm = 0; /* not currently used */
3120
#endif
3121
        }
3122
      else if (b->wds > k
3123
       || ((n = nbits & kmask) !=0
3124
           && hi0bits(x[k-1]) < 32-n)) {
3125
        rshift(b,1);
3126
        if (++e > Emax)
3127
          goto ovfl;
3128
        }
3129
      }
3130
    }
3131
#ifdef IEEE_Arith
3132
  if (denorm)
3133
    word0(rvp) = b->wds > 1 ? b->x[1] & ~0x100000 : 0;
3134
  else
3135
    word0(rvp) = (b->x[1] & ~0x100000) | ((e + 0x3ff + 52) << 20);
3136
  word1(rvp) = b->x[0];
3137
#endif
3138
#ifdef IBM
3139
  if ((j = e & 3)) {
3140
    k = b->x[0] & ((1 << j) - 1);
3141
    rshift(b,j);
3142
    if (k) {
3143
      switch(rounding) {
3144
        case Round_up:
3145
        if (!sign)
3146
          increment(b);
3147
        break;
3148
        case Round_down:
3149
        if (sign)
3150
          increment(b);
3151
        break;
3152
        case Round_near:
3153
        j = 1 << (j-1);
3154
        if (k & j && ((k & (j-1)) | lostbits))
3155
          increment(b);
3156
        }
3157
      }
3158
    }
3159
  e >>= 2;
3160
  word0(rvp) = b->x[1] | ((e + 65 + 13) << 24);
3161
  word1(rvp) = b->x[0];
3162
#endif
3163
#ifdef VAX
3164
  /* The next two lines ignore swap of low- and high-order 2 bytes. */
3165
  /* word0(rvp) = (b->x[1] & ~0x800000) | ((e + 129 + 55) << 23); */
3166
  /* word1(rvp) = b->x[0]; */
3167
  word0(rvp) = ((b->x[1] & ~0x800000) >> 16) | ((e + 129 + 55) << 7) | (b->x[1] << 16);
3168
  word1(rvp) = (b->x[0] >> 16) | (b->x[0] << 16);
3169
#endif
3170
  Bfree(b MTa);
3171
  }
3172
#endif /*!NO_HEX_FP}*/
3173
3174
 static int
3175
dshift(Bigint *b, int p2)
3176
3.47M
{
3177
3.47M
  int rv = hi0bits(b->x[b->wds-1]) - 4;
3178
3.47M
  if (p2 > 0)
3179
2.70M
    rv -= p2;
3180
3.47M
  return rv & kmask;
3181
3.47M
  }
3182
3183
 static int
3184
quorem(Bigint *b, Bigint *S)
3185
67.9M
{
3186
67.9M
  int n;
3187
67.9M
  ULong *bx, *bxe, q, *sx, *sxe;
3188
67.9M
#ifdef ULLong
3189
67.9M
  ULLong borrow, carry, y, ys;
3190
#else
3191
  ULong borrow, carry, y, ys;
3192
#ifdef Pack_32
3193
  ULong si, z, zs;
3194
#endif
3195
#endif
3196
3197
67.9M
  n = S->wds;
3198
#ifdef DEBUG
3199
  /*debug*/ if (b->wds > n)
3200
  /*debug*/ Bug("oversize b in quorem");
3201
#endif
3202
67.9M
  if (b->wds < n)
3203
12.5k
    return 0;
3204
67.9M
  sx = S->x;
3205
67.9M
  sxe = sx + --n;
3206
67.9M
  bx = b->x;
3207
67.9M
  bxe = bx + n;
3208
67.9M
  q = *bxe / (*sxe + 1);  /* ensure q <= true quotient */
3209
#ifdef DEBUG
3210
#ifdef NO_STRTOD_BIGCOMP
3211
  /*debug*/ if (q > 9)
3212
#else
3213
  /* An oversized q is possible when quorem is called from bigcomp and */
3214
  /* the input is near, e.g., twice the smallest denormalized number. */
3215
  /*debug*/ if (q > 15)
3216
#endif
3217
  /*debug*/ Bug("oversized quotient in quorem");
3218
#endif
3219
67.9M
  if (q) {
3220
59.2M
    borrow = 0;
3221
59.2M
    carry = 0;
3222
268M
    do {
3223
268M
#ifdef ULLong
3224
268M
      ys = *sx++ * (ULLong)q + carry;
3225
268M
      carry = ys >> 32;
3226
268M
      y = *bx - (ys & FFFFFFFF) - borrow;
3227
268M
      borrow = y >> 32 & (ULong)1;
3228
268M
      *bx++ = y & FFFFFFFF;
3229
#else
3230
#ifdef Pack_32
3231
      si = *sx++;
3232
      ys = (si & 0xffff) * q + carry;
3233
      zs = (si >> 16) * q + (ys >> 16);
3234
      carry = zs >> 16;
3235
      y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
3236
      borrow = (y & 0x10000) >> 16;
3237
      z = (*bx >> 16) - (zs & 0xffff) - borrow;
3238
      borrow = (z & 0x10000) >> 16;
3239
      Storeinc(bx, z, y);
3240
#else
3241
      ys = *sx++ * q + carry;
3242
      carry = ys >> 16;
3243
      y = *bx - (ys & 0xffff) - borrow;
3244
      borrow = (y & 0x10000) >> 16;
3245
      *bx++ = y & 0xffff;
3246
#endif
3247
#endif
3248
268M
      }
3249
268M
      while(sx <= sxe);
3250
59.2M
    if (!*bxe) {
3251
1.38k
      bx = b->x;
3252
1.38k
      while(--bxe > bx && !*bxe)
3253
0
        --n;
3254
1.38k
      b->wds = n;
3255
1.38k
      }
3256
59.2M
    }
3257
67.9M
  if (cmp(b, S) >= 0) {
3258
861k
    q++;
3259
861k
    borrow = 0;
3260
861k
    carry = 0;
3261
861k
    bx = b->x;
3262
861k
    sx = S->x;
3263
1.85M
    do {
3264
1.85M
#ifdef ULLong
3265
1.85M
      ys = *sx++ + carry;
3266
1.85M
      carry = ys >> 32;
3267
1.85M
      y = *bx - (ys & FFFFFFFF) - borrow;
3268
1.85M
      borrow = y >> 32 & (ULong)1;
3269
1.85M
      *bx++ = y & FFFFFFFF;
3270
#else
3271
#ifdef Pack_32
3272
      si = *sx++;
3273
      ys = (si & 0xffff) + carry;
3274
      zs = (si >> 16) + (ys >> 16);
3275
      carry = zs >> 16;
3276
      y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
3277
      borrow = (y & 0x10000) >> 16;
3278
      z = (*bx >> 16) - (zs & 0xffff) - borrow;
3279
      borrow = (z & 0x10000) >> 16;
3280
      Storeinc(bx, z, y);
3281
#else
3282
      ys = *sx++ + carry;
3283
      carry = ys >> 16;
3284
      y = *bx - (ys & 0xffff) - borrow;
3285
      borrow = (y & 0x10000) >> 16;
3286
      *bx++ = y & 0xffff;
3287
#endif
3288
#endif
3289
1.85M
      }
3290
1.85M
      while(sx <= sxe);
3291
861k
    bx = b->x;
3292
861k
    bxe = bx + n;
3293
861k
    if (!*bxe) {
3294
870k
      while(--bxe > bx && !*bxe)
3295
11.8k
        --n;
3296
859k
      b->wds = n;
3297
859k
      }
3298
861k
    }
3299
67.9M
  return q;
3300
67.9M
  }
3301
3302
#if defined(Avoid_Underflow) || !defined(NO_STRTOD_BIGCOMP) /*{*/
3303
 static double
3304
sulp(U *x, BCinfo *bc)
3305
20.0k
{
3306
20.0k
  U u;
3307
20.0k
  double rv;
3308
20.0k
  int i;
3309
3310
20.0k
  rv = ulp(x);
3311
20.0k
  if (!bc->scale || (i = 2*P + 1 - ((word0(x) & Exp_mask) >> Exp_shift)) <= 0)
3312
19.2k
    return rv; /* Is there an example where i <= 0 ? */
3313
741
  word0(&u) = Exp_1 + (i << Exp_shift);
3314
741
  word1(&u) = 0;
3315
741
  return rv * u.d;
3316
20.0k
  }
3317
#endif /*}*/
3318
3319
#ifndef NO_STRTOD_BIGCOMP
3320
 static void
3321
bigcomp(U *rv, const char *s0, BCinfo *bc MTd)
3322
12.7k
{
3323
12.7k
  Bigint *b, *d;
3324
12.7k
  int b2, bbits, d2, dd, dig, dsign, i, j, nd, nd0, p2, p5, speccase;
3325
3326
12.7k
  dsign = bc->dsign;
3327
12.7k
  nd = bc->nd;
3328
12.7k
  nd0 = bc->nd0;
3329
12.7k
  p5 = nd + bc->e0 - 1;
3330
12.7k
  speccase = 0;
3331
12.7k
#ifndef Sudden_Underflow
3332
12.7k
  if (rv->d == 0.) { /* special case: value near underflow-to-zero */
3333
        /* threshold was rounded to zero */
3334
1.17k
    b = i2b(1 MTa);
3335
1.17k
    p2 = Emin - P + 1;
3336
1.17k
    bbits = 1;
3337
1.17k
#ifdef Avoid_Underflow
3338
1.17k
    word0(rv) = (P+2) << Exp_shift;
3339
#else
3340
    word1(rv) = 1;
3341
#endif
3342
1.17k
    i = 0;
3343
#ifdef Honor_FLT_ROUNDS
3344
    if (bc->rounding == 1)
3345
#endif
3346
1.17k
      {
3347
1.17k
      speccase = 1;
3348
1.17k
      --p2;
3349
1.17k
      dsign = 0;
3350
1.17k
      goto have_i;
3351
1.17k
      }
3352
1.17k
    }
3353
11.5k
  else
3354
11.5k
#endif
3355
11.5k
    b = d2b(rv, &p2, &bbits MTa);
3356
11.5k
#ifdef Avoid_Underflow
3357
11.5k
  p2 -= bc->scale;
3358
11.5k
#endif
3359
  /* floor(log2(rv)) == bbits - 1 + p2 */
3360
  /* Check for denormal case. */
3361
11.5k
  i = P - bbits;
3362
11.5k
  if (i > (j = P - Emin - 1 + p2)) {
3363
#ifdef Sudden_Underflow
3364
    Bfree(b MTa);
3365
    b = i2b(1);
3366
    p2 = Emin;
3367
    i = P - 1;
3368
#ifdef Avoid_Underflow
3369
    word0(rv) = (1 + bc->scale) << Exp_shift;
3370
#else
3371
    word0(rv) = Exp_msk1;
3372
#endif
3373
    word1(rv) = 0;
3374
#else
3375
1.17k
    i = j;
3376
1.17k
#endif
3377
1.17k
    }
3378
#ifdef Honor_FLT_ROUNDS
3379
  if (bc->rounding != 1) {
3380
    if (i > 0)
3381
      b = lshift(b, i MTa);
3382
    if (dsign)
3383
      b = increment(b MTa);
3384
    }
3385
  else
3386
#endif
3387
11.5k
    {
3388
11.5k
    b = lshift(b, ++i MTa);
3389
11.5k
    b->x[0] |= 1;
3390
11.5k
    }
3391
11.5k
#ifndef Sudden_Underflow
3392
12.7k
 have_i:
3393
12.7k
#endif
3394
12.7k
  p2 -= p5 + i;
3395
12.7k
  d = i2b(1 MTa);
3396
  /* Arrange for convenient computation of quotients:
3397
   * shift left if necessary so divisor has 4 leading 0 bits.
3398
   */
3399
12.7k
  if (p5 > 0)
3400
7.20k
    d = pow5mult(d, p5 MTa);
3401
5.53k
  else if (p5 < 0)
3402
4.81k
    b = pow5mult(b, -p5 MTa);
3403
12.7k
  if (p2 > 0) {
3404
6.17k
    b2 = p2;
3405
6.17k
    d2 = 0;
3406
6.17k
    }
3407
6.56k
  else {
3408
6.56k
    b2 = 0;
3409
6.56k
    d2 = -p2;
3410
6.56k
    }
3411
12.7k
  i = dshift(d, d2);
3412
12.7k
  if ((b2 += i) > 0)
3413
12.2k
    b = lshift(b, b2 MTa);
3414
12.7k
  if ((d2 += i) > 0)
3415
11.4k
    d = lshift(d, d2 MTa);
3416
3417
  /* Now b/d = exactly half-way between the two floating-point values */
3418
  /* on either side of the input string.  Compute first digit of b/d. */
3419
3420
12.7k
  if (!(dig = quorem(b,d))) {
3421
0
    b = multadd(b, 10, 0 MTa);  /* very unlikely */
3422
0
    dig = quorem(b,d);
3423
0
    }
3424
3425
  /* Compare b/d with s0 */
3426
3427
182k
  for(i = 0; i < nd0; ) {
3428
179k
    if ((dd = s0[i++] - '0' - dig))
3429
6.84k
      goto ret;
3430
172k
    if (!b->x[0] && b->wds == 1) {
3431
2.10k
      if (i < nd)
3432
826
        dd = 1;
3433
2.10k
      goto ret;
3434
2.10k
      }
3435
170k
    b = multadd(b, 10, 0 MTa);
3436
170k
    dig = quorem(b,d);
3437
170k
    }
3438
40.7k
  for(j = bc->dp1; i++ < nd;) {
3439
40.0k
    if ((dd = s0[j++] - '0' - dig))
3440
2.61k
      goto ret;
3441
37.4k
    if (!b->x[0] && b->wds == 1) {
3442
418
      if (i < nd)
3443
216
        dd = 1;
3444
418
      goto ret;
3445
418
      }
3446
36.9k
    b = multadd(b, 10, 0 MTa);
3447
36.9k
    dig = quorem(b,d);
3448
36.9k
    }
3449
751
  if (dig > 0 || b->x[0] || b->wds > 1)
3450
751
    dd = -1;
3451
12.7k
 ret:
3452
12.7k
  Bfree(b MTa);
3453
12.7k
  Bfree(d MTa);
3454
#ifdef Honor_FLT_ROUNDS
3455
  if (bc->rounding != 1) {
3456
    if (dd < 0) {
3457
      if (bc->rounding == 0) {
3458
        if (!dsign)
3459
          goto retlow1;
3460
        }
3461
      else if (dsign)
3462
        goto rethi1;
3463
      }
3464
    else if (dd > 0) {
3465
      if (bc->rounding == 0) {
3466
        if (dsign)
3467
          goto rethi1;
3468
        goto ret1;
3469
        }
3470
      if (!dsign)
3471
        goto rethi1;
3472
      dval(rv) += 2.*sulp(rv,bc);
3473
      }
3474
    else {
3475
      bc->inexact = 0;
3476
      if (dsign)
3477
        goto rethi1;
3478
      }
3479
    }
3480
  else
3481
#endif
3482
12.7k
  if (speccase) {
3483
1.17k
    if (dd <= 0)
3484
1.17k
      rv->d = 0.;
3485
1.17k
    }
3486
11.5k
  else if (dd < 0) {
3487
6.57k
    if (!dsign)  /* does not happen for round-near */
3488
0
retlow1:
3489
0
      dval(rv) -= sulp(rv,bc);
3490
6.57k
    }
3491
4.99k
  else if (dd > 0) {
3492
3.50k
    if (dsign) {
3493
4.21k
 rethi1:
3494
4.21k
      dval(rv) += sulp(rv,bc);
3495
4.21k
      }
3496
3.50k
    }
3497
1.48k
  else {
3498
    /* Exact half-way case:  apply round-even rule. */
3499
1.48k
    if ((j = ((word0(rv) & Exp_mask) >> Exp_shift) - bc->scale) <= 0) {
3500
0
      i = 1 - j;
3501
0
      if (i <= 31) {
3502
0
        if (word1(rv) & (0x1 << i))
3503
0
          goto odd;
3504
0
        }
3505
0
      else if (word0(rv) & (0x1 << (i-32)))
3506
0
        goto odd;
3507
0
      }
3508
1.48k
    else if (word1(rv) & 1) {
3509
711
 odd:
3510
711
      if (dsign)
3511
711
        goto rethi1;
3512
0
      goto retlow1;
3513
711
      }
3514
1.48k
    }
3515
3516
#ifdef Honor_FLT_ROUNDS
3517
 ret1:
3518
#endif
3519
12.7k
  return;
3520
12.7k
  }
3521
#endif /* NO_STRTOD_BIGCOMP */
3522
3523
 double
3524
strtod2(const char *s00, char **se __XS__d)
3525
27.9M
{
3526
27.9M
  int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c, e, e1;
3527
27.9M
  int esign, i, j, k, nd, nd0, nf, nz, nz0, nz1, sign;
3528
27.9M
  const char *s, *s0, *s1;
3529
27.9M
  double aadj, aadj1;
3530
27.9M
  Long L;
3531
27.9M
  U aadj2, adj, rv, rv0;
3532
27.9M
  ULong y, z;
3533
27.9M
  BCinfo bc;
3534
27.9M
  Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
3535
#ifdef USE_BF96
3536
  ULLong bhi, blo, brv, t00, t01, t02, t10, t11, terv, tg, tlo, yz;
3537
  const BF96 *p10;
3538
  int bexact, erv;
3539
#endif
3540
27.9M
#ifdef Avoid_Underflow
3541
#ifndef ROUND_BIASED
3542
  ULong Lsb;
3543
  ULong Lsb1;
3544
#endif
3545
27.9M
#endif
3546
#ifdef SET_INEXACT
3547
  int oldinexact;
3548
#endif
3549
27.9M
#ifndef NO_STRTOD_BIGCOMP
3550
27.9M
  int req_bigcomp = 0;
3551
27.9M
#endif
3552
#ifdef MULTIPLE_THREADS
3553
  ThInfo *TI = 0;
3554
#endif
3555
#ifdef Honor_FLT_ROUNDS /*{*/
3556
#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
3557
  bc.rounding = Flt_Rounds;
3558
#else /*}{*/
3559
  bc.rounding = 1;
3560
  switch(fegetround()) {
3561
    case FE_TOWARDZERO: bc.rounding = 0; break;
3562
    case FE_UPWARD: bc.rounding = 2; break;
3563
    case FE_DOWNWARD: bc.rounding = 3;
3564
    }
3565
#endif /*}}*/
3566
#endif /*}*/
3567
#ifdef USE_LOCALE
3568
  const char *s2;
3569
#endif
3570
3571
27.9M
  sign = nz0 = nz1 = nz = bc.dplen = bc.uflchk = 0;
3572
27.9M
  dval(&rv) = 0.;
3573
27.9M
  for(s = s00;;s++) switch(c_read8(s)) {
3574
1.28M
    case '-':
3575
1.28M
      sign = 1;
3576
      /* no break */
3577
1.30M
    case '+':
3578
1.30M
      if (c_read8(++s))
3579
1.29M
        goto break2;
3580
      /* no break */
3581
14.0k
    case 0:
3582
14.0k
      goto ret0;
3583
0
    case '\t':
3584
0
    case '\n':
3585
0
    case '\v':
3586
0
    case '\f':
3587
0
    case '\r':
3588
0
    case ' ':
3589
0
      continue;
3590
26.6M
    default:
3591
26.6M
      goto break2;
3592
27.9M
    }
3593
27.9M
 break2:
3594
27.9M
  if (c_read8(s) == '0') {
3595
#ifndef NO_HEX_FP /*{*/
3596
    switch(s[1]) {
3597
      case 'x':
3598
      case 'X':
3599
#ifdef Honor_FLT_ROUNDS
3600
      gethex(&s, &rv, bc.rounding, sign MTb);
3601
#else
3602
      gethex(&s, &rv, 1, sign MTb);
3603
#endif
3604
      goto ret;
3605
      }
3606
#endif /*}*/
3607
765k
    nz0 = 1;
3608
778k
    while(c_read8(++s) == '0') ;
3609
765k
    if (!c_read8(s))
3610
585k
      goto ret;
3611
765k
    }
3612
27.3M
  s0 = s;
3613
27.3M
  nd = nf = 0;
3614
#ifdef USE_BF96
3615
  yz = 0;
3616
  for(; (c = c_read8(s)) >= '0' && c <= '9'; nd++, s++)
3617
    if (nd < 19)
3618
      yz = 10*yz + c - '0';
3619
#else
3620
27.3M
  y = z = 0;
3621
95.1M
  for(; (c = c_read8(s)) >= '0' && c <= '9'; nd++, s++)
3622
67.7M
    if (nd < 9)
3623
47.1M
      y = 10*y + c - '0';
3624
20.6M
    else if (nd < DBL_DIG + 2)
3625
10.1M
      z = 10*z + c - '0';
3626
27.3M
#endif
3627
27.3M
  nd0 = nd;
3628
27.3M
  bc.dp0 = bc.dp1 = (int)(s - s0);
3629
30.4M
  for(s1 = s; s1 > s0 && *--s1 == '0'; )
3630
3.04M
    ++nz1;
3631
#ifdef USE_LOCALE
3632
  s1 = localeconv()->decimal_point;
3633
  if (c == *s1) {
3634
    c = '.';
3635
    if (*++s1) {
3636
      s2 = s;
3637
      for(;;) {
3638
        if (*++s2 != *s1) {
3639
          c = 0;
3640
          break;
3641
          }
3642
        if (!*++s1) {
3643
          s = s2;
3644
          break;
3645
          }
3646
        }
3647
      }
3648
    }
3649
#endif
3650
27.3M
  if (c == '.') {
3651
2.54M
    c = c_read8(++s);
3652
2.54M
    bc.dp1 = (int)(s - s0);
3653
2.54M
    bc.dplen = bc.dp1 - bc.dp0;
3654
2.54M
    if (!nd) {
3655
17.8M
      for(; c == '0'; c = c_read8(++s))
3656
17.5M
        nz++;
3657
307k
      if (c > '0' && c <= '9') {
3658
76.9k
        bc.dp0 = (int)(s0 - s);
3659
76.9k
        bc.dp1 = bc.dp0 + bc.dplen;
3660
76.9k
        s0 = s;
3661
76.9k
        nf += nz;
3662
76.9k
        nz = 0;
3663
76.9k
        goto have_dig;
3664
76.9k
        }
3665
230k
      goto dig_done;
3666
307k
      }
3667
54.2M
    for(; c >= '0' && c <= '9'; c = c_read8(++s)) {
3668
52.0M
 have_dig:
3669
52.0M
      nz++;
3670
52.0M
      if (c -= '0') {
3671
23.5M
        nf += nz;
3672
23.5M
        i = 1;
3673
#ifdef USE_BF96
3674
        for(; i < nz; ++i) {
3675
          if (++nd <= 19)
3676
            yz *= 10;
3677
          }
3678
        if (++nd <= 19)
3679
          yz = 10*yz + c;
3680
#else
3681
51.4M
        for(; i < nz; ++i) {
3682
27.9M
          if (nd++ < 9)
3683
1.24M
            y *= 10;
3684
26.7M
          else if (nd <= DBL_DIG + 2)
3685
1.22M
            z *= 10;
3686
27.9M
          }
3687
23.5M
        if (nd++ < 9)
3688
11.1M
          y = 10*y + c;
3689
12.3M
        else if (nd <= DBL_DIG + 2)
3690
11.6M
          z = 10*z + c;
3691
23.5M
#endif
3692
23.5M
        nz = nz1 = 0;
3693
23.5M
        }
3694
52.0M
      }
3695
2.23M
    }
3696
27.3M
 dig_done:
3697
27.3M
  e = 0;
3698
27.3M
  if (c == 'e' || c == 'E') {
3699
1.84M
    if (!nd && !nz && !nz0) {
3700
286k
      goto ret0;
3701
286k
      }
3702
1.55M
    s00 = s;
3703
1.55M
    esign = 0;
3704
1.55M
    switch(c = c_read8(++s)) {
3705
512k
      case '-':
3706
512k
        esign = 1;
3707
1.48M
      case '+':
3708
1.48M
        c = c_read8(++s);
3709
1.55M
      }
3710
1.55M
    if (c >= '0' && c <= '9') {
3711
1.56M
      while(c == '0')
3712
18.1k
        c = c_read8(++s);
3713
1.54M
      if (c > '0' && c <= '9') {
3714
1.54M
        L = c - '0';
3715
1.54M
        s1 = s;
3716
3.79M
        while((c = *++s) >= '0' && c <= '9') {
3717
2.25M
          if (L <= 19999)
3718
2.18M
            L = 10*L + c - '0';
3719
2.25M
          }
3720
1.54M
        if (L > 19999)
3721
          /* Avoid confusion from exponents
3722
           * so large that e might overflow.
3723
           */
3724
3.20k
          e = 19999; /* safe for 16 bit ints */
3725
1.54M
        else
3726
1.54M
          e = (int)L;
3727
1.54M
        if (esign)
3728
512k
          e = -e;
3729
1.54M
        }
3730
2.73k
      else
3731
2.73k
        e = 0;
3732
1.54M
      }
3733
5.34k
    else
3734
5.34k
      s = s00;
3735
1.55M
    }
3736
27.0M
  if (!nd) {
3737
7.71M
    if (!nz && !nz0) {
3738
7.63M
#ifdef INFNAN_CHECK /*{*/
3739
      /* Check for Nan and Infinity */
3740
7.63M
      if (!bc.dplen)
3741
7.40M
       switch(c) {
3742
#ifndef __XS__
3743
        case 'i':
3744
        case 'I':
3745
        if (match(&s,"nf")) {
3746
          --s;
3747
          if (!match(&s,"inity"))
3748
            ++s;
3749
#else
3750
39.8k
        case 'I':
3751
39.8k
        if (match(&s,"nfinity")) {
3752
37.6k
#endif
3753
37.6k
          word0(&rv) = 0x7ff00000;
3754
37.6k
          word1(&rv) = 0;
3755
37.6k
          goto ret;
3756
37.6k
          }
3757
2.20k
        break;
3758
#ifndef __XS__
3759
        case 'n':
3760
        case 'N':
3761
        if (match(&s, "an")) {
3762
#else
3763
145k
        case 'N':
3764
145k
        if (match(&s, "aN")) {
3765
142k
#endif
3766
142k
          word0(&rv) = NAN_WORD0;
3767
142k
          word1(&rv) = NAN_WORD1;
3768
142k
#ifndef No_Hex_NaN
3769
142k
          if (c_read8(s) == '(') /*)*/
3770
8.07k
            hexnan(&rv, &s);
3771
142k
#endif
3772
142k
          goto ret;
3773
142k
          }
3774
7.40M
        }
3775
7.45M
#endif /*} INFNAN_CHECK */
3776
7.75M
 ret0:
3777
7.75M
      s = s00;
3778
7.75M
      sign = 0;
3779
7.75M
      }
3780
7.83M
    goto ret;
3781
7.71M
    }
3782
19.3M
  bc.e0 = e1 = e -= nf;
3783
3784
  /* Now we have nd0 digits, starting at s0, followed by a
3785
   * decimal point, followed by nd-nd0 digits.  The number we're
3786
   * after is the integer represented by those digits times
3787
   * 10**e */
3788
3789
19.3M
  if (!nd0)
3790
76.9k
    nd0 = nd;
3791
19.3M
#ifndef USE_BF96
3792
19.3M
  k = nd < DBL_DIG + 2 ? nd : DBL_DIG + 2;
3793
19.3M
  dval(&rv) = y;
3794
19.3M
  if (k > 9) {
3795
#ifdef SET_INEXACT
3796
    if (k > DBL_DIG)
3797
      oldinexact = get_inexact();
3798
#endif
3799
3.43M
    dval(&rv) = tens[k - 9] * dval(&rv) + z;
3800
3.43M
    }
3801
19.3M
#endif
3802
19.3M
  bd0 = 0;
3803
19.3M
  if (nd <= DBL_DIG
3804
16.5M
#ifndef RND_PRODQUOT
3805
16.5M
#ifndef Honor_FLT_ROUNDS
3806
16.5M
    && Flt_Rounds == 1
3807
19.3M
#endif
3808
19.3M
#endif
3809
19.3M
      ) {
3810
#ifdef USE_BF96
3811
    dval(&rv) = yz;
3812
#endif
3813
16.5M
    if (!e)
3814
16.2M
      goto ret;
3815
363k
#ifndef ROUND_BIASED_without_Round_Up
3816
363k
    if (e > 0) {
3817
78.1k
      if (e <= Ten_pmax) {
3818
#ifdef SET_INEXACT
3819
        bc.inexact = 0;
3820
        oldinexact = 1;
3821
#endif
3822
#ifdef VAX
3823
        goto vax_ovfl_check;
3824
#else
3825
#ifdef Honor_FLT_ROUNDS
3826
        /* round correctly FLT_ROUNDS = 2 or 3 */
3827
        if (sign) {
3828
          rv.d = -rv.d;
3829
          sign = 0;
3830
          }
3831
#endif
3832
14.4k
        /* rv = */ rounded_product(dval(&rv), tens[e]);
3833
14.4k
        goto ret;
3834
14.4k
#endif
3835
14.4k
        }
3836
63.6k
      i = DBL_DIG - nd;
3837
63.6k
      if (e <= Ten_pmax + i) {
3838
        /* A fancier test would sometimes let us do
3839
         * this for larger i values.
3840
         */
3841
#ifdef SET_INEXACT
3842
        bc.inexact = 0;
3843
        oldinexact = 1;
3844
#endif
3845
#ifdef Honor_FLT_ROUNDS
3846
        /* round correctly FLT_ROUNDS = 2 or 3 */
3847
        if (sign) {
3848
          rv.d = -rv.d;
3849
          sign = 0;
3850
          }
3851
#endif
3852
1.98k
        e -= i;
3853
1.98k
        dval(&rv) *= tens[i];
3854
#ifdef VAX
3855
        /* VAX exponent range is so narrow we must
3856
         * worry about overflow here...
3857
         */
3858
 vax_ovfl_check:
3859
        word0(&rv) -= P*Exp_msk1;
3860
        /* rv = */ rounded_product(dval(&rv), tens[e]);
3861
        if ((word0(&rv) & Exp_mask)
3862
         > Exp_msk1*(DBL_MAX_EXP+Bias-1-P))
3863
          goto ovfl;
3864
        word0(&rv) += P*Exp_msk1;
3865
#else
3866
1.98k
        /* rv = */ rounded_product(dval(&rv), tens[e]);
3867
1.98k
#endif
3868
1.98k
        goto ret;
3869
1.98k
        }
3870
63.6k
      }
3871
285k
#ifndef Inaccurate_Divide
3872
285k
    else if (e >= -Ten_pmax) {
3873
#ifdef SET_INEXACT
3874
        bc.inexact = 0;
3875
        oldinexact = 1;
3876
#endif
3877
#ifdef Honor_FLT_ROUNDS
3878
      /* round correctly FLT_ROUNDS = 2 or 3 */
3879
      if (sign) {
3880
        rv.d = -rv.d;
3881
        sign = 0;
3882
        }
3883
#endif
3884
227k
      /* rv = */ rounded_quotient(dval(&rv), tens[-e]);
3885
227k
      goto ret;
3886
227k
      }
3887
363k
#endif
3888
363k
#endif /* ROUND_BIASED_without_Round_Up */
3889
363k
    }
3890
#ifdef USE_BF96
3891
  k = nd < 19 ? nd : 19;
3892
#endif
3893
2.91M
  e1 += nd - k; /* scale factor = 10^e1 */
3894
3895
2.91M
#ifdef IEEE_Arith
3896
#ifdef SET_INEXACT
3897
  bc.inexact = 1;
3898
#ifndef USE_BF96
3899
  if (k <= DBL_DIG)
3900
#endif
3901
    oldinexact = get_inexact();
3902
#endif
3903
#ifdef Honor_FLT_ROUNDS
3904
  if (bc.rounding >= 2) {
3905
    if (sign)
3906
      bc.rounding = bc.rounding == 2 ? 0 : 2;
3907
    else
3908
      if (bc.rounding != 2)
3909
        bc.rounding = 0;
3910
    }
3911
#endif
3912
2.91M
#endif /*IEEE_Arith*/
3913
3914
#ifdef USE_BF96 /*{*/
3915
  Debug(++dtoa_stats[0]);
3916
  i = e1 + 342;
3917
  if (i < 0)
3918
    goto undfl;
3919
  if (i > 650)
3920
    goto ovfl;
3921
  p10 = &pten[i];
3922
  brv = yz;
3923
  /* shift brv left, with i =  number of bits shifted */
3924
  i = 0;
3925
  if (!(brv & 0xffffffff00000000ull)) {
3926
    i = 32;
3927
    brv <<= 32;
3928
    }
3929
  if (!(brv & 0xffff000000000000ull)) {
3930
    i += 16;
3931
    brv <<= 16;
3932
    }
3933
  if (!(brv & 0xff00000000000000ull)) {
3934
    i += 8;
3935
    brv <<= 8;
3936
    }
3937
  if (!(brv & 0xf000000000000000ull)) {
3938
    i += 4;
3939
    brv <<= 4;
3940
    }
3941
  if (!(brv & 0xc000000000000000ull)) {
3942
    i += 2;
3943
    brv <<= 2;
3944
    }
3945
  if (!(brv & 0x8000000000000000ull)) {
3946
    i += 1;
3947
    brv <<= 1;
3948
    }
3949
  erv = (64 + 0x3fe) + p10->e - i;
3950
  if (erv <= 0 && nd > 19)
3951
    goto many_digits; /* denormal: may need to look at all digits */
3952
  bhi = brv >> 32;
3953
  blo = brv & 0xffffffffull;
3954
  /* Unsigned 32-bit ints lie in [0,2^32-1] and */
3955
  /* unsigned 64-bit ints lie in [0, 2^64-1].  The product of two unsigned */
3956
  /* 32-bit ints is <= 2^64 - 2*2^32-1 + 1 = 2^64 - 1 - 2*(2^32 - 1), so */
3957
  /* we can add two unsigned 32-bit ints to the product of two such ints, */
3958
  /* and 64 bits suffice to contain the result. */
3959
  t01 = bhi * p10->b1;
3960
  t10 = blo * p10->b0 + (t01 & 0xffffffffull);
3961
  t00 = bhi * p10->b0 + (t01 >> 32) + (t10 >> 32);
3962
  if (t00 & 0x8000000000000000ull) {
3963
    if ((t00 & 0x3ff) && (~t00 & 0x3fe)) { /* unambiguous result? */
3964
      if (nd > 19 && ((t00 + (1<<i) + 2) & 0x400) ^ (t00 & 0x400))
3965
        goto many_digits;
3966
      if (erv <= 0)
3967
        goto denormal;
3968
#ifdef Honor_FLT_ROUNDS
3969
      switch(bc.rounding) {
3970
        case 0: goto noround;
3971
        case 2: goto roundup;
3972
        }
3973
#endif
3974
      if (t00 & 0x400 && t00 & 0xbff)
3975
        goto roundup;
3976
      goto noround;
3977
      }
3978
    }
3979
  else {
3980
    if ((t00 & 0x1ff) && (~t00 & 0x1fe)) { /* unambiguous result? */
3981
      if (nd > 19 && ((t00 + (1<<i) + 2) & 0x200) ^ (t00 & 0x200))
3982
        goto many_digits;
3983
      if (erv <= 1)
3984
        goto denormal1;
3985
#ifdef Honor_FLT_ROUNDS
3986
      switch(bc.rounding) {
3987
        case 0: goto noround1;
3988
        case 2: goto roundup1;
3989
        }
3990
#endif
3991
      if (t00 & 0x200)
3992
        goto roundup1;
3993
      goto noround1;
3994
      }
3995
    }
3996
  /* 3 multiplies did not suffice; try a 96-bit approximation */
3997
  Debug(++dtoa_stats[1]);
3998
  t02 = bhi * p10->b2;
3999
  t11 = blo * p10->b1 + (t02 & 0xffffffffull);
4000
  bexact = 1;
4001
  if (e1 < 0 || e1 > 41 || (t10 | t11) & 0xffffffffull || nd > 19)
4002
    bexact = 0;
4003
  tlo = (t10 & 0xffffffffull) + (t02 >> 32) + (t11 >> 32);
4004
  if (!bexact && (tlo + 0x10) >> 32 > tlo >> 32)
4005
    goto many_digits;
4006
  t00 += tlo >> 32;
4007
  if (t00 & 0x8000000000000000ull) {
4008
    if (erv <= 0) { /* denormal result */
4009
      if (nd >= 20 || !((tlo & 0xfffffff0) | (t00 & 0x3ff)))
4010
        goto many_digits;
4011
 denormal:
4012
      if (erv <= -52) {
4013
#ifdef Honor_FLT_ROUNDS
4014
        switch(bc.rounding) {
4015
          case 0: goto undfl;
4016
          case 2: goto tiniest;
4017
          }
4018
#endif
4019
        if (erv < -52 || !(t00 & 0x7fffffffffffffffull))
4020
          goto undfl;
4021
        goto tiniest;
4022
        }
4023
      tg = 1ull << (11 - erv);
4024
      t00 &= ~(tg - 1); /* clear low bits */
4025
#ifdef Honor_FLT_ROUNDS
4026
      switch(bc.rounding) {
4027
        case 0: goto noround_den;
4028
        case 2: goto roundup_den;
4029
        }
4030
#endif
4031
      if (t00 & tg) {
4032
#ifdef Honor_FLT_ROUNDS
4033
 roundup_den:
4034
#endif
4035
        t00 += tg << 1;
4036
        if (!(t00 & 0x8000000000000000ull)) {
4037
          if (++erv > 0)
4038
            goto smallest_normal;
4039
          t00 = 0x8000000000000000ull;
4040
          }
4041
        }
4042
#ifdef Honor_FLT_ROUNDS
4043
 noround_den:
4044
#endif
4045
      LLval(&rv) = t00 >> (12 - erv);
4046
      Set_errno(ERANGE);
4047
      goto ret;
4048
      }
4049
    if (bexact) {
4050
#ifdef SET_INEXACT
4051
      if (!(t00 & 0x7ff) && !(tlo & 0xffffffffull)) {
4052
        bc.inexact = 0;
4053
        goto noround;
4054
        }
4055
#endif
4056
#ifdef Honor_FLT_ROUNDS
4057
      switch(bc.rounding) {
4058
        case 2:
4059
        if (t00 & 0x7ff)
4060
          goto roundup;
4061
        case 0: goto noround;
4062
        }
4063
#endif
4064
      if (t00 & 0x400 && (tlo & 0xffffffff) | (t00 & 0xbff))
4065
        goto roundup;
4066
      goto noround;
4067
      }
4068
    if ((tlo & 0xfffffff0) | (t00 & 0x3ff)
4069
     && (nd <= 19 ||  ((t00 + (1ull << i)) & 0xfffffffffffffc00ull)
4070
        == (t00 & 0xfffffffffffffc00ull))) {
4071
      /* Unambiguous result. */
4072
      /* If nd > 19, then incrementing the 19th digit */
4073
      /* does not affect rv. */
4074
#ifdef Honor_FLT_ROUNDS
4075
      switch(bc.rounding) {
4076
        case 0: goto noround;
4077
        case 2: goto roundup;
4078
        }
4079
#endif
4080
      if (t00 & 0x400) { /* round up */
4081
 roundup:
4082
        t00 += 0x800;
4083
        if (!(t00 & 0x8000000000000000ull)) {
4084
          /* rounded up to a power of 2 */
4085
          if (erv >= 0x7fe)
4086
            goto ovfl;
4087
          terv = erv + 1;
4088
          LLval(&rv) = terv << 52;
4089
          goto ret;
4090
          }
4091
        }
4092
 noround:
4093
      if (erv >= 0x7ff)
4094
        goto ovfl;
4095
      terv = erv;
4096
      LLval(&rv) = (terv << 52) | ((t00 & 0x7ffffffffffff800ull) >> 11);
4097
      goto ret;
4098
      }
4099
    }
4100
  else {
4101
    if (erv <= 1) { /* denormal result */
4102
      if (nd >= 20 || !((tlo & 0xfffffff0) | (t00 & 0x1ff)))
4103
        goto many_digits;
4104
 denormal1:
4105
      if (erv <= -51) {
4106
#ifdef Honor_FLT_ROUNDS
4107
        switch(bc.rounding) {
4108
          case 0: goto undfl;
4109
          case 2: goto tiniest;
4110
          }
4111
#endif
4112
        if (erv < -51 || !(t00 & 0x3fffffffffffffffull))
4113
          goto undfl;
4114
 tiniest:
4115
        LLval(&rv) = 1;
4116
        Set_errno(ERANGE);
4117
        goto ret;
4118
        }
4119
      tg = 1ull << (11 - erv);
4120
#ifdef Honor_FLT_ROUNDS
4121
      switch(bc.rounding) {
4122
        case 0: goto noround1_den;
4123
        case 2: goto roundup1_den;
4124
        }
4125
#endif
4126
      if (t00 & tg) {
4127
#ifdef Honor_FLT_ROUNDS
4128
 roundup1_den:
4129
#endif
4130
        if (0x8000000000000000ull & (t00 += (tg<<1)) && erv == 1) {
4131
4132
 smallest_normal:
4133
          LLval(&rv) = 0x0010000000000000ull;
4134
          goto ret;
4135
          }
4136
        }
4137
#ifdef Honor_FLT_ROUNDS
4138
 noround1_den:
4139
#endif
4140
      if (erv <= -52)
4141
        goto undfl;
4142
      LLval(&rv) = t00 >> (12 - erv);
4143
      Set_errno(ERANGE);
4144
      goto ret;
4145
      }
4146
    if (bexact) {
4147
#ifdef SET_INEXACT
4148
      if (!(t00 & 0x3ff) && !(tlo & 0xffffffffull)) {
4149
        bc.inexact = 0;
4150
        goto noround1;
4151
        }
4152
#endif
4153
#ifdef Honor_FLT_ROUNDS
4154
      switch(bc.rounding) {
4155
        case 2:
4156
        if (t00 & 0x3ff)
4157
          goto roundup1;
4158
        case 0: goto noround1;
4159
        }
4160
#endif
4161
      if (t00 & 0x200 && (t00 & 0x5ff || tlo))
4162
        goto roundup1;
4163
      goto noround1;
4164
      }
4165
    if ((tlo & 0xfffffff0) | (t00 & 0x1ff)
4166
     && (nd <= 19 ||  ((t00 + (1ull << i)) & 0x7ffffffffffffe00ull)
4167
        == (t00 & 0x7ffffffffffffe00ull))) {
4168
      /* Unambiguous result. */
4169
#ifdef Honor_FLT_ROUNDS
4170
      switch(bc.rounding) {
4171
        case 0: goto noround1;
4172
        case 2: goto roundup1;
4173
        }
4174
#endif
4175
      if (t00 & 0x200) { /* round up */
4176
 roundup1:
4177
        t00 += 0x400;
4178
        if (!(t00 & 0x4000000000000000ull)) {
4179
          /* rounded up to a power of 2 */
4180
          if (erv >= 0x7ff)
4181
            goto ovfl;
4182
          terv = erv;
4183
          LLval(&rv) = terv << 52;
4184
          goto ret;
4185
          }
4186
        }
4187
 noround1:
4188
      if (erv >= 0x800)
4189
        goto ovfl;
4190
      terv = erv - 1;
4191
      LLval(&rv) = (terv << 52) | ((t00 & 0x3ffffffffffffc00ull) >> 10);
4192
      goto ret;
4193
      }
4194
    }
4195
 many_digits:
4196
  Debug(++dtoa_stats[2]);
4197
  if (nd > 17) {
4198
    if (nd > 18) {
4199
      yz /= 100;
4200
      e1 += 2;
4201
      }
4202
    else {
4203
      yz /= 10;
4204
      e1 += 1;
4205
      }
4206
    y = yz / 100000000;
4207
    }
4208
  else if (nd > 9) {
4209
    i = nd - 9;
4210
    y = (yz >> i) / pfive[i-1];
4211
    }
4212
  else
4213
    y = yz;
4214
  dval(&rv) = yz;
4215
#endif /*}*/
4216
4217
2.91M
#ifdef IEEE_Arith
4218
2.91M
#ifdef Avoid_Underflow
4219
2.91M
  bc.scale = 0;
4220
2.91M
#endif
4221
2.91M
#endif /*IEEE_Arith*/
4222
4223
  /* Get starting approximation = rv * 10**e1 */
4224
4225
2.91M
  if (e1 > 0) {
4226
1.22M
    if ((i = e1 & 15))
4227
1.17M
      dval(&rv) *= tens[i];
4228
1.22M
    if (e1 &= ~15) {
4229
796k
      if (e1 > DBL_MAX_10_EXP) {
4230
19.9k
 ovfl:
4231
        /* Can't trust HUGE_VAL */
4232
19.9k
#ifdef IEEE_Arith
4233
#ifdef Honor_FLT_ROUNDS
4234
        switch(bc.rounding) {
4235
          case 0: /* toward 0 */
4236
          case 3: /* toward -infinity */
4237
          word0(&rv) = Big0;
4238
          word1(&rv) = Big1;
4239
          break;
4240
          default:
4241
          word0(&rv) = Exp_mask;
4242
          word1(&rv) = 0;
4243
          }
4244
#else /*Honor_FLT_ROUNDS*/
4245
19.9k
        word0(&rv) = Exp_mask;
4246
19.9k
        word1(&rv) = 0;
4247
19.9k
#endif /*Honor_FLT_ROUNDS*/
4248
#ifdef SET_INEXACT
4249
        /* set overflow bit */
4250
        dval(&rv0) = 1e300;
4251
        dval(&rv0) *= dval(&rv0);
4252
#endif
4253
#else /*IEEE_Arith*/
4254
        word0(&rv) = Big0;
4255
        word1(&rv) = Big1;
4256
#endif /*IEEE_Arith*/
4257
26.1k
 range_err:
4258
26.1k
        if (bd0) {
4259
547
          Bfree(bb MTb);
4260
547
          Bfree(bd MTb);
4261
547
          Bfree(bs MTb);
4262
547
          Bfree(bd0 MTb);
4263
547
          Bfree(delta MTb);
4264
547
          }
4265
26.1k
        Set_errno(ERANGE);
4266
26.1k
        goto ret;
4267
19.9k
        }
4268
784k
      e1 >>= 4;
4269
2.30M
      for(j = 0; e1 > 1; j++, e1 >>= 1)
4270
1.51M
        if (e1 & 1)
4271
632k
          dval(&rv) *= bigtens[j];
4272
    /* The last multiplication could overflow. */
4273
784k
      word0(&rv) -= P*Exp_msk1;
4274
784k
      dval(&rv) *= bigtens[j];
4275
784k
      if ((z = word0(&rv) & Exp_mask)
4276
784k
       > Exp_msk1*(DBL_MAX_EXP+Bias-P))
4277
7.06k
        goto ovfl;
4278
777k
      if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
4279
        /* set to largest number */
4280
        /* (Can't trust DBL_MAX) */
4281
1.49k
        word0(&rv) = Big0;
4282
1.49k
        word1(&rv) = Big1;
4283
1.49k
        }
4284
776k
      else
4285
776k
        word0(&rv) += P*Exp_msk1;
4286
777k
      }
4287
1.22M
    }
4288
1.68M
  else if (e1 < 0) {
4289
1.06M
    e1 = -e1;
4290
1.06M
    if ((i = e1 & 15))
4291
1.02M
      dval(&rv) /= tens[i];
4292
1.06M
    if (e1 >>= 4) {
4293
526k
      if (e1 >= 1 << n_bigtens)
4294
2.32k
        goto undfl;
4295
524k
#ifdef Avoid_Underflow
4296
524k
      if (e1 & Scale_Bit)
4297
63.4k
        bc.scale = 2*P;
4298
2.18M
      for(j = 0; e1 > 0; j++, e1 >>= 1)
4299
1.65M
        if (e1 & 1)
4300
980k
          dval(&rv) *= tinytens[j];
4301
524k
      if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
4302
63.4k
            >> Exp_shift)) > 0) {
4303
        /* scaled rv is denormal; clear j low bits */
4304
8.35k
        if (j >= 32) {
4305
5.87k
          if (j > 54)
4306
2.72k
            goto undfl;
4307
3.15k
          word1(&rv) = 0;
4308
3.15k
          if (j >= 53)
4309
1.18k
           word0(&rv) = (P+2)*Exp_msk1;
4310
1.96k
          else
4311
1.96k
           word0(&rv) &= 0xffffffff << (j-32);
4312
3.15k
          }
4313
2.48k
        else
4314
2.48k
          word1(&rv) &= 0xffffffff << j;
4315
8.35k
        }
4316
#else
4317
      for(j = 0; e1 > 1; j++, e1 >>= 1)
4318
        if (e1 & 1)
4319
          dval(&rv) *= tinytens[j];
4320
      /* The last multiplication could underflow. */
4321
      dval(&rv0) = dval(&rv);
4322
      dval(&rv) *= tinytens[j];
4323
      if (!dval(&rv)) {
4324
        dval(&rv) = 2.*dval(&rv0);
4325
        dval(&rv) *= tinytens[j];
4326
#endif
4327
521k
        if (!dval(&rv)) {
4328
6.21k
 undfl:
4329
6.21k
          dval(&rv) = 0.;
4330
#ifdef Honor_FLT_ROUNDS
4331
          if (bc.rounding == 2)
4332
            word1(&rv) = 1;
4333
#endif
4334
6.21k
          goto range_err;
4335
0
          }
4336
#ifndef Avoid_Underflow
4337
        word0(&rv) = Tiny0;
4338
        word1(&rv) = Tiny1;
4339
        /* The refinement below will clean
4340
         * this approximation up.
4341
         */
4342
        }
4343
#endif
4344
521k
      }
4345
1.06M
    }
4346
4347
  /* Now the hard part -- adjusting rv to the correct value.*/
4348
4349
  /* Put digits into bd: true value = bd * 10^e */
4350
4351
2.88M
  bc.nd = nd - nz1;
4352
2.88M
#ifndef NO_STRTOD_BIGCOMP
4353
2.88M
  bc.nd0 = nd0; /* Only needed if nd > strtod_diglim, but done here */
4354
      /* to silence an erroneous warning about bc.nd0 */
4355
      /* possibly not being initialized. */
4356
2.88M
  if (nd > strtod_diglim) {
4357
    /* ASSERT(strtod_diglim >= 18); 18 == one more than the */
4358
    /* minimum number of decimal digits to distinguish double values */
4359
    /* in IEEE arithmetic. */
4360
13.3k
    i = j = 18;
4361
13.3k
    if (i > nd0)
4362
3.62k
      j += bc.dplen;
4363
49.5k
    for(;;) {
4364
49.5k
      if (--j < bc.dp1 && j >= bc.dp0)
4365
497
        j = bc.dp0 - 1;
4366
49.5k
      if (s0[j] != '0')
4367
13.3k
        break;
4368
36.1k
      --i;
4369
36.1k
      }
4370
13.3k
    e += nd - i;
4371
13.3k
    nd = i;
4372
13.3k
    if (nd0 > nd)
4373
9.75k
      nd0 = nd;
4374
13.3k
    if (nd < 9) { /* must recompute y */
4375
1.62k
      y = 0;
4376
6.41k
      for(i = 0; i < nd0; ++i)
4377
4.79k
        y = 10*y + s0[i] - '0';
4378
3.69k
      for(j = bc.dp1; i < nd; ++i)
4379
2.06k
        y = 10*y + s0[j++] - '0';
4380
1.62k
      }
4381
13.3k
    }
4382
2.88M
#endif
4383
2.88M
  bd0 = s2b(s0, nd0, nd, y, bc.dplen MTb);
4384
4385
3.01M
  for(;;) {
4386
3.01M
    bd = Balloc(bd0->k MTb);
4387
3.01M
    Bcopy(bd, bd0);
4388
3.01M
    bb = d2b(&rv, &bbe, &bbbits MTb);  /* rv = bb * 2^bbe */
4389
3.01M
    bs = i2b(1 MTb);
4390
4391
3.01M
    if (e >= 0) {
4392
1.91M
      bb2 = bb5 = 0;
4393
1.91M
      bd2 = bd5 = e;
4394
1.91M
      }
4395
1.10M
    else {
4396
1.10M
      bb2 = bb5 = -e;
4397
1.10M
      bd2 = bd5 = 0;
4398
1.10M
      }
4399
3.01M
    if (bbe >= 0)
4400
1.92M
      bb2 += bbe;
4401
1.09M
    else
4402
1.09M
      bd2 -= bbe;
4403
3.01M
    bs2 = bb2;
4404
#ifdef Honor_FLT_ROUNDS
4405
    if (bc.rounding != 1)
4406
      bs2++;
4407
#endif
4408
3.01M
#ifdef Avoid_Underflow
4409
#ifndef ROUND_BIASED
4410
    Lsb = LSB;
4411
    Lsb1 = 0;
4412
#endif
4413
3.01M
    j = bbe - bc.scale;
4414
3.01M
    i = j + bbbits - 1; /* logb(rv) */
4415
3.01M
    j = P + 1 - bbbits;
4416
3.01M
    if (i < Emin) { /* denormal */
4417
8.02k
      i = Emin - i;
4418
8.02k
      j -= i;
4419
#ifndef ROUND_BIASED
4420
      if (i < 32)
4421
        Lsb <<= i;
4422
      else if (i < 52)
4423
        Lsb1 = Lsb << (i-32);
4424
      else
4425
        Lsb1 = Exp_mask;
4426
#endif
4427
8.02k
      }
4428
#else /*Avoid_Underflow*/
4429
#ifdef Sudden_Underflow
4430
#ifdef IBM
4431
    j = 1 + 4*P - 3 - bbbits + ((bbe + bbbits - 1) & 3);
4432
#else
4433
    j = P + 1 - bbbits;
4434
#endif
4435
#else /*Sudden_Underflow*/
4436
    j = bbe;
4437
    i = j + bbbits - 1; /* logb(rv) */
4438
    if (i < Emin) /* denormal */
4439
      j += P - Emin;
4440
    else
4441
      j = P + 1 - bbbits;
4442
#endif /*Sudden_Underflow*/
4443
#endif /*Avoid_Underflow*/
4444
3.01M
    bb2 += j;
4445
3.01M
    bd2 += j;
4446
3.01M
#ifdef Avoid_Underflow
4447
3.01M
    bd2 += bc.scale;
4448
3.01M
#endif
4449
3.01M
    i = bb2 < bd2 ? bb2 : bd2;
4450
3.01M
    if (i > bs2)
4451
1.26M
      i = bs2;
4452
3.01M
    if (i > 0) {
4453
2.95M
      bb2 -= i;
4454
2.95M
      bd2 -= i;
4455
2.95M
      bs2 -= i;
4456
2.95M
      }
4457
3.01M
    if (bb5 > 0) {
4458
1.10M
      bs = pow5mult(bs, bb5 MTb);
4459
1.10M
      bb1 = mult(bs, bb MTb);
4460
1.10M
      Bfree(bb MTb);
4461
1.10M
      bb = bb1;
4462
1.10M
      }
4463
3.01M
    if (bb2 > 0)
4464
3.01M
      bb = lshift(bb, bb2 MTb);
4465
3.01M
    if (bd5 > 0)
4466
969k
      bd = pow5mult(bd, bd5 MTb);
4467
3.01M
    if (bd2 > 0)
4468
1.26M
      bd = lshift(bd, bd2 MTb);
4469
3.01M
    if (bs2 > 0)
4470
1.60M
      bs = lshift(bs, bs2 MTb);
4471
3.01M
    delta = diff(bb, bd MTb);
4472
3.01M
    bc.dsign = delta->sign;
4473
3.01M
    delta->sign = 0;
4474
3.01M
    i = cmp(delta, bs);
4475
3.01M
#ifndef NO_STRTOD_BIGCOMP /*{*/
4476
3.01M
    if (bc.nd > nd && i <= 0) {
4477
14.0k
      if (bc.dsign) {
4478
        /* Must use bigcomp(). */
4479
11.5k
        req_bigcomp = 1;
4480
11.5k
        break;
4481
11.5k
        }
4482
#ifdef Honor_FLT_ROUNDS
4483
      if (bc.rounding != 1) {
4484
        if (i < 0) {
4485
          req_bigcomp = 1;
4486
          break;
4487
          }
4488
        }
4489
      else
4490
#endif
4491
2.44k
        i = -1; /* Discarded digits make delta smaller. */
4492
2.44k
      }
4493
3.00M
#endif /*}*/
4494
#ifdef Honor_FLT_ROUNDS /*{*/
4495
    if (bc.rounding != 1) {
4496
      if (i < 0) {
4497
        /* Error is less than an ulp */
4498
        if (!delta->x[0] && delta->wds <= 1) {
4499
          /* exact */
4500
#ifdef SET_INEXACT
4501
          bc.inexact = 0;
4502
#endif
4503
          break;
4504
          }
4505
        if (bc.rounding) {
4506
          if (bc.dsign) {
4507
            adj.d = 1.;
4508
            goto apply_adj;
4509
            }
4510
          }
4511
        else if (!bc.dsign) {
4512
          adj.d = -1.;
4513
          if (!word1(&rv)
4514
           && !(word0(&rv) & Frac_mask)) {
4515
            y = word0(&rv) & Exp_mask;
4516
#ifdef Avoid_Underflow
4517
            if (!bc.scale || y > 2*P*Exp_msk1)
4518
#else
4519
            if (y)
4520
#endif
4521
              {
4522
              delta = lshift(delta,Log2P MTb);
4523
              if (cmp(delta, bs) <= 0)
4524
              adj.d = -0.5;
4525
              }
4526
            }
4527
 apply_adj:
4528
#ifdef Avoid_Underflow /*{*/
4529
          if (bc.scale && (y = word0(&rv) & Exp_mask)
4530
            <= 2*P*Exp_msk1)
4531
            word0(&adj) += (2*P+1)*Exp_msk1 - y;
4532
#else
4533
#ifdef Sudden_Underflow
4534
          if ((word0(&rv) & Exp_mask) <=
4535
              P*Exp_msk1) {
4536
            word0(&rv) += P*Exp_msk1;
4537
            dval(&rv) += adj.d*ulp(dval(&rv));
4538
            word0(&rv) -= P*Exp_msk1;
4539
            }
4540
          else
4541
#endif /*Sudden_Underflow*/
4542
#endif /*Avoid_Underflow}*/
4543
          dval(&rv) += adj.d*ulp(&rv);
4544
          }
4545
        break;
4546
        }
4547
      adj.d = ratio(delta, bs);
4548
      if (adj.d < 1.)
4549
        adj.d = 1.;
4550
      if (adj.d <= 0x7ffffffe) {
4551
        /* adj = rounding ? ceil(adj) : floor(adj); */
4552
        y = adj.d;
4553
        if (y != adj.d) {
4554
          if (!((bc.rounding>>1) ^ bc.dsign))
4555
            y++;
4556
          adj.d = y;
4557
          }
4558
        }
4559
#ifdef Avoid_Underflow /*{*/
4560
      if (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
4561
        word0(&adj) += (2*P+1)*Exp_msk1 - y;
4562
#else
4563
#ifdef Sudden_Underflow
4564
      if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
4565
        word0(&rv) += P*Exp_msk1;
4566
        adj.d *= ulp(dval(&rv));
4567
        if (bc.dsign)
4568
          dval(&rv) += adj.d;
4569
        else
4570
          dval(&rv) -= adj.d;
4571
        word0(&rv) -= P*Exp_msk1;
4572
        goto cont;
4573
        }
4574
#endif /*Sudden_Underflow*/
4575
#endif /*Avoid_Underflow}*/
4576
      adj.d *= ulp(&rv);
4577
      if (bc.dsign) {
4578
        if (word0(&rv) == Big0 && word1(&rv) == Big1)
4579
          goto ovfl;
4580
        dval(&rv) += adj.d;
4581
        }
4582
      else
4583
        dval(&rv) -= adj.d;
4584
      goto cont;
4585
      }
4586
#endif /*}Honor_FLT_ROUNDS*/
4587
4588
3.00M
    if (i < 0) {
4589
      /* Error is less than half an ulp -- check for
4590
       * special case of mantissa a power of two.
4591
       */
4592
2.20M
      if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask
4593
88.6k
#ifdef IEEE_Arith /*{*/
4594
88.6k
#ifdef Avoid_Underflow
4595
88.6k
       || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
4596
#else
4597
       || (word0(&rv) & Exp_mask) <= Exp_msk1
4598
#endif
4599
2.20M
#endif /*}*/
4600
2.20M
        ) {
4601
#ifdef SET_INEXACT
4602
        if (!delta->x[0] && delta->wds <= 1)
4603
          bc.inexact = 0;
4604
#endif
4605
2.11M
        break;
4606
2.11M
        }
4607
88.2k
      if (!delta->x[0] && delta->wds <= 1) {
4608
        /* exact result */
4609
#ifdef SET_INEXACT
4610
        bc.inexact = 0;
4611
#endif
4612
6.78k
        break;
4613
6.78k
        }
4614
81.4k
      delta = lshift(delta,Log2P MTb);
4615
81.4k
      if (cmp(delta, bs) > 0)
4616
1.48k
        goto drop_down;
4617
79.9k
      break;
4618
81.4k
      }
4619
800k
    if (i == 0) {
4620
      /* exactly half-way between */
4621
68.4k
      if (bc.dsign) {
4622
15.9k
        if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
4623
636
         &&  word1(&rv) == (
4624
636
#ifdef Avoid_Underflow
4625
636
      (bc.scale && (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1)
4626
636
    ? (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
4627
636
#endif
4628
636
               0xffffffff)) {
4629
          /*boundary case -- increment exponent*/
4630
195
          if (word0(&rv) == Big0 && word1(&rv) == Big1)
4631
0
            goto ovfl;
4632
195
          word0(&rv) = (word0(&rv) & Exp_mask)
4633
195
            + Exp_msk1
4634
#ifdef IBM
4635
            | Exp_msk1 >> 4
4636
#endif
4637
195
            ;
4638
195
          word1(&rv) = 0;
4639
195
#ifdef Avoid_Underflow
4640
195
          bc.dsign = 0;
4641
195
#endif
4642
195
          break;
4643
195
          }
4644
15.9k
        }
4645
52.4k
      else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
4646
1.48k
 drop_down:
4647
        /* boundary case -- decrement exponent */
4648
#ifdef Sudden_Underflow /*{{*/
4649
        L = word0(&rv) & Exp_mask;
4650
#ifdef IBM
4651
        if (L <  Exp_msk1)
4652
#else
4653
#ifdef Avoid_Underflow
4654
        if (L <= (bc.scale ? (2*P+1)*Exp_msk1 : Exp_msk1))
4655
#else
4656
        if (L <= Exp_msk1)
4657
#endif /*Avoid_Underflow*/
4658
#endif /*IBM*/
4659
          {
4660
          if (bc.nd >nd) {
4661
            bc.uflchk = 1;
4662
            break;
4663
            }
4664
          goto undfl;
4665
          }
4666
        L -= Exp_msk1;
4667
#else /*Sudden_Underflow}{*/
4668
1.48k
#ifdef Avoid_Underflow
4669
1.48k
        if (bc.scale) {
4670
0
          L = word0(&rv) & Exp_mask;
4671
0
          if (L <= (2*P+1)*Exp_msk1) {
4672
0
            if (L > (P+2)*Exp_msk1)
4673
              /* round even ==> */
4674
              /* accept rv */
4675
0
              break;
4676
            /* rv = smallest denormal */
4677
0
            if (bc.nd >nd) {
4678
0
              bc.uflchk = 1;
4679
0
              break;
4680
0
              }
4681
0
            goto undfl;
4682
0
            }
4683
0
          }
4684
1.48k
#endif /*Avoid_Underflow*/
4685
1.48k
        L = (word0(&rv) & Exp_mask) - Exp_msk1;
4686
1.48k
#endif /*Sudden_Underflow}}*/
4687
1.48k
        word0(&rv) = L | Bndry_mask1;
4688
1.48k
        word1(&rv) = 0xffffffff;
4689
#ifdef IBM
4690
        goto cont;
4691
#else
4692
1.48k
#ifndef NO_STRTOD_BIGCOMP
4693
1.48k
        if (bc.nd > nd)
4694
796
          goto cont;
4695
692
#endif
4696
692
        break;
4697
1.48k
#endif
4698
1.48k
        }
4699
#ifndef ROUND_BIASED
4700
#ifdef Avoid_Underflow
4701
      if (Lsb1) {
4702
        if (!(word0(&rv) & Lsb1))
4703
          break;
4704
        }
4705
      else if (!(word1(&rv) & Lsb))
4706
        break;
4707
#else
4708
      if (!(word1(&rv) & LSB))
4709
        break;
4710
#endif
4711
#endif
4712
68.2k
      if (bc.dsign)
4713
15.7k
#ifdef Avoid_Underflow
4714
15.7k
        dval(&rv) += sulp(&rv, &bc);
4715
#else
4716
        dval(&rv) += ulp(&rv);
4717
#endif
4718
#ifndef ROUND_BIASED
4719
      else {
4720
#ifdef Avoid_Underflow
4721
        dval(&rv) -= sulp(&rv, &bc);
4722
#else
4723
        dval(&rv) -= ulp(&rv);
4724
#endif
4725
#ifndef Sudden_Underflow
4726
        if (!dval(&rv)) {
4727
          if (bc.nd >nd) {
4728
            bc.uflchk = 1;
4729
            break;
4730
            }
4731
          goto undfl;
4732
          }
4733
#endif
4734
        }
4735
#ifdef Avoid_Underflow
4736
      bc.dsign = 1 - bc.dsign;
4737
#endif
4738
#endif
4739
68.2k
      break;
4740
68.4k
      }
4741
732k
    if ((aadj = ratio(delta, bs)) <= 2.) {
4742
505k
      if (bc.dsign)
4743
176k
        aadj = aadj1 = 1.;
4744
329k
      else if (word1(&rv) || word0(&rv) & Bndry_mask) {
4745
328k
#ifndef Sudden_Underflow
4746
328k
        if (word1(&rv) == Tiny1 && !word0(&rv)) {
4747
0
          if (bc.nd >nd) {
4748
0
            bc.uflchk = 1;
4749
0
            break;
4750
0
            }
4751
0
          goto undfl;
4752
0
          }
4753
328k
#endif
4754
328k
        aadj = 1.;
4755
328k
        aadj1 = -1.;
4756
328k
        }
4757
1.17k
      else {
4758
        /* special case -- power of FLT_RADIX to be */
4759
        /* rounded down... */
4760
4761
1.17k
        if (aadj < 2./FLT_RADIX)
4762
0
          aadj = 1./FLT_RADIX;
4763
1.17k
        else
4764
1.17k
          aadj *= 0.5;
4765
1.17k
        aadj1 = -aadj;
4766
1.17k
        }
4767
505k
      }
4768
226k
    else {
4769
226k
      aadj *= 0.5;
4770
226k
      aadj1 = bc.dsign ? aadj : -aadj;
4771
#ifdef Check_FLT_ROUNDS
4772
      switch(bc.rounding) {
4773
        case 2: /* towards +infinity */
4774
          aadj1 -= 0.5;
4775
          break;
4776
        case 0: /* towards 0 */
4777
        case 3: /* towards -infinity */
4778
          aadj1 += 0.5;
4779
        }
4780
#else
4781
226k
      if (Flt_Rounds == 0)
4782
0
        aadj1 += 0.5;
4783
226k
#endif /*Check_FLT_ROUNDS*/
4784
226k
      }
4785
732k
    y = word0(&rv) & Exp_mask;
4786
4787
    /* Check for overflow */
4788
4789
732k
    if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
4790
5.13k
      dval(&rv0) = dval(&rv);
4791
5.13k
      word0(&rv) -= P*Exp_msk1;
4792
5.13k
      adj.d = aadj1 * ulp(&rv);
4793
5.13k
      dval(&rv) += adj.d;
4794
5.13k
      if ((word0(&rv) & Exp_mask) >=
4795
5.13k
          Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
4796
547
        if (word0(&rv0) == Big0 && word1(&rv0) == Big1)
4797
547
          goto ovfl;
4798
0
        word0(&rv) = Big0;
4799
0
        word1(&rv) = Big1;
4800
0
        goto cont;
4801
547
        }
4802
4.58k
      else
4803
4.58k
        word0(&rv) += P*Exp_msk1;
4804
5.13k
      }
4805
727k
    else {
4806
727k
#ifdef Avoid_Underflow
4807
727k
      if (bc.scale && y <= 2*P*Exp_msk1) {
4808
3.56k
        if (aadj <= 0x7fffffff) {
4809
3.56k
          if ((z = (ULong)aadj) <= 0)
4810
1.17k
            z = 1;
4811
3.56k
          aadj = z;
4812
3.56k
          aadj1 = bc.dsign ? aadj : -aadj;
4813
3.56k
          }
4814
3.56k
        dval(&aadj2) = aadj1;
4815
3.56k
        word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
4816
3.56k
        aadj1 = dval(&aadj2);
4817
3.56k
        adj.d = aadj1 * ulp(&rv);
4818
3.56k
        dval(&rv) += adj.d;
4819
3.56k
        if (rv.d == 0.)
4820
#ifdef NO_STRTOD_BIGCOMP
4821
          goto undfl;
4822
#else
4823
1.17k
          {
4824
1.17k
          req_bigcomp = 1;
4825
1.17k
          break;
4826
1.17k
          }
4827
3.56k
#endif
4828
3.56k
        }
4829
723k
      else {
4830
723k
        adj.d = aadj1 * ulp(&rv);
4831
723k
        dval(&rv) += adj.d;
4832
723k
        }
4833
#else
4834
#ifdef Sudden_Underflow
4835
      if ((word0(&rv) & Exp_mask) <= P*Exp_msk1) {
4836
        dval(&rv0) = dval(&rv);
4837
        word0(&rv) += P*Exp_msk1;
4838
        adj.d = aadj1 * ulp(&rv);
4839
        dval(&rv) += adj.d;
4840
#ifdef IBM
4841
        if ((word0(&rv) & Exp_mask) <  P*Exp_msk1)
4842
#else
4843
        if ((word0(&rv) & Exp_mask) <= P*Exp_msk1)
4844
#endif
4845
          {
4846
          if (word0(&rv0) == Tiny0
4847
           && word1(&rv0) == Tiny1) {
4848
            if (bc.nd >nd) {
4849
              bc.uflchk = 1;
4850
              break;
4851
              }
4852
            goto undfl;
4853
            }
4854
          word0(&rv) = Tiny0;
4855
          word1(&rv) = Tiny1;
4856
          goto cont;
4857
          }
4858
        else
4859
          word0(&rv) -= P*Exp_msk1;
4860
        }
4861
      else {
4862
        adj.d = aadj1 * ulp(&rv);
4863
        dval(&rv) += adj.d;
4864
        }
4865
#else /*Sudden_Underflow*/
4866
      /* Compute adj so that the IEEE rounding rules will
4867
       * correctly round rv + adj in some half-way cases.
4868
       * If rv * ulp(rv) is denormalized (i.e.,
4869
       * y <= (P-1)*Exp_msk1), we must adjust aadj to avoid
4870
       * trouble from bits lost to denormalization;
4871
       * example: 1.2e-307 .
4872
       */
4873
      if (y <= (P-1)*Exp_msk1 && aadj > 1.) {
4874
        aadj1 = (double)(int)(aadj + 0.5);
4875
        if (!bc.dsign)
4876
          aadj1 = -aadj1;
4877
        }
4878
      adj.d = aadj1 * ulp(&rv);
4879
      dval(&rv) += adj.d;
4880
#endif /*Sudden_Underflow*/
4881
#endif /*Avoid_Underflow*/
4882
727k
      }
4883
730k
    z = word0(&rv) & Exp_mask;
4884
730k
#ifndef SET_INEXACT
4885
730k
    if (bc.nd == nd) {
4886
652k
#ifdef Avoid_Underflow
4887
652k
    if (!bc.scale)
4888
619k
#endif
4889
619k
    if (y == z) {
4890
      /* Can we stop now? */
4891
602k
      L = (Long)aadj;
4892
602k
      aadj -= L;
4893
      /* The tolerances below are conservative. */
4894
602k
      if (bc.dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
4895
525k
        if (aadj < .4999999 || aadj > .5000001)
4896
524k
          break;
4897
525k
        }
4898
77.1k
      else if (aadj < .4999999/FLT_RADIX)
4899
77.1k
        break;
4900
602k
      }
4901
652k
    }
4902
128k
#endif
4903
129k
 cont:
4904
129k
    Bfree(bb MTb);
4905
129k
    Bfree(bd MTb);
4906
129k
    Bfree(bs MTb);
4907
129k
    Bfree(delta MTb);
4908
129k
    }
4909
2.88M
  Bfree(bb MTb);
4910
2.88M
  Bfree(bd MTb);
4911
2.88M
  Bfree(bs MTb);
4912
2.88M
  Bfree(bd0 MTb);
4913
2.88M
  Bfree(delta MTb);
4914
2.88M
#ifndef NO_STRTOD_BIGCOMP
4915
2.88M
  if (req_bigcomp) {
4916
12.7k
    bd0 = 0;
4917
12.7k
    bc.e0 += nz1;
4918
12.7k
    bigcomp(&rv, s0, &bc MTb);
4919
12.7k
    y = word0(&rv) & Exp_mask;
4920
12.7k
    if (y == Exp_mask)
4921
896
      goto ovfl;
4922
11.8k
    if (y == 0 && rv.d == 0.)
4923
1.17k
      goto undfl;
4924
11.8k
    }
4925
2.88M
#endif
4926
2.88M
#ifdef Avoid_Underflow
4927
2.88M
  if (bc.scale) {
4928
59.5k
    word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
4929
59.5k
    word1(&rv0) = 0;
4930
59.5k
    dval(&rv) *= dval(&rv0);
4931
59.5k
#ifndef NO_ERRNO
4932
    /* try to avoid the bug of testing an 8087 register value */
4933
59.5k
#ifdef IEEE_Arith
4934
59.5k
    if (!(word0(&rv) & Exp_mask))
4935
#else
4936
    if (word0(&rv) == 0 && word1(&rv) == 0)
4937
#endif
4938
59.5k
      Set_errno(ERANGE);
4939
59.5k
#endif
4940
59.5k
    }
4941
2.88M
#endif /* Avoid_Underflow */
4942
27.9M
 ret:
4943
#ifdef SET_INEXACT
4944
  if (bc.inexact) {
4945
    if (!(word0(&rv) & Exp_mask)) {
4946
      /* set underflow and inexact bits */
4947
      dval(&rv0) = 1e-300;
4948
      dval(&rv0) *= dval(&rv0);
4949
      }
4950
    else if (!oldinexact) {
4951
      word0(&rv0) = Exp_1 + (70 << Exp_shift);
4952
      word1(&rv0) = 0;
4953
      dval(&rv0) += 1.;
4954
      }
4955
    }
4956
  else if (!oldinexact)
4957
    clear_inexact();
4958
#endif
4959
27.9M
  if (se)
4960
27.9M
    *se = (char *)s;
4961
27.9M
  return sign ? -dval(&rv) : dval(&rv);
4962
2.88M
  }
4963
4964
#ifndef MULTIPLE_THREADS
4965
#ifndef __XS__
4966
  static char *dtoa_result;
4967
#endif
4968
#endif
4969
4970
 static char *
4971
rv_alloc(int i MTd)
4972
15.8M
{
4973
15.8M
  int j, k, *r;
4974
4975
15.8M
  j = sizeof(ULong);
4976
15.8M
  for(k = 0;
4977
16.9M
    sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (size_t)i;
4978
15.8M
    j <<= 1)
4979
1.17M
      k++;
4980
15.8M
  r = (int*)Balloc(k MTa);
4981
15.8M
  *r = k;
4982
15.8M
  return
4983
15.8M
#ifndef MULTIPLE_THREADS
4984
#ifndef __XS__
4985
    dtoa_result =
4986
#endif
4987
15.8M
#endif
4988
15.8M
    (char *)(r+1);
4989
15.8M
  }
4990
4991
 static char *
4992
nrv_alloc(const char *s, char *s0, size_t s0len, char **rve, int n MTd)
4993
2.04M
{
4994
2.04M
  char *rv, *t;
4995
4996
2.04M
  if (!s0)
4997
2.04M
    s0 = rv_alloc(n MTa);
4998
0
  else if (s0len <= (size_t)n) {
4999
0
    rv = 0;
5000
0
    t = rv + n;
5001
0
    goto rve_chk;
5002
0
    }
5003
2.04M
  t = rv = s0;
5004
9.37M
  while((*t = *s++))
5005
7.33M
    ++t;
5006
2.04M
 rve_chk:
5007
2.04M
  if (rve)
5008
2.04M
    *rve = t;
5009
2.04M
  return rv;
5010
2.04M
  }
5011
5012
/* freedtoa(s) must be used to free values s returned by dtoa
5013
 * when MULTIPLE_THREADS is #defined.  It should be used in all cases,
5014
 * but for consistency with earlier versions of dtoa, it is optional
5015
 * when MULTIPLE_THREADS is not defined.
5016
 */
5017
5018
 void
5019
freedtoa(char *s __XS__d)
5020
15.8M
{
5021
#ifdef MULTIPLE_THREADS
5022
  ThInfo *TI = 0;
5023
#endif
5024
15.8M
  Bigint *b = (Bigint *)((int *)s - 1);
5025
15.8M
  b->maxwds = 1 << (b->k = *(int*)b);
5026
15.8M
  Bfree(b MTb);
5027
15.8M
#ifndef MULTIPLE_THREADS
5028
#ifndef __XS__
5029
  if (s == dtoa_result)
5030
    dtoa_result = 0;
5031
#endif
5032
15.8M
#endif
5033
15.8M
  }
5034
5035
/* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
5036
 *
5037
 * Inspired by "How to Print Floating-Point Numbers Accurately" by
5038
 * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
5039
 *
5040
 * Modifications:
5041
 *  1. Rather than iterating, we use a simple numeric overestimate
5042
 *     to determine k = floor(log10(d)).  We scale relevant
5043
 *     quantities using O(log2(k)) rather than O(k) multiplications.
5044
 *  2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
5045
 *     try to generate digits strictly left to right.  Instead, we
5046
 *     compute with fewer bits and propagate the carry if necessary
5047
 *     when rounding the final digit up.  This is often faster.
5048
 *  3. Under the assumption that input will be rounded nearest,
5049
 *     mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
5050
 *     That is, we allow equality in stopping tests when the
5051
 *     round-nearest rule will give the same floating-point value
5052
 *     as would satisfaction of the stopping test with strict
5053
 *     inequality.
5054
 *  4. We remove common factors of powers of 2 from relevant
5055
 *     quantities.
5056
 *  5. When converting floating-point integers less than 1e16,
5057
 *     we use floating-point arithmetic rather than resorting
5058
 *     to multiple-precision integers.
5059
 *  6. When asked to produce fewer than 15 digits, we first try
5060
 *     to get by with floating-point arithmetic; we resort to
5061
 *     multiple-precision integer arithmetic only if we cannot
5062
 *     guarantee that the floating-point calculation has given
5063
 *     the correctly rounded result.  For k requested digits and
5064
 *     "uniformly" distributed input, the probability is
5065
 *     something like 10^(k-15) that we must resort to the Long
5066
 *     calculation.
5067
 */
5068
5069
 char *
5070
dtoa_r(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve, char *buf, size_t blen __XS__d)
5071
15.8M
{
5072
 /* Arguments ndigits, decpt, sign are similar to those
5073
  of ecvt and fcvt; trailing zeros are suppressed from
5074
  the returned string.  If not null, *rve is set to point
5075
  to the end of the return value.  If d is +-Infinity or NaN,
5076
  then *decpt is set to 9999.
5077
5078
  mode:
5079
    0 ==> shortest string that yields d when read in
5080
      and rounded to nearest.
5081
    1 ==> like 0, but with Steele & White stopping rule;
5082
      e.g. with IEEE P754 arithmetic , mode 0 gives
5083
      1e23 whereas mode 1 gives 9.999999999999999e22.
5084
    2 ==> max(1,ndigits) significant digits.  This gives a
5085
      return value similar to that of ecvt, except
5086
      that trailing zeros are suppressed.
5087
    3 ==> through ndigits past the decimal point.  This
5088
      gives a return value similar to that from fcvt,
5089
      except that trailing zeros are suppressed, and
5090
      ndigits can be negative.
5091
    4,5 ==> similar to 2 and 3, respectively, but (in
5092
      round-nearest mode) with the tests of mode 0 to
5093
      possibly return a shorter string that rounds to d.
5094
      With IEEE arithmetic and compilation with
5095
      -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
5096
      as modes 2 and 3 when FLT_ROUNDS != 1.
5097
    6-9 ==> Debugging modes similar to mode - 4:  don't try
5098
      fast floating-point estimate (if applicable).
5099
5100
    Values of mode other than 0-9 are treated as mode 0.
5101
5102
  When not NULL, buf is an output buffer of length blen, which must
5103
  be large enough to accommodate suppressed trailing zeros and a trailing
5104
  null byte.  If blen is too small, rv = NULL is returned, in which case
5105
  if rve is not NULL, a subsequent call with blen >= (*rve - rv) + 1
5106
  should succeed in returning buf.
5107
5108
  When buf is NULL, sufficient space is allocated for the return value,
5109
  which, when done using, the caller should pass to freedtoa().
5110
5111
  USE_BF is automatically defined when neither NO_LONG_LONG nor NO_BF96
5112
  is defined.
5113
  */
5114
5115
#ifdef MULTIPLE_THREADS
5116
  ThInfo *TI = 0;
5117
#endif
5118
15.8M
  int bbits, b2, b5, be, dig, i, ilim, ilim1,
5119
15.8M
    j, j1, k, leftright, m2, m5, s2, s5, spec_case;
5120
15.8M
#ifndef Sudden_Underflow
5121
15.8M
  int denorm;
5122
15.8M
#endif
5123
15.8M
  Bigint *b, *b1, *delta, *mlo, *mhi, *S;
5124
15.8M
  U u;
5125
15.8M
  char *s;
5126
#ifdef SET_INEXACT
5127
  int inexact, oldinexact;
5128
#endif
5129
#ifdef USE_BF96 /*{{*/
5130
  BF96 *p10;
5131
  ULLong dbhi, dbits, dblo, den, hb, rb, rblo, res, res0, res3, reslo, sres,
5132
    sulp, tv0, tv1, tv2, tv3, ulp, ulplo, ulpmask, ures, ureslo, zb;
5133
  int eulp, k1, n2, ulpadj, ulpshift;
5134
#else /*}{*/
5135
15.8M
#ifndef Sudden_Underflow
5136
15.8M
  ULong x;
5137
15.8M
#endif
5138
15.8M
  Long L;
5139
15.8M
  U d2, eps;
5140
15.8M
  double ds;
5141
15.8M
  int ieps, ilim0, k0, k_check, try_quick;
5142
15.8M
#ifndef No_leftright
5143
15.8M
#ifdef IEEE_Arith
5144
15.8M
  U eps1;
5145
15.8M
#endif
5146
15.8M
#endif
5147
15.8M
#endif /*}}*/
5148
#ifdef Honor_FLT_ROUNDS /*{*/
5149
  int Rounding;
5150
#ifdef Trust_FLT_ROUNDS /*{{ only define this if FLT_ROUNDS really works! */
5151
  Rounding = Flt_Rounds;
5152
#else /*}{*/
5153
  Rounding = 1;
5154
  switch(fegetround()) {
5155
    case FE_TOWARDZERO: Rounding = 0; break;
5156
    case FE_UPWARD: Rounding = 2; break;
5157
    case FE_DOWNWARD: Rounding = 3;
5158
    }
5159
#endif /*}}*/
5160
#endif /*}*/
5161
5162
15.8M
  u.d = dd;
5163
15.8M
  if (word0(&u) & Sign_bit) {
5164
    /* set sign for everything, including 0's and NaNs */
5165
1.92M
    *sign = 1;
5166
1.92M
    word0(&u) &= ~Sign_bit; /* clear sign bit */
5167
1.92M
    }
5168
13.8M
  else
5169
13.8M
    *sign = 0;
5170
5171
15.8M
#if defined(IEEE_Arith) + defined(VAX)
5172
15.8M
#ifdef IEEE_Arith
5173
15.8M
  if ((word0(&u) & Exp_mask) == Exp_mask)
5174
#else
5175
  if (word0(&u)  == 0x8000)
5176
#endif
5177
1.50M
    {
5178
    /* Infinity or NaN */
5179
1.50M
    *decpt = 9999;
5180
1.50M
#ifdef IEEE_Arith
5181
1.50M
    if (!word1(&u) && !(word0(&u) & 0xfffff))
5182
458k
      return nrv_alloc("Infinity", buf, blen, rve, 8 MTb);
5183
1.04M
#endif
5184
1.04M
    *sign = 0; //NaN doesn't have a sign
5185
1.04M
    return nrv_alloc("NaN", buf, blen, rve, 3 MTb);
5186
1.50M
    }
5187
14.3M
#endif
5188
#ifdef IBM
5189
  dval(&u) += 0; /* normalize */
5190
#endif
5191
14.3M
  if (!dval(&u)) {
5192
541k
    *decpt = 1;
5193
541k
    return nrv_alloc("0", buf, blen, rve, 1 MTb);
5194
541k
    }
5195
5196
#ifdef SET_INEXACT
5197
#ifndef USE_BF96
5198
  try_quick =
5199
#endif
5200
  oldinexact = get_inexact();
5201
  inexact = 1;
5202
#endif
5203
#ifdef Honor_FLT_ROUNDS
5204
  if (Rounding >= 2) {
5205
    if (*sign)
5206
      Rounding = Rounding == 2 ? 0 : 2;
5207
    else
5208
      if (Rounding != 2)
5209
        Rounding = 0;
5210
    }
5211
#endif
5212
#ifdef USE_BF96 /*{{*/
5213
  dbits = (u.LL & 0xfffffffffffffull) << 11;  /* fraction bits */
5214
  if ((be = u.LL >> 52)) /* biased exponent; nonzero ==> normal */ {
5215
    dbits |= 0x8000000000000000ull;
5216
    denorm = ulpadj = 0;
5217
    }
5218
  else {
5219
    denorm = 1;
5220
    ulpadj = be + 1;
5221
    dbits <<= 1;
5222
    if (!(dbits & 0xffffffff00000000ull)) {
5223
      dbits <<= 32;
5224
      be -= 32;
5225
      }
5226
    if (!(dbits & 0xffff000000000000ull)) {
5227
      dbits <<= 16;
5228
      be -= 16;
5229
      }
5230
    if (!(dbits & 0xff00000000000000ull)) {
5231
      dbits <<= 8;
5232
      be -= 8;
5233
      }
5234
    if (!(dbits & 0xf000000000000000ull)) {
5235
      dbits <<= 4;
5236
      be -= 4;
5237
      }
5238
    if (!(dbits & 0xc000000000000000ull)) {
5239
      dbits <<= 2;
5240
      be -= 2;
5241
      }
5242
    if (!(dbits & 0x8000000000000000ull)) {
5243
      dbits <<= 1;
5244
      be -= 1;
5245
      }
5246
    assert(be >= -51);
5247
    ulpadj -= be;
5248
    }
5249
  j = Lhint[be + 51];
5250
  p10 = &pten[j];
5251
  dbhi = dbits >> 32;
5252
  dblo = dbits & 0xffffffffull;
5253
  i = be - 0x3fe;
5254
  if (i < p10->e
5255
  || (i == p10->e && (dbhi < p10->b0 || (dbhi == p10->b0 && dblo < p10->b1))))
5256
    --j;
5257
  k = j - 342;
5258
5259
  /* now 10^k <= dd < 10^(k+1) */
5260
5261
#else /*}{*/
5262
5263
13.7M
  b = d2b(&u, &be, &bbits MTb);
5264
#ifdef Sudden_Underflow
5265
  i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
5266
#else
5267
13.7M
  if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
5268
13.7M
#endif
5269
13.7M
    dval(&d2) = dval(&u);
5270
13.7M
    word0(&d2) &= Frac_mask1;
5271
13.7M
    word0(&d2) |= Exp_11;
5272
#ifdef IBM
5273
    if (j = 11 - hi0bits(word0(&d2) & Frac_mask))
5274
      dval(&d2) /= 1 << j;
5275
#endif
5276
5277
    /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
5278
     * log10(x)  =  log(x) / log(10)
5279
     *    ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
5280
     * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
5281
     *
5282
     * This suggests computing an approximation k to log10(d) by
5283
     *
5284
     * k = (i - Bias)*0.301029995663981
5285
     *  + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
5286
     *
5287
     * We want k to be too large rather than too small.
5288
     * The error in the first-order Taylor series approximation
5289
     * is in our favor, so we just round up the constant enough
5290
     * to compensate for any error in the multiplication of
5291
     * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
5292
     * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
5293
     * adding 1e-13 to the constant term more than suffices.
5294
     * Hence we adjust the constant term to 0.1760912590558.
5295
     * (We could get a more accurate k by invoking log10,
5296
     *  but this is probably not worthwhile.)
5297
     */
5298
5299
13.7M
    i -= Bias;
5300
#ifdef IBM
5301
    i <<= 2;
5302
    i += j;
5303
#endif
5304
13.7M
#ifndef Sudden_Underflow
5305
13.7M
    denorm = 0;
5306
13.7M
    }
5307
1.06k
  else {
5308
    /* d is denormalized */
5309
5310
1.06k
    i = bbits + be + (Bias + (P-1) - 1);
5311
1.06k
    x = i > 32  ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
5312
1.06k
          : word1(&u) << (32 - i);
5313
1.06k
    dval(&d2) = x;
5314
1.06k
    word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
5315
1.06k
    i -= (Bias + (P-1) - 1) + 1;
5316
1.06k
    denorm = 1;
5317
1.06k
    }
5318
13.7M
#endif
5319
13.7M
  ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 + i*0.301029995663981;
5320
13.7M
  k = (int)ds;
5321
13.7M
  if (ds < 0. && ds != k)
5322
1.22M
    k--; /* want k = floor(ds) */
5323
13.7M
  k_check = 1;
5324
13.7M
  if (k >= 0 && k <= Ten_pmax) {
5325
11.6M
    if (dval(&u) < tens[k])
5326
218k
      k--;
5327
11.6M
    k_check = 0;
5328
11.6M
    }
5329
13.7M
  j = bbits - i - 1;
5330
13.7M
  if (j >= 0) {
5331
6.88M
    b2 = 0;
5332
6.88M
    s2 = j;
5333
6.88M
    }
5334
6.88M
  else {
5335
6.88M
    b2 = -j;
5336
6.88M
    s2 = 0;
5337
6.88M
    }
5338
13.7M
  if (k >= 0) {
5339
12.5M
    b5 = 0;
5340
12.5M
    s5 = k;
5341
12.5M
    s2 += k;
5342
12.5M
    }
5343
1.22M
  else {
5344
1.22M
    b2 -= k;
5345
1.22M
    b5 = -k;
5346
1.22M
    s5 = 0;
5347
1.22M
    }
5348
13.7M
#endif /*}}*/
5349
13.7M
  if (mode < 0 || mode > 9)
5350
0
    mode = 0;
5351
5352
13.7M
#ifndef USE_BF96
5353
13.7M
#ifndef SET_INEXACT
5354
#ifdef Check_FLT_ROUNDS
5355
  try_quick = Rounding == 1;
5356
#else
5357
13.7M
  try_quick = 1;
5358
13.7M
#endif
5359
13.7M
#endif /*SET_INEXACT*/
5360
13.7M
#endif
5361
5362
13.7M
  if (mode > 5) {
5363
0
    mode -= 4;
5364
0
#ifndef USE_BF96
5365
0
    try_quick = 0;
5366
0
#endif
5367
0
    }
5368
13.7M
  leftright = 1;
5369
13.7M
  ilim = ilim1 = -1;  /* Values for cases 0 and 1; done here to */
5370
        /* silence erroneous "gcc -Wall" warning. */
5371
13.7M
  switch(mode) {
5372
12.8M
    case 0:
5373
12.8M
    case 1:
5374
12.8M
      i = 18;
5375
12.8M
      ndigits = 0;
5376
12.8M
      break;
5377
849k
    case 2:
5378
849k
      leftright = 0;
5379
      /* no break */
5380
849k
    case 4:
5381
849k
      if (ndigits <= 0)
5382
0
        ndigits = 1;
5383
849k
      ilim = ilim1 = i = ndigits;
5384
849k
      break;
5385
31.5k
    case 3:
5386
31.5k
      leftright = 0;
5387
      /* no break */
5388
31.5k
    case 5:
5389
31.5k
      i = ndigits + k + 1;
5390
31.5k
      ilim = i;
5391
31.5k
      ilim1 = i - 1;
5392
31.5k
      if (i <= 0)
5393
10.9k
        i = 1;
5394
13.7M
    }
5395
13.7M
  if (!buf) {
5396
13.7M
    buf = rv_alloc(i MTb);
5397
13.7M
    blen = sizeof(Bigint) + ((1 << ((int*)buf)[-1]) - 1)*sizeof(ULong) - sizeof(int);
5398
13.7M
    }
5399
0
  else if (blen <= (size_t)i) {
5400
0
    buf = 0;
5401
0
    if (rve)
5402
0
      *rve = buf + i;
5403
0
    return buf;
5404
0
    }
5405
13.7M
  s = buf;
5406
5407
  /* Check for special case that d is a normalized power of 2. */
5408
5409
13.7M
  spec_case = 0;
5410
13.7M
  if (mode < 2 || (leftright
5411
#ifdef Honor_FLT_ROUNDS
5412
      && Rounding == 1
5413
#endif
5414
12.8M
        )) {
5415
12.8M
    if (!word1(&u) && !(word0(&u) & Bndry_mask)
5416
1.06M
#ifndef Sudden_Underflow
5417
1.06M
     && word0(&u) & (Exp_mask & ~Exp_msk1)
5418
12.8M
#endif
5419
12.8M
        ) {
5420
      /* The special case */
5421
1.06M
      spec_case = 1;
5422
1.06M
      }
5423
12.8M
    }
5424
5425
#ifdef USE_BF96 /*{*/
5426
  b = 0;
5427
  if (ilim < 0 && (mode == 3 || mode == 5)) {
5428
    S = mhi = 0;
5429
    goto no_digits;
5430
    }
5431
  i = 1;
5432
  j = 52 + 0x3ff - be;
5433
  ulpshift = 0;
5434
  ulplo = 0;
5435
  /* Can we do an exact computation with 64-bit integer arithmetic? */
5436
  if (k < 0) {
5437
    if (k < -25)
5438
      goto toobig;
5439
    res = dbits >> 11;
5440
    n2 = pfivebits[k1 = -(k + 1)] + 53;
5441
    j1 = j;
5442
    if (n2 > 61) {
5443
      ulpshift = n2 - 61;
5444
      if (res & (ulpmask = (1ull << ulpshift) - 1))
5445
        goto toobig;
5446
      j -= ulpshift;
5447
      res >>= ulpshift;
5448
      }
5449
    /* Yes. */
5450
    res *= ulp = pfive[k1];
5451
    if (ulpshift) {
5452
      ulplo = ulp;
5453
      ulp >>= ulpshift;
5454
      }
5455
    j += k;
5456
    if (ilim == 0) {
5457
      S = mhi = 0;
5458
      if (res > (5ull << j))
5459
        goto one_digit;
5460
      goto no_digits;
5461
      }
5462
    goto no_div;
5463
    }
5464
  if (ilim == 0 && j + k >= 0) {
5465
    S = mhi = 0;
5466
    if ((dbits >> 11) > (pfive[k-1] << j))
5467
      goto one_digit;
5468
    goto no_digits;
5469
    }
5470
  if (k <= dtoa_divmax && j + k >= 0) {
5471
    /* Another "yes" case -- we will use exact integer arithmetic. */
5472
 use_exact:
5473
    Debug(++dtoa_stats[3]);
5474
    res = dbits >> 11;  /* residual */
5475
    ulp = 1;
5476
    if (k <= 0)
5477
      goto no_div;
5478
    j1 = j + k + 1;
5479
    den = pfive[k-i] << (j1 - i);
5480
    for(;;) {
5481
      dig = res / den;
5482
      *s++ = '0' + dig;
5483
      if (!(res -= dig*den)) {
5484
#ifdef SET_INEXACT
5485
        inexact = 0;
5486
        oldinexact = 1;
5487
#endif
5488
        goto retc;
5489
        }
5490
      if (ilim < 0) {
5491
        ures = den - res;
5492
        if (2*res <= ulp
5493
        && (spec_case ? 4*res <= ulp : (2*res < ulp || dig & 1)))
5494
          goto ulp_reached;
5495
        if (2*ures < ulp)
5496
          goto Roundup;
5497
        }
5498
      else if (i == ilim) {
5499
        switch(Rounding) {
5500
          case 0: goto retc;
5501
          case 2: goto Roundup;
5502
          }
5503
        ures = 2*res;
5504
        if (ures > den
5505
        || (ures == den && dig & 1)
5506
        || (spec_case && res <= ulp && 2*res >= ulp))
5507
          goto Roundup;
5508
        goto retc;
5509
        }
5510
      if (j1 < ++i) {
5511
        res *= 10;
5512
        ulp *= 10;
5513
        }
5514
      else {
5515
        if (i > k)
5516
          break;
5517
        den = pfive[k-i] << (j1 - i);
5518
        }
5519
      }
5520
 no_div:
5521
    for(;;) {
5522
      dig = den = res >> j;
5523
      *s++ = '0' + dig;
5524
      if (!(res -= den << j)) {
5525
#ifdef SET_INEXACT
5526
        inexact = 0;
5527
        oldinexact = 1;
5528
#endif
5529
        goto retc;
5530
        }
5531
      if (ilim < 0) {
5532
        ures = (1ull << j) - res;
5533
        if (2*res <= ulp
5534
        && (spec_case ? 4*res <= ulp : (2*res < ulp || dig & 1))) {
5535
 ulp_reached:
5536
          if (ures < res
5537
          || (ures == res && dig & 1))
5538
            goto Roundup;
5539
          goto retc;
5540
          }
5541
        if (2*ures < ulp)
5542
          goto Roundup;
5543
        }
5544
      --j;
5545
      if (i == ilim) {
5546
#ifdef Honor_FLT_ROUNDS
5547
        switch(Rounding) {
5548
          case 0: goto retc;
5549
          case 2: goto Roundup;
5550
          }
5551
#endif
5552
        hb = 1ull << j;
5553
        if (res & hb && (dig & 1 || res & (hb-1)))
5554
          goto Roundup;
5555
        if (spec_case && res <= ulp && 2*res >= ulp) {
5556
 Roundup:
5557
          while(*--s == '9')
5558
            if (s == buf) {
5559
              ++k;
5560
              *s++ = '1';
5561
              goto ret1;
5562
              }
5563
          ++*s++;
5564
          goto ret1;
5565
          }
5566
        goto retc;
5567
        }
5568
      ++i;
5569
      res *= 5;
5570
      if (ulpshift) {
5571
        ulplo = 5*(ulplo & ulpmask);
5572
        ulp = 5*ulp + (ulplo >> ulpshift);
5573
        }
5574
      else
5575
        ulp *= 5;
5576
      }
5577
    }
5578
 toobig:
5579
  if (ilim > 28)
5580
    goto Fast_failed1;
5581
  /* Scale by 10^-k */
5582
  p10 = &pten[342-k];
5583
  tv0 = p10->b2 * dblo; /* rarely matters, but does, e.g., for 9.862818194192001e18 */
5584
  tv1 = p10->b1 * dblo + (tv0 >> 32);
5585
  tv2 = p10->b2 * dbhi + (tv1 & 0xffffffffull);
5586
  tv3 = p10->b0 * dblo + (tv1>>32) + (tv2>>32);
5587
  res3 = p10->b1 * dbhi + (tv3 & 0xffffffffull);
5588
  res = p10->b0 * dbhi + (tv3>>32) + (res3>>32);
5589
  be += p10->e - 0x3fe;
5590
  eulp = j1 = be - 54 + ulpadj;
5591
  if (!(res & 0x8000000000000000ull)) {
5592
    --be;
5593
    res3 <<= 1;
5594
    res = (res << 1) | ((res3 & 0x100000000ull) >> 32);
5595
    }
5596
  res0 = res; /* save for Fast_failed */
5597
#if !defined(SET_INEXACT) && !defined(NO_DTOA_64) /*{*/
5598
  if (ilim > 19)
5599
    goto Fast_failed;
5600
  Debug(++dtoa_stats[4]);
5601
  assert(be >= 0 && be <= 4); /* be = 0 is rare, but possible, e.g., for 1e20 */
5602
  res >>= 4 - be;
5603
  ulp = p10->b0;  /* ulp */
5604
  ulp = (ulp << 29) | (p10->b1 >> 3);
5605
  /* scaled ulp = ulp * 2^(eulp - 60) */
5606
  /* We maintain 61 bits of the scaled ulp. */
5607
  if (ilim == 0) {
5608
    if (!(res & 0x7fffffffffffffeull)
5609
     || !((~res) & 0x7fffffffffffffeull))
5610
      goto Fast_failed1;
5611
    S = mhi = 0;
5612
    if (res >= 0x5000000000000000ull)
5613
      goto one_digit;
5614
    goto no_digits;
5615
    }
5616
  rb = 1; /* upper bound on rounding error */
5617
  for(;;++i) {
5618
    dig = res >> 60;
5619
    *s++ = '0' + dig;
5620
    res &= 0xfffffffffffffffull;
5621
    if (ilim < 0) {
5622
      ures = 0x1000000000000000ull - res;
5623
      if (eulp > 0) {
5624
        assert(eulp <= 4);
5625
        sulp = ulp << (eulp - 1);
5626
        if (res <= ures) {
5627
          if (res + rb > ures - rb)
5628
            goto Fast_failed;
5629
          if (res < sulp)
5630
            goto retc;
5631
          }
5632
        else {
5633
          if (res - rb <= ures + rb)
5634
            goto Fast_failed;
5635
          if (ures < sulp)
5636
            goto Roundup;
5637
          }
5638
        }
5639
      else {
5640
        zb = -(1ull << (eulp + 63));
5641
        if (!(zb & res)) {
5642
          sres = res << (1 - eulp);
5643
          if (sres < ulp && (!spec_case || 2*sres < ulp)) {
5644
            if ((res+rb) << (1 - eulp) >= ulp)
5645
              goto Fast_failed;
5646
            if (ures < res) {
5647
              if (ures + rb >= res - rb)
5648
                goto Fast_failed;
5649
              goto Roundup;
5650
              }
5651
            if (ures - rb < res + rb)
5652
              goto Fast_failed;
5653
            goto retc;
5654
            }
5655
          }
5656
        if (!(zb & ures) && ures << -eulp < ulp) {
5657
          if (ures << (1 - eulp) < ulp)
5658
            goto  Roundup;
5659
          goto Fast_failed;
5660
          }
5661
        }
5662
      }
5663
    else if (i == ilim) {
5664
      ures = 0x1000000000000000ull - res;
5665
      if (ures < res) {
5666
        if (ures <= rb || res - rb <= ures + rb) {
5667
          if (j + k >= 0 && k >= 0 && k <= 27)
5668
            goto use_exact1;
5669
          goto Fast_failed;
5670
          }
5671
#ifdef Honor_FLT_ROUNDS
5672
        if (Rounding == 0)
5673
          goto retc;
5674
#endif
5675
        goto Roundup;
5676
        }
5677
      if (res <= rb || ures - rb <= res + rb) {
5678
        if (j + k >= 0 && k >= 0 && k <= 27) {
5679
 use_exact1:
5680
          s = buf;
5681
          i = 1;
5682
          goto use_exact;
5683
          }
5684
        goto Fast_failed;
5685
        }
5686
#ifdef Honor_FLT_ROUNDS
5687
      if (Rounding == 2)
5688
        goto Roundup;
5689
#endif
5690
      goto retc;
5691
      }
5692
    rb *= 10;
5693
    if (rb >= 0x1000000000000000ull)
5694
      goto Fast_failed;
5695
    res *= 10;
5696
    ulp *= 5;
5697
    if (ulp & 0x8000000000000000ull) {
5698
      eulp += 4;
5699
      ulp >>= 3;
5700
      }
5701
    else {
5702
      eulp += 3;
5703
      ulp >>= 2;
5704
      }
5705
    }
5706
#endif /*}*/
5707
#ifndef NO_BF96
5708
 Fast_failed:
5709
#endif
5710
  Debug(++dtoa_stats[5]);
5711
  s = buf;
5712
  i = 4 - be;
5713
  res = res0 >> i;
5714
  reslo = 0xffffffffull & res3;
5715
  if (i)
5716
    reslo = (res0 << (64 - i)) >> 32 | (reslo >> i);
5717
  rb = 0;
5718
  rblo = 4; /* roundoff bound */
5719
  ulp = p10->b0;  /* ulp */
5720
  ulp = (ulp << 29) | (p10->b1 >> 3);
5721
  eulp = j1;
5722
  for(i = 1;;++i) {
5723
    dig = res >> 60;
5724
    *s++ = '0' + dig;
5725
    res &= 0xfffffffffffffffull;
5726
#ifdef SET_INEXACT
5727
    if (!res && !reslo) {
5728
      if (!(res3 & 0xffffffffull)) {
5729
        inexact = 0;
5730
        oldinexact = 1;
5731
        }
5732
      goto retc;
5733
      }
5734
#endif
5735
    if (ilim < 0) {
5736
      ures = 0x1000000000000000ull - res;
5737
      ureslo = 0;
5738
      if (reslo) {
5739
        ureslo = 0x100000000ull - reslo;
5740
        --ures;
5741
        }
5742
      if (eulp > 0) {
5743
        assert(eulp <= 4);
5744
        sulp = (ulp << (eulp - 1)) - rb;
5745
        if (res <= ures) {
5746
          if (res < sulp) {
5747
            if (res+rb < ures-rb)
5748
              goto retc;
5749
            }
5750
          }
5751
        else if (ures < sulp) {
5752
          if (res-rb > ures+rb)
5753
            goto Roundup;
5754
          }
5755
        goto Fast_failed1;
5756
        }
5757
      else {
5758
        zb = -(1ull << (eulp + 60));
5759
        if (!(zb & (res + rb))) {
5760
          sres = (res - rb) << (1 - eulp);
5761
          if (sres < ulp && (!spec_case || 2*sres < ulp)) {
5762
            sres = res << (1 - eulp);
5763
            if ((j = eulp + 31) > 0)
5764
              sres += (rblo + reslo) >> j;
5765
            else
5766
              sres += (rblo + reslo) << -j;
5767
            if (sres + (rb << (1 - eulp)) >= ulp)
5768
              goto Fast_failed1;
5769
            if (sres >= ulp)
5770
              goto more96;
5771
            if (ures < res
5772
            || (ures == res && ureslo < reslo)) {
5773
              if (ures + rb >= res - rb)
5774
                goto Fast_failed1;
5775
              goto Roundup;
5776
              }
5777
            if (ures - rb <= res + rb)
5778
              goto Fast_failed1;
5779
            goto retc;
5780
            }
5781
          }
5782
        if (!(zb & ures) && (ures-rb) << (1 - eulp) < ulp) {
5783
          if ((ures + rb) << (1 - eulp) < ulp)
5784
            goto Roundup;
5785
          goto Fast_failed1;
5786
          }
5787
        }
5788
      }
5789
    else if (i == ilim) {
5790
      ures = 0x1000000000000000ull - res;
5791
      sres = ureslo = 0;
5792
      if (reslo) {
5793
        ureslo = 0x100000000ull - reslo;
5794
        --ures;
5795
        sres = (reslo + rblo) >> 31;
5796
        }
5797
      sres += 2*rb;
5798
      if (ures <= res) {
5799
        if (ures <=sres || res - ures <= sres)
5800
          goto Fast_failed1;
5801
#ifdef Honor_FLT_ROUNDS
5802
        if (Rounding == 0)
5803
          goto retc;
5804
#endif
5805
        goto Roundup;
5806
        }
5807
      if (res <= sres || ures - res <= sres)
5808
        goto Fast_failed1;
5809
#ifdef Honor_FLT_ROUNDS
5810
      if (Rounding == 2)
5811
        goto Roundup;
5812
#endif
5813
      goto retc;
5814
      }
5815
 more96:
5816
    rblo *= 10;
5817
    rb = 10*rb + (rblo >> 32);
5818
    rblo &= 0xffffffffull;
5819
    if (rb >= 0x1000000000000000ull)
5820
      goto Fast_failed1;
5821
    reslo *= 10;
5822
    res = 10*res + (reslo >> 32);
5823
    reslo &= 0xffffffffull;
5824
    ulp *= 5;
5825
    if (ulp & 0x8000000000000000ull) {
5826
      eulp += 4;
5827
      ulp >>= 3;
5828
      }
5829
    else {
5830
      eulp += 3;
5831
      ulp >>= 2;
5832
      }
5833
    }
5834
 Fast_failed1:
5835
  Debug(++dtoa_stats[6]);
5836
  S = mhi = mlo = 0;
5837
#ifdef USE_BF96
5838
  b = d2b(&u, &be, &bbits MTb);
5839
#endif
5840
  s = buf;
5841
  i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1));
5842
  i -= Bias;
5843
  if (ulpadj)
5844
    i -= ulpadj - 1;
5845
  j = bbits - i - 1;
5846
  if (j >= 0) {
5847
    b2 = 0;
5848
    s2 = j;
5849
    }
5850
  else {
5851
    b2 = -j;
5852
    s2 = 0;
5853
    }
5854
  if (k >= 0) {
5855
    b5 = 0;
5856
    s5 = k;
5857
    s2 += k;
5858
    }
5859
  else {
5860
    b2 -= k;
5861
    b5 = -k;
5862
    s5 = 0;
5863
    }
5864
#endif /*}*/
5865
5866
#ifdef Honor_FLT_ROUNDS
5867
  if (mode > 1 && Rounding != 1)
5868
    leftright = 0;
5869
#endif
5870
5871
13.7M
#ifndef USE_BF96 /*{*/
5872
13.7M
  if (ilim >= 0 && ilim <= Quick_max && try_quick) {
5873
5874
    /* Try to get by with floating-point arithmetic. */
5875
5876
482k
    i = 0;
5877
482k
    dval(&d2) = dval(&u);
5878
482k
    j1 = -(k0 = k);
5879
482k
    ilim0 = ilim;
5880
482k
    ieps = 2; /* conservative */
5881
482k
    if (k > 0) {
5882
145k
      ds = tens[k&0xf];
5883
145k
      j = k >> 4;
5884
145k
      if (j & Bletch) {
5885
        /* prevent overflows */
5886
81.6k
        j &= Bletch - 1;
5887
81.6k
        dval(&u) /= bigtens[n_bigtens-1];
5888
81.6k
        ieps++;
5889
81.6k
        }
5890
279k
      for(; j; j >>= 1, i++)
5891
134k
        if (j & 1) {
5892
82.2k
          ieps++;
5893
82.2k
          ds *= bigtens[i];
5894
82.2k
          }
5895
145k
      dval(&u) /= ds;
5896
145k
      }
5897
337k
    else if (j1 > 0) {
5898
237k
      dval(&u) *= tens[j1 & 0xf];
5899
238k
      for(j = j1 >> 4; j; j >>= 1, i++)
5900
608
        if (j & 1) {
5901
357
          ieps++;
5902
357
          dval(&u) *= bigtens[i];
5903
357
          }
5904
237k
      }
5905
482k
    if (k_check && dval(&u) < 1. && ilim > 0) {
5906
718
      if (ilim1 <= 0)
5907
0
        goto fast_failed;
5908
718
      ilim = ilim1;
5909
718
      k--;
5910
718
      dval(&u) *= 10.;
5911
718
      ieps++;
5912
718
      }
5913
482k
    dval(&eps) = ieps*dval(&u) + 7.;
5914
482k
    word0(&eps) -= (P-1)*Exp_msk1;
5915
482k
    if (ilim == 0) {
5916
9.38k
      S = mhi = 0;
5917
9.38k
      dval(&u) -= 5.;
5918
9.38k
      if (dval(&u) > dval(&eps))
5919
5.56k
        goto one_digit;
5920
3.81k
      if (dval(&u) < -dval(&eps))
5921
3.81k
        goto no_digits;
5922
3
      goto fast_failed;
5923
3.81k
      }
5924
473k
#ifndef No_leftright
5925
473k
    if (leftright) {
5926
      /* Use Steele & White method of only
5927
       * generating digits needed.
5928
       */
5929
0
      dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
5930
0
#ifdef IEEE_Arith
5931
0
      if (j1 >= 307) {
5932
0
        eps1.d = 1.01e256; /* 1.01 allows roundoff in the next few lines */
5933
0
        word0(&eps1) -= Exp_msk1 * (Bias+P-1);
5934
0
        dval(&eps1) *= tens[j1 & 0xf];
5935
0
        for(i = 0, j = (j1-256) >> 4; j; j >>= 1, i++)
5936
0
          if (j & 1)
5937
0
            dval(&eps1) *= bigtens[i];
5938
0
        if (eps.d < eps1.d)
5939
0
          eps.d = eps1.d;
5940
0
        if (10. - u.d < 10.*eps.d && eps.d < 1.) {
5941
          /* eps.d < 1. excludes trouble with the tiniest denormal */
5942
0
          *s++ = '1';
5943
0
          ++k;
5944
0
          goto ret1;
5945
0
          }
5946
0
        }
5947
0
#endif
5948
0
      for(i = 0;;) {
5949
0
        L = (int)dval(&u);
5950
0
        dval(&u) -= L;
5951
0
        *s++ = '0' + (int)L;
5952
0
        if (1. - dval(&u) < dval(&eps))
5953
0
          goto bump_up;
5954
0
        if (dval(&u) < dval(&eps))
5955
0
          goto retc;
5956
0
        if (++i >= ilim)
5957
0
          break;
5958
0
        dval(&eps) *= 10.;
5959
0
        dval(&u) *= 10.;
5960
0
        }
5961
0
      }
5962
473k
    else {
5963
473k
#endif
5964
      /* Generate ilim digits, then fix them up. */
5965
473k
      dval(&eps) *= tens[ilim-1];
5966
1.08M
      for(i = 1;; i++, dval(&u) *= 10.) {
5967
1.08M
        L = (Long)(dval(&u));
5968
1.08M
        if (!(dval(&u) -= L))
5969
126k
          ilim = i;
5970
1.08M
        *s++ = '0' + (int)L;
5971
1.08M
        if (i == ilim) {
5972
473k
          if (dval(&u) > 0.5 + dval(&eps))
5973
156k
            goto bump_up;
5974
316k
          else if (dval(&u) < 0.5 - dval(&eps))
5975
272k
            goto retc;
5976
44.3k
          break;
5977
473k
          }
5978
1.08M
        }
5979
473k
#ifndef No_leftright
5980
473k
      }
5981
44.3k
#endif
5982
44.3k
 fast_failed:
5983
44.3k
    s = buf;
5984
44.3k
    dval(&u) = dval(&d2);
5985
44.3k
    k = k0;
5986
44.3k
    ilim = ilim0;
5987
44.3k
    }
5988
5989
  /* Do we have a "small" integer? */
5990
5991
13.3M
  if (be >= 0 && k <= Int_max) {
5992
    /* Yes. */
5993
9.87M
    ds = tens[k];
5994
9.87M
    if (ndigits < 0 && ilim <= 0) {
5995
0
      S = mhi = 0;
5996
0
      if (ilim < 0 || dval(&u) <= 5*ds)
5997
0
        goto no_digits;
5998
0
      goto one_digit;
5999
0
      }
6000
29.0M
    for(i = 1;; i++, dval(&u) *= 10.) {
6001
29.0M
      L = (Long)(dval(&u) / ds);
6002
29.0M
      dval(&u) -= L*ds;
6003
#ifdef Check_FLT_ROUNDS
6004
      /* If FLT_ROUNDS == 2, L will usually be high by 1 */
6005
      if (dval(&u) < 0) {
6006
        L--;
6007
        dval(&u) += ds;
6008
        }
6009
#endif
6010
29.0M
      *s++ = '0' + (int)L;
6011
29.0M
      if (!dval(&u)) {
6012
#ifdef SET_INEXACT
6013
        inexact = 0;
6014
#endif
6015
9.87M
        break;
6016
9.87M
        }
6017
19.1M
      if (i == ilim) {
6018
#ifdef Honor_FLT_ROUNDS
6019
        if (mode > 1)
6020
        switch(Rounding) {
6021
          case 0: goto retc;
6022
          case 2: goto bump_up;
6023
          }
6024
#endif
6025
12
        dval(&u) += dval(&u);
6026
12
#ifdef ROUND_BIASED
6027
12
        if (dval(&u) >= ds)
6028
#else
6029
        if (dval(&u) > ds || (dval(&u) == ds && L & 1))
6030
#endif
6031
12
          {
6032
156k
 bump_up:
6033
170k
          while(*--s == '9')
6034
13.7k
            if (s == buf) {
6035
183
              k++;
6036
183
              *s = '0';
6037
183
              break;
6038
183
              }
6039
156k
          ++*s++;
6040
156k
          }
6041
156k
        break;
6042
12
        }
6043
19.1M
      }
6044
10.0M
    goto retc;
6045
9.87M
    }
6046
6047
3.45M
#endif /*}*/
6048
3.45M
  m2 = b2;
6049
3.45M
  m5 = b5;
6050
3.45M
  mhi = mlo = 0;
6051
3.45M
  if (leftright) {
6052
3.01M
    i =
6053
3.01M
#ifndef Sudden_Underflow
6054
3.01M
      denorm ? be + (Bias + (P-1) - 1 + 1) :
6055
3.01M
#endif
6056
#ifdef IBM
6057
      1 + 4*P - 3 - bbits + ((bbits + be - 1) & 3);
6058
#else
6059
3.01M
      1 + P - bbits;
6060
3.01M
#endif
6061
3.01M
    b2 += i;
6062
3.01M
    s2 += i;
6063
3.01M
    mhi = i2b(1 MTb);
6064
3.01M
    }
6065
3.45M
  if (m2 > 0 && s2 > 0) {
6066
2.61M
    i = m2 < s2 ? m2 : s2;
6067
2.61M
    b2 -= i;
6068
2.61M
    m2 -= i;
6069
2.61M
    s2 -= i;
6070
2.61M
    }
6071
3.45M
  if (b5 > 0) {
6072
1.03M
    if (leftright) {
6073
596k
      if (m5 > 0) {
6074
596k
        mhi = pow5mult(mhi, m5 MTb);
6075
596k
        b1 = mult(mhi, b MTb);
6076
596k
        Bfree(b MTb);
6077
596k
        b = b1;
6078
596k
        }
6079
596k
      if ((j = b5 - m5))
6080
0
        b = pow5mult(b, j MTb);
6081
596k
      }
6082
438k
    else
6083
438k
      b = pow5mult(b, b5 MTb);
6084
1.03M
    }
6085
3.45M
  S = i2b(1 MTb);
6086
3.45M
  if (s5 > 0)
6087
2.39M
    S = pow5mult(S, s5 MTb);
6088
6089
3.45M
  if (spec_case) {
6090
246k
    b2 += Log2P;
6091
246k
    s2 += Log2P;
6092
246k
    }
6093
6094
  /* Arrange for convenient computation of quotients:
6095
   * shift left if necessary so divisor has 4 leading 0 bits.
6096
   *
6097
   * Perhaps we should just compute leading 28 bits of S once
6098
   * and for all and pass them and a shift to quorem, so it
6099
   * can do shifts and ors to compute the numerator for q.
6100
   */
6101
3.45M
  i = dshift(S, s2);
6102
3.45M
  b2 += i;
6103
3.45M
  m2 += i;
6104
3.45M
  s2 += i;
6105
3.45M
  if (b2 > 0)
6106
3.41M
    b = lshift(b, b2 MTb);
6107
3.45M
  if (s2 > 0)
6108
3.43M
    S = lshift(S, s2 MTb);
6109
3.45M
#ifndef USE_BF96
6110
3.45M
  if (k_check) {
6111
1.87M
    if (cmp(b,S) < 0) {
6112
30.8k
      k--;
6113
30.8k
      b = multadd(b, 10, 0 MTb);  /* we botched the k estimate */
6114
30.8k
      if (leftright)
6115
29.0k
        mhi = multadd(mhi, 10, 0 MTb);
6116
30.8k
      ilim = ilim1;
6117
30.8k
      }
6118
1.87M
    }
6119
3.45M
#endif
6120
3.45M
  if (ilim <= 0 && (mode == 3 || mode == 5)) {
6121
1.55k
    if (ilim < 0 || cmp(b,S = multadd(S,5,0 MTb)) <= 0) {
6122
      /* no digits, fcvt style */
6123
5.37k
 no_digits:
6124
5.37k
      k = -1 - ndigits;
6125
5.37k
      goto ret;
6126
1.55k
      }
6127
5.56k
 one_digit:
6128
5.56k
    *s++ = '1';
6129
5.56k
    ++k;
6130
5.56k
    goto ret;
6131
1.55k
    }
6132
3.45M
  if (leftright) {
6133
3.01M
    if (m2 > 0)
6134
2.99M
      mhi = lshift(mhi, m2 MTb);
6135
6136
    /* Compute mlo -- check for special case
6137
     * that d is a normalized power of 2.
6138
     */
6139
6140
3.01M
    mlo = mhi;
6141
3.01M
    if (spec_case) {
6142
246k
      mhi = Balloc(mhi->k MTb);
6143
246k
      Bcopy(mhi, mlo);
6144
246k
      mhi = lshift(mhi, Log2P MTb);
6145
246k
      }
6146
6147
46.0M
    for(i = 1;;i++) {
6148
46.0M
      dig = quorem(b,S) + '0';
6149
      /* Do we yet have the shortest decimal string
6150
       * that will round to d?
6151
       */
6152
46.0M
      j = cmp(b, mlo);
6153
46.0M
      delta = diff(S, mhi MTb);
6154
46.0M
      j1 = delta->sign ? 1 : cmp(b, delta);
6155
46.0M
      Bfree(delta MTb);
6156
#ifndef ROUND_BIASED
6157
      if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
6158
#ifdef Honor_FLT_ROUNDS
6159
        && (mode <= 1 || Rounding >= 1)
6160
#endif
6161
                   ) {
6162
        if (dig == '9')
6163
          goto round_9_up;
6164
        if (j > 0)
6165
          dig++;
6166
#ifdef SET_INEXACT
6167
        else if (!b->x[0] && b->wds <= 1)
6168
          inexact = 0;
6169
#endif
6170
        *s++ = dig;
6171
        goto ret;
6172
        }
6173
#endif
6174
46.0M
      if (j < 0 || (j == 0 && mode != 1
6175
#ifndef ROUND_BIASED
6176
              && !(word1(&u) & 1)
6177
#endif
6178
43.7M
          )) {
6179
2.31M
        if (!b->x[0] && b->wds <= 1) {
6180
#ifdef SET_INEXACT
6181
          inexact = 0;
6182
#endif
6183
612k
          goto accept_dig;
6184
612k
          }
6185
#ifdef Honor_FLT_ROUNDS
6186
        if (mode > 1)
6187
         switch(Rounding) {
6188
          case 0: goto accept_dig;
6189
          case 2: goto keep_dig;
6190
          }
6191
#endif /*Honor_FLT_ROUNDS*/
6192
1.70M
        if (j1 > 0) {
6193
837k
          b = lshift(b, 1 MTb);
6194
837k
          j1 = cmp(b, S);
6195
837k
#ifdef ROUND_BIASED
6196
837k
          if (j1 >= 0 /*)*/
6197
#else
6198
          if ((j1 > 0 || (j1 == 0 && dig & 1))
6199
#endif
6200
412k
          && dig++ == '9')
6201
95
            goto round_9_up;
6202
837k
          }
6203
2.31M
 accept_dig:
6204
2.31M
        *s++ = dig;
6205
2.31M
        goto ret;
6206
1.70M
        }
6207
43.7M
      if (j1 > 0) {
6208
#ifdef Honor_FLT_ROUNDS
6209
        if (!Rounding && mode > 1)
6210
          goto accept_dig;
6211
#endif
6212
698k
        if (dig == '9') { /* possible if i == 1 */
6213
1.47k
 round_9_up:
6214
1.47k
          *s++ = '9';
6215
1.47k
          goto roundoff;
6216
1.38k
          }
6217
697k
        *s++ = dig + 1;
6218
697k
        goto ret;
6219
698k
        }
6220
#ifdef Honor_FLT_ROUNDS
6221
 keep_dig:
6222
#endif
6223
43.0M
      *s++ = dig;
6224
43.0M
      if (i == ilim)
6225
0
        break;
6226
43.0M
      b = multadd(b, 10, 0 MTb);
6227
43.0M
      if (mlo == mhi)
6228
39.2M
        mlo = mhi = multadd(mhi, 10, 0 MTb);
6229
3.72M
      else {
6230
3.72M
        mlo = multadd(mlo, 10, 0 MTb);
6231
3.72M
        mhi = multadd(mhi, 10, 0 MTb);
6232
3.72M
        }
6233
43.0M
      }
6234
3.01M
    }
6235
440k
  else
6236
21.7M
    for(i = 1;; i++) {
6237
21.7M
      dig = quorem(b,S) + '0';
6238
21.7M
      *s++ = dig;
6239
21.7M
      if (!b->x[0] && b->wds <= 1) {
6240
#ifdef SET_INEXACT
6241
        inexact = 0;
6242
#endif
6243
4.20k
        goto ret;
6244
4.20k
        }
6245
21.7M
      if (i >= ilim)
6246
436k
        break;
6247
21.2M
      b = multadd(b, 10, 0 MTb);
6248
21.2M
      }
6249
6250
  /* Round off last digit */
6251
6252
#ifdef Honor_FLT_ROUNDS
6253
  if (mode > 1)
6254
    switch(Rounding) {
6255
      case 0: goto ret;
6256
      case 2: goto roundoff;
6257
      }
6258
#endif
6259
436k
  b = lshift(b, 1 MTb);
6260
436k
  j = cmp(b, S);
6261
436k
#ifdef ROUND_BIASED
6262
436k
  if (j >= 0)
6263
#else
6264
  if (j > 0 || (j == 0 && dig & 1))
6265
#endif
6266
241k
    {
6267
242k
 roundoff:
6268
263k
    while(*--s == '9')
6269
21.9k
      if (s == buf) {
6270
1.47k
        k++;
6271
1.47k
        *s++ = '1';
6272
1.47k
        goto ret;
6273
1.47k
        }
6274
241k
    ++*s++;
6275
241k
    }
6276
3.46M
 ret:
6277
3.46M
  Bfree(S MTb);
6278
3.46M
  if (mhi) {
6279
3.01M
    if (mlo && mlo != mhi)
6280
246k
      Bfree(mlo MTb);
6281
3.01M
    Bfree(mhi MTb);
6282
3.01M
    }
6283
13.7M
 retc:
6284
13.8M
  while(s > buf && s[-1] == '0')
6285
36.2k
    --s;
6286
13.7M
 ret1:
6287
13.7M
  if (b)
6288
13.7M
    Bfree(b MTb);
6289
13.7M
  *s = 0;
6290
13.7M
  *decpt = k + 1;
6291
13.7M
  if (rve)
6292
13.7M
    *rve = s;
6293
#ifdef SET_INEXACT
6294
  if (inexact) {
6295
    if (!oldinexact) {
6296
      word0(&u) = Exp_1 + (70 << Exp_shift);
6297
      word1(&u) = 0;
6298
      dval(&u) += 1.;
6299
      }
6300
    }
6301
  else if (!oldinexact)
6302
    clear_inexact();
6303
#endif
6304
13.7M
  return buf;
6305
13.7M
  }
6306
6307
 char *
6308
fx_dtoa(double dd, int mode, int ndigits, int *decpt, int *sign, char **rve __XS__d)
6309
15.8M
{
6310
  /*  Sufficient space is allocated to the return value
6311
    to hold the suppressed trailing zeros.
6312
    See dtoa_r() above for details on the other arguments.
6313
  */
6314
15.8M
#ifndef MULTIPLE_THREADS
6315
#ifndef __XS__
6316
  if (dtoa_result)
6317
    freedtoa(dtoa_result __XS__a);
6318
#endif
6319
15.8M
#endif
6320
15.8M
  return dtoa_r(dd, mode, ndigits, decpt, sign, rve, 0, 0 __XS__a);
6321
15.8M
  }
6322
6323
#ifdef __cplusplus
6324
}
6325
#endif
6326
6327
static void fxDTOACleanup(txMachine* the, ThInfo* DTOA);
6328
static void fxDTOASetup(txMachine* the, ThInfo* DTOA);
6329
6330
void fxDTOACleanup(txMachine* the, ThInfo* DTOA)
6331
43.7M
{
6332
43.7M
#if mxUseChunkHeap
6333
43.7M
  if (DTOA->dirty)
6334
5.20M
#endif
6335
5.20M
  {
6336
5.20M
    Bigint* b;
6337
5.20M
    int i, c = Kmax +1 ;
6338
46.8M
    for (i = 0; i < c; i++) {
6339
41.6M
      b = DTOA->Freelist[i];
6340
60.1M
      while(b) {
6341
18.4M
        Bigint* next = b->next;
6342
18.4M
        fxDTOAFree(b, DTOA);
6343
18.4M
        b = next;
6344
18.4M
      }
6345
41.6M
    }
6346
6347
5.20M
    b = DTOA->P5s;
6348
10.1M
    while(b) {
6349
4.92M
      Bigint* next = b->next;
6350
4.92M
      fxDTOAFree(b, DTOA);
6351
4.92M
      b = next;
6352
4.92M
    }
6353
5.20M
  }
6354
43.7M
}
6355
6356
static void* fxDTOAMalloc(size_t size, void* it)
6357
87.1M
{
6358
87.1M
  ThInfo* DTOA = it;
6359
87.1M
  txMachine* the = DTOA->the;
6360
87.1M
  void* block = C_NULL;
6361
87.1M
#if mxUseChunkHeap
6362
87.1M
  if (the) {
6363
87.1M
    if ((DTOA->current + size) <= (txByte*)(the->firstBlock->limit)) {
6364
63.7M
      block = DTOA->current;
6365
63.7M
      DTOA->current += size;
6366
63.7M
      return block;
6367
63.7M
    }
6368
87.1M
  }
6369
23.3M
  DTOA->dirty = 1;
6370
23.3M
#endif
6371
23.3M
  block = c_malloc(size);
6372
23.3M
  if (!block)
6373
0
    fxAbort(the, XS_NOT_ENOUGH_MEMORY_EXIT);
6374
  //fprintf(stderr, "malloc %zu %p\n", size, block);
6375
23.3M
  return block;
6376
87.1M
}
6377
6378
static void fxDTOAFree(void* block, void* it)
6379
23.3M
{
6380
  //fprintf(stderr, "free %p\n", block);
6381
23.3M
#if mxUseChunkHeap
6382
23.3M
  ThInfo* DTOA = it;
6383
23.3M
  txMachine* the = DTOA->the;
6384
23.3M
    if (the) {
6385
23.3M
        if (((txByte*)(the->firstBlock->current) <= (txByte*)block) && ((txByte*)block < DTOA->current))
6386
33.7k
          return;
6387
23.3M
    }
6388
23.3M
#endif
6389
23.3M
  c_free(block);
6390
23.3M
}
6391
6392
void fxDTOASetup(txMachine* the, ThInfo* DTOA)
6393
43.7M
{
6394
43.7M
  c_memset(DTOA, 0, sizeof(ThInfo));
6395
43.7M
  if (the) {
6396
43.7M
    DTOA->the = the;
6397
43.7M
#if mxUseChunkHeap
6398
  #if 0
6399
    if ((txByte*)(the->firstBlock->limit) - (txByte*)(the->firstBlock->current) > 192)
6400
      DTOA->current = (txByte*)(the->firstBlock->limit) - 192;
6401
    else
6402
  #endif
6403
43.7M
      DTOA->current = (txByte*)(the->firstBlock->current);
6404
43.7M
#endif
6405
43.7M
  }
6406
43.7M
}
6407
6408
txString fxIntegerToString(void* the, txInteger theValue, txString theBuffer, txSize theSize)
6409
41.1M
{
6410
41.1M
  c_snprintf(theBuffer, theSize, "%d", (int)theValue);
6411
41.1M
  return theBuffer;
6412
41.1M
}
6413
6414
txInteger fxNumberToInteger(txNumber theValue)
6415
3.82M
{
6416
3.82M
  if (c_fpclassify(theValue) == C_FP_NORMAL) {
6417
7.86M
    #define MODULO 4294967296.0
6418
2.57M
    txNumber aNumber = c_fmod(c_trunc(theValue), MODULO);
6419
2.57M
    if (aNumber >= MODULO / 2)
6420
689k
      aNumber -= MODULO;
6421
1.88M
    else if (aNumber < -MODULO / 2)
6422
148k
      aNumber += MODULO;
6423
2.57M
    return (txInteger)aNumber;
6424
2.57M
  }
6425
1.25M
  return 0; 
6426
3.82M
}
6427
6428
txString fxNumberToString(void* the, txNumber theValue, txString theBuffer, txSize theSize, txByte theMode, txInteger thePrecision)
6429
15.8M
{
6430
15.8M
  ThInfo DTOA;
6431
15.8M
  char* base = C_NULL;
6432
15.8M
  int mode, precision, decpt, sign, count, exponent, pad;
6433
15.8M
  char* start;
6434
15.8M
  char* stop;
6435
15.8M
  char* result;
6436
6437
15.8M
  fxDTOASetup(the, &DTOA);
6438
15.8M
  switch (theMode) {
6439
168
  case 'e':
6440
168
    if (thePrecision > 0) {
6441
147
      mode = 2;
6442
147
      precision = thePrecision;
6443
147
    }
6444
21
    else {
6445
21
      mode = 0;
6446
21
      precision = 0;
6447
21
    }
6448
168
    break;
6449
136k
  case 'f':
6450
136k
    mode = 3;
6451
136k
    precision = thePrecision;
6452
136k
    break;
6453
849k
  case 'g':
6454
849k
    mode = 2;
6455
849k
    precision = thePrecision;
6456
849k
    break;
6457
14.8M
  default:
6458
14.8M
    mode = 0;
6459
14.8M
    precision = 0;
6460
14.8M
    break;
6461
15.8M
  }
6462
15.8M
  base = start = fx_dtoa(theValue, mode, precision, &decpt, &sign, &stop, &DTOA);
6463
15.8M
  count = mxPtrDiff(stop - start);
6464
15.8M
  result = theBuffer;
6465
15.8M
  theSize--; // C string
6466
15.8M
  if (sign && theValue) {
6467
1.77M
    *result++ = '-';
6468
1.77M
    theSize--;
6469
1.77M
  }
6470
15.8M
    if (decpt != 9999) {
6471
14.3M
    switch (theMode) {
6472
168
    case 'e':
6473
168
      exponent = 1;
6474
168
      if (thePrecision > 0)
6475
147
        pad = thePrecision;
6476
21
      else
6477
21
        pad = count;
6478
168
      break;
6479
136k
    case 'f':
6480
136k
      exponent = 0;
6481
136k
      pad = thePrecision;
6482
136k
      break;
6483
849k
    case 'g':
6484
849k
      if ((decpt < -5) || (thePrecision < decpt)) {
6485
289k
        exponent = 1;
6486
289k
        pad = thePrecision; 
6487
289k
      }
6488
560k
      else {
6489
560k
        exponent = 0;
6490
560k
                pad = thePrecision - count - decpt + 1;
6491
560k
      }
6492
849k
      break;
6493
13.3M
    default:
6494
13.3M
      if ((decpt < -5) || (21 < decpt)) {
6495
1.40M
        exponent = 1;
6496
1.40M
        pad = count;  
6497
1.40M
      }
6498
11.9M
      else {
6499
11.9M
        exponent = 0;
6500
11.9M
        pad = 0;  
6501
11.9M
      }
6502
13.3M
      break;
6503
14.3M
    }
6504
14.3M
    if (exponent) {
6505
1.69M
      theSize -= 5 - pad;
6506
1.69M
      if (theSize < 0) goto error;
6507
1.69M
      *result++ = *start++;
6508
1.69M
      pad--;
6509
1.69M
      if (pad > 0) {
6510
1.64M
        *result++ = '.';
6511
30.6M
        while ((start < stop) && (pad > 0)) {
6512
29.0M
          *result++ = *start++;
6513
29.0M
          pad--;
6514
29.0M
        }
6515
1.68M
        while  (pad > 0) {
6516
31.6k
          *result++ = '0';
6517
31.6k
          pad--;
6518
31.6k
        }
6519
1.64M
      }
6520
1.69M
      *result++ = 'e';
6521
1.69M
      decpt--;
6522
1.69M
      if (decpt < 0) {
6523
697k
        *result++ = '-';
6524
697k
        decpt = 0 - decpt;
6525
697k
      }
6526
996k
      else
6527
996k
        *result++ = '+';
6528
1.69M
      pad = 1000;
6529
4.45M
      while (pad > 1) {
6530
4.43M
        if (decpt >= pad) 
6531
1.68M
          break;
6532
2.75M
        pad /= 10;
6533
2.75M
      }
6534
5.71M
      while (pad) {
6535
4.02M
        *result++ = '0' + (decpt / pad);
6536
4.02M
        decpt %= pad;
6537
4.02M
        pad /= 10;
6538
4.02M
      }
6539
1.69M
    }
6540
12.6M
    else if (decpt <= 0) {
6541
525k
      theSize -= 2 - decpt + (mxPtrDiff(stop - start));
6542
525k
      if (theSize < 0) goto error;
6543
525k
      *result++ = '0';
6544
525k
      if ((decpt < 0) || (start < stop) || (pad > 0))
6545
520k
        *result++ = '.';
6546
1.61M
      while (decpt < 0) {
6547
1.08M
        *result++ = '0';
6548
1.08M
        decpt++;
6549
1.08M
        pad--;
6550
1.08M
      }
6551
14.6M
      while (start < stop) {
6552
14.1M
        *result++ = *start++;
6553
14.1M
        pad--;
6554
14.1M
      }
6555
525k
      if (pad > 0) {
6556
8.59k
        theSize -= pad;
6557
8.59k
        if (theSize < 0) goto error;
6558
17.4k
        while (pad > 0) {
6559
8.85k
          *result++ = '0';
6560
8.85k
          pad--;
6561
8.85k
        }
6562
8.59k
      }
6563
525k
    }
6564
12.0M
    else if (decpt < count) {
6565
791k
      theSize -= decpt + 1 + (mxPtrDiff(stop - start));
6566
791k
      if (theSize < 0) goto error;
6567
8.77M
      while (decpt > 0) {
6568
7.98M
        *result++ = *start++;
6569
7.98M
        decpt--;
6570
7.98M
      }
6571
791k
      *result++ = '.';
6572
3.86M
      while (start < stop) {
6573
3.07M
        *result++ = *start++;
6574
3.07M
        pad--;
6575
3.07M
      }
6576
791k
      if (pad > 0) {
6577
52
        theSize -= pad;
6578
52
        if (theSize < 0) goto error;
6579
170
        while (pad > 0) {
6580
118
          *result++ = '0';
6581
118
          pad--;
6582
118
        }
6583
52
      }
6584
791k
    }
6585
11.3M
    else {
6586
11.3M
      theSize -= (mxPtrDiff(stop - start));
6587
11.3M
      if (theSize < 0) goto error;
6588
53.6M
      while (start < stop) {
6589
42.3M
        *result++ = *start++;
6590
42.3M
        decpt--;
6591
42.3M
      }
6592
11.3M
            theSize -= decpt;
6593
11.3M
            if (theSize < 0) goto error;
6594
13.1M
      while (decpt > 0) {
6595
1.85M
        *result++ = '0';
6596
1.85M
        decpt--;
6597
1.85M
      }
6598
11.3M
      if (pad > 0) {
6599
191k
        theSize -= 1 + pad;
6600
191k
        if (theSize < 0) goto error;
6601
191k
        *result++ = '.';
6602
383k
        while (pad > 0) {
6603
191k
          *result++ = '0';
6604
191k
          pad--;
6605
191k
        }
6606
191k
      }
6607
11.3M
    }
6608
14.3M
  }
6609
1.50M
  else {
6610
1.50M
    theSize -= (mxPtrDiff(stop - start));
6611
1.50M
    if (theSize < 0) goto error;
6612
8.29M
    while (start < stop)
6613
6.79M
      *result++ = *start++;
6614
1.50M
  }
6615
15.8M
error:
6616
15.8M
  *result = 0;
6617
15.8M
  if (base)
6618
15.8M
    freedtoa(base, &DTOA);
6619
15.8M
  fxDTOACleanup(the, &DTOA);
6620
15.8M
  return theBuffer;
6621
15.8M
}
6622
6623
txNumber fxStringToNumber(void* the, txString theString, txFlag whole)
6624
28.4M
{
6625
28.4M
  txNumber result = whole ? 0 : NAN;
6626
28.4M
  txString p = fxSkipSpaces(theString), q;
6627
28.4M
  char c = *p;
6628
28.4M
  if (c) {
6629
28.0M
    txU4 d = *(p + 1);
6630
28.0M
    if (whole && (c == '0') && ((d == 'B') || (d == 'b') || (d == 'O') || (d == 'o') || (d == 'X') || (d == 'x'))) {
6631
36.7k
      p += 2;
6632
36.7k
            q = p;
6633
36.7k
      if ((d == 'B') || (d == 'b')) {
6634
52.3k
        while ((c = *p)) {
6635
51.5k
          if (('0' <= c) && (c <= '1'))
6636
51.1k
            result = (result * 2) + (c - '0');
6637
380
          else
6638
380
            break;
6639
51.1k
          p++;
6640
51.1k
        }
6641
1.16k
      }
6642
35.6k
      else if ((d == 'O') || (d == 'o')) {
6643
15.7k
        while ((c = *p)) {
6644
14.7k
          if (('0' <= c) && (c <= '7'))
6645
13.7k
            result = (result * 8) + (c - '0');
6646
1.04k
          else
6647
1.04k
            break;
6648
13.7k
          p++;
6649
13.7k
        }
6650
1.97k
      }
6651
33.6k
      else if ((d == 'X') || (d == 'x')) {
6652
440k
        while ((c = *p)) {
6653
439k
          if (('0' <= c) && (c <= '9'))
6654
403k
            result = (result * 16) + (c - '0');
6655
36.2k
          else if (('a' <= c) && (c <= 'f'))
6656
2.77k
            result = (result * 16) + (10 + c - 'a');
6657
33.4k
          else if (('A' <= c) && (c <= 'F'))
6658
723
            result = (result * 16) + (10 + c - 'A');
6659
32.7k
          else
6660
32.7k
            break;
6661
406k
          p++;
6662
406k
        }
6663
33.6k
      }
6664
36.7k
      if (p == q)
6665
28.1k
        result = NAN;
6666
8.57k
      else
6667
8.57k
            q = p;
6668
36.7k
    }
6669
27.9M
    else {
6670
27.9M
      ThInfo DTOA;
6671
27.9M
      fxDTOASetup(the, &DTOA);
6672
27.9M
      result = strtod2(p, &q, &DTOA);
6673
27.9M
      fxDTOACleanup(the, &DTOA);
6674
27.9M
      if ((p == q) && !whole)
6675
12
        result = NAN;
6676
27.9M
    }
6677
28.0M
    if (whole) {
6678
19.5M
      p = fxSkipSpaces(q);
6679
19.5M
      if (*p)
6680
8.12M
        result = NAN;
6681
19.5M
    }
6682
28.0M
  }
6683
28.4M
  return result;
6684
28.4M
}