Coverage Report

Created: 2025-11-16 07:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mysql-server/strings/dtoa.cc
Line
Count
Source
1
/* Copyright (c) 2007, 2025, Oracle and/or its affiliates.
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License, version 2.0,
5
   as published by the Free Software Foundation.
6
7
   This program is designed to work with certain software (including
8
   but not limited to OpenSSL) that is licensed under separate terms,
9
   as designated in a particular file or component or in included license
10
   documentation.  The authors of MySQL hereby grant you an additional
11
   permission to link the program and your derivative works with the
12
   separately licensed software that they have either included with
13
   the program or referenced in the documentation.
14
15
   Without limiting anything contained in the foregoing, this file,
16
   which is part of C Driver for MySQL (Connector/C), is also subject to the
17
   Universal FOSS Exception, version 1.0, a copy of which can be found at
18
   http://oss.oracle.com/licenses/universal-foss-exception.
19
20
   This program is distributed in the hope that it will be useful,
21
   but WITHOUT ANY WARRANTY; without even the implied warranty of
22
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
   GNU General Public License, version 2.0, for more details.
24
25
   You should have received a copy of the GNU General Public License
26
   along with this program; if not, write to the Free Software
27
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
28
29
/****************************************************************
30
31
  This file incorporates work covered by the following copyright and
32
  permission notice:
33
34
  The author of this software is David M. Gay.
35
36
  Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
37
38
  Permission to use, copy, modify, and distribute this software for any
39
  purpose without fee is hereby granted, provided that this entire notice
40
  is included in all copies of any software which is or includes a copy
41
  or modification of this software and in all copies of the supporting
42
  documentation for such software.
43
44
  THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
45
  WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
46
  REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
47
  OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
48
49
 ***************************************************************/
50
51
#include "my_config.h"
52
53
#include "mysql/strings/dtoa.h"
54
55
#include <algorithm>
56
#include <cassert>
57
#include <cerrno>
58
#include <cfloat>
59
#include <cstdint>
60
#include <cstdlib>
61
#include <cstring>
62
#include <limits>
63
64
#include "my_pointer_arithmetic.h"
65
#include "template_utils.h"
66
67
#ifndef EOVERFLOW
68
#define EOVERFLOW 84
69
#endif
70
71
/**
72
   Appears to suffice to not call malloc() in most cases.
73
   @todo
74
     see if it is possible to get rid of malloc().
75
     this constant is sufficient to avoid malloc() on all inputs I have tried.
76
*/
77
#define DTOA_BUFF_SIZE (460 * sizeof(void *))
78
79
/* Magic value returned by dtoa() to indicate overflow */
80
0
#define DTOA_OVERFLOW 9999
81
82
static double my_strtod_int(const char *, const char **, int *, char *, size_t);
83
static char *dtoa(double, int, int, int *, int *, char **, char *, size_t);
84
static void dtoa_free(char *, char *, size_t);
85
86
/**
87
   @brief
88
   Converts a given floating point number to a zero-terminated string
89
   representation using the 'f' format.
90
91
   @details
92
   This function is a wrapper around dtoa() to do the same as
93
   sprintf(to, "%-.*f", precision, x), though the conversion is usually more
94
   precise. The only difference is in handling [-,+]infinity and nan values,
95
   in which case we print '0\0' to the output string and indicate an overflow.
96
97
   @param x           the input floating point number.
98
   @param precision   the number of digits after the decimal point.
99
                      All properties of sprintf() apply:
100
                      - if the number of significant digits after the decimal
101
                        point is less than precision, the resulting string is
102
                        right-padded with zeros
103
                      - if the precision is 0, no decimal point appears
104
                      - if a decimal point appears, at least one digit appears
105
                        before it
106
   @param to          pointer to the output buffer. The longest string which
107
                      my_fcvt() can return is FLOATING_POINT_BUFFER bytes
108
                      (including the terminating '\0').
109
   @param error       if not NULL, points to a location where the status of
110
                      conversion is stored upon return.
111
                      false  successful conversion
112
                      true   the input number is [-,+]infinity or nan.
113
                             The output string in this case is always '0'.
114
   @param shorten     Whether to minimize the number of significant digits. If
115
                      true, write only the minimum number of digits necessary to
116
                      reproduce the double value when parsing the string. If
117
                      false, zeros are added to the end to reach the precision
118
                      limit.
119
120
   @return            number of written characters (excluding terminating '\0')
121
*/
122
123
static size_t my_fcvt_internal(double x, int precision, bool shorten, char *to,
124
0
                               bool *error) {
125
0
  int decpt, sign, len, i;
126
0
  char *res, *src, *end, *dst = to;
127
0
  char buf[DTOA_BUFF_SIZE];
128
0
  assert(precision >= 0 && precision < DECIMAL_NOT_SPECIFIED && to != nullptr);
129
130
0
  res = dtoa(x, 5, precision, &decpt, &sign, &end, buf, sizeof(buf));
131
132
0
  if (decpt == DTOA_OVERFLOW) {
133
0
    dtoa_free(res, buf, sizeof(buf));
134
0
    *to++ = '0';
135
0
    *to = '\0';
136
0
    if (error != nullptr) *error = true;
137
0
    return 1;
138
0
  }
139
140
0
  src = res;
141
0
  len = (int)(end - src);
142
143
0
  if (sign) *dst++ = '-';
144
145
0
  if (decpt <= 0) {
146
0
    *dst++ = '0';
147
0
    *dst++ = '.';
148
0
    for (i = decpt; i < 0; i++) *dst++ = '0';
149
0
  }
150
151
0
  for (i = 1; i <= len; i++) {
152
0
    *dst++ = *src++;
153
0
    if (i == decpt && i < len) *dst++ = '.';
154
0
  }
155
0
  while (i++ <= decpt) *dst++ = '0';
156
157
0
  if (precision > 0 && !shorten) {
158
0
    if (len <= decpt) *dst++ = '.';
159
160
0
    for (i = precision - std::max(0, (len - decpt)); i > 0; i--) *dst++ = '0';
161
0
  }
162
163
0
  *dst = '\0';
164
0
  if (error != nullptr) *error = false;
165
166
0
  dtoa_free(res, buf, sizeof(buf));
167
168
0
  return dst - to;
169
0
}
170
171
/**
172
   @brief
173
   Converts a given floating point number to a zero-terminated string
174
   representation using the 'f' format.
175
176
   @details
177
   This function is a wrapper around dtoa() to do the same as
178
   sprintf(to, "%-.*f", precision, x), though the conversion is usually more
179
   precise. The only difference is in handling [-,+]infinity and nan values,
180
   in which case we print '0\0' to the output string and indicate an overflow.
181
182
   @param x           the input floating point number.
183
   @param precision   the number of digits after the decimal point.
184
                      All properties of sprintf() apply:
185
                      - if the number of significant digits after the decimal
186
                        point is less than precision, the resulting string is
187
                        right-padded with zeros
188
                      - if the precision is 0, no decimal point appears
189
                      - if a decimal point appears, at least one digit appears
190
                        before it
191
   @param to          pointer to the output buffer. The longest string which
192
                      my_fcvt() can return is FLOATING_POINT_BUFFER bytes
193
                      (including the terminating '\0').
194
   @param error       if not NULL, points to a location where the status of
195
                      conversion is stored upon return.
196
                      false  successful conversion
197
                      true   the input number is [-,+]infinity or nan.
198
                             The output string in this case is always '0'.
199
200
   @return            number of written characters (excluding terminating '\0')
201
*/
202
0
size_t my_fcvt(double x, int precision, char *to, bool *error) {
203
0
  return my_fcvt_internal(x, precision, false, to, error);
204
0
}
205
206
/// Identical to my_fcvt, except that trailing zeros after the decimal point
207
/// will be removed.
208
size_t my_fcvt_no_trailing_zero(double x, int precision, char *to,
209
0
                                bool *error) {
210
0
  return my_fcvt_internal(x, precision, true, to, error);
211
0
}
212
213
/**
214
   @brief
215
   Converts a given floating point number to a zero-terminated string
216
   representation using the 'f' format.
217
218
   @details
219
   This function is a wrapper around dtoa() to do almost the same as
220
   sprintf(to, "%-.*f", precision, x), though the conversion is usually more
221
   precise. The only difference is in handling [-,+]infinity and nan values,
222
   in which case we print '0\0' to the output string and indicate an overflow.
223
224
   The string always contains the minimum number of digits necessary to
225
   reproduce the same binary double value if the string is parsed back to a
226
   double value.
227
228
   @param x           the input floating point number.
229
   @param to          pointer to the output buffer. The longest string which
230
                      my_fcvt() can return is FLOATING_POINT_BUFFER bytes
231
                      (including the terminating '\0').
232
   @param error       if not NULL, points to a location where the status of
233
                      conversion is stored upon return.
234
                      false  successful conversion
235
                      true   the input number is [-,+]infinity or nan.
236
                             The output string in this case is always '0'.
237
238
   @return            number of written characters (excluding terminating '\0')
239
*/
240
0
size_t my_fcvt_compact(double x, char *to, bool *error) {
241
0
  return my_fcvt_internal(x, std::numeric_limits<double>::max_digits10, true,
242
0
                          to, error);
243
0
}
244
245
/**
246
   @brief
247
   Converts a given floating point number to a zero-terminated string
248
   representation with a given field width using the 'e' format
249
   (aka scientific notation) or the 'f' one.
250
251
   @details
252
   The format is chosen automatically to provide the most number of significant
253
   digits (and thus, precision) with a given field width. In many cases, the
254
   result is similar to that of sprintf(to, "%g", x) with a few notable
255
   differences:
256
   - the conversion is usually more precise than C library functions.
257
   - there is no 'precision' argument. instead, we specify the number of
258
     characters available for conversion (i.e. a field width).
259
   - the result never exceeds the specified field width. If the field is too
260
     short to contain even a rounded decimal representation, my_gcvt()
261
     indicates overflow and truncates the output string to the specified width.
262
   - float-type arguments are handled differently than double ones. For a
263
     float input number (i.e. when the 'type' argument is MY_GCVT_ARG_FLOAT)
264
     we deliberately limit the precision of conversion by FLT_DIG digits to
265
     avoid garbage past the significant digits.
266
   - unlike sprintf(), in cases where the 'e' format is preferred,  we don't
267
     zero-pad the exponent to save space for significant digits. The '+' sign
268
     for a positive exponent does not appear for the same reason.
269
270
   @param x           the input floating point number.
271
   @param type        is either MY_GCVT_ARG_FLOAT or MY_GCVT_ARG_DOUBLE.
272
                      Specifies the type of the input number (see notes above).
273
   @param width       field width in characters. The minimal field width to
274
                      hold any number representation (albeit rounded) is 7
275
                      characters ("-Ne-NNN").
276
   @param to          pointer to the output buffer. The result is always
277
                      zero-terminated, and the longest returned string is thus
278
                      'width + 1' bytes.
279
   @param error       if not NULL, points to a location where the status of
280
                      conversion is stored upon return.
281
                      false  successful conversion
282
                      true   the input number is [-,+]infinity or nan.
283
                             The output string in this case is always '0'.
284
   @return            number of written characters (excluding terminating '\0')
285
286
   @todo
287
   Check if it is possible and  makes sense to do our own rounding on top of
288
   dtoa() instead of calling dtoa() twice in (rare) cases when the resulting
289
   string representation does not fit in the specified field width and we want
290
   to re-round the input number with fewer significant digits. Examples:
291
292
     my_gcvt(-9e-3, ..., 4, ...);
293
     my_gcvt(-9e-3, ..., 2, ...);
294
     my_gcvt(1.87e-3, ..., 4, ...);
295
     my_gcvt(55, ..., 1, ...);
296
297
   We do our best to minimize such cases by:
298
299
   - passing to dtoa() the field width as the number of significant digits
300
301
   - removing the sign of the number early (and decreasing the width before
302
     passing it to dtoa())
303
304
   - choosing the proper format to preserve the most number of significant
305
     digits.
306
*/
307
308
size_t my_gcvt(double x, my_gcvt_arg_type type, int width, char *to,
309
0
               bool *error) {
310
0
  int decpt, sign, len, exp_len;
311
0
  char *res, *src, *end, *dst = to, *dend = dst + width;
312
0
  char buf[DTOA_BUFF_SIZE];
313
0
  bool have_space, force_e_format;
314
0
  assert(width > 0 && to != nullptr);
315
316
  /* We want to remove '-' from equations early */
317
0
  if (x < 0.) width--;
318
319
0
  res =
320
0
      dtoa(x, 4, type == MY_GCVT_ARG_DOUBLE ? width : std::min(width, FLT_DIG),
321
0
           &decpt, &sign, &end, buf, sizeof(buf));
322
0
  if (decpt == DTOA_OVERFLOW) {
323
0
    dtoa_free(res, buf, sizeof(buf));
324
0
    *to++ = '0';
325
0
    *to = '\0';
326
0
    if (error != nullptr) *error = true;
327
0
    return 1;
328
0
  }
329
330
0
  if (error != nullptr) *error = false;
331
332
0
  src = res;
333
0
  len = (int)(end - res);
334
335
  /*
336
    Number of digits in the exponent from the 'e' conversion.
337
     The sign of the exponent is taken into account separately, we don't need
338
     to count it here.
339
   */
340
0
  exp_len = 1 + (decpt >= 101 || decpt <= -99) + (decpt >= 11 || decpt <= -9);
341
342
  /*
343
     Do we have enough space for all digits in the 'f' format?
344
     Let 'len' be the number of significant digits returned by dtoa,
345
     and F be the length of the resulting decimal representation.
346
     Consider the following cases:
347
     1. decpt <= 0, i.e. we have "0.NNN" => F = len - decpt + 2
348
     2. 0 < decpt < len, i.e. we have "NNN.NNN" => F = len + 1
349
     3. len <= decpt, i.e. we have "NNN00" => F = decpt
350
  */
351
0
  have_space = (decpt <= 0                 ? len - decpt + 2
352
0
                : decpt > 0 && decpt < len ? len + 1
353
0
                                           : decpt) <= width;
354
  /*
355
    The following is true when no significant digits can be placed with the
356
    specified field width using the 'f' format, and the 'e' format
357
    will not be truncated.
358
  */
359
0
  force_e_format = (decpt <= 0 && width <= 2 - decpt && width >= 3 + exp_len);
360
  /*
361
    Assume that we don't have enough space to place all significant digits in
362
    the 'f' format. We have to choose between the 'e' format and the 'f' one
363
    to keep as many significant digits as possible.
364
    Let E and F be the lengths of decimal representation in the 'e' and 'f'
365
    formats, respectively. We want to use the 'f' format if, and only if F <= E.
366
    Consider the following cases:
367
    1. decpt <= 0.
368
       F = len - decpt + 2 (see above)
369
       E = len + (len > 1) + 1 + 1 (decpt <= -99) + (decpt <= -9) + 1
370
       ("N.NNe-MMM")
371
       (F <= E) <=> (len == 1 && decpt >= -1) || (len > 1 && decpt >= -2)
372
       We also need to ensure that if the 'f' format is chosen,
373
       the field width allows us to place at least one significant digit
374
       (i.e. width > 2 - decpt). If not, we prefer the 'e' format.
375
    2. 0 < decpt < len
376
       F = len + 1 (see above)
377
       E = len + 1 + 1 + ... ("N.NNeMMM")
378
       F is always less than E.
379
    3. len <= decpt <= width
380
       In this case we have enough space to represent the number in the 'f'
381
       format, so we prefer it with some exceptions.
382
    4. width < decpt
383
       The number cannot be represented in the 'f' format at all, always use
384
       the 'e' 'one.
385
  */
386
0
  if ((have_space ||
387
       /*
388
         Not enough space, let's see if the 'f' format provides the most number
389
         of significant digits.
390
       */
391
0
       ((decpt <= width &&
392
0
         (decpt >= -1 || (decpt == -2 && (len > 1 || !force_e_format)))) &&
393
0
        !force_e_format)) &&
394
395
      /*
396
        Use the 'e' format in some cases even if we have enough space for the
397
        'f' one. See comment for MAX_DECPT_FOR_F_FORMAT.
398
      */
399
0
      (!have_space || (decpt >= -MAX_DECPT_FOR_F_FORMAT + 1 &&
400
0
                       (decpt <= MAX_DECPT_FOR_F_FORMAT || len > decpt)))) {
401
    /* 'f' format */
402
0
    int i;
403
404
0
    width -= (decpt < len) + (decpt <= 0 ? 1 - decpt : 0);
405
406
    /* Do we have to truncate any digits? */
407
0
    if (width < len) {
408
0
      if (width < decpt) {
409
0
        if (error != nullptr) *error = true;
410
0
        width = decpt;
411
0
      }
412
413
      /*
414
        We want to truncate (len - width) least significant digits after the
415
        decimal point. For this we are calling dtoa with mode=5, passing the
416
        number of significant digits = (len-decpt) - (len-width) = width-decpt
417
      */
418
0
      dtoa_free(res, buf, sizeof(buf));
419
0
      res = dtoa(x, 5, width - decpt, &decpt, &sign, &end, buf, sizeof(buf));
420
0
      src = res;
421
0
      len = (int)(end - res);
422
0
    }
423
424
0
    if (len == 0) {
425
      /* Underflow. Just print '0' and exit */
426
0
      *dst++ = '0';
427
0
      goto end;
428
0
    }
429
430
    /*
431
      At this point we are sure we have enough space to put all digits
432
      returned by dtoa
433
    */
434
0
    if (sign && dst < dend) *dst++ = '-';
435
0
    if (decpt <= 0) {
436
0
      if (dst < dend) *dst++ = '0';
437
0
      if (len > 0 && dst < dend) *dst++ = '.';
438
0
      for (; decpt < 0 && dst < dend; decpt++) *dst++ = '0';
439
0
    }
440
441
0
    for (i = 1; i <= len && dst < dend; i++) {
442
0
      *dst++ = *src++;
443
0
      if (i == decpt && i < len && dst < dend) *dst++ = '.';
444
0
    }
445
0
    while (i++ <= decpt && dst < dend) *dst++ = '0';
446
0
  } else {
447
    /* 'e' format */
448
0
    int decpt_sign = 0;
449
450
0
    if (--decpt < 0) {
451
0
      decpt = -decpt;
452
0
      width--;
453
0
      decpt_sign = 1;
454
0
    }
455
0
    width -= 1 + exp_len; /* eNNN */
456
457
0
    if (len > 1) width--;
458
459
0
    if (width <= 0) {
460
      /* Overflow */
461
0
      if (error != nullptr) *error = true;
462
0
      width = 0;
463
0
    }
464
465
    /* Do we have to truncate any digits? */
466
0
    if (width < len) {
467
      /* Yes, re-convert with a smaller width */
468
0
      dtoa_free(res, buf, sizeof(buf));
469
0
      res = dtoa(x, 4, width, &decpt, &sign, &end, buf, sizeof(buf));
470
0
      src = res;
471
0
      len = (int)(end - res);
472
0
      if (--decpt < 0) decpt = -decpt;
473
0
    }
474
    /*
475
      At this point we are sure we have enough space to put all digits
476
      returned by dtoa
477
    */
478
0
    if (sign && dst < dend) *dst++ = '-';
479
0
    if (dst < dend) *dst++ = *src++;
480
0
    if (len > 1 && dst < dend) {
481
0
      *dst++ = '.';
482
0
      while (src < end && dst < dend) *dst++ = *src++;
483
0
    }
484
0
    if (dst < dend) *dst++ = 'e';
485
0
    if (decpt_sign && dst < dend) *dst++ = '-';
486
487
0
    if (decpt >= 100 && dst < dend) {
488
0
      *dst++ = decpt / 100 + '0';
489
0
      decpt %= 100;
490
0
      if (dst < dend) *dst++ = decpt / 10 + '0';
491
0
    } else if (decpt >= 10 && dst < dend)
492
0
      *dst++ = decpt / 10 + '0';
493
0
    if (dst < dend) *dst++ = decpt % 10 + '0';
494
0
  }
495
496
0
end:
497
0
  dtoa_free(res, buf, sizeof(buf));
498
0
  *dst = '\0';
499
500
0
  return dst - to;
501
0
}
502
503
/**
504
   @brief
505
   Converts string to double (string does not have to be zero-terminated)
506
507
   @details
508
   This is a wrapper around dtoa's version of strtod().
509
510
   @param str     input string
511
   @param end     address of a pointer to the first character after the input
512
                  string. Upon return the pointer is set to point to the first
513
                  rejected character.
514
   @param error   Upon return is set to EOVERFLOW in case of underflow or
515
                  overflow.
516
517
   @return        The resulting double value. In case of underflow, 0.0 is
518
                  returned. In case overflow, signed DBL_MAX is returned.
519
*/
520
521
0
double my_strtod(const char *str, const char **end, int *error) {
522
0
  char buf[DTOA_BUFF_SIZE];
523
0
  double res;
524
0
  assert(end != nullptr &&
525
0
         ((str != nullptr && *end != nullptr) ||
526
0
          (str == nullptr && *end == nullptr)) &&
527
0
         error != nullptr);
528
529
0
  res = my_strtod_int(str, end, error, buf, sizeof(buf));
530
0
  return (*error == 0) ? res : (res < 0 ? -DBL_MAX : DBL_MAX);
531
0
}
532
533
/****************************************************************
534
 *
535
 * The author of this software is David M. Gay.
536
 *
537
 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
538
 *
539
 * Permission to use, copy, modify, and distribute this software for any
540
 * purpose without fee is hereby granted, provided that this entire notice
541
 * is included in all copies of any software which is or includes a copy
542
 * or modification of this software and in all copies of the supporting
543
 * documentation for such software.
544
 *
545
 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
546
 * WARRANTY.  IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
547
 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
548
 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
549
 *
550
 ***************************************************************/
551
/* Please send bug reports to David M. Gay (dmg at acm dot org,
552
 * with " at " changed at "@" and " dot " changed to ".").      */
553
554
/*
555
  Original copy of the software is located at http://www.netlib.org/fp/dtoa.c
556
  It was adjusted to serve MySQL server needs:
557
  * strtod() was modified to not expect a zero-terminated string.
558
    It now honors 'se' (end of string) argument as the input parameter,
559
    not just as the output one.
560
  * in dtoa(), in case of overflow/underflow/NaN result string now contains "0";
561
    decpt is set to DTOA_OVERFLOW to indicate overflow.
562
  * support for VAX, IBM mainframe and 16-bit hardware removed
563
  * we always assume that 64-bit integer type is available
564
  * support for Kernigan-Ritchie style headers (pre-ANSI compilers)
565
    removed
566
  * all gcc warnings ironed out
567
  * we always assume multithreaded environment, so we had to change
568
    memory allocation procedures to use stack in most cases;
569
    malloc is used as the last resort.
570
  * pow5mult rewritten to use pre-calculated pow5 list instead of
571
    the one generated on the fly.
572
*/
573
574
/*
575
  On a machine with IEEE extended-precision registers, it is
576
  necessary to specify double-precision (53-bit) rounding precision
577
  before invoking strtod or dtoa.  If the machine uses (the equivalent
578
  of) Intel 80x87 arithmetic, the call
579
       _control87(PC_53, MCW_PC);
580
  does this with many compilers.  Whether this or another call is
581
  appropriate depends on the compiler; for this to work, it may be
582
  necessary to #include "float.h" or another system-dependent header
583
  file.
584
*/
585
586
/*
587
  #define Honor_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
588
       and dtoa should round accordingly.
589
  #define Check_FLT_ROUNDS if FLT_ROUNDS can assume the values 2 or 3
590
       and Honor_FLT_ROUNDS is not #defined.
591
592
  TODO: check if we can get rid of the above two
593
*/
594
595
typedef union {
596
  double d;
597
  uint32_t L[2];
598
} U;
599
600
#if defined(WORDS_BIGENDIAN)
601
#define word0(x) (x)->L[0]
602
#define word1(x) (x)->L[1]
603
#else
604
0
#define word0(x) (x)->L[1]
605
0
#define word1(x) (x)->L[0]
606
#endif
607
608
0
#define dval(x) (x)->d
609
610
/* #define P DBL_MANT_DIG */
611
/* Ten_pmax= floor(P*log(2)/log(5)) */
612
/* Bletch= (highest power of 2 < DBL_MAX_10_EXP) / 16 */
613
/* Quick_max= floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
614
/* Int_max= floor(P*log(FLT_RADIX)/log(10) - 1) */
615
616
0
#define Exp_shift 20
617
0
#define Exp_shift1 20
618
0
#define Exp_msk1 0x100000
619
0
#define Exp_mask 0x7ff00000
620
0
#define P 53
621
0
#define Bias 1023
622
0
#define Emin (-1022)
623
0
#define Exp_1 0x3ff00000
624
0
#define Exp_11 0x3ff00000
625
0
#define Ebits 11
626
0
#define Frac_mask 0xfffff
627
0
#define Frac_mask1 0xfffff
628
0
#define Ten_pmax 22
629
0
#define Bletch 0x10
630
0
#define Bndry_mask 0xfffff
631
0
#define Bndry_mask1 0xfffff
632
0
#define LSB 1
633
0
#define Sign_bit 0x80000000
634
0
#define Log2P 1
635
0
#define Tiny1 1
636
0
#define Quick_max 14
637
0
#define Int_max 14
638
639
#ifndef Flt_Rounds
640
#ifdef FLT_ROUNDS
641
0
#define Flt_Rounds FLT_ROUNDS
642
#else
643
#define Flt_Rounds 1
644
#endif
645
#endif /*Flt_Rounds*/
646
647
#ifdef Honor_FLT_ROUNDS
648
#define Rounding rounding
649
#undef Check_FLT_ROUNDS
650
#define Check_FLT_ROUNDS
651
#endif
652
653
0
#define rounded_product(a, b) a *= b
654
0
#define rounded_quotient(a, b) a /= b
655
656
0
#define Big0 (Frac_mask1 | Exp_msk1 * (DBL_MAX_EXP + Bias - 1))
657
0
#define Big1 0xffffffff
658
0
#define FFFFFFFF 0xffffffffUL
659
660
/* This is tested to be enough for dtoa */
661
662
0
#define Kmax 15
663
664
#define Bcopy(x, y)                          \
665
0
  memcpy((char *)&x->sign, (char *)&y->sign, \
666
0
         2 * sizeof(int) + (y)->wds * sizeof(uint32_t))
667
668
/* Arbitrary-length integer */
669
670
typedef struct Bigint {
671
  union {
672
    uint32_t *x;         /* points right after this Bigint object */
673
    struct Bigint *next; /* to maintain free lists */
674
  } p;
675
  int k;      /* 2^k = maxwds */
676
  int maxwds; /* maximum length in 32-bit words */
677
  int sign;   /* not zero if number is negative */
678
  int wds;    /* current length in 32-bit words */
679
} Bigint;
680
681
/* A simple stack-memory based allocator for Bigints */
682
683
typedef struct Stack_alloc {
684
  char *begin;
685
  char *free;
686
  char *end;
687
  /*
688
    Having list of free blocks lets us reduce maximum required amount
689
    of memory from ~4000 bytes to < 1680 (tested on x86).
690
  */
691
  Bigint *freelist[Kmax + 1];
692
} Stack_alloc;
693
694
/*
695
  Try to allocate object on stack, and resort to malloc if all
696
  stack memory is used. Ensure allocated objects to be aligned by the pointer
697
  size in order to not break the alignment rules when storing a pointer to a
698
  Bigint.
699
*/
700
701
0
static Bigint *Balloc(int k, Stack_alloc *alloc) {
702
0
  Bigint *rv;
703
0
  assert(k <= Kmax);
704
0
  if (k <= Kmax && alloc->freelist[k]) {
705
0
    rv = alloc->freelist[k];
706
0
    alloc->freelist[k] = rv->p.next;
707
0
  } else {
708
0
    int x, len;
709
710
0
    x = 1 << k;
711
0
    len = MY_ALIGN(sizeof(Bigint) + x * sizeof(uint32_t), SIZEOF_CHARP);
712
713
0
    if (alloc->free + len <= alloc->end) {
714
0
      rv = (Bigint *)alloc->free;
715
0
      alloc->free += len;
716
0
    } else
717
0
      rv = (Bigint *)malloc(len);
718
719
0
    rv->k = k;
720
0
    rv->maxwds = x;
721
0
  }
722
0
  rv->sign = rv->wds = 0;
723
0
  rv->p.x = pointer_cast<uint32_t *>((rv + 1));
724
0
  return rv;
725
0
}
726
727
/*
728
  If object was allocated on stack, try putting it to the free
729
  list. Otherwise call free().
730
*/
731
732
0
static void Bfree(Bigint *v, Stack_alloc *alloc) {
733
0
  char *gptr = (char *)v; /* generic pointer */
734
0
  if (gptr < alloc->begin || gptr >= alloc->end)
735
0
    free(gptr);
736
0
  else if (v->k <= Kmax) {
737
    /*
738
      Maintain free lists only for stack objects: this way we don't
739
      have to bother with freeing lists in the end of dtoa;
740
      heap should not be used normally anyway.
741
    */
742
0
    v->p.next = alloc->freelist[v->k];
743
0
    alloc->freelist[v->k] = v;
744
0
  }
745
0
}
746
747
/*
748
  This is to place return value of dtoa in: tries to use stack
749
  as well, but passes by free lists management and just aligns len by
750
  the pointer size in order to not break the alignment rules when storing a
751
  pointer to a Bigint.
752
*/
753
754
0
static char *dtoa_alloc(int i, Stack_alloc *alloc) {
755
0
  char *rv;
756
0
  int const aligned_size = MY_ALIGN(i, SIZEOF_CHARP);
757
0
  if (alloc->free + aligned_size <= alloc->end) {
758
0
    rv = alloc->free;
759
0
    alloc->free += aligned_size;
760
0
  } else
761
0
    rv = static_cast<char *>(malloc(i));
762
0
  return rv;
763
0
}
764
765
/*
766
  dtoa_free() must be used to free values s returned by dtoa()
767
  This is the counterpart of dtoa_alloc()
768
*/
769
770
0
static void dtoa_free(char *gptr, char *buf, size_t buf_size) {
771
0
  if (gptr < buf || gptr >= buf + buf_size) free(gptr);
772
0
}
773
774
/* Bigint arithmetic functions */
775
776
/* Multiply by m and add a */
777
778
0
static Bigint *multadd(Bigint *b, int m, int a, Stack_alloc *alloc) {
779
0
  uint64_t y = 0;
780
0
  Bigint *b1 = nullptr;
781
782
0
  int wds = b->wds;
783
0
  uint32_t *x = b->p.x;
784
0
  int i = 0;
785
0
  uint64_t carry = a;
786
0
  do {
787
0
    y = *x * (uint64_t)m + carry;
788
0
    carry = y >> 32;
789
0
    *x++ = (uint32_t)(y & FFFFFFFF);
790
0
  } while (++i < wds);
791
0
  if (carry) {
792
0
    if (wds >= b->maxwds) {
793
0
      b1 = Balloc(b->k + 1, alloc);
794
0
      Bcopy(b1, b);
795
0
      Bfree(b, alloc);
796
0
      b = b1;
797
0
    }
798
0
    b->p.x[wds++] = (uint32_t)carry;
799
0
    b->wds = wds;
800
0
  }
801
0
  return b;
802
0
}
803
804
/**
805
  Converts a string to Bigint.
806
807
  Now we have nd0 digits, starting at s, followed by a
808
  decimal point, followed by nd-nd0 digits.
809
  Unless nd0 == nd, in which case we have a number of the form:
810
     ".xxxxxx"    or    "xxxxxx."
811
812
  @param s     Input string, already partially parsed by my_strtod_int().
813
  @param nd0   Number of digits before decimal point.
814
  @param nd    Total number of digits.
815
  @param y9    Pre-computed value of the first nine digits.
816
  @param alloc Stack allocator for Bigints.
817
 */
818
static Bigint *s2b(const char *s, int nd0, int nd, uint32_t y9,
819
0
                   Stack_alloc *alloc) {
820
0
  Bigint *b;
821
0
  int i, k;
822
0
  int32_t y = 0;
823
824
0
  int32_t const x = (nd + 8) / 9;
825
0
  for (k = 0, y = 1; x > y; y <<= 1, k++)
826
0
    ;
827
0
  b = Balloc(k, alloc);
828
0
  b->p.x[0] = y9;
829
0
  b->wds = 1;
830
831
0
  i = 9;
832
0
  if (9 < nd0) {
833
0
    s += 9;
834
0
    do b = multadd(b, 10, *s++ - '0', alloc);
835
0
    while (++i < nd0);
836
0
    s++; /* skip '.' */
837
0
  } else
838
0
    s += 10;
839
  /* now do the fractional part */
840
0
  for (; i < nd; i++) b = multadd(b, 10, *s++ - '0', alloc);
841
0
  return b;
842
0
}
843
844
0
static int hi0bits(uint32_t x) {
845
0
  int k = 0;
846
847
0
  if (!(x & 0xffff0000)) {
848
0
    k = 16;
849
0
    x <<= 16;
850
0
  }
851
0
  if (!(x & 0xff000000)) {
852
0
    k += 8;
853
0
    x <<= 8;
854
0
  }
855
0
  if (!(x & 0xf0000000)) {
856
0
    k += 4;
857
0
    x <<= 4;
858
0
  }
859
0
  if (!(x & 0xc0000000)) {
860
0
    k += 2;
861
0
    x <<= 2;
862
0
  }
863
0
  if (!(x & 0x80000000)) {
864
0
    k++;
865
0
    if (!(x & 0x40000000)) return 32;
866
0
  }
867
0
  return k;
868
0
}
869
870
0
static int lo0bits(uint32_t *y) {
871
0
  int k;
872
0
  uint32_t x = *y;
873
874
0
  if (x & 7) {
875
0
    if (x & 1) return 0;
876
0
    if (x & 2) {
877
0
      *y = x >> 1;
878
0
      return 1;
879
0
    }
880
0
    *y = x >> 2;
881
0
    return 2;
882
0
  }
883
0
  k = 0;
884
0
  if (!(x & 0xffff)) {
885
0
    k = 16;
886
0
    x >>= 16;
887
0
  }
888
0
  if (!(x & 0xff)) {
889
0
    k += 8;
890
0
    x >>= 8;
891
0
  }
892
0
  if (!(x & 0xf)) {
893
0
    k += 4;
894
0
    x >>= 4;
895
0
  }
896
0
  if (!(x & 0x3)) {
897
0
    k += 2;
898
0
    x >>= 2;
899
0
  }
900
0
  if (!(x & 1)) {
901
0
    k++;
902
0
    x >>= 1;
903
0
    if (!x) return 32;
904
0
  }
905
0
  *y = x;
906
0
  return k;
907
0
}
908
909
/* Convert integer to Bigint number */
910
911
0
static Bigint *i2b(int i, Stack_alloc *alloc) {
912
0
  Bigint *b;
913
914
0
  b = Balloc(1, alloc);
915
0
  b->p.x[0] = i;
916
0
  b->wds = 1;
917
0
  return b;
918
0
}
919
920
/* Multiply two Bigint numbers */
921
922
0
static Bigint *mult(Bigint *a, Bigint *b, Stack_alloc *alloc) {
923
0
  Bigint *c;
924
0
  int k, wa, wb, wc;
925
0
  uint32_t *x = nullptr;
926
0
  uint32_t *xa = nullptr;
927
0
  uint32_t *xc = nullptr;
928
0
  uint32_t y = 0;
929
0
  uint64_t carry = 0;
930
0
  uint64_t z = 0;
931
932
0
  if (a->wds < b->wds) {
933
0
    c = a;
934
0
    a = b;
935
0
    b = c;
936
0
  }
937
0
  k = a->k;
938
0
  wa = a->wds;
939
0
  wb = b->wds;
940
0
  wc = wa + wb;
941
0
  if (wc > a->maxwds) k++;
942
0
  c = Balloc(k, alloc);
943
0
  for (x = c->p.x, xa = x + wc; x < xa; x++) *x = 0;
944
0
  xa = a->p.x;
945
0
  uint32_t *xae = xa + wa;
946
0
  uint32_t *xb = b->p.x;
947
0
  uint32_t *xbe = xb + wb;
948
0
  uint32_t *xc0 = c->p.x;
949
0
  for (; xb < xbe; xc0++) {
950
0
    if ((y = *xb++)) {
951
0
      x = xa;
952
0
      xc = xc0;
953
0
      carry = 0;
954
0
      do {
955
0
        z = *x++ * (uint64_t)y + *xc + carry;
956
0
        carry = z >> 32;
957
0
        *xc++ = (uint32_t)(z & FFFFFFFF);
958
0
      } while (x < xae);
959
0
      *xc = (uint32_t)carry;
960
0
    }
961
0
  }
962
0
  for (xc0 = c->p.x, xc = xc0 + wc; wc > 0 && !*--xc; --wc)
963
0
    ;
964
0
  c->wds = wc;
965
0
  return c;
966
0
}
967
968
/*
969
  Precalculated array of powers of 5: tested to be enough for
970
  vasting majority of dtoa_r cases.
971
*/
972
973
static uint32_t powers5[] = {
974
    625UL,
975
976
    390625UL,
977
978
    2264035265UL, 35UL,
979
980
    2242703233UL, 762134875UL,  1262UL,
981
982
    3211403009UL, 1849224548UL, 3668416493UL, 3913284084UL, 1593091UL,
983
984
    781532673UL,  64985353UL,   253049085UL,  594863151UL,  3553621484UL,
985
    3288652808UL, 3167596762UL, 2788392729UL, 3911132675UL, 590UL,
986
987
    2553183233UL, 3201533787UL, 3638140786UL, 303378311UL,  1809731782UL,
988
    3477761648UL, 3583367183UL, 649228654UL,  2915460784UL, 487929380UL,
989
    1011012442UL, 1677677582UL, 3428152256UL, 1710878487UL, 1438394610UL,
990
    2161952759UL, 4100910556UL, 1608314830UL, 349175UL};
991
992
static Bigint p5_a[] = {
993
    /*  { x } - k - maxwds - sign - wds */
994
    {{powers5}, 1, 1, 0, 1},       {{powers5 + 1}, 1, 1, 0, 1},
995
    {{powers5 + 2}, 1, 2, 0, 2},   {{powers5 + 4}, 2, 3, 0, 3},
996
    {{powers5 + 7}, 3, 5, 0, 5},   {{powers5 + 12}, 4, 10, 0, 10},
997
    {{powers5 + 22}, 5, 19, 0, 19}};
998
999
0
#define P5A_MAX (sizeof(p5_a) / sizeof(*p5_a) - 1)
1000
1001
0
static Bigint *pow5mult(Bigint *b, int k, Stack_alloc *alloc) {
1002
0
  Bigint *b1, *p5, *p51 = nullptr;
1003
0
  int i;
1004
0
  static int const p05[3] = {5, 25, 125};
1005
0
  bool overflow = false;
1006
1007
0
  if ((i = k & 3)) b = multadd(b, p05[i - 1], 0, alloc);
1008
1009
0
  if (!(k >>= 2)) return b;
1010
0
  p5 = p5_a;
1011
0
  for (;;) {
1012
0
    if (k & 1) {
1013
0
      b1 = mult(b, p5, alloc);
1014
0
      Bfree(b, alloc);
1015
0
      b = b1;
1016
0
    }
1017
0
    if (!(k >>= 1)) break;
1018
    /* Calculate next power of 5 */
1019
0
    if (overflow) {
1020
0
      p51 = mult(p5, p5, alloc);
1021
0
      Bfree(p5, alloc);
1022
0
      p5 = p51;
1023
0
    } else if (p5 < p5_a + P5A_MAX)
1024
0
      ++p5;
1025
0
    else if (p5 == p5_a + P5A_MAX) {
1026
0
      p5 = mult(p5, p5, alloc);
1027
0
      overflow = true;
1028
0
    }
1029
0
  }
1030
0
  if (p51) Bfree(p51, alloc);
1031
0
  return b;
1032
0
}
1033
1034
0
static Bigint *lshift(Bigint *b, int k, Stack_alloc *alloc) {
1035
0
  int i, k1, n, n1;
1036
0
  Bigint *b1;
1037
0
  uint32_t z = 0;
1038
1039
0
  n = k >> 5;
1040
0
  k1 = b->k;
1041
0
  n1 = n + b->wds + 1;
1042
0
  for (i = b->maxwds; n1 > i; i <<= 1) k1++;
1043
0
  b1 = Balloc(k1, alloc);
1044
0
  uint32_t *x1 = b1->p.x;
1045
0
  for (i = 0; i < n; i++) *x1++ = 0;
1046
0
  uint32_t *x = b->p.x;
1047
0
  uint32_t *xe = x + b->wds;
1048
0
  if (k &= 0x1f) {
1049
0
    k1 = 32 - k;
1050
0
    z = 0;
1051
0
    do {
1052
0
      *x1++ = *x << k | z;
1053
0
      z = *x++ >> k1;
1054
0
    } while (x < xe);
1055
0
    if ((*x1 = z)) ++n1;
1056
0
  } else
1057
0
    do *x1++ = *x++;
1058
0
    while (x < xe);
1059
0
  b1->wds = n1 - 1;
1060
0
  Bfree(b, alloc);
1061
0
  return b1;
1062
0
}
1063
1064
0
static int cmp(Bigint *a, Bigint *b) {
1065
0
  int i, j;
1066
1067
0
  i = a->wds;
1068
0
  j = b->wds;
1069
0
  if (i -= j) return i;
1070
0
  uint32_t *xa0 = a->p.x;
1071
0
  uint32_t *xa = xa0 + j;
1072
0
  uint32_t *xb0 = b->p.x;
1073
0
  uint32_t *xb = xb0 + j;
1074
0
  for (;;) {
1075
0
    if (*--xa != *--xb) return *xa < *xb ? -1 : 1;
1076
0
    if (xa <= xa0) break;
1077
0
  }
1078
0
  return 0;
1079
0
}
1080
1081
0
static Bigint *diff(Bigint *a, Bigint *b, Stack_alloc *alloc) {
1082
0
  Bigint *c = nullptr;
1083
0
  int i, wa, wb;
1084
0
  uint64_t borrow = 0;
1085
0
  uint64_t y = 0;
1086
1087
0
  i = cmp(a, b);
1088
0
  if (!i) {
1089
0
    c = Balloc(0, alloc);
1090
0
    c->wds = 1;
1091
0
    c->p.x[0] = 0;
1092
0
    return c;
1093
0
  }
1094
0
  if (i < 0) {
1095
0
    c = a;
1096
0
    a = b;
1097
0
    b = c;
1098
0
    i = 1;
1099
0
  } else
1100
0
    i = 0;
1101
0
  c = Balloc(a->k, alloc);
1102
0
  c->sign = i;
1103
0
  wa = a->wds;
1104
0
  uint32_t *xa = a->p.x;
1105
0
  uint32_t *xae = xa + wa;
1106
0
  wb = b->wds;
1107
0
  uint32_t *xb = b->p.x;
1108
0
  uint32_t *xbe = xb + wb;
1109
0
  uint32_t *xc = c->p.x;
1110
0
  borrow = 0;
1111
0
  do {
1112
0
    y = (uint64_t)*xa++ - *xb++ - borrow;
1113
0
    borrow = y >> 32 & (uint32_t)1;
1114
0
    *xc++ = (uint32_t)(y & FFFFFFFF);
1115
0
  } while (xb < xbe);
1116
0
  while (xa < xae) {
1117
0
    y = *xa++ - borrow;
1118
0
    borrow = y >> 32 & (uint32_t)1;
1119
0
    *xc++ = (uint32_t)(y & FFFFFFFF);
1120
0
  }
1121
0
  while (!*--xc) wa--;
1122
0
  c->wds = wa;
1123
0
  return c;
1124
0
}
1125
1126
0
static double ulp(U *x) {
1127
0
  U u;
1128
1129
0
  int32_t const L = (word0(x) & Exp_mask) - (P - 1) * Exp_msk1;
1130
0
  word0(&u) = L;
1131
0
  word1(&u) = 0;
1132
0
  return dval(&u);
1133
0
}
1134
1135
0
static double b2d(Bigint *a, int *e) {
1136
0
  uint32_t z = 0;
1137
0
  int k;
1138
0
  U d;
1139
0
#define d0 word0(&d)
1140
0
#define d1 word1(&d)
1141
1142
0
  uint32_t *xa0 = a->p.x;
1143
0
  uint32_t *xa = xa0 + a->wds;
1144
0
  uint32_t y = *--xa;
1145
0
  k = hi0bits(y);
1146
0
  *e = 32 - k;
1147
0
  if (k < Ebits) {
1148
0
    d0 = Exp_1 | y >> (Ebits - k);
1149
0
    uint32_t const w = xa > xa0 ? *--xa : 0;
1150
0
    d1 = y << ((32 - Ebits) + k) | w >> (Ebits - k);
1151
0
    goto ret_d;
1152
0
  }
1153
0
  z = xa > xa0 ? *--xa : 0;
1154
0
  if (k -= Ebits) {
1155
0
    d0 = Exp_1 | y << k | z >> (32 - k);
1156
0
    y = xa > xa0 ? *--xa : 0;
1157
0
    d1 = z << k | y >> (32 - k);
1158
0
  } else {
1159
0
    d0 = Exp_1 | y;
1160
0
    d1 = z;
1161
0
  }
1162
0
ret_d:
1163
0
#undef d0
1164
0
#undef d1
1165
0
  return dval(&d);
1166
0
}
1167
1168
0
static Bigint *d2b(U *d, int *e, int *bits, Stack_alloc *alloc) {
1169
0
  Bigint *b;
1170
0
  int de, k;
1171
0
  uint32_t y = 0;
1172
0
  uint32_t z = 0;
1173
0
  int i;
1174
0
#define d0 word0(d)
1175
0
#define d1 word1(d)
1176
1177
0
  b = Balloc(1, alloc);
1178
0
  uint32_t *x = b->p.x;
1179
1180
0
  z = d0 & Frac_mask;
1181
0
  d0 &= 0x7fffffff; /* clear sign bit, which we ignore */
1182
0
  if ((de = (int)(d0 >> Exp_shift))) z |= Exp_msk1;
1183
0
  if ((y = d1)) {
1184
0
    if ((k = lo0bits(&y))) {
1185
0
      x[0] = y | z << (32 - k);
1186
0
      z >>= k;
1187
0
    } else
1188
0
      x[0] = y;
1189
0
    i = b->wds = (x[1] = z) ? 2 : 1;
1190
0
  } else {
1191
0
    k = lo0bits(&z);
1192
0
    x[0] = z;
1193
0
    i = b->wds = 1;
1194
0
    k += 32;
1195
0
  }
1196
0
  if (de) {
1197
0
    *e = de - Bias - (P - 1) + k;
1198
0
    *bits = P - k;
1199
0
  } else {
1200
0
    *e = de - Bias - (P - 1) + 1 + k;
1201
0
    *bits = 32 * i - hi0bits(x[i - 1]);
1202
0
  }
1203
0
  return b;
1204
0
#undef d0
1205
0
#undef d1
1206
0
}
1207
1208
0
static double ratio(Bigint *a, Bigint *b) {
1209
0
  U da, db;
1210
0
  int k, ka, kb;
1211
1212
0
  dval(&da) = b2d(a, &ka);
1213
0
  dval(&db) = b2d(b, &kb);
1214
0
  k = ka - kb + 32 * (a->wds - b->wds);
1215
0
  if (k > 0)
1216
0
    word0(&da) += k * Exp_msk1;
1217
0
  else {
1218
0
    k = -k;
1219
0
    word0(&db) += k * Exp_msk1;
1220
0
  }
1221
0
  return dval(&da) / dval(&db);
1222
0
}
1223
1224
static const double tens[] = {1e0,  1e1,  1e2,  1e3,  1e4,  1e5,  1e6,  1e7,
1225
                              1e8,  1e9,  1e10, 1e11, 1e12, 1e13, 1e14, 1e15,
1226
                              1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22};
1227
1228
static const double bigtens[] = {1e16, 1e32, 1e64, 1e128, 1e256};
1229
static const double tinytens[] = {
1230
    1e-16, 1e-32, 1e-64, 1e-128,
1231
    9007199254740992. * 9007199254740992.e-256 /* = 2^106 * 1e-53 */
1232
};
1233
/*
1234
  The factor of 2^53 in tinytens[4] helps us avoid setting the underflow
1235
  flag unnecessarily.  It leads to a song and dance at the end of strtod.
1236
*/
1237
0
#define Scale_Bit 0x10
1238
0
#define n_bigtens 5
1239
1240
/*
1241
  strtod for IEEE--arithmetic machines.
1242
1243
  This strtod returns a nearest machine number to the input decimal
1244
  string (or sets errno to EOVERFLOW). Ties are broken by the IEEE round-even
1245
  rule.
1246
1247
  Inspired loosely by William D. Clinger's paper "How to Read Floating
1248
  Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
1249
1250
  Modifications:
1251
1252
   1. We only require IEEE (not IEEE double-extended).
1253
   2. We get by with floating-point arithmetic in a case that
1254
     Clinger missed -- when we're computing d * 10^n
1255
     for a small integer d and the integer n is not too
1256
     much larger than 22 (the maximum integer k for which
1257
     we can represent 10^k exactly), we may be able to
1258
     compute (d*10^k) * 10^(e-k) with just one roundoff.
1259
   3. Rather than a bit-at-a-time adjustment of the binary
1260
     result in the hard case, we use floating-point
1261
     arithmetic to determine the adjustment to within
1262
     one bit; only in really hard cases do we need to
1263
     compute a second residual.
1264
   4. Because of 3., we don't need a large table of powers of 10
1265
     for ten-to-e (just some small tables, e.g. of 10^k
1266
     for 0 <= k <= 22).
1267
*/
1268
1269
static double my_strtod_int(const char *s00, const char **se, int *error,
1270
0
                            char *buf, size_t buf_size) {
1271
0
  int scale;
1272
0
  int bb2, bb5, bbe, bd2, bd5, bbbits, bs2, c = 0, dsign, e, e1, esign, i, j, k,
1273
0
                                            nd, nd0, nf, nz, nz0, sign;
1274
0
  const char *s, *s0, *s1, *end = *se;
1275
0
  double aadj, aadj1;
1276
0
  U aadj2, adj, rv, rv0;
1277
0
  int32_t L = 0;
1278
0
  uint32_t y = 0;
1279
0
  uint32_t z = 0;
1280
0
  Bigint *bb = nullptr, *bb1, *bd = nullptr, *bd0, *bs = nullptr,
1281
0
         *delta = nullptr;
1282
#ifdef Honor_FLT_ROUNDS
1283
  int rounding;
1284
#endif
1285
0
  Stack_alloc alloc;
1286
1287
0
  *error = 0;
1288
1289
0
  alloc.begin = alloc.free = buf;
1290
0
  alloc.end = buf + buf_size;
1291
0
  memset(alloc.freelist, 0, sizeof(alloc.freelist));
1292
1293
0
  sign = nz0 = nz = 0;
1294
0
  dval(&rv) = 0.;
1295
0
  for (s = s00; s < end; s++) switch (*s) {
1296
0
      case '-':
1297
0
        sign = 1;
1298
0
        [[fallthrough]];
1299
0
      case '+':
1300
0
        s++;
1301
0
        goto break2;
1302
0
      case '\t':
1303
0
      case '\n':
1304
0
      case '\v':
1305
0
      case '\f':
1306
0
      case '\r':
1307
0
      case ' ':
1308
0
        continue;
1309
0
      default:
1310
0
        goto break2;
1311
0
    }
1312
0
break2:
1313
0
  if (s >= end) goto ret0;
1314
1315
  // Gobble up leading zeros.
1316
0
  if (*s == '0') {
1317
0
    nz0 = 1;
1318
0
    while (++s < end && *s == '0')
1319
0
      ;
1320
0
    if (s >= end) goto ret;
1321
0
  }
1322
0
  s0 = s;
1323
0
  y = z = 0;
1324
0
  for (nd = nf = 0; s < end && (c = *s) >= '0' && c <= '9'; nd++, s++)
1325
0
    if (nd < 9)
1326
0
      y = 10 * y + c - '0';
1327
0
    else if (nd < 16)
1328
0
      z = 10 * z + c - '0';
1329
0
  nd0 = nd;
1330
0
  if (s < end && c == '.') {
1331
0
    if (++s < end) c = *s;
1332
    // Only leading zeros, now count number of leading zeros after the '.'
1333
0
    if (!nd) {
1334
0
      for (; s < end; ++s) {
1335
0
        c = *s;
1336
0
        if (c != '0') break;
1337
0
        nz++;
1338
0
      }
1339
0
      if (s < end && c > '0' && c <= '9') {
1340
0
        s0 = s;
1341
0
        nf += nz;
1342
0
        nz = 0;
1343
0
      } else
1344
0
        goto dig_done;
1345
0
    }
1346
0
    for (; s < end; ++s) {
1347
0
      c = *s;
1348
0
      if (c < '0' || c > '9') break;
1349
1350
      // We have seen some digits, but not enough of them are non-zero.
1351
      // Gobble up all the rest of the digits, and look for exponent.
1352
0
      if (nd > 0 && nz > DBL_MAX_10_EXP) {
1353
0
        continue;
1354
0
      }
1355
1356
      /*
1357
        Here we are parsing the fractional part.
1358
        We can stop counting digits after a while: the extra digits
1359
        will not contribute to the actual result produced by s2b().
1360
        We have to continue scanning, in case there is an exponent part.
1361
       */
1362
0
      if (nd < 2 * DBL_DIG) {
1363
0
        nz++;
1364
0
        if (c -= '0') {
1365
0
          nf += nz;
1366
0
          for (i = 1; i < nz; i++)
1367
0
            if (nd++ < 9)
1368
0
              y *= 10;
1369
0
            else if (nd <= DBL_DIG + 1)
1370
0
              z *= 10;
1371
0
          if (nd++ < 9)
1372
0
            y = 10 * y + c;
1373
0
          else if (nd <= DBL_DIG + 1)
1374
0
            z = 10 * z + c;
1375
0
          nz = 0;
1376
0
        }
1377
0
      }
1378
0
    }
1379
0
  }
1380
0
dig_done:
1381
0
  e = 0;
1382
0
  if (s < end && (c == 'e' || c == 'E')) {
1383
0
    if (!nd && !nz && !nz0) goto ret0;
1384
0
    s00 = s;
1385
0
    esign = 0;
1386
0
    if (++s < end) switch (c = *s) {
1387
0
        case '-':
1388
0
          esign = 1;
1389
0
          [[fallthrough]];
1390
0
        case '+':
1391
0
          if (++s < end) c = *s;
1392
0
      }
1393
0
    if (s < end && c >= '0' && c <= '9') {
1394
0
      while (s < end && *s == '0') ++s;  // Skip leading zeros in exponent.
1395
0
      if (s < end) c = *s;  // First significant digit in exponent, if any.
1396
0
      if (s < end && c > '0' && c <= '9') {
1397
0
        L = c - '0';
1398
0
        s1 = s;
1399
        // Avoid overflow in loop body below.
1400
0
        while (++s < end && (c = *s) >= '0' && c <= '9' &&
1401
0
               L < (std::numeric_limits<int32_t>::max() - 255) / 10) {
1402
0
          L = 10 * L + c - '0';
1403
0
        }
1404
0
        if (s - s1 > 8 || L > 19999)
1405
          /* Avoid confusion from exponents
1406
           * so large that e might overflow.
1407
           */
1408
0
          e = 19999; /* safe for 16 bit ints */
1409
0
        else
1410
0
          e = (int)L;
1411
0
        if (esign) e = -e;
1412
0
      } else
1413
0
        e = 0;
1414
0
    } else
1415
0
      s = s00;
1416
0
  }
1417
0
  if (!nd) {
1418
0
    if (!nz && !nz0) {
1419
0
    ret0:
1420
0
      s = s00;
1421
0
      sign = 0;
1422
0
    }
1423
0
    goto ret;
1424
0
  }
1425
0
  e1 = e -= nf;
1426
1427
  /*
1428
    Now we have nd0 digits, starting at s0, followed by a
1429
    decimal point, followed by nd-nd0 digits.  The number we're
1430
    after is the integer represented by those digits times
1431
    10**e
1432
   */
1433
1434
0
  if (!nd0) nd0 = nd;
1435
0
  k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
1436
0
  dval(&rv) = y;
1437
0
  if (k > 9) {
1438
0
    dval(&rv) = tens[k - 9] * dval(&rv) + z;
1439
0
  }
1440
0
  bd0 = nullptr;
1441
0
  if (nd <= DBL_DIG
1442
0
#ifndef Honor_FLT_ROUNDS
1443
0
      && Flt_Rounds == 1
1444
0
#endif
1445
0
  ) {
1446
0
    if (!e) goto ret;
1447
0
    if (e > 0) {
1448
0
      if (e <= Ten_pmax) {
1449
#ifdef Honor_FLT_ROUNDS
1450
        /* round correctly FLT_ROUNDS = 2 or 3 */
1451
        if (sign) {
1452
          rv.d = -rv.d;
1453
          sign = 0;
1454
        }
1455
#endif
1456
0
        /* rv = */ rounded_product(dval(&rv), tens[e]);
1457
0
        goto ret;
1458
0
      }
1459
0
      i = DBL_DIG - nd;
1460
0
      if (e <= Ten_pmax + i) {
1461
        /*
1462
          A fancier test would sometimes let us do
1463
          this for larger i values.
1464
        */
1465
#ifdef Honor_FLT_ROUNDS
1466
        /* round correctly FLT_ROUNDS = 2 or 3 */
1467
        if (sign) {
1468
          rv.d = -rv.d;
1469
          sign = 0;
1470
        }
1471
#endif
1472
0
        e -= i;
1473
0
        dval(&rv) *= tens[i];
1474
0
        /* rv = */ rounded_product(dval(&rv), tens[e]);
1475
0
        goto ret;
1476
0
      }
1477
0
    }
1478
0
#ifndef Inaccurate_Divide
1479
0
    else if (e >= -Ten_pmax) {
1480
#ifdef Honor_FLT_ROUNDS
1481
      /* round correctly FLT_ROUNDS = 2 or 3 */
1482
      if (sign) {
1483
        rv.d = -rv.d;
1484
        sign = 0;
1485
      }
1486
#endif
1487
0
      /* rv = */ rounded_quotient(dval(&rv), tens[-e]);
1488
0
      goto ret;
1489
0
    }
1490
0
#endif
1491
0
  }
1492
0
  e1 += nd - k;
1493
1494
0
  scale = 0;
1495
#ifdef Honor_FLT_ROUNDS
1496
  if ((rounding = Flt_Rounds) >= 2) {
1497
    if (sign)
1498
      rounding = rounding == 2 ? 0 : 2;
1499
    else if (rounding != 2)
1500
      rounding = 0;
1501
  }
1502
#endif
1503
1504
  /* Get starting approximation = rv * 10**e1 */
1505
1506
0
  if (e1 > 0) {
1507
0
    if ((i = e1 & 15)) dval(&rv) *= tens[i];
1508
0
    if (e1 &= ~15) {
1509
0
      if (e1 > DBL_MAX_10_EXP) {
1510
0
      ovfl:
1511
0
        *error = EOVERFLOW;
1512
        /* Can't trust HUGE_VAL */
1513
#ifdef Honor_FLT_ROUNDS
1514
        switch (rounding) {
1515
          case 0: /* toward 0 */
1516
          case 3: /* toward -infinity */
1517
            word0(&rv) = Big0;
1518
            word1(&rv) = Big1;
1519
            break;
1520
          default:
1521
            word0(&rv) = Exp_mask;
1522
            word1(&rv) = 0;
1523
        }
1524
#else  /*Honor_FLT_ROUNDS*/
1525
0
        word0(&rv) = Exp_mask;
1526
0
        word1(&rv) = 0;
1527
0
#endif /*Honor_FLT_ROUNDS*/
1528
0
        if (bd0) goto retfree;
1529
0
        goto ret;
1530
0
      }
1531
0
      e1 >>= 4;
1532
0
      for (j = 0; e1 > 1; j++, e1 >>= 1)
1533
0
        if (e1 & 1) dval(&rv) *= bigtens[j];
1534
      /* The last multiplication could overflow. */
1535
0
      word0(&rv) -= P * Exp_msk1;
1536
0
      dval(&rv) *= bigtens[j];
1537
0
      if ((z = word0(&rv) & Exp_mask) > Exp_msk1 * (DBL_MAX_EXP + Bias - P))
1538
0
        goto ovfl;
1539
0
      if (z > Exp_msk1 * (DBL_MAX_EXP + Bias - 1 - P)) {
1540
        /* set to largest number (Can't trust DBL_MAX) */
1541
0
        word0(&rv) = Big0;
1542
0
        word1(&rv) = Big1;
1543
0
      } else
1544
0
        word0(&rv) += P * Exp_msk1;
1545
0
    }
1546
0
  } else if (e1 < 0) {
1547
0
    e1 = -e1;
1548
0
    if ((i = e1 & 15)) dval(&rv) /= tens[i];
1549
0
    if ((e1 >>= 4)) {
1550
0
      if (e1 >= 1 << n_bigtens) goto undfl;
1551
0
      if (e1 & Scale_Bit) scale = 2 * P;
1552
0
      for (j = 0; e1 > 0; j++, e1 >>= 1)
1553
0
        if (e1 & 1) dval(&rv) *= tinytens[j];
1554
0
      if (scale &&
1555
0
          (j = 2 * P + 1 - ((word0(&rv) & Exp_mask) >> Exp_shift)) > 0) {
1556
        /* scaled rv is denormal; zap j low bits */
1557
0
        if (j >= 32) {
1558
0
          word1(&rv) = 0;
1559
0
          if (j >= 53)
1560
0
            word0(&rv) = (P + 2) * Exp_msk1;
1561
0
          else
1562
0
            word0(&rv) &= 0xffffffff << (j - 32);
1563
0
        } else
1564
0
          word1(&rv) &= 0xffffffff << j;
1565
0
      }
1566
0
      if (!dval(&rv)) {
1567
0
      undfl:
1568
0
        dval(&rv) = 0.;
1569
0
        if (bd0) goto retfree;
1570
0
        goto ret;
1571
0
      }
1572
0
    }
1573
0
  }
1574
1575
  /* Now the hard part -- adjusting rv to the correct value.*/
1576
1577
  /* Put digits into bd: true value = bd * 10^e */
1578
1579
0
  bd0 = s2b(s0, nd0, nd, y, &alloc);
1580
1581
0
  for (;;) {
1582
0
    bd = Balloc(bd0->k, &alloc);
1583
0
    Bcopy(bd, bd0);
1584
0
    bb = d2b(&rv, &bbe, &bbbits, &alloc); /* rv = bb * 2^bbe */
1585
0
    bs = i2b(1, &alloc);
1586
1587
0
    if (e >= 0) {
1588
0
      bb2 = bb5 = 0;
1589
0
      bd2 = bd5 = e;
1590
0
    } else {
1591
0
      bb2 = bb5 = -e;
1592
0
      bd2 = bd5 = 0;
1593
0
    }
1594
0
    if (bbe >= 0)
1595
0
      bb2 += bbe;
1596
0
    else
1597
0
      bd2 -= bbe;
1598
0
    bs2 = bb2;
1599
#ifdef Honor_FLT_ROUNDS
1600
    if (rounding != 1) bs2++;
1601
#endif
1602
0
    j = bbe - scale;
1603
0
    i = j + bbbits - 1; /* logb(rv) */
1604
0
    if (i < Emin)       /* denormal */
1605
0
      j += P - Emin;
1606
0
    else
1607
0
      j = P + 1 - bbbits;
1608
0
    bb2 += j;
1609
0
    bd2 += j;
1610
0
    bd2 += scale;
1611
0
    i = bb2 < bd2 ? bb2 : bd2;
1612
0
    if (i > bs2) i = bs2;
1613
0
    if (i > 0) {
1614
0
      bb2 -= i;
1615
0
      bd2 -= i;
1616
0
      bs2 -= i;
1617
0
    }
1618
0
    if (bb5 > 0) {
1619
0
      bs = pow5mult(bs, bb5, &alloc);
1620
0
      bb1 = mult(bs, bb, &alloc);
1621
0
      Bfree(bb, &alloc);
1622
0
      bb = bb1;
1623
0
    }
1624
0
    if (bb2 > 0) bb = lshift(bb, bb2, &alloc);
1625
0
    if (bd5 > 0) bd = pow5mult(bd, bd5, &alloc);
1626
0
    if (bd2 > 0) bd = lshift(bd, bd2, &alloc);
1627
0
    if (bs2 > 0) bs = lshift(bs, bs2, &alloc);
1628
0
    delta = diff(bb, bd, &alloc);
1629
0
    dsign = delta->sign;
1630
0
    delta->sign = 0;
1631
0
    i = cmp(delta, bs);
1632
#ifdef Honor_FLT_ROUNDS
1633
    if (rounding != 1) {
1634
      if (i < 0) {
1635
        /* Error is less than an ulp */
1636
        if (!delta->p.x[0] && delta->wds <= 1) {
1637
          /* exact */
1638
          break;
1639
        }
1640
        if (rounding) {
1641
          if (dsign) {
1642
            adj.d = 1.;
1643
            goto apply_adj;
1644
          }
1645
        } else if (!dsign) {
1646
          adj.d = -1.;
1647
          if (!word1(&rv) && !(word0(&rv) & Frac_mask)) {
1648
            y = word0(&rv) & Exp_mask;
1649
            if (!scale || y > 2 * P * Exp_msk1) {
1650
              delta = lshift(delta, Log2P, &alloc);
1651
              if (cmp(delta, bs) <= 0) adj.d = -0.5;
1652
            }
1653
          }
1654
        apply_adj:
1655
          if (scale && (y = word0(&rv) & Exp_mask) <= 2 * P * Exp_msk1)
1656
            word0(&adj) += (2 * P + 1) * Exp_msk1 - y;
1657
          dval(&rv) += adj.d * ulp(&rv);
1658
        }
1659
        break;
1660
      }
1661
      adj.d = ratio(delta, bs);
1662
      if (adj.d < 1.) adj.d = 1.;
1663
      if (adj.d <= 0x7ffffffe) {
1664
        /* adj = rounding ? ceil(adj) : floor(adj); */
1665
        y = adj.d;
1666
        if (y != adj.d) {
1667
          if (!((rounding >> 1) ^ dsign)) y++;
1668
          adj.d = y;
1669
        }
1670
      }
1671
      if (scale && (y = word0(&rv) & Exp_mask) <= 2 * P * Exp_msk1)
1672
        word0(&adj) += (2 * P + 1) * Exp_msk1 - y;
1673
      adj.d *= ulp(&rv);
1674
      if (dsign)
1675
        dval(&rv) += adj.d;
1676
      else
1677
        dval(&rv) -= adj.d;
1678
      goto cont;
1679
    }
1680
#endif /*Honor_FLT_ROUNDS*/
1681
1682
0
    if (i < 0) {
1683
      /*
1684
        Error is less than half an ulp -- check for special case of mantissa
1685
        a power of two.
1686
      */
1687
0
      if (dsign || word1(&rv) || word0(&rv) & Bndry_mask ||
1688
0
          (word0(&rv) & Exp_mask) <= (2 * P + 1) * Exp_msk1) {
1689
0
        break;
1690
0
      }
1691
0
      if (!delta->p.x[0] && delta->wds <= 1) {
1692
        /* exact result */
1693
0
        break;
1694
0
      }
1695
0
      delta = lshift(delta, Log2P, &alloc);
1696
0
      if (cmp(delta, bs) > 0) goto drop_down;
1697
0
      break;
1698
0
    }
1699
0
    if (i == 0) {
1700
      /* exactly half-way between */
1701
0
      if (dsign) {
1702
0
        if ((word0(&rv) & Bndry_mask1) == Bndry_mask1 &&
1703
0
            word1(&rv) ==
1704
0
                ((scale && (y = word0(&rv) & Exp_mask) <= 2 * P * Exp_msk1)
1705
0
                     ? (0xffffffff &
1706
0
                        (0xffffffff << (2 * P + 1 - (y >> Exp_shift))))
1707
0
                     : 0xffffffff)) {
1708
          /*boundary case -- increment exponent*/
1709
0
          word0(&rv) = (word0(&rv) & Exp_mask) + Exp_msk1;
1710
0
          word1(&rv) = 0;
1711
0
          dsign = 0;
1712
0
          break;
1713
0
        }
1714
0
      } else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
1715
0
      drop_down:
1716
        /* boundary case -- decrement exponent */
1717
0
        if (scale) {
1718
0
          L = word0(&rv) & Exp_mask;
1719
0
          if (L <= (2 * P + 1) * Exp_msk1) {
1720
0
            if (L > (P + 2) * Exp_msk1) /* round even ==> accept rv */
1721
0
              break;
1722
            /* rv = smallest denormal */
1723
0
            goto undfl;
1724
0
          }
1725
0
        }
1726
0
        L = (word0(&rv) & Exp_mask) - Exp_msk1;
1727
0
        word0(&rv) = L | Bndry_mask1;
1728
0
        word1(&rv) = 0xffffffff;
1729
0
        break;
1730
0
      }
1731
0
      if (!(word1(&rv) & LSB)) break;
1732
0
      if (dsign)
1733
0
        dval(&rv) += ulp(&rv);
1734
0
      else {
1735
0
        dval(&rv) -= ulp(&rv);
1736
0
        if (!dval(&rv)) goto undfl;
1737
0
      }
1738
0
      dsign = 1 - dsign;
1739
0
      break;
1740
0
    }
1741
0
    if ((aadj = ratio(delta, bs)) <= 2.) {
1742
0
      if (dsign)
1743
0
        aadj = aadj1 = 1.;
1744
0
      else if (word1(&rv) || word0(&rv) & Bndry_mask) {
1745
0
        if (word1(&rv) == Tiny1 && !word0(&rv)) goto undfl;
1746
0
        aadj = 1.;
1747
0
        aadj1 = -1.;
1748
0
      } else {
1749
        /* special case -- power of FLT_RADIX to be rounded down... */
1750
0
        if (aadj < 2. / FLT_RADIX)
1751
0
          aadj = 1. / FLT_RADIX;
1752
0
        else
1753
0
          aadj *= 0.5;
1754
0
        aadj1 = -aadj;
1755
0
      }
1756
0
    } else {
1757
0
      aadj *= 0.5;
1758
0
      aadj1 = dsign ? aadj : -aadj;
1759
#ifdef Check_FLT_ROUNDS
1760
      switch (Rounding) {
1761
        case 2: /* towards +infinity */
1762
          aadj1 -= 0.5;
1763
          break;
1764
        case 0: /* towards 0 */
1765
        case 3: /* towards -infinity */
1766
          aadj1 += 0.5;
1767
      }
1768
#else
1769
0
      if (Flt_Rounds == 0) aadj1 += 0.5;
1770
0
#endif /*Check_FLT_ROUNDS*/
1771
0
    }
1772
0
    y = word0(&rv) & Exp_mask;
1773
1774
    /* Check for overflow */
1775
1776
0
    if (y == Exp_msk1 * (DBL_MAX_EXP + Bias - 1)) {
1777
0
      dval(&rv0) = dval(&rv);
1778
0
      word0(&rv) -= P * Exp_msk1;
1779
0
      adj.d = aadj1 * ulp(&rv);
1780
0
      dval(&rv) += adj.d;
1781
0
      if ((word0(&rv) & Exp_mask) >= Exp_msk1 * (DBL_MAX_EXP + Bias - P)) {
1782
0
        if (word0(&rv0) == Big0 && word1(&rv0) == Big1) goto ovfl;
1783
0
        word0(&rv) = Big0;
1784
0
        word1(&rv) = Big1;
1785
0
        goto cont;
1786
0
      } else
1787
0
        word0(&rv) += P * Exp_msk1;
1788
0
    } else {
1789
0
      if (scale && y <= 2 * P * Exp_msk1) {
1790
0
        if (aadj <= 0x7fffffff) {
1791
0
          if ((z = (uint32_t)aadj) <= 0) z = 1;
1792
0
          aadj = z;
1793
0
          aadj1 = dsign ? aadj : -aadj;
1794
0
        }
1795
0
        dval(&aadj2) = aadj1;
1796
0
        word0(&aadj2) += (2 * P + 1) * Exp_msk1 - y;
1797
0
        aadj1 = dval(&aadj2);
1798
0
        adj.d = aadj1 * ulp(&rv);
1799
0
        dval(&rv) += adj.d;
1800
0
        if (rv.d == 0.) goto undfl;
1801
0
      } else {
1802
0
        adj.d = aadj1 * ulp(&rv);
1803
0
        dval(&rv) += adj.d;
1804
0
      }
1805
0
    }
1806
0
    z = word0(&rv) & Exp_mask;
1807
0
    if (!scale)
1808
0
      if (y == z) {
1809
        /* Can we stop now? */
1810
0
        L = (int32_t)aadj;
1811
0
        aadj -= L;
1812
        /* The tolerances below are conservative. */
1813
0
        if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
1814
0
          if (aadj < .4999999 || aadj > .5000001) break;
1815
0
        } else if (aadj < .4999999 / FLT_RADIX)
1816
0
          break;
1817
0
      }
1818
0
  cont:
1819
0
    Bfree(bb, &alloc);
1820
0
    Bfree(bd, &alloc);
1821
0
    Bfree(bs, &alloc);
1822
0
    Bfree(delta, &alloc);
1823
0
  }
1824
0
  if (scale) {
1825
0
    word0(&rv0) = Exp_1 - 2 * P * Exp_msk1;
1826
0
    word1(&rv0) = 0;
1827
0
    dval(&rv) *= dval(&rv0);
1828
0
  }
1829
0
retfree:
1830
0
  Bfree(bb, &alloc);
1831
0
  Bfree(bd, &alloc);
1832
0
  Bfree(bs, &alloc);
1833
0
  Bfree(bd0, &alloc);
1834
0
  Bfree(delta, &alloc);
1835
0
ret:
1836
0
  *se = s;
1837
0
  return sign ? -dval(&rv) : dval(&rv);
1838
0
}
1839
1840
0
static int quorem(Bigint *b, Bigint *S) {
1841
0
  int n;
1842
0
  uint32_t *bx = nullptr;
1843
0
  uint32_t *bxe = nullptr;
1844
0
  uint32_t *sx = nullptr;
1845
0
  uint32_t *sxe = nullptr;
1846
0
  uint64_t borrow = 0;
1847
0
  uint64_t carry = 0;
1848
0
  uint64_t y = 0;
1849
0
  uint64_t ys = 0;
1850
1851
0
  n = S->wds;
1852
0
  if (b->wds < n) return 0;
1853
0
  sx = S->p.x;
1854
0
  sxe = sx + --n;
1855
0
  bx = b->p.x;
1856
0
  bxe = bx + n;
1857
0
  uint32_t q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
1858
0
  if (q) {
1859
0
    borrow = 0;
1860
0
    carry = 0;
1861
0
    do {
1862
0
      ys = *sx++ * (uint64_t)q + carry;
1863
0
      carry = ys >> 32;
1864
0
      y = *bx - (ys & FFFFFFFF) - borrow;
1865
0
      borrow = y >> 32 & (uint32_t)1;
1866
0
      *bx++ = (uint32_t)(y & FFFFFFFF);
1867
0
    } while (sx <= sxe);
1868
0
    if (!*bxe) {
1869
0
      bx = b->p.x;
1870
0
      while (--bxe > bx && !*bxe) --n;
1871
0
      b->wds = n;
1872
0
    }
1873
0
  }
1874
0
  if (cmp(b, S) >= 0) {
1875
0
    q++;
1876
0
    borrow = 0;
1877
0
    carry = 0;
1878
0
    bx = b->p.x;
1879
0
    sx = S->p.x;
1880
0
    do {
1881
0
      ys = *sx++ + carry;
1882
0
      carry = ys >> 32;
1883
0
      y = *bx - (ys & FFFFFFFF) - borrow;
1884
0
      borrow = y >> 32 & (uint32_t)1;
1885
0
      *bx++ = (uint32_t)(y & FFFFFFFF);
1886
0
    } while (sx <= sxe);
1887
0
    bx = b->p.x;
1888
0
    bxe = bx + n;
1889
0
    if (!*bxe) {
1890
0
      while (--bxe > bx && !*bxe) --n;
1891
0
      b->wds = n;
1892
0
    }
1893
0
  }
1894
0
  return q;
1895
0
}
1896
1897
/*
1898
   dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
1899
1900
   Inspired by "How to Print Floating-Point Numbers Accurately" by
1901
   Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
1902
1903
   Modifications:
1904
        1. Rather than iterating, we use a simple numeric overestimate
1905
           to determine k= floor(log10(d)).  We scale relevant
1906
           quantities using O(log2(k)) rather than O(k) multiplications.
1907
        2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
1908
           try to generate digits strictly left to right.  Instead, we
1909
           compute with fewer bits and propagate the carry if necessary
1910
           when rounding the final digit up.  This is often faster.
1911
        3. Under the assumption that input will be rounded nearest,
1912
           mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
1913
           That is, we allow equality in stopping tests when the
1914
           round-nearest rule will give the same floating-point value
1915
           as would satisfaction of the stopping test with strict
1916
           inequality.
1917
        4. We remove common factors of powers of 2 from relevant
1918
           quantities.
1919
        5. When converting floating-point integers less than 1e16,
1920
           we use floating-point arithmetic rather than resorting
1921
           to multiple-precision integers.
1922
        6. When asked to produce fewer than 15 digits, we first try
1923
           to get by with floating-point arithmetic; we resort to
1924
           multiple-precision integer arithmetic only if we cannot
1925
           guarantee that the floating-point calculation has given
1926
           the correctly rounded result.  For k requested digits and
1927
           "uniformly" distributed input, the probability is
1928
           something like 10^(k-15) that we must resort to the int32_t
1929
           calculation.
1930
 */
1931
1932
static char *dtoa(double dd, int mode, int ndigits, int *decpt, int *sign,
1933
0
                  char **rve, char *buf, size_t buf_size) {
1934
  /*
1935
    Arguments ndigits, decpt, sign are similar to those
1936
    of ecvt and fcvt; trailing zeros are suppressed from
1937
    the returned string.  If not null, *rve is set to point
1938
    to the end of the return value.  If d is +-Infinity or NaN,
1939
    then *decpt is set to DTOA_OVERFLOW.
1940
1941
    mode:
1942
          0 ==> shortest string that yields d when read in
1943
                and rounded to nearest.
1944
          1 ==> like 0, but with Steele & White stopping rule;
1945
                e.g. with IEEE P754 arithmetic , mode 0 gives
1946
                1e23 whereas mode 1 gives 9.999999999999999e22.
1947
          2 ==> max(1,ndigits) significant digits.  This gives a
1948
                return value similar to that of ecvt, except
1949
                that trailing zeros are suppressed.
1950
          3 ==> through ndigits past the decimal point.  This
1951
                gives a return value similar to that from fcvt,
1952
                except that trailing zeros are suppressed, and
1953
                ndigits can be negative.
1954
          4,5 ==> similar to 2 and 3, respectively, but (in
1955
                round-nearest mode) with the tests of mode 0 to
1956
                possibly return a shorter string that rounds to d.
1957
                With IEEE arithmetic and compilation with
1958
                -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
1959
                as modes 2 and 3 when FLT_ROUNDS != 1.
1960
          6-9 ==> Debugging modes similar to mode - 4:  don't try
1961
                fast floating-point estimate (if applicable).
1962
1963
      Values of mode other than 0-9 are treated as mode 0.
1964
1965
    Sufficient space is allocated to the return value
1966
    to hold the suppressed trailing zeros.
1967
  */
1968
1969
0
  int bbits, b2, b5, be, dig, i, ieps, ilim = 0, ilim0, ilim1 = 0, j, j1, k, k0,
1970
0
                                       k_check, leftright, m2, m5, s2, s5,
1971
0
                                       spec_case, try_quick;
1972
0
  int32_t L = 0;
1973
0
  int denorm;
1974
0
  uint32_t x = 0;
1975
0
  Bigint *b, *b1, *delta, *mlo = nullptr, *mhi, *S;
1976
0
  U d2, eps, u;
1977
0
  double ds;
1978
0
  char *s, *s0;
1979
#ifdef Honor_FLT_ROUNDS
1980
  int rounding;
1981
#endif
1982
0
  Stack_alloc alloc;
1983
1984
0
  alloc.begin = alloc.free = buf;
1985
0
  alloc.end = buf + buf_size;
1986
0
  memset(alloc.freelist, 0, sizeof(alloc.freelist));
1987
1988
0
  u.d = dd;
1989
0
  if (word0(&u) & Sign_bit) {
1990
    /* set sign for everything, including 0's and NaNs */
1991
0
    *sign = 1;
1992
0
    word0(&u) &= ~Sign_bit; /* clear sign bit */
1993
0
  } else
1994
0
    *sign = 0;
1995
1996
  /* If infinity, set decpt to DTOA_OVERFLOW, if 0 set it to 1 */
1997
0
  if (((word0(&u) & Exp_mask) == Exp_mask && (*decpt = DTOA_OVERFLOW)) ||
1998
0
      (!dval(&u) && (*decpt = 1))) {
1999
    /* Infinity, NaN, 0 */
2000
0
    char *res = (char *)dtoa_alloc(2, &alloc);
2001
0
    res[0] = '0';
2002
0
    res[1] = '\0';
2003
0
    if (rve) *rve = res + 1;
2004
0
    return res;
2005
0
  }
2006
2007
#ifdef Honor_FLT_ROUNDS
2008
  if ((rounding = Flt_Rounds) >= 2) {
2009
    if (*sign)
2010
      rounding = rounding == 2 ? 0 : 2;
2011
    else if (rounding != 2)
2012
      rounding = 0;
2013
  }
2014
#endif
2015
2016
0
  b = d2b(&u, &be, &bbits, &alloc);
2017
0
  if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask >> Exp_shift1)))) {
2018
0
    dval(&d2) = dval(&u);
2019
0
    word0(&d2) &= Frac_mask1;
2020
0
    word0(&d2) |= Exp_11;
2021
2022
    /*
2023
      log(x)       ~=~ log(1.5) + (x-1.5)/1.5
2024
      log10(x)      =  log(x) / log(10)
2025
                   ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
2026
      log10(d)= (i-Bias)*log(2)/log(10) + log10(d2)
2027
2028
      This suggests computing an approximation k to log10(d) by
2029
2030
      k= (i - Bias)*0.301029995663981
2031
           + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
2032
2033
      We want k to be too large rather than too small.
2034
      The error in the first-order Taylor series approximation
2035
      is in our favor, so we just round up the constant enough
2036
      to compensate for any error in the multiplication of
2037
      (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
2038
      and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
2039
      adding 1e-13 to the constant term more than suffices.
2040
      Hence we adjust the constant term to 0.1760912590558.
2041
      (We could get a more accurate k by invoking log10,
2042
       but this is probably not worthwhile.)
2043
    */
2044
2045
0
    i -= Bias;
2046
0
    denorm = 0;
2047
0
  } else {
2048
    /* d is denormalized */
2049
2050
0
    i = bbits + be + (Bias + (P - 1) - 1);
2051
0
    x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
2052
0
               : word1(&u) << (32 - i);
2053
0
    dval(&d2) = x;
2054
0
    word0(&d2) -= 31 * Exp_msk1; /* adjust exponent */
2055
0
    i -= (Bias + (P - 1) - 1) + 1;
2056
0
    denorm = 1;
2057
0
  }
2058
0
  ds = (dval(&d2) - 1.5) * 0.289529654602168 + 0.1760912590558 +
2059
0
       i * 0.301029995663981;
2060
0
  k = (int)ds;
2061
0
  if (ds < 0. && ds != k) k--; /* want k= floor(ds) */
2062
0
  k_check = 1;
2063
0
  if (k >= 0 && k <= Ten_pmax) {
2064
0
    if (dval(&u) < tens[k]) k--;
2065
0
    k_check = 0;
2066
0
  }
2067
0
  j = bbits - i - 1;
2068
0
  if (j >= 0) {
2069
0
    b2 = 0;
2070
0
    s2 = j;
2071
0
  } else {
2072
0
    b2 = -j;
2073
0
    s2 = 0;
2074
0
  }
2075
0
  if (k >= 0) {
2076
0
    b5 = 0;
2077
0
    s5 = k;
2078
0
    s2 += k;
2079
0
  } else {
2080
0
    b2 -= k;
2081
0
    b5 = -k;
2082
0
    s5 = 0;
2083
0
  }
2084
0
  if (mode < 0 || mode > 9) mode = 0;
2085
2086
#ifdef Check_FLT_ROUNDS
2087
  try_quick = Rounding == 1;
2088
#else
2089
0
  try_quick = 1;
2090
0
#endif
2091
2092
0
  if (mode > 5) {
2093
0
    mode -= 4;
2094
0
    try_quick = 0;
2095
0
  }
2096
0
  leftright = 1;
2097
0
  switch (mode) {
2098
0
    case 0:
2099
0
    case 1:
2100
0
      ilim = ilim1 = -1;
2101
0
      i = 18;
2102
0
      ndigits = 0;
2103
0
      break;
2104
0
    case 2:
2105
0
      leftright = 0;
2106
0
      [[fallthrough]];
2107
0
    case 4:
2108
0
      if (ndigits <= 0) ndigits = 1;
2109
0
      ilim = ilim1 = i = ndigits;
2110
0
      break;
2111
0
    case 3:
2112
0
      leftright = 0;
2113
0
      [[fallthrough]];
2114
0
    case 5:
2115
0
      i = ndigits + k + 1;
2116
0
      ilim = i;
2117
0
      ilim1 = i - 1;
2118
0
      if (i <= 0) i = 1;
2119
0
  }
2120
0
  s = s0 = dtoa_alloc(i, &alloc);
2121
2122
#ifdef Honor_FLT_ROUNDS
2123
  if (mode > 1 && rounding != 1) leftright = 0;
2124
#endif
2125
2126
0
  if (ilim >= 0 && ilim <= Quick_max && try_quick) {
2127
    /* Try to get by with floating-point arithmetic. */
2128
0
    i = 0;
2129
0
    dval(&d2) = dval(&u);
2130
0
    k0 = k;
2131
0
    ilim0 = ilim;
2132
0
    ieps = 2; /* conservative */
2133
0
    if (k > 0) {
2134
0
      ds = tens[k & 0xf];
2135
0
      j = k >> 4;
2136
0
      if (j & Bletch) {
2137
        /* prevent overflows */
2138
0
        j &= Bletch - 1;
2139
0
        dval(&u) /= bigtens[n_bigtens - 1];
2140
0
        ieps++;
2141
0
      }
2142
0
      for (; j; j >>= 1, i++) {
2143
0
        if (j & 1) {
2144
0
          ieps++;
2145
0
          ds *= bigtens[i];
2146
0
        }
2147
0
      }
2148
0
      dval(&u) /= ds;
2149
0
    } else if ((j1 = -k)) {
2150
0
      dval(&u) *= tens[j1 & 0xf];
2151
0
      for (j = j1 >> 4; j; j >>= 1, i++) {
2152
0
        if (j & 1) {
2153
0
          ieps++;
2154
0
          dval(&u) *= bigtens[i];
2155
0
        }
2156
0
      }
2157
0
    }
2158
0
    if (k_check && dval(&u) < 1. && ilim > 0) {
2159
0
      if (ilim1 <= 0) goto fast_failed;
2160
0
      ilim = ilim1;
2161
0
      k--;
2162
0
      dval(&u) *= 10.;
2163
0
      ieps++;
2164
0
    }
2165
0
    dval(&eps) = ieps * dval(&u) + 7.;
2166
0
    word0(&eps) -= (P - 1) * Exp_msk1;
2167
0
    if (ilim == 0) {
2168
0
      S = mhi = nullptr;
2169
0
      dval(&u) -= 5.;
2170
0
      if (dval(&u) > dval(&eps)) goto one_digit;
2171
0
      if (dval(&u) < -dval(&eps)) goto no_digits;
2172
0
      goto fast_failed;
2173
0
    }
2174
0
    if (leftright) {
2175
      /* Use Steele & White method of only generating digits needed. */
2176
0
      dval(&eps) = 0.5 / tens[ilim - 1] - dval(&eps);
2177
0
      for (i = 0;;) {
2178
0
        L = (int32_t)dval(&u);
2179
0
        dval(&u) -= L;
2180
0
        *s++ = '0' + (int)L;
2181
0
        if (dval(&u) < dval(&eps)) goto ret1;
2182
0
        if (1. - dval(&u) < dval(&eps)) goto bump_up;
2183
0
        if (++i >= ilim) break;
2184
0
        dval(&eps) *= 10.;
2185
0
        dval(&u) *= 10.;
2186
0
      }
2187
0
    } else {
2188
      /* Generate ilim digits, then fix them up. */
2189
0
      dval(&eps) *= tens[ilim - 1];
2190
0
      for (i = 1;; i++, dval(&u) *= 10.) {
2191
0
        L = (int32_t)(dval(&u));
2192
0
        if (!(dval(&u) -= L)) ilim = i;
2193
0
        *s++ = '0' + (int)L;
2194
0
        if (i == ilim) {
2195
0
          if (dval(&u) > 0.5 + dval(&eps))
2196
0
            goto bump_up;
2197
0
          else if (dval(&u) < 0.5 - dval(&eps)) {
2198
0
            while (*--s == '0')
2199
0
              ;
2200
0
            s++;
2201
0
            goto ret1;
2202
0
          }
2203
0
          break;
2204
0
        }
2205
0
      }
2206
0
    }
2207
0
  fast_failed:
2208
0
    s = s0;
2209
0
    dval(&u) = dval(&d2);
2210
0
    k = k0;
2211
0
    ilim = ilim0;
2212
0
  }
2213
2214
  /* Do we have a "small" integer? */
2215
2216
0
  if (be >= 0 && k <= Int_max) {
2217
    /* Yes. */
2218
0
    ds = tens[k];
2219
0
    if (ndigits < 0 && ilim <= 0) {
2220
0
      S = mhi = nullptr;
2221
0
      if (ilim < 0 || dval(&u) <= 5 * ds) goto no_digits;
2222
0
      goto one_digit;
2223
0
    }
2224
0
    for (i = 1;; i++, dval(&u) *= 10.) {
2225
0
      L = (int32_t)(dval(&u) / ds);
2226
0
      dval(&u) -= L * ds;
2227
#ifdef Check_FLT_ROUNDS
2228
      /* If FLT_ROUNDS == 2, L will usually be high by 1 */
2229
      if (dval(&u) < 0) {
2230
        L--;
2231
        dval(&u) += ds;
2232
      }
2233
#endif
2234
0
      *s++ = '0' + (int)L;
2235
0
      if (!dval(&u)) {
2236
0
        break;
2237
0
      }
2238
0
      if (i == ilim) {
2239
#ifdef Honor_FLT_ROUNDS
2240
        if (mode > 1) {
2241
          switch (rounding) {
2242
            case 0:
2243
              goto ret1;
2244
            case 2:
2245
              goto bump_up;
2246
          }
2247
        }
2248
#endif
2249
0
        dval(&u) += dval(&u);
2250
0
        if (dval(&u) > ds || (dval(&u) == ds && L & 1)) {
2251
0
        bump_up:
2252
0
          while (*--s == '9')
2253
0
            if (s == s0) {
2254
0
              k++;
2255
0
              *s = '0';
2256
0
              break;
2257
0
            }
2258
0
          ++*s++;
2259
0
        }
2260
0
        break;
2261
0
      }
2262
0
    }
2263
0
    goto ret1;
2264
0
  }
2265
2266
0
  m2 = b2;
2267
0
  m5 = b5;
2268
0
  mhi = mlo = nullptr;
2269
0
  if (leftright) {
2270
0
    i = denorm ? be + (Bias + (P - 1) - 1 + 1) : 1 + P - bbits;
2271
0
    b2 += i;
2272
0
    s2 += i;
2273
0
    mhi = i2b(1, &alloc);
2274
0
  }
2275
0
  if (m2 > 0 && s2 > 0) {
2276
0
    i = m2 < s2 ? m2 : s2;
2277
0
    b2 -= i;
2278
0
    m2 -= i;
2279
0
    s2 -= i;
2280
0
  }
2281
0
  if (b5 > 0) {
2282
0
    if (leftright) {
2283
0
      if (m5 > 0) {
2284
0
        mhi = pow5mult(mhi, m5, &alloc);
2285
0
        b1 = mult(mhi, b, &alloc);
2286
0
        Bfree(b, &alloc);
2287
0
        b = b1;
2288
0
      }
2289
0
      if ((j = b5 - m5)) b = pow5mult(b, j, &alloc);
2290
0
    } else
2291
0
      b = pow5mult(b, b5, &alloc);
2292
0
  }
2293
0
  S = i2b(1, &alloc);
2294
0
  if (s5 > 0) S = pow5mult(S, s5, &alloc);
2295
2296
  /* Check for special case that d is a normalized power of 2. */
2297
2298
0
  spec_case = 0;
2299
0
  if ((mode < 2 || leftright)
2300
#ifdef Honor_FLT_ROUNDS
2301
      && rounding == 1
2302
#endif
2303
0
  ) {
2304
0
    if (!word1(&u) && !(word0(&u) & Bndry_mask) &&
2305
0
        word0(&u) & (Exp_mask & ~Exp_msk1)) {
2306
      /* The special case */
2307
0
      b2 += Log2P;
2308
0
      s2 += Log2P;
2309
0
      spec_case = 1;
2310
0
    }
2311
0
  }
2312
2313
  /*
2314
    Arrange for convenient computation of quotients:
2315
    shift left if necessary so divisor has 4 leading 0 bits.
2316
2317
    Perhaps we should just compute leading 28 bits of S once
2318
    a nd for all and pass them and a shift to quorem, so it
2319
    can do shifts and ors to compute the numerator for q.
2320
  */
2321
0
  if ((i = ((s5 ? 32 - hi0bits(S->p.x[S->wds - 1]) : 1) + s2) & 0x1f))
2322
0
    i = 32 - i;
2323
0
  if (i > 4) {
2324
0
    i -= 4;
2325
0
    b2 += i;
2326
0
    m2 += i;
2327
0
    s2 += i;
2328
0
  } else if (i < 4) {
2329
0
    i += 28;
2330
0
    b2 += i;
2331
0
    m2 += i;
2332
0
    s2 += i;
2333
0
  }
2334
0
  if (b2 > 0) b = lshift(b, b2, &alloc);
2335
0
  if (s2 > 0) S = lshift(S, s2, &alloc);
2336
0
  if (k_check) {
2337
0
    if (cmp(b, S) < 0) {
2338
0
      k--;
2339
      /* we botched the k estimate */
2340
0
      b = multadd(b, 10, 0, &alloc);
2341
0
      if (leftright) mhi = multadd(mhi, 10, 0, &alloc);
2342
0
      ilim = ilim1;
2343
0
    }
2344
0
  }
2345
0
  if (ilim <= 0 && (mode == 3 || mode == 5)) {
2346
0
    if (ilim < 0 || cmp(b, S = multadd(S, 5, 0, &alloc)) <= 0) {
2347
      /* no digits, fcvt style */
2348
0
    no_digits:
2349
0
      k = -1 - ndigits;
2350
0
      goto ret;
2351
0
    }
2352
0
  one_digit:
2353
0
    *s++ = '1';
2354
0
    k++;
2355
0
    goto ret;
2356
0
  }
2357
0
  if (leftright) {
2358
0
    if (m2 > 0) mhi = lshift(mhi, m2, &alloc);
2359
2360
    /*
2361
      Compute mlo -- check for special case that d is a normalized power of 2.
2362
    */
2363
2364
0
    mlo = mhi;
2365
0
    if (spec_case) {
2366
0
      mhi = Balloc(mhi->k, &alloc);
2367
0
      Bcopy(mhi, mlo);
2368
0
      mhi = lshift(mhi, Log2P, &alloc);
2369
0
    }
2370
2371
0
    for (i = 1;; i++) {
2372
0
      dig = quorem(b, S) + '0';
2373
      /* Do we yet have the shortest decimal string that will round to d? */
2374
0
      j = cmp(b, mlo);
2375
0
      delta = diff(S, mhi, &alloc);
2376
0
      j1 = delta->sign ? 1 : cmp(b, delta);
2377
0
      Bfree(delta, &alloc);
2378
0
      if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
2379
#ifdef Honor_FLT_ROUNDS
2380
          && rounding >= 1
2381
#endif
2382
0
      ) {
2383
0
        if (dig == '9') goto round_9_up;
2384
0
        if (j > 0) dig++;
2385
0
        *s++ = dig;
2386
0
        goto ret;
2387
0
      }
2388
0
      if (j < 0 || (j == 0 && mode != 1 && !(word1(&u) & 1))) {
2389
0
        if (!b->p.x[0] && b->wds <= 1) {
2390
0
          goto accept_dig;
2391
0
        }
2392
#ifdef Honor_FLT_ROUNDS
2393
        if (mode > 1) switch (rounding) {
2394
            case 0:
2395
              goto accept_dig;
2396
            case 2:
2397
              goto keep_dig;
2398
          }
2399
#endif /*Honor_FLT_ROUNDS*/
2400
0
        if (j1 > 0) {
2401
0
          b = lshift(b, 1, &alloc);
2402
0
          j1 = cmp(b, S);
2403
0
          if ((j1 > 0 || (j1 == 0 && dig & 1)) && dig++ == '9') goto round_9_up;
2404
0
        }
2405
0
      accept_dig:
2406
0
        *s++ = dig;
2407
0
        goto ret;
2408
0
      }
2409
0
      if (j1 > 0) {
2410
#ifdef Honor_FLT_ROUNDS
2411
        if (!rounding) goto accept_dig;
2412
#endif
2413
0
        if (dig == '9') { /* possible if i == 1 */
2414
0
        round_9_up:
2415
0
          *s++ = '9';
2416
0
          goto roundoff;
2417
0
        }
2418
0
        *s++ = dig + 1;
2419
0
        goto ret;
2420
0
      }
2421
#ifdef Honor_FLT_ROUNDS
2422
    keep_dig:
2423
#endif
2424
0
      *s++ = dig;
2425
0
      if (i == ilim) break;
2426
0
      b = multadd(b, 10, 0, &alloc);
2427
0
      if (mlo == mhi)
2428
0
        mlo = mhi = multadd(mhi, 10, 0, &alloc);
2429
0
      else {
2430
0
        mlo = multadd(mlo, 10, 0, &alloc);
2431
0
        mhi = multadd(mhi, 10, 0, &alloc);
2432
0
      }
2433
0
    }
2434
0
  } else
2435
0
    for (i = 1;; i++) {
2436
0
      *s++ = dig = quorem(b, S) + '0';
2437
0
      if (!b->p.x[0] && b->wds <= 1) {
2438
0
        goto ret;
2439
0
      }
2440
0
      if (i >= ilim) break;
2441
0
      b = multadd(b, 10, 0, &alloc);
2442
0
    }
2443
2444
    /* Round off last digit */
2445
2446
#ifdef Honor_FLT_ROUNDS
2447
  switch (rounding) {
2448
    case 0:
2449
      goto trimzeros;
2450
    case 2:
2451
      goto roundoff;
2452
  }
2453
#endif
2454
0
  b = lshift(b, 1, &alloc);
2455
0
  j = cmp(b, S);
2456
0
  if (j > 0 || (j == 0 && dig & 1)) {
2457
0
  roundoff:
2458
0
    while (*--s == '9')
2459
0
      if (s == s0) {
2460
0
        k++;
2461
0
        *s++ = '1';
2462
0
        goto ret;
2463
0
      }
2464
0
    ++*s++;
2465
0
  } else {
2466
#ifdef Honor_FLT_ROUNDS
2467
  trimzeros:
2468
#endif
2469
0
    while (*--s == '0')
2470
0
      ;
2471
0
    s++;
2472
0
  }
2473
0
ret:
2474
0
  Bfree(S, &alloc);
2475
0
  if (mhi) {
2476
0
    if (mlo && mlo != mhi) Bfree(mlo, &alloc);
2477
0
    Bfree(mhi, &alloc);
2478
0
  }
2479
0
ret1:
2480
0
  Bfree(b, &alloc);
2481
0
  *s = 0;
2482
0
  *decpt = k + 1;
2483
0
  if (rve) *rve = s;
2484
0
  return s0;
2485
0
}