You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2958 lines
80 KiB

Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
Merged revisions 77477-77478,77481-77483,77490-77493 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77477 | mark.dickinson | 2010-01-13 18:21:53 +0000 (Wed, 13 Jan 2010) | 1 line Add comments explaining the role of the bigcomp function in dtoa.c. ........ r77478 | mark.dickinson | 2010-01-13 19:02:37 +0000 (Wed, 13 Jan 2010) | 1 line Clarify that sulp expects a nonnegative input, but that +0.0 is fine. ........ r77481 | mark.dickinson | 2010-01-13 20:55:03 +0000 (Wed, 13 Jan 2010) | 1 line Simplify and annotate the bigcomp function, removing unused special cases. ........ r77482 | mark.dickinson | 2010-01-13 22:15:53 +0000 (Wed, 13 Jan 2010) | 1 line Fix buggy comparison: LHS of comparison was being treated as unsigned. ........ r77483 | mark.dickinson | 2010-01-13 22:20:10 +0000 (Wed, 13 Jan 2010) | 1 line More dtoa.c cleanup; remove the need for bc.dplen, bc.dp0 and bc.dp1. ........ r77490 | mark.dickinson | 2010-01-14 13:02:36 +0000 (Thu, 14 Jan 2010) | 1 line Fix off-by-one error introduced in r77483. I have a test for this, but it currently fails due to a different dtoa.c bug; I'll add the test once that bug is fixed. ........ r77491 | mark.dickinson | 2010-01-14 13:14:49 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix a dtoa.c bug (bug 6) causing incorrect rounding. Tests to follow. ........ r77492 | mark.dickinson | 2010-01-14 14:40:20 +0000 (Thu, 14 Jan 2010) | 1 line Issue 7632: fix incorrect rounding for long input strings with values very close to a power of 2. (See Bug 4 in the tracker discussion.) ........ r77493 | mark.dickinson | 2010-01-14 15:22:33 +0000 (Thu, 14 Jan 2010) | 1 line Issue #7632: add tests for bugs fixed so far. ........
16 years ago
  1. /****************************************************************
  2. *
  3. * The author of this software is David M. Gay.
  4. *
  5. * Copyright (c) 1991, 2000, 2001 by Lucent Technologies.
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  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
  10. * or modification of this software and in all copies of the supporting
  11. * documentation for such software.
  12. *
  13. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  14. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHOR NOR LUCENT MAKES ANY
  15. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  16. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  17. *
  18. ***************************************************************/
  19. /****************************************************************
  20. * This is dtoa.c by David M. Gay, downloaded from
  21. * http://www.netlib.org/fp/dtoa.c on April 15, 2009 and modified for
  22. * inclusion into the Python core by Mark E. T. Dickinson and Eric V. Smith.
  23. *
  24. * Please remember to check http://www.netlib.org/fp regularly (and especially
  25. * before any Python release) for bugfixes and updates.
  26. *
  27. * The major modifications from Gay's original code are as follows:
  28. *
  29. * 0. The original code has been specialized to Python's needs by removing
  30. * many of the #ifdef'd sections. In particular, code to support VAX and
  31. * IBM floating-point formats, hex NaNs, hex floats, locale-aware
  32. * treatment of the decimal point, and setting of the inexact flag have
  33. * been removed.
  34. *
  35. * 1. We use PyMem_Malloc and PyMem_Free in place of malloc and free.
  36. *
  37. * 2. The public functions strtod, dtoa and freedtoa all now have
  38. * a _Py_dg_ prefix.
  39. *
  40. * 3. Instead of assuming that PyMem_Malloc always succeeds, we thread
  41. * PyMem_Malloc failures through the code. The functions
  42. *
  43. * Balloc, multadd, s2b, i2b, mult, pow5mult, lshift, diff, d2b
  44. *
  45. * of return type *Bigint all return NULL to indicate a malloc failure.
  46. * Similarly, rv_alloc and nrv_alloc (return type char *) return NULL on
  47. * failure. bigcomp now has return type int (it used to be void) and
  48. * returns -1 on failure and 0 otherwise. _Py_dg_dtoa returns NULL
  49. * on failure. _Py_dg_strtod indicates failure due to malloc failure
  50. * by returning -1.0, setting errno=ENOMEM and *se to s00.
  51. *
  52. * 4. The static variable dtoa_result has been removed. Callers of
  53. * _Py_dg_dtoa are expected to call _Py_dg_freedtoa to free
  54. * the memory allocated by _Py_dg_dtoa.
  55. *
  56. * 5. The code has been reformatted to better fit with Python's
  57. * C style guide (PEP 7).
  58. *
  59. * 6. A bug in the memory allocation has been fixed: to avoid FREEing memory
  60. * that hasn't been MALLOC'ed, private_mem should only be used when k <=
  61. * Kmax.
  62. *
  63. * 7. _Py_dg_strtod has been modified so that it doesn't accept strings with
  64. * leading whitespace.
  65. *
  66. ***************************************************************/
  67. /* Please send bug reports for the original dtoa.c code to David M. Gay (dmg
  68. * at acm dot org, with " at " changed at "@" and " dot " changed to ".").
  69. * Please report bugs for this modified version using the Python issue tracker
  70. * (http://bugs.python.org). */
  71. /* On a machine with IEEE extended-precision registers, it is
  72. * necessary to specify double-precision (53-bit) rounding precision
  73. * before invoking strtod or dtoa. If the machine uses (the equivalent
  74. * of) Intel 80x87 arithmetic, the call
  75. * _control87(PC_53, MCW_PC);
  76. * does this with many compilers. Whether this or another call is
  77. * appropriate depends on the compiler; for this to work, it may be
  78. * necessary to #include "float.h" or another system-dependent header
  79. * file.
  80. */
  81. /* strtod for IEEE-, VAX-, and IBM-arithmetic machines.
  82. *
  83. * This strtod returns a nearest machine number to the input decimal
  84. * string (or sets errno to ERANGE). With IEEE arithmetic, ties are
  85. * broken by the IEEE round-even rule. Otherwise ties are broken by
  86. * biased rounding (add half and chop).
  87. *
  88. * Inspired loosely by William D. Clinger's paper "How to Read Floating
  89. * Point Numbers Accurately" [Proc. ACM SIGPLAN '90, pp. 92-101].
  90. *
  91. * Modifications:
  92. *
  93. * 1. We only require IEEE, IBM, or VAX double-precision
  94. * arithmetic (not IEEE double-extended).
  95. * 2. We get by with floating-point arithmetic in a case that
  96. * Clinger missed -- when we're computing d * 10^n
  97. * for a small integer d and the integer n is not too
  98. * much larger than 22 (the maximum integer k for which
  99. * we can represent 10^k exactly), we may be able to
  100. * compute (d*10^k) * 10^(e-k) with just one roundoff.
  101. * 3. Rather than a bit-at-a-time adjustment of the binary
  102. * result in the hard case, we use floating-point
  103. * arithmetic to determine the adjustment to within
  104. * one bit; only in really hard cases do we need to
  105. * compute a second residual.
  106. * 4. Because of 3., we don't need a large table of powers of 10
  107. * for ten-to-e (just some small tables, e.g. of 10^k
  108. * for 0 <= k <= 22).
  109. */
  110. /* Linking of Python's #defines to Gay's #defines starts here. */
  111. #include "Python.h"
  112. /* if PY_NO_SHORT_FLOAT_REPR is defined, then don't even try to compile
  113. the following code */
  114. #ifndef PY_NO_SHORT_FLOAT_REPR
  115. #include "float.h"
  116. #define MALLOC PyMem_Malloc
  117. #define FREE PyMem_Free
  118. /* This code should also work for ARM mixed-endian format on little-endian
  119. machines, where doubles have byte order 45670123 (in increasing address
  120. order, 0 being the least significant byte). */
  121. #ifdef DOUBLE_IS_LITTLE_ENDIAN_IEEE754
  122. # define IEEE_8087
  123. #endif
  124. #if defined(DOUBLE_IS_BIG_ENDIAN_IEEE754) || \
  125. defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754)
  126. # define IEEE_MC68k
  127. #endif
  128. #if defined(IEEE_8087) + defined(IEEE_MC68k) != 1
  129. #error "Exactly one of IEEE_8087 or IEEE_MC68k should be defined."
  130. #endif
  131. /* The code below assumes that the endianness of integers matches the
  132. endianness of the two 32-bit words of a double. Check this. */
  133. #if defined(WORDS_BIGENDIAN) && (defined(DOUBLE_IS_LITTLE_ENDIAN_IEEE754) || \
  134. defined(DOUBLE_IS_ARM_MIXED_ENDIAN_IEEE754))
  135. #error "doubles and ints have incompatible endianness"
  136. #endif
  137. #if !defined(WORDS_BIGENDIAN) && defined(DOUBLE_IS_BIG_ENDIAN_IEEE754)
  138. #error "doubles and ints have incompatible endianness"
  139. #endif
  140. #if defined(HAVE_UINT32_T) && defined(HAVE_INT32_T)
  141. typedef PY_UINT32_T ULong;
  142. typedef PY_INT32_T Long;
  143. #else
  144. #error "Failed to find an exact-width 32-bit integer type"
  145. #endif
  146. #if defined(HAVE_UINT64_T)
  147. #define ULLong PY_UINT64_T
  148. #else
  149. #undef ULLong
  150. #endif
  151. #undef DEBUG
  152. #ifdef Py_DEBUG
  153. #define DEBUG
  154. #endif
  155. /* End Python #define linking */
  156. #ifdef DEBUG
  157. #define Bug(x) {fprintf(stderr, "%s\n", x); exit(1);}
  158. #endif
  159. #ifndef PRIVATE_MEM
  160. #define PRIVATE_MEM 2304
  161. #endif
  162. #define PRIVATE_mem ((PRIVATE_MEM+sizeof(double)-1)/sizeof(double))
  163. static double private_mem[PRIVATE_mem], *pmem_next = private_mem;
  164. #ifdef __cplusplus
  165. extern "C" {
  166. #endif
  167. typedef union { double d; ULong L[2]; } U;
  168. #ifdef IEEE_8087
  169. #define word0(x) (x)->L[1]
  170. #define word1(x) (x)->L[0]
  171. #else
  172. #define word0(x) (x)->L[0]
  173. #define word1(x) (x)->L[1]
  174. #endif
  175. #define dval(x) (x)->d
  176. #ifndef STRTOD_DIGLIM
  177. #define STRTOD_DIGLIM 40
  178. #endif
  179. /* maximum permitted exponent value for strtod; exponents larger than
  180. MAX_ABS_EXP in absolute value get truncated to +-MAX_ABS_EXP. MAX_ABS_EXP
  181. should fit into an int. */
  182. #ifndef MAX_ABS_EXP
  183. #define MAX_ABS_EXP 19999U
  184. #endif
  185. /* The following definition of Storeinc is appropriate for MIPS processors.
  186. * An alternative that might be better on some machines is
  187. * #define Storeinc(a,b,c) (*a++ = b << 16 | c & 0xffff)
  188. */
  189. #if defined(IEEE_8087)
  190. #define Storeinc(a,b,c) (((unsigned short *)a)[1] = (unsigned short)b, \
  191. ((unsigned short *)a)[0] = (unsigned short)c, a++)
  192. #else
  193. #define Storeinc(a,b,c) (((unsigned short *)a)[0] = (unsigned short)b, \
  194. ((unsigned short *)a)[1] = (unsigned short)c, a++)
  195. #endif
  196. /* #define P DBL_MANT_DIG */
  197. /* Ten_pmax = floor(P*log(2)/log(5)) */
  198. /* Bletch = (highest power of 2 < DBL_MAX_10_EXP) / 16 */
  199. /* Quick_max = floor((P-1)*log(FLT_RADIX)/log(10) - 1) */
  200. /* Int_max = floor(P*log(FLT_RADIX)/log(10) - 1) */
  201. #define Exp_shift 20
  202. #define Exp_shift1 20
  203. #define Exp_msk1 0x100000
  204. #define Exp_msk11 0x100000
  205. #define Exp_mask 0x7ff00000
  206. #define P 53
  207. #define Nbits 53
  208. #define Bias 1023
  209. #define Emax 1023
  210. #define Emin (-1022)
  211. #define Etiny (-1074) /* smallest denormal is 2**Etiny */
  212. #define Exp_1 0x3ff00000
  213. #define Exp_11 0x3ff00000
  214. #define Ebits 11
  215. #define Frac_mask 0xfffff
  216. #define Frac_mask1 0xfffff
  217. #define Ten_pmax 22
  218. #define Bletch 0x10
  219. #define Bndry_mask 0xfffff
  220. #define Bndry_mask1 0xfffff
  221. #define Sign_bit 0x80000000
  222. #define Log2P 1
  223. #define Tiny0 0
  224. #define Tiny1 1
  225. #define Quick_max 14
  226. #define Int_max 14
  227. #ifndef Flt_Rounds
  228. #ifdef FLT_ROUNDS
  229. #define Flt_Rounds FLT_ROUNDS
  230. #else
  231. #define Flt_Rounds 1
  232. #endif
  233. #endif /*Flt_Rounds*/
  234. #define Rounding Flt_Rounds
  235. #define Big0 (Frac_mask1 | Exp_msk1*(DBL_MAX_EXP+Bias-1))
  236. #define Big1 0xffffffff
  237. /* Standard NaN used by _Py_dg_stdnan. */
  238. #define NAN_WORD0 0x7ff80000
  239. #define NAN_WORD1 0
  240. /* Bits of the representation of positive infinity. */
  241. #define POSINF_WORD0 0x7ff00000
  242. #define POSINF_WORD1 0
  243. /* struct BCinfo is used to pass information from _Py_dg_strtod to bigcomp */
  244. typedef struct BCinfo BCinfo;
  245. struct
  246. BCinfo {
  247. int e0, nd, nd0, scale;
  248. };
  249. #define FFFFFFFF 0xffffffffUL
  250. #define Kmax 7
  251. /* struct Bigint is used to represent arbitrary-precision integers. These
  252. integers are stored in sign-magnitude format, with the magnitude stored as
  253. an array of base 2**32 digits. Bigints are always normalized: if x is a
  254. Bigint then x->wds >= 1, and either x->wds == 1 or x[wds-1] is nonzero.
  255. The Bigint fields are as follows:
  256. - next is a header used by Balloc and Bfree to keep track of lists
  257. of freed Bigints; it's also used for the linked list of
  258. powers of 5 of the form 5**2**i used by pow5mult.
  259. - k indicates which pool this Bigint was allocated from
  260. - maxwds is the maximum number of words space was allocated for
  261. (usually maxwds == 2**k)
  262. - sign is 1 for negative Bigints, 0 for positive. The sign is unused
  263. (ignored on inputs, set to 0 on outputs) in almost all operations
  264. involving Bigints: a notable exception is the diff function, which
  265. ignores signs on inputs but sets the sign of the output correctly.
  266. - wds is the actual number of significant words
  267. - x contains the vector of words (digits) for this Bigint, from least
  268. significant (x[0]) to most significant (x[wds-1]).
  269. */
  270. struct
  271. Bigint {
  272. struct Bigint *next;
  273. int k, maxwds, sign, wds;
  274. ULong x[1];
  275. };
  276. typedef struct Bigint Bigint;
  277. #ifndef Py_USING_MEMORY_DEBUGGER
  278. /* Memory management: memory is allocated from, and returned to, Kmax+1 pools
  279. of memory, where pool k (0 <= k <= Kmax) is for Bigints b with b->maxwds ==
  280. 1 << k. These pools are maintained as linked lists, with freelist[k]
  281. pointing to the head of the list for pool k.
  282. On allocation, if there's no free slot in the appropriate pool, MALLOC is
  283. called to get more memory. This memory is not returned to the system until
  284. Python quits. There's also a private memory pool that's allocated from
  285. in preference to using MALLOC.
  286. For Bigints with more than (1 << Kmax) digits (which implies at least 1233
  287. decimal digits), memory is directly allocated using MALLOC, and freed using
  288. FREE.
  289. XXX: it would be easy to bypass this memory-management system and
  290. translate each call to Balloc into a call to PyMem_Malloc, and each
  291. Bfree to PyMem_Free. Investigate whether this has any significant
  292. performance on impact. */
  293. static Bigint *freelist[Kmax+1];
  294. /* Allocate space for a Bigint with up to 1<<k digits */
  295. static Bigint *
  296. Balloc(int k)
  297. {
  298. int x;
  299. Bigint *rv;
  300. unsigned int len;
  301. if (k <= Kmax && (rv = freelist[k]))
  302. freelist[k] = rv->next;
  303. else {
  304. x = 1 << k;
  305. len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
  306. /sizeof(double);
  307. if (k <= Kmax && pmem_next - private_mem + len <= PRIVATE_mem) {
  308. rv = (Bigint*)pmem_next;
  309. pmem_next += len;
  310. }
  311. else {
  312. rv = (Bigint*)MALLOC(len*sizeof(double));
  313. if (rv == NULL)
  314. return NULL;
  315. }
  316. rv->k = k;
  317. rv->maxwds = x;
  318. }
  319. rv->sign = rv->wds = 0;
  320. return rv;
  321. }
  322. /* Free a Bigint allocated with Balloc */
  323. static void
  324. Bfree(Bigint *v)
  325. {
  326. if (v) {
  327. if (v->k > Kmax)
  328. FREE((void*)v);
  329. else {
  330. v->next = freelist[v->k];
  331. freelist[v->k] = v;
  332. }
  333. }
  334. }
  335. #else
  336. /* Alternative versions of Balloc and Bfree that use PyMem_Malloc and
  337. PyMem_Free directly in place of the custom memory allocation scheme above.
  338. These are provided for the benefit of memory debugging tools like
  339. Valgrind. */
  340. /* Allocate space for a Bigint with up to 1<<k digits */
  341. static Bigint *
  342. Balloc(int k)
  343. {
  344. int x;
  345. Bigint *rv;
  346. unsigned int len;
  347. x = 1 << k;
  348. len = (sizeof(Bigint) + (x-1)*sizeof(ULong) + sizeof(double) - 1)
  349. /sizeof(double);
  350. rv = (Bigint*)MALLOC(len*sizeof(double));
  351. if (rv == NULL)
  352. return NULL;
  353. rv->k = k;
  354. rv->maxwds = x;
  355. rv->sign = rv->wds = 0;
  356. return rv;
  357. }
  358. /* Free a Bigint allocated with Balloc */
  359. static void
  360. Bfree(Bigint *v)
  361. {
  362. if (v) {
  363. FREE((void*)v);
  364. }
  365. }
  366. #endif /* Py_USING_MEMORY_DEBUGGER */
  367. #define Bcopy(x,y) memcpy((char *)&x->sign, (char *)&y->sign, \
  368. y->wds*sizeof(Long) + 2*sizeof(int))
  369. /* Multiply a Bigint b by m and add a. Either modifies b in place and returns
  370. a pointer to the modified b, or Bfrees b and returns a pointer to a copy.
  371. On failure, return NULL. In this case, b will have been already freed. */
  372. static Bigint *
  373. multadd(Bigint *b, int m, int a) /* multiply by m and add a */
  374. {
  375. int i, wds;
  376. #ifdef ULLong
  377. ULong *x;
  378. ULLong carry, y;
  379. #else
  380. ULong carry, *x, y;
  381. ULong xi, z;
  382. #endif
  383. Bigint *b1;
  384. wds = b->wds;
  385. x = b->x;
  386. i = 0;
  387. carry = a;
  388. do {
  389. #ifdef ULLong
  390. y = *x * (ULLong)m + carry;
  391. carry = y >> 32;
  392. *x++ = (ULong)(y & FFFFFFFF);
  393. #else
  394. xi = *x;
  395. y = (xi & 0xffff) * m + carry;
  396. z = (xi >> 16) * m + (y >> 16);
  397. carry = z >> 16;
  398. *x++ = (z << 16) + (y & 0xffff);
  399. #endif
  400. }
  401. while(++i < wds);
  402. if (carry) {
  403. if (wds >= b->maxwds) {
  404. b1 = Balloc(b->k+1);
  405. if (b1 == NULL){
  406. Bfree(b);
  407. return NULL;
  408. }
  409. Bcopy(b1, b);
  410. Bfree(b);
  411. b = b1;
  412. }
  413. b->x[wds++] = (ULong)carry;
  414. b->wds = wds;
  415. }
  416. return b;
  417. }
  418. /* convert a string s containing nd decimal digits (possibly containing a
  419. decimal separator at position nd0, which is ignored) to a Bigint. This
  420. function carries on where the parsing code in _Py_dg_strtod leaves off: on
  421. entry, y9 contains the result of converting the first 9 digits. Returns
  422. NULL on failure. */
  423. static Bigint *
  424. s2b(const char *s, int nd0, int nd, ULong y9)
  425. {
  426. Bigint *b;
  427. int i, k;
  428. Long x, y;
  429. x = (nd + 8) / 9;
  430. for(k = 0, y = 1; x > y; y <<= 1, k++) ;
  431. b = Balloc(k);
  432. if (b == NULL)
  433. return NULL;
  434. b->x[0] = y9;
  435. b->wds = 1;
  436. if (nd <= 9)
  437. return b;
  438. s += 9;
  439. for (i = 9; i < nd0; i++) {
  440. b = multadd(b, 10, *s++ - '0');
  441. if (b == NULL)
  442. return NULL;
  443. }
  444. s++;
  445. for(; i < nd; i++) {
  446. b = multadd(b, 10, *s++ - '0');
  447. if (b == NULL)
  448. return NULL;
  449. }
  450. return b;
  451. }
  452. /* count leading 0 bits in the 32-bit integer x. */
  453. static int
  454. hi0bits(ULong x)
  455. {
  456. int k = 0;
  457. if (!(x & 0xffff0000)) {
  458. k = 16;
  459. x <<= 16;
  460. }
  461. if (!(x & 0xff000000)) {
  462. k += 8;
  463. x <<= 8;
  464. }
  465. if (!(x & 0xf0000000)) {
  466. k += 4;
  467. x <<= 4;
  468. }
  469. if (!(x & 0xc0000000)) {
  470. k += 2;
  471. x <<= 2;
  472. }
  473. if (!(x & 0x80000000)) {
  474. k++;
  475. if (!(x & 0x40000000))
  476. return 32;
  477. }
  478. return k;
  479. }
  480. /* count trailing 0 bits in the 32-bit integer y, and shift y right by that
  481. number of bits. */
  482. static int
  483. lo0bits(ULong *y)
  484. {
  485. int k;
  486. ULong x = *y;
  487. if (x & 7) {
  488. if (x & 1)
  489. return 0;
  490. if (x & 2) {
  491. *y = x >> 1;
  492. return 1;
  493. }
  494. *y = x >> 2;
  495. return 2;
  496. }
  497. k = 0;
  498. if (!(x & 0xffff)) {
  499. k = 16;
  500. x >>= 16;
  501. }
  502. if (!(x & 0xff)) {
  503. k += 8;
  504. x >>= 8;
  505. }
  506. if (!(x & 0xf)) {
  507. k += 4;
  508. x >>= 4;
  509. }
  510. if (!(x & 0x3)) {
  511. k += 2;
  512. x >>= 2;
  513. }
  514. if (!(x & 1)) {
  515. k++;
  516. x >>= 1;
  517. if (!x)
  518. return 32;
  519. }
  520. *y = x;
  521. return k;
  522. }
  523. /* convert a small nonnegative integer to a Bigint */
  524. static Bigint *
  525. i2b(int i)
  526. {
  527. Bigint *b;
  528. b = Balloc(1);
  529. if (b == NULL)
  530. return NULL;
  531. b->x[0] = i;
  532. b->wds = 1;
  533. return b;
  534. }
  535. /* multiply two Bigints. Returns a new Bigint, or NULL on failure. Ignores
  536. the signs of a and b. */
  537. static Bigint *
  538. mult(Bigint *a, Bigint *b)
  539. {
  540. Bigint *c;
  541. int k, wa, wb, wc;
  542. ULong *x, *xa, *xae, *xb, *xbe, *xc, *xc0;
  543. ULong y;
  544. #ifdef ULLong
  545. ULLong carry, z;
  546. #else
  547. ULong carry, z;
  548. ULong z2;
  549. #endif
  550. if ((!a->x[0] && a->wds == 1) || (!b->x[0] && b->wds == 1)) {
  551. c = Balloc(0);
  552. if (c == NULL)
  553. return NULL;
  554. c->wds = 1;
  555. c->x[0] = 0;
  556. return c;
  557. }
  558. if (a->wds < b->wds) {
  559. c = a;
  560. a = b;
  561. b = c;
  562. }
  563. k = a->k;
  564. wa = a->wds;
  565. wb = b->wds;
  566. wc = wa + wb;
  567. if (wc > a->maxwds)
  568. k++;
  569. c = Balloc(k);
  570. if (c == NULL)
  571. return NULL;
  572. for(x = c->x, xa = x + wc; x < xa; x++)
  573. *x = 0;
  574. xa = a->x;
  575. xae = xa + wa;
  576. xb = b->x;
  577. xbe = xb + wb;
  578. xc0 = c->x;
  579. #ifdef ULLong
  580. for(; xb < xbe; xc0++) {
  581. if ((y = *xb++)) {
  582. x = xa;
  583. xc = xc0;
  584. carry = 0;
  585. do {
  586. z = *x++ * (ULLong)y + *xc + carry;
  587. carry = z >> 32;
  588. *xc++ = (ULong)(z & FFFFFFFF);
  589. }
  590. while(x < xae);
  591. *xc = (ULong)carry;
  592. }
  593. }
  594. #else
  595. for(; xb < xbe; xb++, xc0++) {
  596. if (y = *xb & 0xffff) {
  597. x = xa;
  598. xc = xc0;
  599. carry = 0;
  600. do {
  601. z = (*x & 0xffff) * y + (*xc & 0xffff) + carry;
  602. carry = z >> 16;
  603. z2 = (*x++ >> 16) * y + (*xc >> 16) + carry;
  604. carry = z2 >> 16;
  605. Storeinc(xc, z2, z);
  606. }
  607. while(x < xae);
  608. *xc = carry;
  609. }
  610. if (y = *xb >> 16) {
  611. x = xa;
  612. xc = xc0;
  613. carry = 0;
  614. z2 = *xc;
  615. do {
  616. z = (*x & 0xffff) * y + (*xc >> 16) + carry;
  617. carry = z >> 16;
  618. Storeinc(xc, z, z2);
  619. z2 = (*x++ >> 16) * y + (*xc & 0xffff) + carry;
  620. carry = z2 >> 16;
  621. }
  622. while(x < xae);
  623. *xc = z2;
  624. }
  625. }
  626. #endif
  627. for(xc0 = c->x, xc = xc0 + wc; wc > 0 && !*--xc; --wc) ;
  628. c->wds = wc;
  629. return c;
  630. }
  631. #ifndef Py_USING_MEMORY_DEBUGGER
  632. /* p5s is a linked list of powers of 5 of the form 5**(2**i), i >= 2 */
  633. static Bigint *p5s;
  634. /* multiply the Bigint b by 5**k. Returns a pointer to the result, or NULL on
  635. failure; if the returned pointer is distinct from b then the original
  636. Bigint b will have been Bfree'd. Ignores the sign of b. */
  637. static Bigint *
  638. pow5mult(Bigint *b, int k)
  639. {
  640. Bigint *b1, *p5, *p51;
  641. int i;
  642. static int p05[3] = { 5, 25, 125 };
  643. if ((i = k & 3)) {
  644. b = multadd(b, p05[i-1], 0);
  645. if (b == NULL)
  646. return NULL;
  647. }
  648. if (!(k >>= 2))
  649. return b;
  650. p5 = p5s;
  651. if (!p5) {
  652. /* first time */
  653. p5 = i2b(625);
  654. if (p5 == NULL) {
  655. Bfree(b);
  656. return NULL;
  657. }
  658. p5s = p5;
  659. p5->next = 0;
  660. }
  661. for(;;) {
  662. if (k & 1) {
  663. b1 = mult(b, p5);
  664. Bfree(b);
  665. b = b1;
  666. if (b == NULL)
  667. return NULL;
  668. }
  669. if (!(k >>= 1))
  670. break;
  671. p51 = p5->next;
  672. if (!p51) {
  673. p51 = mult(p5,p5);
  674. if (p51 == NULL) {
  675. Bfree(b);
  676. return NULL;
  677. }
  678. p51->next = 0;
  679. p5->next = p51;
  680. }
  681. p5 = p51;
  682. }
  683. return b;
  684. }
  685. #else
  686. /* Version of pow5mult that doesn't cache powers of 5. Provided for
  687. the benefit of memory debugging tools like Valgrind. */
  688. static Bigint *
  689. pow5mult(Bigint *b, int k)
  690. {
  691. Bigint *b1, *p5, *p51;
  692. int i;
  693. static int p05[3] = { 5, 25, 125 };
  694. if ((i = k & 3)) {
  695. b = multadd(b, p05[i-1], 0);
  696. if (b == NULL)
  697. return NULL;
  698. }
  699. if (!(k >>= 2))
  700. return b;
  701. p5 = i2b(625);
  702. if (p5 == NULL) {
  703. Bfree(b);
  704. return NULL;
  705. }
  706. for(;;) {
  707. if (k & 1) {
  708. b1 = mult(b, p5);
  709. Bfree(b);
  710. b = b1;
  711. if (b == NULL) {
  712. Bfree(p5);
  713. return NULL;
  714. }
  715. }
  716. if (!(k >>= 1))
  717. break;
  718. p51 = mult(p5, p5);
  719. Bfree(p5);
  720. p5 = p51;
  721. if (p5 == NULL) {
  722. Bfree(b);
  723. return NULL;
  724. }
  725. }
  726. Bfree(p5);
  727. return b;
  728. }
  729. #endif /* Py_USING_MEMORY_DEBUGGER */
  730. /* shift a Bigint b left by k bits. Return a pointer to the shifted result,
  731. or NULL on failure. If the returned pointer is distinct from b then the
  732. original b will have been Bfree'd. Ignores the sign of b. */
  733. static Bigint *
  734. lshift(Bigint *b, int k)
  735. {
  736. int i, k1, n, n1;
  737. Bigint *b1;
  738. ULong *x, *x1, *xe, z;
  739. if (!k || (!b->x[0] && b->wds == 1))
  740. return b;
  741. n = k >> 5;
  742. k1 = b->k;
  743. n1 = n + b->wds + 1;
  744. for(i = b->maxwds; n1 > i; i <<= 1)
  745. k1++;
  746. b1 = Balloc(k1);
  747. if (b1 == NULL) {
  748. Bfree(b);
  749. return NULL;
  750. }
  751. x1 = b1->x;
  752. for(i = 0; i < n; i++)
  753. *x1++ = 0;
  754. x = b->x;
  755. xe = x + b->wds;
  756. if (k &= 0x1f) {
  757. k1 = 32 - k;
  758. z = 0;
  759. do {
  760. *x1++ = *x << k | z;
  761. z = *x++ >> k1;
  762. }
  763. while(x < xe);
  764. if ((*x1 = z))
  765. ++n1;
  766. }
  767. else do
  768. *x1++ = *x++;
  769. while(x < xe);
  770. b1->wds = n1 - 1;
  771. Bfree(b);
  772. return b1;
  773. }
  774. /* Do a three-way compare of a and b, returning -1 if a < b, 0 if a == b and
  775. 1 if a > b. Ignores signs of a and b. */
  776. static int
  777. cmp(Bigint *a, Bigint *b)
  778. {
  779. ULong *xa, *xa0, *xb, *xb0;
  780. int i, j;
  781. i = a->wds;
  782. j = b->wds;
  783. #ifdef DEBUG
  784. if (i > 1 && !a->x[i-1])
  785. Bug("cmp called with a->x[a->wds-1] == 0");
  786. if (j > 1 && !b->x[j-1])
  787. Bug("cmp called with b->x[b->wds-1] == 0");
  788. #endif
  789. if (i -= j)
  790. return i;
  791. xa0 = a->x;
  792. xa = xa0 + j;
  793. xb0 = b->x;
  794. xb = xb0 + j;
  795. for(;;) {
  796. if (*--xa != *--xb)
  797. return *xa < *xb ? -1 : 1;
  798. if (xa <= xa0)
  799. break;
  800. }
  801. return 0;
  802. }
  803. /* Take the difference of Bigints a and b, returning a new Bigint. Returns
  804. NULL on failure. The signs of a and b are ignored, but the sign of the
  805. result is set appropriately. */
  806. static Bigint *
  807. diff(Bigint *a, Bigint *b)
  808. {
  809. Bigint *c;
  810. int i, wa, wb;
  811. ULong *xa, *xae, *xb, *xbe, *xc;
  812. #ifdef ULLong
  813. ULLong borrow, y;
  814. #else
  815. ULong borrow, y;
  816. ULong z;
  817. #endif
  818. i = cmp(a,b);
  819. if (!i) {
  820. c = Balloc(0);
  821. if (c == NULL)
  822. return NULL;
  823. c->wds = 1;
  824. c->x[0] = 0;
  825. return c;
  826. }
  827. if (i < 0) {
  828. c = a;
  829. a = b;
  830. b = c;
  831. i = 1;
  832. }
  833. else
  834. i = 0;
  835. c = Balloc(a->k);
  836. if (c == NULL)
  837. return NULL;
  838. c->sign = i;
  839. wa = a->wds;
  840. xa = a->x;
  841. xae = xa + wa;
  842. wb = b->wds;
  843. xb = b->x;
  844. xbe = xb + wb;
  845. xc = c->x;
  846. borrow = 0;
  847. #ifdef ULLong
  848. do {
  849. y = (ULLong)*xa++ - *xb++ - borrow;
  850. borrow = y >> 32 & (ULong)1;
  851. *xc++ = (ULong)(y & FFFFFFFF);
  852. }
  853. while(xb < xbe);
  854. while(xa < xae) {
  855. y = *xa++ - borrow;
  856. borrow = y >> 32 & (ULong)1;
  857. *xc++ = (ULong)(y & FFFFFFFF);
  858. }
  859. #else
  860. do {
  861. y = (*xa & 0xffff) - (*xb & 0xffff) - borrow;
  862. borrow = (y & 0x10000) >> 16;
  863. z = (*xa++ >> 16) - (*xb++ >> 16) - borrow;
  864. borrow = (z & 0x10000) >> 16;
  865. Storeinc(xc, z, y);
  866. }
  867. while(xb < xbe);
  868. while(xa < xae) {
  869. y = (*xa & 0xffff) - borrow;
  870. borrow = (y & 0x10000) >> 16;
  871. z = (*xa++ >> 16) - borrow;
  872. borrow = (z & 0x10000) >> 16;
  873. Storeinc(xc, z, y);
  874. }
  875. #endif
  876. while(!*--xc)
  877. wa--;
  878. c->wds = wa;
  879. return c;
  880. }
  881. /* Given a positive normal double x, return the difference between x and the
  882. next double up. Doesn't give correct results for subnormals. */
  883. static double
  884. ulp(U *x)
  885. {
  886. Long L;
  887. U u;
  888. L = (word0(x) & Exp_mask) - (P-1)*Exp_msk1;
  889. word0(&u) = L;
  890. word1(&u) = 0;
  891. return dval(&u);
  892. }
  893. /* Convert a Bigint to a double plus an exponent */
  894. static double
  895. b2d(Bigint *a, int *e)
  896. {
  897. ULong *xa, *xa0, w, y, z;
  898. int k;
  899. U d;
  900. xa0 = a->x;
  901. xa = xa0 + a->wds;
  902. y = *--xa;
  903. #ifdef DEBUG
  904. if (!y) Bug("zero y in b2d");
  905. #endif
  906. k = hi0bits(y);
  907. *e = 32 - k;
  908. if (k < Ebits) {
  909. word0(&d) = Exp_1 | y >> (Ebits - k);
  910. w = xa > xa0 ? *--xa : 0;
  911. word1(&d) = y << ((32-Ebits) + k) | w >> (Ebits - k);
  912. goto ret_d;
  913. }
  914. z = xa > xa0 ? *--xa : 0;
  915. if (k -= Ebits) {
  916. word0(&d) = Exp_1 | y << k | z >> (32 - k);
  917. y = xa > xa0 ? *--xa : 0;
  918. word1(&d) = z << k | y >> (32 - k);
  919. }
  920. else {
  921. word0(&d) = Exp_1 | y;
  922. word1(&d) = z;
  923. }
  924. ret_d:
  925. return dval(&d);
  926. }
  927. /* Convert a scaled double to a Bigint plus an exponent. Similar to d2b,
  928. except that it accepts the scale parameter used in _Py_dg_strtod (which
  929. should be either 0 or 2*P), and the normalization for the return value is
  930. different (see below). On input, d should be finite and nonnegative, and d
  931. / 2**scale should be exactly representable as an IEEE 754 double.
  932. Returns a Bigint b and an integer e such that
  933. dval(d) / 2**scale = b * 2**e.
  934. Unlike d2b, b is not necessarily odd: b and e are normalized so
  935. that either 2**(P-1) <= b < 2**P and e >= Etiny, or b < 2**P
  936. and e == Etiny. This applies equally to an input of 0.0: in that
  937. case the return values are b = 0 and e = Etiny.
  938. The above normalization ensures that for all possible inputs d,
  939. 2**e gives ulp(d/2**scale).
  940. Returns NULL on failure.
  941. */
  942. static Bigint *
  943. sd2b(U *d, int scale, int *e)
  944. {
  945. Bigint *b;
  946. b = Balloc(1);
  947. if (b == NULL)
  948. return NULL;
  949. /* First construct b and e assuming that scale == 0. */
  950. b->wds = 2;
  951. b->x[0] = word1(d);
  952. b->x[1] = word0(d) & Frac_mask;
  953. *e = Etiny - 1 + (int)((word0(d) & Exp_mask) >> Exp_shift);
  954. if (*e < Etiny)
  955. *e = Etiny;
  956. else
  957. b->x[1] |= Exp_msk1;
  958. /* Now adjust for scale, provided that b != 0. */
  959. if (scale && (b->x[0] || b->x[1])) {
  960. *e -= scale;
  961. if (*e < Etiny) {
  962. scale = Etiny - *e;
  963. *e = Etiny;
  964. /* We can't shift more than P-1 bits without shifting out a 1. */
  965. assert(0 < scale && scale <= P - 1);
  966. if (scale >= 32) {
  967. /* The bits shifted out should all be zero. */
  968. assert(b->x[0] == 0);
  969. b->x[0] = b->x[1];
  970. b->x[1] = 0;
  971. scale -= 32;
  972. }
  973. if (scale) {
  974. /* The bits shifted out should all be zero. */
  975. assert(b->x[0] << (32 - scale) == 0);
  976. b->x[0] = (b->x[0] >> scale) | (b->x[1] << (32 - scale));
  977. b->x[1] >>= scale;
  978. }
  979. }
  980. }
  981. /* Ensure b is normalized. */
  982. if (!b->x[1])
  983. b->wds = 1;
  984. return b;
  985. }
  986. /* Convert a double to a Bigint plus an exponent. Return NULL on failure.
  987. Given a finite nonzero double d, return an odd Bigint b and exponent *e
  988. such that fabs(d) = b * 2**e. On return, *bbits gives the number of
  989. significant bits of b; that is, 2**(*bbits-1) <= b < 2**(*bbits).
  990. If d is zero, then b == 0, *e == -1010, *bbits = 0.
  991. */
  992. static Bigint *
  993. d2b(U *d, int *e, int *bits)
  994. {
  995. Bigint *b;
  996. int de, k;
  997. ULong *x, y, z;
  998. int i;
  999. b = Balloc(1);
  1000. if (b == NULL)
  1001. return NULL;
  1002. x = b->x;
  1003. z = word0(d) & Frac_mask;
  1004. word0(d) &= 0x7fffffff; /* clear sign bit, which we ignore */
  1005. if ((de = (int)(word0(d) >> Exp_shift)))
  1006. z |= Exp_msk1;
  1007. if ((y = word1(d))) {
  1008. if ((k = lo0bits(&y))) {
  1009. x[0] = y | z << (32 - k);
  1010. z >>= k;
  1011. }
  1012. else
  1013. x[0] = y;
  1014. i =
  1015. b->wds = (x[1] = z) ? 2 : 1;
  1016. }
  1017. else {
  1018. k = lo0bits(&z);
  1019. x[0] = z;
  1020. i =
  1021. b->wds = 1;
  1022. k += 32;
  1023. }
  1024. if (de) {
  1025. *e = de - Bias - (P-1) + k;
  1026. *bits = P - k;
  1027. }
  1028. else {
  1029. *e = de - Bias - (P-1) + 1 + k;
  1030. *bits = 32*i - hi0bits(x[i-1]);
  1031. }
  1032. return b;
  1033. }
  1034. /* Compute the ratio of two Bigints, as a double. The result may have an
  1035. error of up to 2.5 ulps. */
  1036. static double
  1037. ratio(Bigint *a, Bigint *b)
  1038. {
  1039. U da, db;
  1040. int k, ka, kb;
  1041. dval(&da) = b2d(a, &ka);
  1042. dval(&db) = b2d(b, &kb);
  1043. k = ka - kb + 32*(a->wds - b->wds);
  1044. if (k > 0)
  1045. word0(&da) += k*Exp_msk1;
  1046. else {
  1047. k = -k;
  1048. word0(&db) += k*Exp_msk1;
  1049. }
  1050. return dval(&da) / dval(&db);
  1051. }
  1052. static const double
  1053. tens[] = {
  1054. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9,
  1055. 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19,
  1056. 1e20, 1e21, 1e22
  1057. };
  1058. static const double
  1059. bigtens[] = { 1e16, 1e32, 1e64, 1e128, 1e256 };
  1060. static const double tinytens[] = { 1e-16, 1e-32, 1e-64, 1e-128,
  1061. 9007199254740992.*9007199254740992.e-256
  1062. /* = 2^106 * 1e-256 */
  1063. };
  1064. /* The factor of 2^53 in tinytens[4] helps us avoid setting the underflow */
  1065. /* flag unnecessarily. It leads to a song and dance at the end of strtod. */
  1066. #define Scale_Bit 0x10
  1067. #define n_bigtens 5
  1068. #define ULbits 32
  1069. #define kshift 5
  1070. #define kmask 31
  1071. static int
  1072. dshift(Bigint *b, int p2)
  1073. {
  1074. int rv = hi0bits(b->x[b->wds-1]) - 4;
  1075. if (p2 > 0)
  1076. rv -= p2;
  1077. return rv & kmask;
  1078. }
  1079. /* special case of Bigint division. The quotient is always in the range 0 <=
  1080. quotient < 10, and on entry the divisor S is normalized so that its top 4
  1081. bits (28--31) are zero and bit 27 is set. */
  1082. static int
  1083. quorem(Bigint *b, Bigint *S)
  1084. {
  1085. int n;
  1086. ULong *bx, *bxe, q, *sx, *sxe;
  1087. #ifdef ULLong
  1088. ULLong borrow, carry, y, ys;
  1089. #else
  1090. ULong borrow, carry, y, ys;
  1091. ULong si, z, zs;
  1092. #endif
  1093. n = S->wds;
  1094. #ifdef DEBUG
  1095. /*debug*/ if (b->wds > n)
  1096. /*debug*/ Bug("oversize b in quorem");
  1097. #endif
  1098. if (b->wds < n)
  1099. return 0;
  1100. sx = S->x;
  1101. sxe = sx + --n;
  1102. bx = b->x;
  1103. bxe = bx + n;
  1104. q = *bxe / (*sxe + 1); /* ensure q <= true quotient */
  1105. #ifdef DEBUG
  1106. /*debug*/ if (q > 9)
  1107. /*debug*/ Bug("oversized quotient in quorem");
  1108. #endif
  1109. if (q) {
  1110. borrow = 0;
  1111. carry = 0;
  1112. do {
  1113. #ifdef ULLong
  1114. ys = *sx++ * (ULLong)q + carry;
  1115. carry = ys >> 32;
  1116. y = *bx - (ys & FFFFFFFF) - borrow;
  1117. borrow = y >> 32 & (ULong)1;
  1118. *bx++ = (ULong)(y & FFFFFFFF);
  1119. #else
  1120. si = *sx++;
  1121. ys = (si & 0xffff) * q + carry;
  1122. zs = (si >> 16) * q + (ys >> 16);
  1123. carry = zs >> 16;
  1124. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  1125. borrow = (y & 0x10000) >> 16;
  1126. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  1127. borrow = (z & 0x10000) >> 16;
  1128. Storeinc(bx, z, y);
  1129. #endif
  1130. }
  1131. while(sx <= sxe);
  1132. if (!*bxe) {
  1133. bx = b->x;
  1134. while(--bxe > bx && !*bxe)
  1135. --n;
  1136. b->wds = n;
  1137. }
  1138. }
  1139. if (cmp(b, S) >= 0) {
  1140. q++;
  1141. borrow = 0;
  1142. carry = 0;
  1143. bx = b->x;
  1144. sx = S->x;
  1145. do {
  1146. #ifdef ULLong
  1147. ys = *sx++ + carry;
  1148. carry = ys >> 32;
  1149. y = *bx - (ys & FFFFFFFF) - borrow;
  1150. borrow = y >> 32 & (ULong)1;
  1151. *bx++ = (ULong)(y & FFFFFFFF);
  1152. #else
  1153. si = *sx++;
  1154. ys = (si & 0xffff) + carry;
  1155. zs = (si >> 16) + (ys >> 16);
  1156. carry = zs >> 16;
  1157. y = (*bx & 0xffff) - (ys & 0xffff) - borrow;
  1158. borrow = (y & 0x10000) >> 16;
  1159. z = (*bx >> 16) - (zs & 0xffff) - borrow;
  1160. borrow = (z & 0x10000) >> 16;
  1161. Storeinc(bx, z, y);
  1162. #endif
  1163. }
  1164. while(sx <= sxe);
  1165. bx = b->x;
  1166. bxe = bx + n;
  1167. if (!*bxe) {
  1168. while(--bxe > bx && !*bxe)
  1169. --n;
  1170. b->wds = n;
  1171. }
  1172. }
  1173. return q;
  1174. }
  1175. /* sulp(x) is a version of ulp(x) that takes bc.scale into account.
  1176. Assuming that x is finite and nonnegative (positive zero is fine
  1177. here) and x / 2^bc.scale is exactly representable as a double,
  1178. sulp(x) is equivalent to 2^bc.scale * ulp(x / 2^bc.scale). */
  1179. static double
  1180. sulp(U *x, BCinfo *bc)
  1181. {
  1182. U u;
  1183. if (bc->scale && 2*P + 1 > (int)((word0(x) & Exp_mask) >> Exp_shift)) {
  1184. /* rv/2^bc->scale is subnormal */
  1185. word0(&u) = (P+2)*Exp_msk1;
  1186. word1(&u) = 0;
  1187. return u.d;
  1188. }
  1189. else {
  1190. assert(word0(x) || word1(x)); /* x != 0.0 */
  1191. return ulp(x);
  1192. }
  1193. }
  1194. /* The bigcomp function handles some hard cases for strtod, for inputs
  1195. with more than STRTOD_DIGLIM digits. It's called once an initial
  1196. estimate for the double corresponding to the input string has
  1197. already been obtained by the code in _Py_dg_strtod.
  1198. The bigcomp function is only called after _Py_dg_strtod has found a
  1199. double value rv such that either rv or rv + 1ulp represents the
  1200. correctly rounded value corresponding to the original string. It
  1201. determines which of these two values is the correct one by
  1202. computing the decimal digits of rv + 0.5ulp and comparing them with
  1203. the corresponding digits of s0.
  1204. In the following, write dv for the absolute value of the number represented
  1205. by the input string.
  1206. Inputs:
  1207. s0 points to the first significant digit of the input string.
  1208. rv is a (possibly scaled) estimate for the closest double value to the
  1209. value represented by the original input to _Py_dg_strtod. If
  1210. bc->scale is nonzero, then rv/2^(bc->scale) is the approximation to
  1211. the input value.
  1212. bc is a struct containing information gathered during the parsing and
  1213. estimation steps of _Py_dg_strtod. Description of fields follows:
  1214. bc->e0 gives the exponent of the input value, such that dv = (integer
  1215. given by the bd->nd digits of s0) * 10**e0
  1216. bc->nd gives the total number of significant digits of s0. It will
  1217. be at least 1.
  1218. bc->nd0 gives the number of significant digits of s0 before the
  1219. decimal separator. If there's no decimal separator, bc->nd0 ==
  1220. bc->nd.
  1221. bc->scale is the value used to scale rv to avoid doing arithmetic with
  1222. subnormal values. It's either 0 or 2*P (=106).
  1223. Outputs:
  1224. On successful exit, rv/2^(bc->scale) is the closest double to dv.
  1225. Returns 0 on success, -1 on failure (e.g., due to a failed malloc call). */
  1226. static int
  1227. bigcomp(U *rv, const char *s0, BCinfo *bc)
  1228. {
  1229. Bigint *b, *d;
  1230. int b2, d2, dd, i, nd, nd0, odd, p2, p5;
  1231. nd = bc->nd;
  1232. nd0 = bc->nd0;
  1233. p5 = nd + bc->e0;
  1234. b = sd2b(rv, bc->scale, &p2);
  1235. if (b == NULL)
  1236. return -1;
  1237. /* record whether the lsb of rv/2^(bc->scale) is odd: in the exact halfway
  1238. case, this is used for round to even. */
  1239. odd = b->x[0] & 1;
  1240. /* left shift b by 1 bit and or a 1 into the least significant bit;
  1241. this gives us b * 2**p2 = rv/2^(bc->scale) + 0.5 ulp. */
  1242. b = lshift(b, 1);
  1243. if (b == NULL)
  1244. return -1;
  1245. b->x[0] |= 1;
  1246. p2--;
  1247. p2 -= p5;
  1248. d = i2b(1);
  1249. if (d == NULL) {
  1250. Bfree(b);
  1251. return -1;
  1252. }
  1253. /* Arrange for convenient computation of quotients:
  1254. * shift left if necessary so divisor has 4 leading 0 bits.
  1255. */
  1256. if (p5 > 0) {
  1257. d = pow5mult(d, p5);
  1258. if (d == NULL) {
  1259. Bfree(b);
  1260. return -1;
  1261. }
  1262. }
  1263. else if (p5 < 0) {
  1264. b = pow5mult(b, -p5);
  1265. if (b == NULL) {
  1266. Bfree(d);
  1267. return -1;
  1268. }
  1269. }
  1270. if (p2 > 0) {
  1271. b2 = p2;
  1272. d2 = 0;
  1273. }
  1274. else {
  1275. b2 = 0;
  1276. d2 = -p2;
  1277. }
  1278. i = dshift(d, d2);
  1279. if ((b2 += i) > 0) {
  1280. b = lshift(b, b2);
  1281. if (b == NULL) {
  1282. Bfree(d);
  1283. return -1;
  1284. }
  1285. }
  1286. if ((d2 += i) > 0) {
  1287. d = lshift(d, d2);
  1288. if (d == NULL) {
  1289. Bfree(b);
  1290. return -1;
  1291. }
  1292. }
  1293. /* Compare s0 with b/d: set dd to -1, 0, or 1 according as s0 < b/d, s0 ==
  1294. * b/d, or s0 > b/d. Here the digits of s0 are thought of as representing
  1295. * a number in the range [0.1, 1). */
  1296. if (cmp(b, d) >= 0)
  1297. /* b/d >= 1 */
  1298. dd = -1;
  1299. else {
  1300. i = 0;
  1301. for(;;) {
  1302. b = multadd(b, 10, 0);
  1303. if (b == NULL) {
  1304. Bfree(d);
  1305. return -1;
  1306. }
  1307. dd = s0[i < nd0 ? i : i+1] - '0' - quorem(b, d);
  1308. i++;
  1309. if (dd)
  1310. break;
  1311. if (!b->x[0] && b->wds == 1) {
  1312. /* b/d == 0 */
  1313. dd = i < nd;
  1314. break;
  1315. }
  1316. if (!(i < nd)) {
  1317. /* b/d != 0, but digits of s0 exhausted */
  1318. dd = -1;
  1319. break;
  1320. }
  1321. }
  1322. }
  1323. Bfree(b);
  1324. Bfree(d);
  1325. if (dd > 0 || (dd == 0 && odd))
  1326. dval(rv) += sulp(rv, bc);
  1327. return 0;
  1328. }
  1329. /* Return a 'standard' NaN value.
  1330. There are exactly two quiet NaNs that don't arise by 'quieting' signaling
  1331. NaNs (see IEEE 754-2008, section 6.2.1). If sign == 0, return the one whose
  1332. sign bit is cleared. Otherwise, return the one whose sign bit is set.
  1333. */
  1334. double
  1335. _Py_dg_stdnan(int sign)
  1336. {
  1337. U rv;
  1338. word0(&rv) = NAN_WORD0;
  1339. word1(&rv) = NAN_WORD1;
  1340. if (sign)
  1341. word0(&rv) |= Sign_bit;
  1342. return dval(&rv);
  1343. }
  1344. /* Return positive or negative infinity, according to the given sign (0 for
  1345. * positive infinity, 1 for negative infinity). */
  1346. double
  1347. _Py_dg_infinity(int sign)
  1348. {
  1349. U rv;
  1350. word0(&rv) = POSINF_WORD0;
  1351. word1(&rv) = POSINF_WORD1;
  1352. return sign ? -dval(&rv) : dval(&rv);
  1353. }
  1354. double
  1355. _Py_dg_strtod(const char *s00, char **se)
  1356. {
  1357. int bb2, bb5, bbe, bd2, bd5, bs2, c, dsign, e, e1, error;
  1358. int esign, i, j, k, lz, nd, nd0, odd, sign;
  1359. const char *s, *s0, *s1;
  1360. double aadj, aadj1;
  1361. U aadj2, adj, rv, rv0;
  1362. ULong y, z, abs_exp;
  1363. Long L;
  1364. BCinfo bc;
  1365. Bigint *bb, *bb1, *bd, *bd0, *bs, *delta;
  1366. dval(&rv) = 0.;
  1367. /* Start parsing. */
  1368. c = *(s = s00);
  1369. /* Parse optional sign, if present. */
  1370. sign = 0;
  1371. switch (c) {
  1372. case '-':
  1373. sign = 1;
  1374. /* no break */
  1375. case '+':
  1376. c = *++s;
  1377. }
  1378. /* Skip leading zeros: lz is true iff there were leading zeros. */
  1379. s1 = s;
  1380. while (c == '0')
  1381. c = *++s;
  1382. lz = s != s1;
  1383. /* Point s0 at the first nonzero digit (if any). nd0 will be the position
  1384. of the point relative to s0. nd will be the total number of digits
  1385. ignoring leading zeros. */
  1386. s0 = s1 = s;
  1387. while ('0' <= c && c <= '9')
  1388. c = *++s;
  1389. nd0 = nd = s - s1;
  1390. /* Parse decimal point and following digits. */
  1391. if (c == '.') {
  1392. c = *++s;
  1393. if (!nd) {
  1394. s1 = s;
  1395. while (c == '0')
  1396. c = *++s;
  1397. lz = lz || s != s1;
  1398. nd0 -= s - s1;
  1399. s0 = s;
  1400. }
  1401. s1 = s;
  1402. while ('0' <= c && c <= '9')
  1403. c = *++s;
  1404. nd += s - s1;
  1405. }
  1406. /* Now lz is true if and only if there were leading zero digits, and nd
  1407. gives the total number of digits ignoring leading zeros. A valid input
  1408. must have at least one digit. */
  1409. if (!nd && !lz) {
  1410. if (se)
  1411. *se = (char *)s00;
  1412. goto parse_error;
  1413. }
  1414. /* Parse exponent. */
  1415. e = 0;
  1416. if (c == 'e' || c == 'E') {
  1417. s00 = s;
  1418. c = *++s;
  1419. /* Exponent sign. */
  1420. esign = 0;
  1421. switch (c) {
  1422. case '-':
  1423. esign = 1;
  1424. /* no break */
  1425. case '+':
  1426. c = *++s;
  1427. }
  1428. /* Skip zeros. lz is true iff there are leading zeros. */
  1429. s1 = s;
  1430. while (c == '0')
  1431. c = *++s;
  1432. lz = s != s1;
  1433. /* Get absolute value of the exponent. */
  1434. s1 = s;
  1435. abs_exp = 0;
  1436. while ('0' <= c && c <= '9') {
  1437. abs_exp = 10*abs_exp + (c - '0');
  1438. c = *++s;
  1439. }
  1440. /* abs_exp will be correct modulo 2**32. But 10**9 < 2**32, so if
  1441. there are at most 9 significant exponent digits then overflow is
  1442. impossible. */
  1443. if (s - s1 > 9 || abs_exp > MAX_ABS_EXP)
  1444. e = (int)MAX_ABS_EXP;
  1445. else
  1446. e = (int)abs_exp;
  1447. if (esign)
  1448. e = -e;
  1449. /* A valid exponent must have at least one digit. */
  1450. if (s == s1 && !lz)
  1451. s = s00;
  1452. }
  1453. /* Adjust exponent to take into account position of the point. */
  1454. e -= nd - nd0;
  1455. if (nd0 <= 0)
  1456. nd0 = nd;
  1457. /* Finished parsing. Set se to indicate how far we parsed */
  1458. if (se)
  1459. *se = (char *)s;
  1460. /* If all digits were zero, exit with return value +-0.0. Otherwise,
  1461. strip trailing zeros: scan back until we hit a nonzero digit. */
  1462. if (!nd)
  1463. goto ret;
  1464. for (i = nd; i > 0; ) {
  1465. --i;
  1466. if (s0[i < nd0 ? i : i+1] != '0') {
  1467. ++i;
  1468. break;
  1469. }
  1470. }
  1471. e += nd - i;
  1472. nd = i;
  1473. if (nd0 > nd)
  1474. nd0 = nd;
  1475. /* Summary of parsing results. After parsing, and dealing with zero
  1476. * inputs, we have values s0, nd0, nd, e, sign, where:
  1477. *
  1478. * - s0 points to the first significant digit of the input string
  1479. *
  1480. * - nd is the total number of significant digits (here, and
  1481. * below, 'significant digits' means the set of digits of the
  1482. * significand of the input that remain after ignoring leading
  1483. * and trailing zeros).
  1484. *
  1485. * - nd0 indicates the position of the decimal point, if present; it
  1486. * satisfies 1 <= nd0 <= nd. The nd significant digits are in
  1487. * s0[0:nd0] and s0[nd0+1:nd+1] using the usual Python half-open slice
  1488. * notation. (If nd0 < nd, then s0[nd0] contains a '.' character; if
  1489. * nd0 == nd, then s0[nd0] could be any non-digit character.)
  1490. *
  1491. * - e is the adjusted exponent: the absolute value of the number
  1492. * represented by the original input string is n * 10**e, where
  1493. * n is the integer represented by the concatenation of
  1494. * s0[0:nd0] and s0[nd0+1:nd+1]
  1495. *
  1496. * - sign gives the sign of the input: 1 for negative, 0 for positive
  1497. *
  1498. * - the first and last significant digits are nonzero
  1499. */
  1500. /* put first DBL_DIG+1 digits into integer y and z.
  1501. *
  1502. * - y contains the value represented by the first min(9, nd)
  1503. * significant digits
  1504. *
  1505. * - if nd > 9, z contains the value represented by significant digits
  1506. * with indices in [9, min(16, nd)). So y * 10**(min(16, nd) - 9) + z
  1507. * gives the value represented by the first min(16, nd) sig. digits.
  1508. */
  1509. bc.e0 = e1 = e;
  1510. y = z = 0;
  1511. for (i = 0; i < nd; i++) {
  1512. if (i < 9)
  1513. y = 10*y + s0[i < nd0 ? i : i+1] - '0';
  1514. else if (i < DBL_DIG+1)
  1515. z = 10*z + s0[i < nd0 ? i : i+1] - '0';
  1516. else
  1517. break;
  1518. }
  1519. k = nd < DBL_DIG + 1 ? nd : DBL_DIG + 1;
  1520. dval(&rv) = y;
  1521. if (k > 9) {
  1522. dval(&rv) = tens[k - 9] * dval(&rv) + z;
  1523. }
  1524. bd0 = 0;
  1525. if (nd <= DBL_DIG
  1526. && Flt_Rounds == 1
  1527. ) {
  1528. if (!e)
  1529. goto ret;
  1530. if (e > 0) {
  1531. if (e <= Ten_pmax) {
  1532. dval(&rv) *= tens[e];
  1533. goto ret;
  1534. }
  1535. i = DBL_DIG - nd;
  1536. if (e <= Ten_pmax + i) {
  1537. /* A fancier test would sometimes let us do
  1538. * this for larger i values.
  1539. */
  1540. e -= i;
  1541. dval(&rv) *= tens[i];
  1542. dval(&rv) *= tens[e];
  1543. goto ret;
  1544. }
  1545. }
  1546. else if (e >= -Ten_pmax) {
  1547. dval(&rv) /= tens[-e];
  1548. goto ret;
  1549. }
  1550. }
  1551. e1 += nd - k;
  1552. bc.scale = 0;
  1553. /* Get starting approximation = rv * 10**e1 */
  1554. if (e1 > 0) {
  1555. if ((i = e1 & 15))
  1556. dval(&rv) *= tens[i];
  1557. if (e1 &= ~15) {
  1558. if (e1 > DBL_MAX_10_EXP)
  1559. goto ovfl;
  1560. e1 >>= 4;
  1561. for(j = 0; e1 > 1; j++, e1 >>= 1)
  1562. if (e1 & 1)
  1563. dval(&rv) *= bigtens[j];
  1564. /* The last multiplication could overflow. */
  1565. word0(&rv) -= P*Exp_msk1;
  1566. dval(&rv) *= bigtens[j];
  1567. if ((z = word0(&rv) & Exp_mask)
  1568. > Exp_msk1*(DBL_MAX_EXP+Bias-P))
  1569. goto ovfl;
  1570. if (z > Exp_msk1*(DBL_MAX_EXP+Bias-1-P)) {
  1571. /* set to largest number */
  1572. /* (Can't trust DBL_MAX) */
  1573. word0(&rv) = Big0;
  1574. word1(&rv) = Big1;
  1575. }
  1576. else
  1577. word0(&rv) += P*Exp_msk1;
  1578. }
  1579. }
  1580. else if (e1 < 0) {
  1581. /* The input decimal value lies in [10**e1, 10**(e1+16)).
  1582. If e1 <= -512, underflow immediately.
  1583. If e1 <= -256, set bc.scale to 2*P.
  1584. So for input value < 1e-256, bc.scale is always set;
  1585. for input value >= 1e-240, bc.scale is never set.
  1586. For input values in [1e-256, 1e-240), bc.scale may or may
  1587. not be set. */
  1588. e1 = -e1;
  1589. if ((i = e1 & 15))
  1590. dval(&rv) /= tens[i];
  1591. if (e1 >>= 4) {
  1592. if (e1 >= 1 << n_bigtens)
  1593. goto undfl;
  1594. if (e1 & Scale_Bit)
  1595. bc.scale = 2*P;
  1596. for(j = 0; e1 > 0; j++, e1 >>= 1)
  1597. if (e1 & 1)
  1598. dval(&rv) *= tinytens[j];
  1599. if (bc.scale && (j = 2*P + 1 - ((word0(&rv) & Exp_mask)
  1600. >> Exp_shift)) > 0) {
  1601. /* scaled rv is denormal; clear j low bits */
  1602. if (j >= 32) {
  1603. word1(&rv) = 0;
  1604. if (j >= 53)
  1605. word0(&rv) = (P+2)*Exp_msk1;
  1606. else
  1607. word0(&rv) &= 0xffffffff << (j-32);
  1608. }
  1609. else
  1610. word1(&rv) &= 0xffffffff << j;
  1611. }
  1612. if (!dval(&rv))
  1613. goto undfl;
  1614. }
  1615. }
  1616. /* Now the hard part -- adjusting rv to the correct value.*/
  1617. /* Put digits into bd: true value = bd * 10^e */
  1618. bc.nd = nd;
  1619. bc.nd0 = nd0; /* Only needed if nd > STRTOD_DIGLIM, but done here */
  1620. /* to silence an erroneous warning about bc.nd0 */
  1621. /* possibly not being initialized. */
  1622. if (nd > STRTOD_DIGLIM) {
  1623. /* ASSERT(STRTOD_DIGLIM >= 18); 18 == one more than the */
  1624. /* minimum number of decimal digits to distinguish double values */
  1625. /* in IEEE arithmetic. */
  1626. /* Truncate input to 18 significant digits, then discard any trailing
  1627. zeros on the result by updating nd, nd0, e and y suitably. (There's
  1628. no need to update z; it's not reused beyond this point.) */
  1629. for (i = 18; i > 0; ) {
  1630. /* scan back until we hit a nonzero digit. significant digit 'i'
  1631. is s0[i] if i < nd0, s0[i+1] if i >= nd0. */
  1632. --i;
  1633. if (s0[i < nd0 ? i : i+1] != '0') {
  1634. ++i;
  1635. break;
  1636. }
  1637. }
  1638. e += nd - i;
  1639. nd = i;
  1640. if (nd0 > nd)
  1641. nd0 = nd;
  1642. if (nd < 9) { /* must recompute y */
  1643. y = 0;
  1644. for(i = 0; i < nd0; ++i)
  1645. y = 10*y + s0[i] - '0';
  1646. for(; i < nd; ++i)
  1647. y = 10*y + s0[i+1] - '0';
  1648. }
  1649. }
  1650. bd0 = s2b(s0, nd0, nd, y);
  1651. if (bd0 == NULL)
  1652. goto failed_malloc;
  1653. /* Notation for the comments below. Write:
  1654. - dv for the absolute value of the number represented by the original
  1655. decimal input string.
  1656. - if we've truncated dv, write tdv for the truncated value.
  1657. Otherwise, set tdv == dv.
  1658. - srv for the quantity rv/2^bc.scale; so srv is the current binary
  1659. approximation to tdv (and dv). It should be exactly representable
  1660. in an IEEE 754 double.
  1661. */
  1662. for(;;) {
  1663. /* This is the main correction loop for _Py_dg_strtod.
  1664. We've got a decimal value tdv, and a floating-point approximation
  1665. srv=rv/2^bc.scale to tdv. The aim is to determine whether srv is
  1666. close enough (i.e., within 0.5 ulps) to tdv, and to compute a new
  1667. approximation if not.
  1668. To determine whether srv is close enough to tdv, compute integers
  1669. bd, bb and bs proportional to tdv, srv and 0.5 ulp(srv)
  1670. respectively, and then use integer arithmetic to determine whether
  1671. |tdv - srv| is less than, equal to, or greater than 0.5 ulp(srv).
  1672. */
  1673. bd = Balloc(bd0->k);
  1674. if (bd == NULL) {
  1675. Bfree(bd0);
  1676. goto failed_malloc;
  1677. }
  1678. Bcopy(bd, bd0);
  1679. bb = sd2b(&rv, bc.scale, &bbe); /* srv = bb * 2^bbe */
  1680. if (bb == NULL) {
  1681. Bfree(bd);
  1682. Bfree(bd0);
  1683. goto failed_malloc;
  1684. }
  1685. /* Record whether lsb of bb is odd, in case we need this
  1686. for the round-to-even step later. */
  1687. odd = bb->x[0] & 1;
  1688. /* tdv = bd * 10**e; srv = bb * 2**bbe */
  1689. bs = i2b(1);
  1690. if (bs == NULL) {
  1691. Bfree(bb);
  1692. Bfree(bd);
  1693. Bfree(bd0);
  1694. goto failed_malloc;
  1695. }
  1696. if (e >= 0) {
  1697. bb2 = bb5 = 0;
  1698. bd2 = bd5 = e;
  1699. }
  1700. else {
  1701. bb2 = bb5 = -e;
  1702. bd2 = bd5 = 0;
  1703. }
  1704. if (bbe >= 0)
  1705. bb2 += bbe;
  1706. else
  1707. bd2 -= bbe;
  1708. bs2 = bb2;
  1709. bb2++;
  1710. bd2++;
  1711. /* At this stage bd5 - bb5 == e == bd2 - bb2 + bbe, bb2 - bs2 == 1,
  1712. and bs == 1, so:
  1713. tdv == bd * 10**e = bd * 2**(bbe - bb2 + bd2) * 5**(bd5 - bb5)
  1714. srv == bb * 2**bbe = bb * 2**(bbe - bb2 + bb2)
  1715. 0.5 ulp(srv) == 2**(bbe-1) = bs * 2**(bbe - bb2 + bs2)
  1716. It follows that:
  1717. M * tdv = bd * 2**bd2 * 5**bd5
  1718. M * srv = bb * 2**bb2 * 5**bb5
  1719. M * 0.5 ulp(srv) = bs * 2**bs2 * 5**bb5
  1720. for some constant M. (Actually, M == 2**(bb2 - bbe) * 5**bb5, but
  1721. this fact is not needed below.)
  1722. */
  1723. /* Remove factor of 2**i, where i = min(bb2, bd2, bs2). */
  1724. i = bb2 < bd2 ? bb2 : bd2;
  1725. if (i > bs2)
  1726. i = bs2;
  1727. if (i > 0) {
  1728. bb2 -= i;
  1729. bd2 -= i;
  1730. bs2 -= i;
  1731. }
  1732. /* Scale bb, bd, bs by the appropriate powers of 2 and 5. */
  1733. if (bb5 > 0) {
  1734. bs = pow5mult(bs, bb5);
  1735. if (bs == NULL) {
  1736. Bfree(bb);
  1737. Bfree(bd);
  1738. Bfree(bd0);
  1739. goto failed_malloc;
  1740. }
  1741. bb1 = mult(bs, bb);
  1742. Bfree(bb);
  1743. bb = bb1;
  1744. if (bb == NULL) {
  1745. Bfree(bs);
  1746. Bfree(bd);
  1747. Bfree(bd0);
  1748. goto failed_malloc;
  1749. }
  1750. }
  1751. if (bb2 > 0) {
  1752. bb = lshift(bb, bb2);
  1753. if (bb == NULL) {
  1754. Bfree(bs);
  1755. Bfree(bd);
  1756. Bfree(bd0);
  1757. goto failed_malloc;
  1758. }
  1759. }
  1760. if (bd5 > 0) {
  1761. bd = pow5mult(bd, bd5);
  1762. if (bd == NULL) {
  1763. Bfree(bb);
  1764. Bfree(bs);
  1765. Bfree(bd0);
  1766. goto failed_malloc;
  1767. }
  1768. }
  1769. if (bd2 > 0) {
  1770. bd = lshift(bd, bd2);
  1771. if (bd == NULL) {
  1772. Bfree(bb);
  1773. Bfree(bs);
  1774. Bfree(bd0);
  1775. goto failed_malloc;
  1776. }
  1777. }
  1778. if (bs2 > 0) {
  1779. bs = lshift(bs, bs2);
  1780. if (bs == NULL) {
  1781. Bfree(bb);
  1782. Bfree(bd);
  1783. Bfree(bd0);
  1784. goto failed_malloc;
  1785. }
  1786. }
  1787. /* Now bd, bb and bs are scaled versions of tdv, srv and 0.5 ulp(srv),
  1788. respectively. Compute the difference |tdv - srv|, and compare
  1789. with 0.5 ulp(srv). */
  1790. delta = diff(bb, bd);
  1791. if (delta == NULL) {
  1792. Bfree(bb);
  1793. Bfree(bs);
  1794. Bfree(bd);
  1795. Bfree(bd0);
  1796. goto failed_malloc;
  1797. }
  1798. dsign = delta->sign;
  1799. delta->sign = 0;
  1800. i = cmp(delta, bs);
  1801. if (bc.nd > nd && i <= 0) {
  1802. if (dsign)
  1803. break; /* Must use bigcomp(). */
  1804. /* Here rv overestimates the truncated decimal value by at most
  1805. 0.5 ulp(rv). Hence rv either overestimates the true decimal
  1806. value by <= 0.5 ulp(rv), or underestimates it by some small
  1807. amount (< 0.1 ulp(rv)); either way, rv is within 0.5 ulps of
  1808. the true decimal value, so it's possible to exit.
  1809. Exception: if scaled rv is a normal exact power of 2, but not
  1810. DBL_MIN, then rv - 0.5 ulp(rv) takes us all the way down to the
  1811. next double, so the correctly rounded result is either rv - 0.5
  1812. ulp(rv) or rv; in this case, use bigcomp to distinguish. */
  1813. if (!word1(&rv) && !(word0(&rv) & Bndry_mask)) {
  1814. /* rv can't be 0, since it's an overestimate for some
  1815. nonzero value. So rv is a normal power of 2. */
  1816. j = (int)(word0(&rv) & Exp_mask) >> Exp_shift;
  1817. /* rv / 2^bc.scale = 2^(j - 1023 - bc.scale); use bigcomp if
  1818. rv / 2^bc.scale >= 2^-1021. */
  1819. if (j - bc.scale >= 2) {
  1820. dval(&rv) -= 0.5 * sulp(&rv, &bc);
  1821. break; /* Use bigcomp. */
  1822. }
  1823. }
  1824. {
  1825. bc.nd = nd;
  1826. i = -1; /* Discarded digits make delta smaller. */
  1827. }
  1828. }
  1829. if (i < 0) {
  1830. /* Error is less than half an ulp -- check for
  1831. * special case of mantissa a power of two.
  1832. */
  1833. if (dsign || word1(&rv) || word0(&rv) & Bndry_mask
  1834. || (word0(&rv) & Exp_mask) <= (2*P+1)*Exp_msk1
  1835. ) {
  1836. break;
  1837. }
  1838. if (!delta->x[0] && delta->wds <= 1) {
  1839. /* exact result */
  1840. break;
  1841. }
  1842. delta = lshift(delta,Log2P);
  1843. if (delta == NULL) {
  1844. Bfree(bb);
  1845. Bfree(bs);
  1846. Bfree(bd);
  1847. Bfree(bd0);
  1848. goto failed_malloc;
  1849. }
  1850. if (cmp(delta, bs) > 0)
  1851. goto drop_down;
  1852. break;
  1853. }
  1854. if (i == 0) {
  1855. /* exactly half-way between */
  1856. if (dsign) {
  1857. if ((word0(&rv) & Bndry_mask1) == Bndry_mask1
  1858. && word1(&rv) == (
  1859. (bc.scale &&
  1860. (y = word0(&rv) & Exp_mask) <= 2*P*Exp_msk1) ?
  1861. (0xffffffff & (0xffffffff << (2*P+1-(y>>Exp_shift)))) :
  1862. 0xffffffff)) {
  1863. /*boundary case -- increment exponent*/
  1864. word0(&rv) = (word0(&rv) & Exp_mask)
  1865. + Exp_msk1
  1866. ;
  1867. word1(&rv) = 0;
  1868. /* dsign = 0; */
  1869. break;
  1870. }
  1871. }
  1872. else if (!(word0(&rv) & Bndry_mask) && !word1(&rv)) {
  1873. drop_down:
  1874. /* boundary case -- decrement exponent */
  1875. if (bc.scale) {
  1876. L = word0(&rv) & Exp_mask;
  1877. if (L <= (2*P+1)*Exp_msk1) {
  1878. if (L > (P+2)*Exp_msk1)
  1879. /* round even ==> */
  1880. /* accept rv */
  1881. break;
  1882. /* rv = smallest denormal */
  1883. if (bc.nd > nd)
  1884. break;
  1885. goto undfl;
  1886. }
  1887. }
  1888. L = (word0(&rv) & Exp_mask) - Exp_msk1;
  1889. word0(&rv) = L | Bndry_mask1;
  1890. word1(&rv) = 0xffffffff;
  1891. break;
  1892. }
  1893. if (!odd)
  1894. break;
  1895. if (dsign)
  1896. dval(&rv) += sulp(&rv, &bc);
  1897. else {
  1898. dval(&rv) -= sulp(&rv, &bc);
  1899. if (!dval(&rv)) {
  1900. if (bc.nd >nd)
  1901. break;
  1902. goto undfl;
  1903. }
  1904. }
  1905. /* dsign = 1 - dsign; */
  1906. break;
  1907. }
  1908. if ((aadj = ratio(delta, bs)) <= 2.) {
  1909. if (dsign)
  1910. aadj = aadj1 = 1.;
  1911. else if (word1(&rv) || word0(&rv) & Bndry_mask) {
  1912. if (word1(&rv) == Tiny1 && !word0(&rv)) {
  1913. if (bc.nd >nd)
  1914. break;
  1915. goto undfl;
  1916. }
  1917. aadj = 1.;
  1918. aadj1 = -1.;
  1919. }
  1920. else {
  1921. /* special case -- power of FLT_RADIX to be */
  1922. /* rounded down... */
  1923. if (aadj < 2./FLT_RADIX)
  1924. aadj = 1./FLT_RADIX;
  1925. else
  1926. aadj *= 0.5;
  1927. aadj1 = -aadj;
  1928. }
  1929. }
  1930. else {
  1931. aadj *= 0.5;
  1932. aadj1 = dsign ? aadj : -aadj;
  1933. if (Flt_Rounds == 0)
  1934. aadj1 += 0.5;
  1935. }
  1936. y = word0(&rv) & Exp_mask;
  1937. /* Check for overflow */
  1938. if (y == Exp_msk1*(DBL_MAX_EXP+Bias-1)) {
  1939. dval(&rv0) = dval(&rv);
  1940. word0(&rv) -= P*Exp_msk1;
  1941. adj.d = aadj1 * ulp(&rv);
  1942. dval(&rv) += adj.d;
  1943. if ((word0(&rv) & Exp_mask) >=
  1944. Exp_msk1*(DBL_MAX_EXP+Bias-P)) {
  1945. if (word0(&rv0) == Big0 && word1(&rv0) == Big1) {
  1946. Bfree(bb);
  1947. Bfree(bd);
  1948. Bfree(bs);
  1949. Bfree(bd0);
  1950. Bfree(delta);
  1951. goto ovfl;
  1952. }
  1953. word0(&rv) = Big0;
  1954. word1(&rv) = Big1;
  1955. goto cont;
  1956. }
  1957. else
  1958. word0(&rv) += P*Exp_msk1;
  1959. }
  1960. else {
  1961. if (bc.scale && y <= 2*P*Exp_msk1) {
  1962. if (aadj <= 0x7fffffff) {
  1963. if ((z = (ULong)aadj) <= 0)
  1964. z = 1;
  1965. aadj = z;
  1966. aadj1 = dsign ? aadj : -aadj;
  1967. }
  1968. dval(&aadj2) = aadj1;
  1969. word0(&aadj2) += (2*P+1)*Exp_msk1 - y;
  1970. aadj1 = dval(&aadj2);
  1971. }
  1972. adj.d = aadj1 * ulp(&rv);
  1973. dval(&rv) += adj.d;
  1974. }
  1975. z = word0(&rv) & Exp_mask;
  1976. if (bc.nd == nd) {
  1977. if (!bc.scale)
  1978. if (y == z) {
  1979. /* Can we stop now? */
  1980. L = (Long)aadj;
  1981. aadj -= L;
  1982. /* The tolerances below are conservative. */
  1983. if (dsign || word1(&rv) || word0(&rv) & Bndry_mask) {
  1984. if (aadj < .4999999 || aadj > .5000001)
  1985. break;
  1986. }
  1987. else if (aadj < .4999999/FLT_RADIX)
  1988. break;
  1989. }
  1990. }
  1991. cont:
  1992. Bfree(bb);
  1993. Bfree(bd);
  1994. Bfree(bs);
  1995. Bfree(delta);
  1996. }
  1997. Bfree(bb);
  1998. Bfree(bd);
  1999. Bfree(bs);
  2000. Bfree(bd0);
  2001. Bfree(delta);
  2002. if (bc.nd > nd) {
  2003. error = bigcomp(&rv, s0, &bc);
  2004. if (error)
  2005. goto failed_malloc;
  2006. }
  2007. if (bc.scale) {
  2008. word0(&rv0) = Exp_1 - 2*P*Exp_msk1;
  2009. word1(&rv0) = 0;
  2010. dval(&rv) *= dval(&rv0);
  2011. }
  2012. ret:
  2013. return sign ? -dval(&rv) : dval(&rv);
  2014. parse_error:
  2015. return 0.0;
  2016. failed_malloc:
  2017. errno = ENOMEM;
  2018. return -1.0;
  2019. undfl:
  2020. return sign ? -0.0 : 0.0;
  2021. ovfl:
  2022. errno = ERANGE;
  2023. /* Can't trust HUGE_VAL */
  2024. word0(&rv) = Exp_mask;
  2025. word1(&rv) = 0;
  2026. return sign ? -dval(&rv) : dval(&rv);
  2027. }
  2028. static char *
  2029. rv_alloc(int i)
  2030. {
  2031. int j, k, *r;
  2032. j = sizeof(ULong);
  2033. for(k = 0;
  2034. sizeof(Bigint) - sizeof(ULong) - sizeof(int) + j <= (unsigned)i;
  2035. j <<= 1)
  2036. k++;
  2037. r = (int*)Balloc(k);
  2038. if (r == NULL)
  2039. return NULL;
  2040. *r = k;
  2041. return (char *)(r+1);
  2042. }
  2043. static char *
  2044. nrv_alloc(char *s, char **rve, int n)
  2045. {
  2046. char *rv, *t;
  2047. rv = rv_alloc(n);
  2048. if (rv == NULL)
  2049. return NULL;
  2050. t = rv;
  2051. while((*t = *s++)) t++;
  2052. if (rve)
  2053. *rve = t;
  2054. return rv;
  2055. }
  2056. /* freedtoa(s) must be used to free values s returned by dtoa
  2057. * when MULTIPLE_THREADS is #defined. It should be used in all cases,
  2058. * but for consistency with earlier versions of dtoa, it is optional
  2059. * when MULTIPLE_THREADS is not defined.
  2060. */
  2061. void
  2062. _Py_dg_freedtoa(char *s)
  2063. {
  2064. Bigint *b = (Bigint *)((int *)s - 1);
  2065. b->maxwds = 1 << (b->k = *(int*)b);
  2066. Bfree(b);
  2067. }
  2068. /* dtoa for IEEE arithmetic (dmg): convert double to ASCII string.
  2069. *
  2070. * Inspired by "How to Print Floating-Point Numbers Accurately" by
  2071. * Guy L. Steele, Jr. and Jon L. White [Proc. ACM SIGPLAN '90, pp. 112-126].
  2072. *
  2073. * Modifications:
  2074. * 1. Rather than iterating, we use a simple numeric overestimate
  2075. * to determine k = floor(log10(d)). We scale relevant
  2076. * quantities using O(log2(k)) rather than O(k) multiplications.
  2077. * 2. For some modes > 2 (corresponding to ecvt and fcvt), we don't
  2078. * try to generate digits strictly left to right. Instead, we
  2079. * compute with fewer bits and propagate the carry if necessary
  2080. * when rounding the final digit up. This is often faster.
  2081. * 3. Under the assumption that input will be rounded nearest,
  2082. * mode 0 renders 1e23 as 1e23 rather than 9.999999999999999e22.
  2083. * That is, we allow equality in stopping tests when the
  2084. * round-nearest rule will give the same floating-point value
  2085. * as would satisfaction of the stopping test with strict
  2086. * inequality.
  2087. * 4. We remove common factors of powers of 2 from relevant
  2088. * quantities.
  2089. * 5. When converting floating-point integers less than 1e16,
  2090. * we use floating-point arithmetic rather than resorting
  2091. * to multiple-precision integers.
  2092. * 6. When asked to produce fewer than 15 digits, we first try
  2093. * to get by with floating-point arithmetic; we resort to
  2094. * multiple-precision integer arithmetic only if we cannot
  2095. * guarantee that the floating-point calculation has given
  2096. * the correctly rounded result. For k requested digits and
  2097. * "uniformly" distributed input, the probability is
  2098. * something like 10^(k-15) that we must resort to the Long
  2099. * calculation.
  2100. */
  2101. /* Additional notes (METD): (1) returns NULL on failure. (2) to avoid memory
  2102. leakage, a successful call to _Py_dg_dtoa should always be matched by a
  2103. call to _Py_dg_freedtoa. */
  2104. char *
  2105. _Py_dg_dtoa(double dd, int mode, int ndigits,
  2106. int *decpt, int *sign, char **rve)
  2107. {
  2108. /* Arguments ndigits, decpt, sign are similar to those
  2109. of ecvt and fcvt; trailing zeros are suppressed from
  2110. the returned string. If not null, *rve is set to point
  2111. to the end of the return value. If d is +-Infinity or NaN,
  2112. then *decpt is set to 9999.
  2113. mode:
  2114. 0 ==> shortest string that yields d when read in
  2115. and rounded to nearest.
  2116. 1 ==> like 0, but with Steele & White stopping rule;
  2117. e.g. with IEEE P754 arithmetic , mode 0 gives
  2118. 1e23 whereas mode 1 gives 9.999999999999999e22.
  2119. 2 ==> max(1,ndigits) significant digits. This gives a
  2120. return value similar to that of ecvt, except
  2121. that trailing zeros are suppressed.
  2122. 3 ==> through ndigits past the decimal point. This
  2123. gives a return value similar to that from fcvt,
  2124. except that trailing zeros are suppressed, and
  2125. ndigits can be negative.
  2126. 4,5 ==> similar to 2 and 3, respectively, but (in
  2127. round-nearest mode) with the tests of mode 0 to
  2128. possibly return a shorter string that rounds to d.
  2129. With IEEE arithmetic and compilation with
  2130. -DHonor_FLT_ROUNDS, modes 4 and 5 behave the same
  2131. as modes 2 and 3 when FLT_ROUNDS != 1.
  2132. 6-9 ==> Debugging modes similar to mode - 4: don't try
  2133. fast floating-point estimate (if applicable).
  2134. Values of mode other than 0-9 are treated as mode 0.
  2135. Sufficient space is allocated to the return value
  2136. to hold the suppressed trailing zeros.
  2137. */
  2138. int bbits, b2, b5, be, dig, i, ieps, ilim, ilim0, ilim1,
  2139. j, j1, k, k0, k_check, leftright, m2, m5, s2, s5,
  2140. spec_case, try_quick;
  2141. Long L;
  2142. int denorm;
  2143. ULong x;
  2144. Bigint *b, *b1, *delta, *mlo, *mhi, *S;
  2145. U d2, eps, u;
  2146. double ds;
  2147. char *s, *s0;
  2148. /* set pointers to NULL, to silence gcc compiler warnings and make
  2149. cleanup easier on error */
  2150. mlo = mhi = S = 0;
  2151. s0 = 0;
  2152. u.d = dd;
  2153. if (word0(&u) & Sign_bit) {
  2154. /* set sign for everything, including 0's and NaNs */
  2155. *sign = 1;
  2156. word0(&u) &= ~Sign_bit; /* clear sign bit */
  2157. }
  2158. else
  2159. *sign = 0;
  2160. /* quick return for Infinities, NaNs and zeros */
  2161. if ((word0(&u) & Exp_mask) == Exp_mask)
  2162. {
  2163. /* Infinity or NaN */
  2164. *decpt = 9999;
  2165. if (!word1(&u) && !(word0(&u) & 0xfffff))
  2166. return nrv_alloc("Infinity", rve, 8);
  2167. return nrv_alloc("NaN", rve, 3);
  2168. }
  2169. if (!dval(&u)) {
  2170. *decpt = 1;
  2171. return nrv_alloc("0", rve, 1);
  2172. }
  2173. /* compute k = floor(log10(d)). The computation may leave k
  2174. one too large, but should never leave k too small. */
  2175. b = d2b(&u, &be, &bbits);
  2176. if (b == NULL)
  2177. goto failed_malloc;
  2178. if ((i = (int)(word0(&u) >> Exp_shift1 & (Exp_mask>>Exp_shift1)))) {
  2179. dval(&d2) = dval(&u);
  2180. word0(&d2) &= Frac_mask1;
  2181. word0(&d2) |= Exp_11;
  2182. /* log(x) ~=~ log(1.5) + (x-1.5)/1.5
  2183. * log10(x) = log(x) / log(10)
  2184. * ~=~ log(1.5)/log(10) + (x-1.5)/(1.5*log(10))
  2185. * log10(d) = (i-Bias)*log(2)/log(10) + log10(d2)
  2186. *
  2187. * This suggests computing an approximation k to log10(d) by
  2188. *
  2189. * k = (i - Bias)*0.301029995663981
  2190. * + ( (d2-1.5)*0.289529654602168 + 0.176091259055681 );
  2191. *
  2192. * We want k to be too large rather than too small.
  2193. * The error in the first-order Taylor series approximation
  2194. * is in our favor, so we just round up the constant enough
  2195. * to compensate for any error in the multiplication of
  2196. * (i - Bias) by 0.301029995663981; since |i - Bias| <= 1077,
  2197. * and 1077 * 0.30103 * 2^-52 ~=~ 7.2e-14,
  2198. * adding 1e-13 to the constant term more than suffices.
  2199. * Hence we adjust the constant term to 0.1760912590558.
  2200. * (We could get a more accurate k by invoking log10,
  2201. * but this is probably not worthwhile.)
  2202. */
  2203. i -= Bias;
  2204. denorm = 0;
  2205. }
  2206. else {
  2207. /* d is denormalized */
  2208. i = bbits + be + (Bias + (P-1) - 1);
  2209. x = i > 32 ? word0(&u) << (64 - i) | word1(&u) >> (i - 32)
  2210. : word1(&u) << (32 - i);
  2211. dval(&d2) = x;
  2212. word0(&d2) -= 31*Exp_msk1; /* adjust exponent */
  2213. i -= (Bias + (P-1) - 1) + 1;
  2214. denorm = 1;
  2215. }
  2216. ds = (dval(&d2)-1.5)*0.289529654602168 + 0.1760912590558 +
  2217. i*0.301029995663981;
  2218. k = (int)ds;
  2219. if (ds < 0. && ds != k)
  2220. k--; /* want k = floor(ds) */
  2221. k_check = 1;
  2222. if (k >= 0 && k <= Ten_pmax) {
  2223. if (dval(&u) < tens[k])
  2224. k--;
  2225. k_check = 0;
  2226. }
  2227. j = bbits - i - 1;
  2228. if (j >= 0) {
  2229. b2 = 0;
  2230. s2 = j;
  2231. }
  2232. else {
  2233. b2 = -j;
  2234. s2 = 0;
  2235. }
  2236. if (k >= 0) {
  2237. b5 = 0;
  2238. s5 = k;
  2239. s2 += k;
  2240. }
  2241. else {
  2242. b2 -= k;
  2243. b5 = -k;
  2244. s5 = 0;
  2245. }
  2246. if (mode < 0 || mode > 9)
  2247. mode = 0;
  2248. try_quick = 1;
  2249. if (mode > 5) {
  2250. mode -= 4;
  2251. try_quick = 0;
  2252. }
  2253. leftright = 1;
  2254. ilim = ilim1 = -1; /* Values for cases 0 and 1; done here to */
  2255. /* silence erroneous "gcc -Wall" warning. */
  2256. switch(mode) {
  2257. case 0:
  2258. case 1:
  2259. i = 18;
  2260. ndigits = 0;
  2261. break;
  2262. case 2:
  2263. leftright = 0;
  2264. /* no break */
  2265. case 4:
  2266. if (ndigits <= 0)
  2267. ndigits = 1;
  2268. ilim = ilim1 = i = ndigits;
  2269. break;
  2270. case 3:
  2271. leftright = 0;
  2272. /* no break */
  2273. case 5:
  2274. i = ndigits + k + 1;
  2275. ilim = i;
  2276. ilim1 = i - 1;
  2277. if (i <= 0)
  2278. i = 1;
  2279. }
  2280. s0 = rv_alloc(i);
  2281. if (s0 == NULL)
  2282. goto failed_malloc;
  2283. s = s0;
  2284. if (ilim >= 0 && ilim <= Quick_max && try_quick) {
  2285. /* Try to get by with floating-point arithmetic. */
  2286. i = 0;
  2287. dval(&d2) = dval(&u);
  2288. k0 = k;
  2289. ilim0 = ilim;
  2290. ieps = 2; /* conservative */
  2291. if (k > 0) {
  2292. ds = tens[k&0xf];
  2293. j = k >> 4;
  2294. if (j & Bletch) {
  2295. /* prevent overflows */
  2296. j &= Bletch - 1;
  2297. dval(&u) /= bigtens[n_bigtens-1];
  2298. ieps++;
  2299. }
  2300. for(; j; j >>= 1, i++)
  2301. if (j & 1) {
  2302. ieps++;
  2303. ds *= bigtens[i];
  2304. }
  2305. dval(&u) /= ds;
  2306. }
  2307. else if ((j1 = -k)) {
  2308. dval(&u) *= tens[j1 & 0xf];
  2309. for(j = j1 >> 4; j; j >>= 1, i++)
  2310. if (j & 1) {
  2311. ieps++;
  2312. dval(&u) *= bigtens[i];
  2313. }
  2314. }
  2315. if (k_check && dval(&u) < 1. && ilim > 0) {
  2316. if (ilim1 <= 0)
  2317. goto fast_failed;
  2318. ilim = ilim1;
  2319. k--;
  2320. dval(&u) *= 10.;
  2321. ieps++;
  2322. }
  2323. dval(&eps) = ieps*dval(&u) + 7.;
  2324. word0(&eps) -= (P-1)*Exp_msk1;
  2325. if (ilim == 0) {
  2326. S = mhi = 0;
  2327. dval(&u) -= 5.;
  2328. if (dval(&u) > dval(&eps))
  2329. goto one_digit;
  2330. if (dval(&u) < -dval(&eps))
  2331. goto no_digits;
  2332. goto fast_failed;
  2333. }
  2334. if (leftright) {
  2335. /* Use Steele & White method of only
  2336. * generating digits needed.
  2337. */
  2338. dval(&eps) = 0.5/tens[ilim-1] - dval(&eps);
  2339. for(i = 0;;) {
  2340. L = (Long)dval(&u);
  2341. dval(&u) -= L;
  2342. *s++ = '0' + (int)L;
  2343. if (dval(&u) < dval(&eps))
  2344. goto ret1;
  2345. if (1. - dval(&u) < dval(&eps))
  2346. goto bump_up;
  2347. if (++i >= ilim)
  2348. break;
  2349. dval(&eps) *= 10.;
  2350. dval(&u) *= 10.;
  2351. }
  2352. }
  2353. else {
  2354. /* Generate ilim digits, then fix them up. */
  2355. dval(&eps) *= tens[ilim-1];
  2356. for(i = 1;; i++, dval(&u) *= 10.) {
  2357. L = (Long)(dval(&u));
  2358. if (!(dval(&u) -= L))
  2359. ilim = i;
  2360. *s++ = '0' + (int)L;
  2361. if (i == ilim) {
  2362. if (dval(&u) > 0.5 + dval(&eps))
  2363. goto bump_up;
  2364. else if (dval(&u) < 0.5 - dval(&eps)) {
  2365. while(*--s == '0');
  2366. s++;
  2367. goto ret1;
  2368. }
  2369. break;
  2370. }
  2371. }
  2372. }
  2373. fast_failed:
  2374. s = s0;
  2375. dval(&u) = dval(&d2);
  2376. k = k0;
  2377. ilim = ilim0;
  2378. }
  2379. /* Do we have a "small" integer? */
  2380. if (be >= 0 && k <= Int_max) {
  2381. /* Yes. */
  2382. ds = tens[k];
  2383. if (ndigits < 0 && ilim <= 0) {
  2384. S = mhi = 0;
  2385. if (ilim < 0 || dval(&u) <= 5*ds)
  2386. goto no_digits;
  2387. goto one_digit;
  2388. }
  2389. for(i = 1;; i++, dval(&u) *= 10.) {
  2390. L = (Long)(dval(&u) / ds);
  2391. dval(&u) -= L*ds;
  2392. *s++ = '0' + (int)L;
  2393. if (!dval(&u)) {
  2394. break;
  2395. }
  2396. if (i == ilim) {
  2397. dval(&u) += dval(&u);
  2398. if (dval(&u) > ds || (dval(&u) == ds && L & 1)) {
  2399. bump_up:
  2400. while(*--s == '9')
  2401. if (s == s0) {
  2402. k++;
  2403. *s = '0';
  2404. break;
  2405. }
  2406. ++*s++;
  2407. }
  2408. break;
  2409. }
  2410. }
  2411. goto ret1;
  2412. }
  2413. m2 = b2;
  2414. m5 = b5;
  2415. if (leftright) {
  2416. i =
  2417. denorm ? be + (Bias + (P-1) - 1 + 1) :
  2418. 1 + P - bbits;
  2419. b2 += i;
  2420. s2 += i;
  2421. mhi = i2b(1);
  2422. if (mhi == NULL)
  2423. goto failed_malloc;
  2424. }
  2425. if (m2 > 0 && s2 > 0) {
  2426. i = m2 < s2 ? m2 : s2;
  2427. b2 -= i;
  2428. m2 -= i;
  2429. s2 -= i;
  2430. }
  2431. if (b5 > 0) {
  2432. if (leftright) {
  2433. if (m5 > 0) {
  2434. mhi = pow5mult(mhi, m5);
  2435. if (mhi == NULL)
  2436. goto failed_malloc;
  2437. b1 = mult(mhi, b);
  2438. Bfree(b);
  2439. b = b1;
  2440. if (b == NULL)
  2441. goto failed_malloc;
  2442. }
  2443. if ((j = b5 - m5)) {
  2444. b = pow5mult(b, j);
  2445. if (b == NULL)
  2446. goto failed_malloc;
  2447. }
  2448. }
  2449. else {
  2450. b = pow5mult(b, b5);
  2451. if (b == NULL)
  2452. goto failed_malloc;
  2453. }
  2454. }
  2455. S = i2b(1);
  2456. if (S == NULL)
  2457. goto failed_malloc;
  2458. if (s5 > 0) {
  2459. S = pow5mult(S, s5);
  2460. if (S == NULL)
  2461. goto failed_malloc;
  2462. }
  2463. /* Check for special case that d is a normalized power of 2. */
  2464. spec_case = 0;
  2465. if ((mode < 2 || leftright)
  2466. ) {
  2467. if (!word1(&u) && !(word0(&u) & Bndry_mask)
  2468. && word0(&u) & (Exp_mask & ~Exp_msk1)
  2469. ) {
  2470. /* The special case */
  2471. b2 += Log2P;
  2472. s2 += Log2P;
  2473. spec_case = 1;
  2474. }
  2475. }
  2476. /* Arrange for convenient computation of quotients:
  2477. * shift left if necessary so divisor has 4 leading 0 bits.
  2478. *
  2479. * Perhaps we should just compute leading 28 bits of S once
  2480. * and for all and pass them and a shift to quorem, so it
  2481. * can do shifts and ors to compute the numerator for q.
  2482. */
  2483. #define iInc 28
  2484. i = dshift(S, s2);
  2485. b2 += i;
  2486. m2 += i;
  2487. s2 += i;
  2488. if (b2 > 0) {
  2489. b = lshift(b, b2);
  2490. if (b == NULL)
  2491. goto failed_malloc;
  2492. }
  2493. if (s2 > 0) {
  2494. S = lshift(S, s2);
  2495. if (S == NULL)
  2496. goto failed_malloc;
  2497. }
  2498. if (k_check) {
  2499. if (cmp(b,S) < 0) {
  2500. k--;
  2501. b = multadd(b, 10, 0); /* we botched the k estimate */
  2502. if (b == NULL)
  2503. goto failed_malloc;
  2504. if (leftright) {
  2505. mhi = multadd(mhi, 10, 0);
  2506. if (mhi == NULL)
  2507. goto failed_malloc;
  2508. }
  2509. ilim = ilim1;
  2510. }
  2511. }
  2512. if (ilim <= 0 && (mode == 3 || mode == 5)) {
  2513. if (ilim < 0) {
  2514. /* no digits, fcvt style */
  2515. no_digits:
  2516. k = -1 - ndigits;
  2517. goto ret;
  2518. }
  2519. else {
  2520. S = multadd(S, 5, 0);
  2521. if (S == NULL)
  2522. goto failed_malloc;
  2523. if (cmp(b, S) <= 0)
  2524. goto no_digits;
  2525. }
  2526. one_digit:
  2527. *s++ = '1';
  2528. k++;
  2529. goto ret;
  2530. }
  2531. if (leftright) {
  2532. if (m2 > 0) {
  2533. mhi = lshift(mhi, m2);
  2534. if (mhi == NULL)
  2535. goto failed_malloc;
  2536. }
  2537. /* Compute mlo -- check for special case
  2538. * that d is a normalized power of 2.
  2539. */
  2540. mlo = mhi;
  2541. if (spec_case) {
  2542. mhi = Balloc(mhi->k);
  2543. if (mhi == NULL)
  2544. goto failed_malloc;
  2545. Bcopy(mhi, mlo);
  2546. mhi = lshift(mhi, Log2P);
  2547. if (mhi == NULL)
  2548. goto failed_malloc;
  2549. }
  2550. for(i = 1;;i++) {
  2551. dig = quorem(b,S) + '0';
  2552. /* Do we yet have the shortest decimal string
  2553. * that will round to d?
  2554. */
  2555. j = cmp(b, mlo);
  2556. delta = diff(S, mhi);
  2557. if (delta == NULL)
  2558. goto failed_malloc;
  2559. j1 = delta->sign ? 1 : cmp(b, delta);
  2560. Bfree(delta);
  2561. if (j1 == 0 && mode != 1 && !(word1(&u) & 1)
  2562. ) {
  2563. if (dig == '9')
  2564. goto round_9_up;
  2565. if (j > 0)
  2566. dig++;
  2567. *s++ = dig;
  2568. goto ret;
  2569. }
  2570. if (j < 0 || (j == 0 && mode != 1
  2571. && !(word1(&u) & 1)
  2572. )) {
  2573. if (!b->x[0] && b->wds <= 1) {
  2574. goto accept_dig;
  2575. }
  2576. if (j1 > 0) {
  2577. b = lshift(b, 1);
  2578. if (b == NULL)
  2579. goto failed_malloc;
  2580. j1 = cmp(b, S);
  2581. if ((j1 > 0 || (j1 == 0 && dig & 1))
  2582. && dig++ == '9')
  2583. goto round_9_up;
  2584. }
  2585. accept_dig:
  2586. *s++ = dig;
  2587. goto ret;
  2588. }
  2589. if (j1 > 0) {
  2590. if (dig == '9') { /* possible if i == 1 */
  2591. round_9_up:
  2592. *s++ = '9';
  2593. goto roundoff;
  2594. }
  2595. *s++ = dig + 1;
  2596. goto ret;
  2597. }
  2598. *s++ = dig;
  2599. if (i == ilim)
  2600. break;
  2601. b = multadd(b, 10, 0);
  2602. if (b == NULL)
  2603. goto failed_malloc;
  2604. if (mlo == mhi) {
  2605. mlo = mhi = multadd(mhi, 10, 0);
  2606. if (mlo == NULL)
  2607. goto failed_malloc;
  2608. }
  2609. else {
  2610. mlo = multadd(mlo, 10, 0);
  2611. if (mlo == NULL)
  2612. goto failed_malloc;
  2613. mhi = multadd(mhi, 10, 0);
  2614. if (mhi == NULL)
  2615. goto failed_malloc;
  2616. }
  2617. }
  2618. }
  2619. else
  2620. for(i = 1;; i++) {
  2621. *s++ = dig = quorem(b,S) + '0';
  2622. if (!b->x[0] && b->wds <= 1) {
  2623. goto ret;
  2624. }
  2625. if (i >= ilim)
  2626. break;
  2627. b = multadd(b, 10, 0);
  2628. if (b == NULL)
  2629. goto failed_malloc;
  2630. }
  2631. /* Round off last digit */
  2632. b = lshift(b, 1);
  2633. if (b == NULL)
  2634. goto failed_malloc;
  2635. j = cmp(b, S);
  2636. if (j > 0 || (j == 0 && dig & 1)) {
  2637. roundoff:
  2638. while(*--s == '9')
  2639. if (s == s0) {
  2640. k++;
  2641. *s++ = '1';
  2642. goto ret;
  2643. }
  2644. ++*s++;
  2645. }
  2646. else {
  2647. while(*--s == '0');
  2648. s++;
  2649. }
  2650. ret:
  2651. Bfree(S);
  2652. if (mhi) {
  2653. if (mlo && mlo != mhi)
  2654. Bfree(mlo);
  2655. Bfree(mhi);
  2656. }
  2657. ret1:
  2658. Bfree(b);
  2659. *s = 0;
  2660. *decpt = k + 1;
  2661. if (rve)
  2662. *rve = s;
  2663. return s0;
  2664. failed_malloc:
  2665. if (S)
  2666. Bfree(S);
  2667. if (mlo && mlo != mhi)
  2668. Bfree(mlo);
  2669. if (mhi)
  2670. Bfree(mhi);
  2671. if (b)
  2672. Bfree(b);
  2673. if (s0)
  2674. _Py_dg_freedtoa(s0);
  2675. return NULL;
  2676. }
  2677. #ifdef __cplusplus
  2678. }
  2679. #endif
  2680. #endif /* PY_NO_SHORT_FLOAT_REPR */