Bug 295706 - [libm] Fix up for recent rsqrt[fl] functions.
Summary: [libm] Fix up for recent rsqrt[fl] functions.
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 16.0-CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Robert Clausecker
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-05-29 19:24 UTC by Steve Kargl
Modified: 2026-06-14 12:38 UTC (History)
2 users (show)

See Also:


Attachments
Patch to correct rounding and a few comments. (3.61 KB, patch)
2026-05-29 19:24 UTC, Steve Kargl
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Steve Kargl freebsd_committer freebsd_triage 2026-05-29 19:24:00 UTC
Created attachment 271330 [details]
Patch to correct rounding and a few comments.

Paul Zimmermann (of Core-Math and MPFR fame) graciously tested
the recently committed rsqrt[fl]() functions.  He identified 127
incorrectly rounded values for rsqrtf() in round-to-nearest mode.
This patch fixes the rounding in RN.  Exhaustive testing now shows
that rsqrtf() is corrected rounded for RN.  He also tested rsqrt()
and rsqrtl() in the interval [1,4).  Both appear to be correctly
rounded.  Finally, the patch includes small changes to comments.
 

A concise list of changes is

* lib/msun/src/s_rsqrt.c:
  . Fix comments.
 
* lib/msun/src/s_rsqrtf.c 
  . Fix comments.
  . Exhaustive testing by Paul Zimmermann found 127 incorrectly
    rounded values in round-to-nearests.  These gave have the 
    form 0x1.13e07pN with N an odd integer.  With this patch, all
    values are now correctly rounded in round-to-nearest.

* lib/msun/src/s_rsqrtl.c
   . Fix comments.
   . Move all variable declarations to top of function and sort.
Comment 1 Robert Clausecker freebsd_committer freebsd_triage 2026-06-04 16:30:09 UTC
(In reply to Steve Kargl from comment #0)


Thank you for fixing this one.

Could you add the exhaustive test harness to our test suite
so that people trying to edit the code in the future can validate their changes?
Comment 2 Steve Kargl freebsd_committer freebsd_triage 2026-06-04 18:02:11 UTC
(In reply to Robert Clausecker from comment #1)
Unfortunately, my testing framework (aka the tlibm program)
is unsuitable for the FreeBSD test suite.  It depends on MPFR
and GMP, which are LGPL libraries and unavailable in /usr/src.
I recall putting a version of tlibm in my home directory
on the FreeBSD cluster, but it's fairly old. Someday I'll
clean up the code, and make it available through github or
codeberg or ...
Comment 3 Thierry Thomas freebsd_committer freebsd_triage 2026-06-04 21:44:18 UTC
Julia has a test program for its OpenLibm, under a MIT license, available at
https://github.com/JuliaMath/openlibm-test/tree/main

Not checked, but I guess that we could use it: replace their functions in src/math by ours (under /usr/src/lib/msun/src).
Comment 4 Steve Kargl freebsd_committer freebsd_triage 2026-06-04 23:29:53 UTC
(In reply to Thierry Thomas from comment #3)
For full disclosure, I've never run the msun/tests (or I at least
do not recall ever running the tests).  So, I am in no position
to judge whether the openlibm-test suite is an improvement or 
not.

It appears that the openlibm-test suite covers only C99 math functions.
FreeBSD's libm has additional functions from C11/C23.  A quick peek
shows that it has no tests for cospi[fl], sinpi[fl], tanpi[fl], rsqrt[fl],
and the soon to be submitted acospi[fl], asinpi[fl], and atanpi[fl]
functions.
Comment 5 Robert Clausecker freebsd_committer freebsd_triage 2026-06-05 08:11:56 UTC
(In reply to Steve Kargl from comment #2)

You can put test tools that don't work within our build system into tools/test.
I've done that for the arc4random biastest I had written for the arc4random_uniform() rewrite for example.
Comment 6 Steve Kargl freebsd_committer freebsd_triage 2026-06-07 16:41:32 UTC
(In reply to Robert Clausecker from comment #5)
I was unaware of the tools/test directory.  Can a test depend on
packages from the ports collection or must it be self-contained
and use only FreeBSD base componenets? tlibm require math/mpfr
and math/gmp.  I'll see if I can cleanup the code for a possible
submission.

But, for this bug report, I'll actually interested in getting
the patch applied before the source code for rsqrt[fl] propagates
to other projects (e.g., openbsd and/or netbsd).
Comment 7 Robert Clausecker freebsd_committer freebsd_triage 2026-06-07 18:44:21 UTC
(In reply to Steve Kargl from comment #6)

There aren't really any rules for tools/test AFAIK and the directory is not hooked into any build, so it can depend on whatever.

I think I can land your patch, but it would be good to land it with your test code in one go.
Comment 8 Steve Kargl freebsd_committer freebsd_triage 2026-06-07 19:20:46 UTC
(In reply to Robert Clausecker from comment #7)
Unfortunately, the test code is in no condition to submit,
and it isn't a simply matter to just fix it.  Fortunately,
Paul Zimmermann has independently confirmed that the patch
fixes rsqrtf(x) to be correctly rounded in round-to-nearest
for all input values (except, of course, exception cases
+-inf, NaN).  My main concern is that the code currently
in the tree is grabbed by other projects and then they
never update to the patched code.
Comment 9 Robert Clausecker freebsd_committer freebsd_triage 2026-06-07 20:30:31 UTC
Ok, on it.

It's unfortunate that the function ends up so much more complicated just to fix these few cases.
Comment 10 Robert Clausecker freebsd_committer freebsd_triage 2026-06-07 21:01:48 UTC
Thank you for your contribution.
Comment 11 commit-hook freebsd_committer freebsd_triage 2026-06-07 21:01:48 UTC
A commit in branch main references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=c3f6dcea199289329c1d3b91b69e5a4821fc3dff

commit c3f6dcea199289329c1d3b91b69e5a4821fc3dff
Author:     Steve Kargl <kargl@FreeBSD.org>
AuthorDate: 2026-06-07 19:12:16 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-06-07 20:59:19 +0000

    msun: Fix up for recent rsqrt[fl] functions

    Paul Zimmermann (of Core-Math and MPFR fame) graciously tested
    the recently committed rsqrt[fl]() functions.  He identified 127
    incorrectly rounded values for rsqrtf() in round-to-nearest mode.
    This patch fixes the rounding in RN.  Exhaustive testing now shows
    that rsqrtf() is corrected rounded for RN.  He also tested rsqrt()
    and rsqrtl() in the interval [1,4).  Both appear to be correctly
    rounded.  Finally, the patch includes small changes to comments.

    A concise list of changes is

    * lib/msun/src/s_rsqrt.c:
      . Fix comments.

    * lib/msun/src/s_rsqrtf.c
      . Fix comments.
      . Exhaustive testing by Paul Zimmermann found 127 incorrectly
        rounded values in round-to-nearests.  These gave have the
        form 0x1.13e07pN with N an odd integer.  With this patch, all
        values are now correctly rounded in round-to-nearest.

    * lib/msun/src/s_rsqrtl.c
       . Fix comments.
       . Move all variable declarations to top of function and sort.

    PR:             295706
    MFC after:      1 week
    Fixes:          3085fc9d97bd83785ba3ba43e0378d7d67987d1f

 lib/msun/src/s_rsqrt.c  |  4 ++--
 lib/msun/src/s_rsqrtf.c | 37 ++++++++++++++++++++++---------------
 lib/msun/src/s_rsqrtl.c |  8 +++-----
 3 files changed, 27 insertions(+), 22 deletions(-)
Comment 12 Steve Kargl freebsd_committer freebsd_triage 2026-06-07 23:28:43 UTC
(In reply to Robert Clausecker from comment #10)
No! Thank you for committing the patch.
When I retire I'll pursue getting my commit bit back,
Yes, I had commit access in the pre-git era.
Comment 13 Robert Clausecker freebsd_committer freebsd_triage 2026-06-09 09:27:27 UTC
(In reply to Steve Kargl from comment #12)

Looking forwards to seeing you among the ranks of committers again.
Comment 14 commit-hook freebsd_committer freebsd_triage 2026-06-14 12:38:00 UTC
A commit in branch stable/15 references this bug:

URL: https://cgit.FreeBSD.org/src/commit/?id=87466b1956975ed49a8df7d0de019f0e4f7b7b97

commit 87466b1956975ed49a8df7d0de019f0e4f7b7b97
Author:     Steve Kargl <kargl@FreeBSD.org>
AuthorDate: 2026-06-07 19:12:16 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-06-14 09:35:23 +0000

    msun: Fix up for recent rsqrt[fl] functions

    Paul Zimmermann (of Core-Math and MPFR fame) graciously tested
    the recently committed rsqrt[fl]() functions.  He identified 127
    incorrectly rounded values for rsqrtf() in round-to-nearest mode.
    This patch fixes the rounding in RN.  Exhaustive testing now shows
    that rsqrtf() is corrected rounded for RN.  He also tested rsqrt()
    and rsqrtl() in the interval [1,4).  Both appear to be correctly
    rounded.  Finally, the patch includes small changes to comments.

    A concise list of changes is

    * lib/msun/src/s_rsqrt.c:
      . Fix comments.

    * lib/msun/src/s_rsqrtf.c
      . Fix comments.
      . Exhaustive testing by Paul Zimmermann found 127 incorrectly
        rounded values in round-to-nearests.  These gave have the
        form 0x1.13e07pN with N an odd integer.  With this patch, all
        values are now correctly rounded in round-to-nearest.

    * lib/msun/src/s_rsqrtl.c
       . Fix comments.
       . Move all variable declarations to top of function and sort.

    PR:             295706
    MFC after:      1 week
    Fixes:          3085fc9d97bd83785ba3ba43e0378d7d67987d1f

    (cherry picked from commit c3f6dcea199289329c1d3b91b69e5a4821fc3dff)

 lib/msun/src/s_rsqrt.c  |  4 ++--
 lib/msun/src/s_rsqrtf.c | 37 ++++++++++++++++++++++---------------
 lib/msun/src/s_rsqrtl.c |  8 +++-----
 3 files changed, 27 insertions(+), 22 deletions(-)