LEFT | RIGHT |
1 /**************************************************************** | 1 /**************************************************************** |
2 * | 2 * |
3 * The author of this software is David M. Gay. | 3 * The author of this software is David M. Gay. |
4 * | 4 * |
5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies. | 5 * Copyright (c) 1991, 2000, 2001 by Lucent Technologies. |
6 * | 6 * |
7 * Permission to use, copy, modify, and distribute this software for any | 7 * Permission to use, copy, modify, and distribute this software for any |
8 * purpose without fee is hereby granted, provided that this entire notice | 8 * purpose without fee is hereby granted, provided that this entire notice |
9 * is included in all copies of any software which is or includes a copy | 9 * is included in all copies of any software which is or includes a copy |
10 * or modification of this software and in all copies of the supporting | 10 * or modification of this software and in all copies of the supporting |
11 * documentation for such software. | 11 * documentation for such software. |
12 * | 12 * |
13 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED | 13 * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED |
14 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY | 14 * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY |
15 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY | 15 * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY |
16 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. | 16 * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. |
17 * | 17 * |
18 ***************************************************************/ | 18 ***************************************************************/ |
19 | 19 |
20 /**************************************************************** | 20 /**************************************************************** |
21 * This is dtoa.c by David M. Gay, downloaded from | 21 * This is dtoa.c by David M. Gay, downloaded from |
22 * http://www.netlib.org/fp/dtoa.c on March 17, 2009 and modified for | 22 * http://www.netlib.org/fp/dtoa.c on April 15, 2009 and modified for |
23 * inclusion into the Python core by Mark E. T. Dickinson and Eric V. Smith. | 23 * inclusion into the Python core by Mark E. T. Dickinson and Eric V. Smith. |
24 * The major modifications are as follows: | 24 * The major modifications are as follows: |
25 * | 25 * |
26 * 0. The original code has been specialized to Python's needs by removing | 26 * 0. The original code has been specialized to Python's needs by removing |
27 * many of the #ifdef'd sections. In particular, code to support VAX and | 27 * many of the #ifdef'd sections. In particular, code to support VAX and |
28 * IBM floating-point formats, hex NaNs, hex floats, locale-aware | 28 * IBM floating-point formats, hex NaNs, hex floats, locale-aware |
29 * treatment of the decimal point, and setting of the inexact flag have | 29 * treatment of the decimal point, and setting of the inexact flag have |
30 * been removed. | 30 * been removed. |
31 * | 31 * |
32 * 1. We use PyMem_Malloc and PyMem_Free in place of malloc and free. | 32 * 1. We use PyMem_Malloc and PyMem_Free in place of malloc and free. |
(...skipping 10 matching lines...) Expand all Loading... |
43 * Similarly, rv_alloc and nrv_alloc (return type char *) return NULL on | 43 * Similarly, rv_alloc and nrv_alloc (return type char *) return NULL on |
44 * failure. bigcomp now has return type int (it used to be void) and | 44 * failure. bigcomp now has return type int (it used to be void) and |
45 * returns -1 on failure and 0 otherwise. _Py_dg_dtoa returns NULL | 45 * returns -1 on failure and 0 otherwise. _Py_dg_dtoa returns NULL |
46 * on failure. _Py_dg_strtod indicates failure due to malloc failure | 46 * on failure. _Py_dg_strtod indicates failure due to malloc failure |
47 * by returning -1.0, setting errno=ENOMEM and *se to s00. | 47 * by returning -1.0, setting errno=ENOMEM and *se to s00. |
48 * | 48 * |
49 * 4. The static variable dtoa_result has been removed. Callers of | 49 * 4. The static variable dtoa_result has been removed. Callers of |
50 * _Py_dg_dtoa are expected to call _Py_dg_freedtoa to free | 50 * _Py_dg_dtoa are expected to call _Py_dg_freedtoa to free |
51 * the memory allocated by _Py_dg_dtoa. | 51 * the memory allocated by _Py_dg_dtoa. |
52 * | 52 * |
53 * 5. A bug in the original dtoa.c code, in which '.nan' and '.inf' | 53 * 5. The code has been reformatted to better fit with Python's |
54 * were accepted as valid inputs to strtod, has been fixed. | |
55 * | |
56 * 6. The code has been reformatted to better fit with Python's | |
57 * C style guide (PEP 7). | 54 * C style guide (PEP 7). |
58 * | 55 * |
59 ***************************************************************/ | 56 ***************************************************************/ |
60 | 57 |
61 /* Please send bug reports for the original dtoa.c code to David M. Gay (dmg | 58 /* Please send bug reports for the original dtoa.c code to David M. Gay (dmg |
62 * at acm dot org, with " at " changed at "@" and " dot " changed to "."). | 59 * at acm dot org, with " at " changed at "@" and " dot " changed to "."). |
63 * Please report bugs for this modified version using the Python issue tracker | 60 * Please report bugs for this modified version using the Python issue tracker |
64 * (http://bugs.python.org). */ | 61 * (http://bugs.python.org). */ |
65 | 62 |
66 /* On a machine with IEEE extended-precision registers, it is | 63 /* On a machine with IEEE extended-precision registers, it is |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 * one bit; only in really hard cases do we need to | 97 * one bit; only in really hard cases do we need to |
101 * compute a second residual. | 98 * compute a second residual. |
102 * 4. Because of 3., we don't need a large table of powers of 10 | 99 * 4. Because of 3., we don't need a large table of powers of 10 |
103 * for ten-to-e (just some small tables, e.g. of 10^k | 100 * for ten-to-e (just some small tables, e.g. of 10^k |
104 * for 0 <= k <= 22). | 101 * for 0 <= k <= 22). |
105 */ | 102 */ |
106 | 103 |
107 /* Linking of Python's #defines to Gay's #defines starts here. */ | 104 /* Linking of Python's #defines to Gay's #defines starts here. */ |
108 | 105 |
109 #include "Python.h" | 106 #include "Python.h" |
| 107 |
| 108 /* if PY_NO_SHORT_FLOAT_REPR is defined, then don't even try to compile |
| 109 the following code */ |
| 110 #ifndef PY_NO_SHORT_FLOAT_REPR |
| 111 |
110 #include "float.h" | 112 #include "float.h" |
111 | 113 |
112 #define MALLOC PyMem_Malloc | 114 #define MALLOC PyMem_Malloc |
113 #define FREE PyMem_Free | 115 #define FREE PyMem_Free |
114 | 116 |
115 /* This code should also work for ARM mixed-endian format on little-endian | 117 /* This code should also work for ARM mixed-endian format on little-endian |
116 machines, where doubles have byte order 45670123 (in increasing address | 118 machines, where doubles have byte order 45670123 (in increasing address |
117 order, 0 being the least significant byte). */ | 119 order, 0 being the least significant byte). */ |
118 #ifdef DOUBLE_IS_LITTLE_ENDIAN_IEEE754 | 120 #ifdef DOUBLE_IS_LITTLE_ENDIAN_IEEE754 |
119 # define IEEE_8087 | 121 # define IEEE_8087 |
(...skipping 1278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1398 z *= 10; | 1400 z *= 10; |
1399 if (nd++ < 9) | 1401 if (nd++ < 9) |
1400 y = 10*y + c; | 1402 y = 10*y + c; |
1401 else if (nd <= DBL_DIG + 1) | 1403 else if (nd <= DBL_DIG + 1) |
1402 z = 10*z + c; | 1404 z = 10*z + c; |
1403 nz = 0; | 1405 nz = 0; |
1404 } | 1406 } |
1405 } | 1407 } |
1406 } | 1408 } |
1407 dig_done: | 1409 dig_done: |
1408 if (!nd && !nz && !nz0 && (s != s0)) { | |
1409 /* no digits, just a '.'. Fail. */ | |
1410 goto ret0; | |
1411 } | |
1412 e = 0; | 1410 e = 0; |
1413 if (c == 'e' || c == 'E') { | 1411 if (c == 'e' || c == 'E') { |
1414 if (!nd && !nz && !nz0) { | 1412 if (!nd && !nz && !nz0) { |
1415 goto ret0; | 1413 goto ret0; |
1416 } | 1414 } |
1417 s00 = s; | 1415 s00 = s; |
1418 esign = 0; | 1416 esign = 0; |
1419 switch(c = *++s) { | 1417 switch(c = *++s) { |
1420 case '-': | 1418 case '-': |
1421 esign = 1; | 1419 esign = 1; |
(...skipping 20 matching lines...) Expand all Loading... |
1442 } | 1440 } |
1443 else | 1441 else |
1444 e = 0; | 1442 e = 0; |
1445 } | 1443 } |
1446 else | 1444 else |
1447 s = s00; | 1445 s = s00; |
1448 } | 1446 } |
1449 if (!nd) { | 1447 if (!nd) { |
1450 if (!nz && !nz0) { | 1448 if (!nz && !nz0) { |
1451 /* Check for Nan and Infinity */ | 1449 /* Check for Nan and Infinity */ |
1452 switch(c) { | 1450 if (!bc.dplen) |
1453 case 'i': | 1451 switch(c) { |
1454 case 'I': | 1452 case 'i': |
1455 if (match(&s,"nf")) { | 1453 case 'I': |
1456 --s; | 1454 if (match(&s,"nf")) { |
1457 if (!match(&s,"inity")) | 1455 --s; |
1458 ++s; | 1456 if (!match(&s,"inity")) |
1459 word0(&rv) = 0x7ff00000; | 1457 ++s; |
1460 word1(&rv) = 0; | 1458 word0(&rv) = 0x7ff00000; |
1461 goto ret; | 1459 word1(&rv) = 0; |
| 1460 goto ret; |
| 1461 } |
| 1462 break; |
| 1463 case 'n': |
| 1464 case 'N': |
| 1465 if (match(&s, "an")) { |
| 1466 word0(&rv) = NAN_WORD0; |
| 1467 word1(&rv) = NAN_WORD1; |
| 1468 goto ret; |
| 1469 } |
1462 } | 1470 } |
1463 break; | |
1464 case 'n': | |
1465 case 'N': | |
1466 if (match(&s, "an")) { | |
1467 word0(&rv) = NAN_WORD0; | |
1468 word1(&rv) = NAN_WORD1; | |
1469 goto ret; | |
1470 } | |
1471 } | |
1472 ret0: | 1471 ret0: |
1473 s = s00; | 1472 s = s00; |
1474 sign = 0; | 1473 sign = 0; |
1475 } | 1474 } |
1476 goto ret; | 1475 goto ret; |
1477 } | 1476 } |
1478 bc.e0 = e1 = e -= nf; | 1477 bc.e0 = e1 = e -= nf; |
1479 | 1478 |
1480 /* Now we have nd0 digits, starting at s0, followed by a | 1479 /* Now we have nd0 digits, starting at s0, followed by a |
1481 * decimal point, followed by nd-nd0 digits. The number we're | 1480 * decimal point, followed by nd-nd0 digits. The number we're |
(...skipping 1154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2636 Bfree(mhi); | 2635 Bfree(mhi); |
2637 if (b) | 2636 if (b) |
2638 Bfree(b); | 2637 Bfree(b); |
2639 if (s0) | 2638 if (s0) |
2640 _Py_dg_freedtoa(s0); | 2639 _Py_dg_freedtoa(s0); |
2641 return NULL; | 2640 return NULL; |
2642 } | 2641 } |
2643 #ifdef __cplusplus | 2642 #ifdef __cplusplus |
2644 } | 2643 } |
2645 #endif | 2644 #endif |
| 2645 |
| 2646 #endif /* PY_NO_SHORT_FLOAT_REPR */ |
LEFT | RIGHT |