lib msun remainder() function gives incorrect result on powerpc64, as reported by rem_test.c. Expected result is 4.9406564584124654e-324, but -4.9406564584124654e-324 is given. Reduced test case: #include <openlibm.h> #include <assert.h> int main() { double x=-3.3770170061145426e-226; double y=2.4703282292062327e-323; double expected_rem = 4.9406564584124654e-324; double rem; rem = remainder(x, y); assert(rem == expected_rem); return(0); } * compile with "cc rem.c -lm"
^Triage: canonicalize assignment.
Does not look like a libm issue. % cc -o z a.c -lm a.c:1:10: fatal error: 'openlibm.h' file not found 1 | #include <openlibm.h> | ^~~~~~~~~~~~ 1 error generated.
(In reply to Steve Kargl from comment #2) openlibm is derived from our libm. I can reproduce with our libm. This seems to be a problem with the C implementation of remainder()
I have put up a potential fix for review. https://reviews.freebsd.org/D53023
(In reply to Ahmad Khalifa from comment #3) Yes, I know openlibm is based on FreeBSD's libm. I've contributing fixes to FreeBSD's libm for decades and some of those have been sent to openlibm.
For others readers, I've had a private exchange with Ahmad about his patch. I'm including that contents of my reply here: I do not have a phabricator account. That method to review patches came after I stopped committing. I did look at your patch, and do believe that it is the correct fix. If you compare to remainderf(), you'll see that the float version considers the entire significand. I also checked remainderl(). It calls remquol() and it's implementation is much different than the float and double versions. I failed to mention that I also checked NetBSD and OpenBSD source code. These have the same issue as FreeBSD and they share the same code.
A commit in branch main references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=25cca51ed294890d20a3c0290814cd26875db686 commit 25cca51ed294890d20a3c0290814cd26875db686 Author: Ahmad Khalifa <vexeduxr@FreeBSD.org> AuthorDate: 2025-10-10 09:30:52 +0000 Commit: Ahmad Khalifa <vexeduxr@FreeBSD.org> CommitDate: 2025-10-11 10:13:19 +0000 libm: remainder: make sure x is zero Make sure the entirety of x is zero before flipping the sign bit. Otherwise the sign would be wrong for small values of x when x is negative and |n*y| > |x| Reported by: alfredo PR: 251091 Reviewed by: kargl MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D53023 lib/msun/src/e_remainder.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
A commit in branch stable/13 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=d06e31d4a7622b535d903a1b84ce59e211ca2b18 commit d06e31d4a7622b535d903a1b84ce59e211ca2b18 Author: Ahmad Khalifa <vexeduxr@FreeBSD.org> AuthorDate: 2025-10-10 09:30:52 +0000 Commit: Ahmad Khalifa <vexeduxr@FreeBSD.org> CommitDate: 2025-10-14 14:01:59 +0000 libm: remainder: make sure x is zero Make sure the entirety of x is zero before flipping the sign bit. Otherwise the sign would be wrong for small values of x when x is negative and |n*y| > |x| Reported by: alfredo PR: 251091 Reviewed by: kargl MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D53023 (cherry picked from commit 25cca51ed294890d20a3c0290814cd26875db686) lib/msun/src/e_remainder.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
A commit in branch stable/15 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=583e976d6d09770a6299a19603d4f0dbaa562cac commit 583e976d6d09770a6299a19603d4f0dbaa562cac Author: Ahmad Khalifa <vexeduxr@FreeBSD.org> AuthorDate: 2025-10-10 09:30:52 +0000 Commit: Ahmad Khalifa <vexeduxr@FreeBSD.org> CommitDate: 2025-10-14 13:42:31 +0000 libm: remainder: make sure x is zero Make sure the entirety of x is zero before flipping the sign bit. Otherwise the sign would be wrong for small values of x when x is negative and |n*y| > |x| Reported by: alfredo PR: 251091 Reviewed by: kargl MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D53023 (cherry picked from commit 25cca51ed294890d20a3c0290814cd26875db686) lib/msun/src/e_remainder.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
A commit in branch stable/14 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=439909c48b0e8b0e8dda03b9a359b73dcabb546e commit 439909c48b0e8b0e8dda03b9a359b73dcabb546e Author: Ahmad Khalifa <vexeduxr@FreeBSD.org> AuthorDate: 2025-10-10 09:30:52 +0000 Commit: Ahmad Khalifa <vexeduxr@FreeBSD.org> CommitDate: 2025-10-14 14:00:19 +0000 libm: remainder: make sure x is zero Make sure the entirety of x is zero before flipping the sign bit. Otherwise the sign would be wrong for small values of x when x is negative and |n*y| > |x| Reported by: alfredo PR: 251091 Reviewed by: kargl MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D53023 (cherry picked from commit 25cca51ed294890d20a3c0290814cd26875db686) lib/msun/src/e_remainder.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
A commit in branch releng/15.0 references this bug: URL: https://cgit.FreeBSD.org/src/commit/?id=4c87cff1c8c53f06d8d25813fa32f1cc35657897 commit 4c87cff1c8c53f06d8d25813fa32f1cc35657897 Author: Ahmad Khalifa <vexeduxr@FreeBSD.org> AuthorDate: 2025-10-10 09:30:52 +0000 Commit: Colin Percival <cperciva@FreeBSD.org> CommitDate: 2025-10-16 17:37:22 +0000 libm: remainder: make sure x is zero Make sure the entirety of x is zero before flipping the sign bit. Otherwise the sign would be wrong for small values of x when x is negative and |n*y| > |x| Approved by: re (cperciva) Reported by: alfredo PR: 251091 Reviewed by: kargl MFC after: 3 days Differential Revision: https://reviews.freebsd.org/D53023 (cherry picked from commit 25cca51ed294890d20a3c0290814cd26875db686) (cherry picked from commit 583e976d6d09770a6299a19603d4f0dbaa562cac) lib/msun/src/e_remainder.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)