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.

2918 lines
79 KiB

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