Bug 251091 - lib msun remainder() gives incorrect result on powerpc64
Summary: lib msun remainder() gives incorrect result on powerpc64
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: misc (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Some People
Assignee: Ahmad Khalifa
URL:
Keywords:
Depends on:
Blocks: 290099
  Show dependency tree / graph
 
Reported: 2020-11-13 02:05 UTC by Alfredo Dal'Ava Junior
Modified: 2025-10-16 17:39 UTC (History)
4 users (show)

See Also:
linimon: mfc-stable15?
linimon: mfc-stable14?


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Alfredo Dal'Ava Junior freebsd_committer freebsd_triage 2020-11-13 02:05:09 UTC
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"
Comment 1 Mark Linimon freebsd_committer freebsd_triage 2024-01-05 03:36:03 UTC
^Triage: canonicalize assignment.
Comment 2 Steve Kargl freebsd_committer freebsd_triage 2025-10-08 21:42:33 UTC
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.
Comment 3 Ahmad Khalifa freebsd_committer freebsd_triage 2025-10-10 12:50:55 UTC
(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()
Comment 4 Ahmad Khalifa freebsd_committer freebsd_triage 2025-10-10 12:59:12 UTC
I have put up a potential fix for review. https://reviews.freebsd.org/D53023
Comment 5 Steve Kargl freebsd_committer freebsd_triage 2025-10-10 15:23:29 UTC
(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.
Comment 6 Steve Kargl freebsd_committer freebsd_triage 2025-10-10 23:33:24 UTC
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.
Comment 7 commit-hook freebsd_committer freebsd_triage 2025-10-11 10:17:38 UTC
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(-)
Comment 8 commit-hook freebsd_committer freebsd_triage 2025-10-14 14:16:36 UTC
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(-)
Comment 9 commit-hook freebsd_committer freebsd_triage 2025-10-14 14:16:38 UTC
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(-)
Comment 10 commit-hook freebsd_committer freebsd_triage 2025-10-14 14:16:39 UTC
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(-)
Comment 11 commit-hook freebsd_committer freebsd_triage 2025-10-16 17:39:05 UTC
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(-)