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.

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