Coverage Report

Created: 2026-08-31 06:25

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