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.

2989 lines
81 KiB

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