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.

2876 lines
78 KiB

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