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.
(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?
(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 ...
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).
(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.
(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.
(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).
(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.
(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.
Ok, on it. It's unfortunate that the function ends up so much more complicated just to fix these few cases.
Thank you for your contribution.
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(-)
(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.
(In reply to Steve Kargl from comment #12) Looking forwards to seeing you among the ranks of committers again.
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(-)