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