Bug 295089 - [libm] implementation of rsqrt, rsqrtf, and rsqrtl.
Summary: [libm] implementation of rsqrt, rsqrtf, and rsqrtl.
Status: In Progress
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: 16.0-CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Konstantin Belousov
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-05-07 22:36 UTC by Steve Kargl
Modified: 2026-07-28 15:45 UTC (History)
4 users (show)

See Also:
linimon: mfc-stable14?


Attachments
Patch with libm changes (21.05 KB, patch)
2026-05-07 22:36 UTC, Steve Kargl
no flags Details | Diff
New diff (21.73 KB, patch)
2026-05-10 01:16 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-07 22:36:22 UTC
Created attachment 270500 [details]
Patch with libm changes

The attached diff implements the inverse square root function, i.e,
rsqrt(x) = 1 / sqrt(x).  Exhaustive testing of the float version
suggests that it is correctly rounded in round-to-nearest for all
test values in the range [0x1p-127,0x1p126] except for 127 values.
Exhaustive testing of rsqrt and rsqrtl cannot be done, but 1100M
values of x for rsqrt and 400M values for rsqrtl were tested.  All
tested values were correctly rounded.

I do not have access to LD128 (i.e., IEEE 128-bit floating point)
hardware, so the implementation of rsqrtl() is untested.

The following is a summary of changes to source code.

* lib/msun/Makefile:
  . Add s_rsqrt.c and s_rsqrtf.c to COMMON_SRCS.
  . For non-53-bit long double targets, add s_rsqrtl.c to COMMON_SRCS.
  . Add MLINKS for rsqrt.3, rsqrtf.3, and rsqrtl.3 to sqrt.3.

* lib/msun/Symbol.map:
  . Add rsqrt, rsqrtf, and rsqrtl to the Symbol map for shared libm.so.

* lib/msun/man/sqrt.3:
  . Update the sqrt.3 manual page to include information for rsqrt[fl].
  . Note, these function come from ISO C23 (and IEEE-754 2008).

* lib/msun/src/math.h:
  . Add prototypes for new functions.

* lib/msun/src/math_private.h:
  . Add _SPLIT, _FAST2SUM, _SLOW2SUM, _XADD, _MUL, and _XMUL
    macros to perform type-type arthimetic (i.e., float-float).

* src/s_rsqrt.c:
  . New file with the implementation of 'double rsqrt(double)'.
  . For 53-bit long double targets, add a weak reference for rsqrtl.

* src/s_rsqrtf.c:
  . New file with the implementation of 'float rsqrt(float)'.

* src/s_rsqrtl.c
  . New file with the implementation of 'long double rsqrt(long double)'.
    Note, the LD80 version uses bit twiddling and LD128 version is a
    straight C language implementation.  The LD128 is untested due to
    lack of hardware.
Comment 1 Steve Kargl freebsd_committer freebsd_triage 2026-05-10 01:16:04 UTC
Created attachment 270556 [details]
New diff

The new diff corrects two (silly) issues found by
kib@.  It also includes fixes for correct rounding
of rsqrt and rsqrtf (in at least round-to-nearest
mode).
Comment 2 commit-hook freebsd_committer freebsd_triage 2026-05-10 16:38:21 UTC
A commit in branch main references this bug:

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

commit 3085fc9d97bd83785ba3ba43e0378d7d67987d1f
Author:     Steve Kargl <kargl@FreeBSD.org>
AuthorDate: 2026-05-08 14:06:08 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-05-10 16:36:33 +0000

    [libm] implementation of rsqrt, rsqrtf, and rsqrtl

    From the PR:
    The attached diff implements the inverse square root function, i.e,
    rsqrt(x) = 1 / sqrt(x).  Exhaustive testing of the float version
    suggests that it is correctly rounded in round-to-nearest for all
    test values in the range [0x1p-127,0x1p126].
    Exhaustive testing of rsqrt and rsqrtl cannot be done, but 1100M
    values of x for rsqrt and 400M values for rsqrtl were tested.  All
    tested values were correctly rounded.

    I do not have access to LD128 (i.e., IEEE 128-bit floating point)
    hardware, so the implementation of rsqrtl() is untested.

    The following is a summary of changes to source code.

    * lib/msun/Makefile:
      . Add s_rsqrt.c and s_rsqrtf.c to COMMON_SRCS.
      . For non-53-bit long double targets, add s_rsqrtl.c to COMMON_SRCS.
      . Add MLINKS for rsqrt.3, rsqrtf.3, and rsqrtl.3 to sqrt.3.

    * lib/msun/Symbol.map:
      . Add rsqrt, rsqrtf, and rsqrtl to the Symbol map for shared libm.so.

    * lib/msun/man/sqrt.3:
      . Update the sqrt.3 manual page to include information for rsqrt[fl].
      . Note, these function come from ISO C23 (and IEEE-754 2008).

    * lib/msun/src/math.h:
      . Add prototypes for new functions.

    * lib/msun/src/math_private.h:
      . Add _SPLIT, _FAST2SUM, _SLOW2SUM, _XADD, _MUL, and _XMUL
        macros to perform type-type arthimetic (i.e., float-float).

    * src/s_rsqrt.c:
      . New file with the implementation of 'double rsqrt(double)'.
      . For 53-bit long double targets, add a weak reference for rsqrtl.

    * src/s_rsqrtf.c:
      . New file with the implementation of 'float rsqrt(float)'.

    * src/s_rsqrtl.c
      . New file with the implementation of 'long double rsqrt(long double)'.
        Note, the LD80 version uses bit twiddling and LD128 version is a
        straight C language implementation.  The LD128 is untested due to
        lack of hardware.

    PR:     295089
    MFC after:      1 week

 lib/msun/Makefile             |   6 +-
 lib/msun/Symbol.map           |   3 +
 lib/msun/man/sqrt.3           |  53 ++++++++++-
 lib/msun/src/math.h           |   3 +
 lib/msun/src/math_private.h   |  83 +++++++++++++++++
 lib/msun/src/s_rsqrt.c (new)  | 153 +++++++++++++++++++++++++++++++
 lib/msun/src/s_rsqrtf.c (new) | 155 ++++++++++++++++++++++++++++++++
 lib/msun/src/s_rsqrtl.c (new) | 203 ++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 654 insertions(+), 5 deletions(-)
Comment 3 Robert Clausecker freebsd_committer freebsd_triage 2026-05-10 17:39:06 UTC
Oh, didn't realise you had filed a PR!
Great to see it merged!
Comment 4 commit-hook freebsd_committer freebsd_triage 2026-05-17 00:30:02 UTC
A commit in branch stable/15 references this bug:

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

commit 0093bb670537f19e99fff2d46b4831d7b9d44b4c
Author:     Steve Kargl <kargl@FreeBSD.org>
AuthorDate: 2026-05-08 14:06:08 +0000
Commit:     Konstantin Belousov <kib@FreeBSD.org>
CommitDate: 2026-05-17 00:27:48 +0000

    [libm] implementation of rsqrt, rsqrtf, and rsqrtl

    PR:     295089

    (cherry picked from commit 3085fc9d97bd83785ba3ba43e0378d7d67987d1f)

 lib/msun/Makefile             |   6 +-
 lib/msun/Symbol.map           |   3 +
 lib/msun/man/sqrt.3           |  53 ++++++++++-
 lib/msun/src/math.h           |   3 +
 lib/msun/src/math_private.h   |  83 +++++++++++++++++
 lib/msun/src/s_rsqrt.c (new)  | 153 +++++++++++++++++++++++++++++++
 lib/msun/src/s_rsqrtf.c (new) | 155 ++++++++++++++++++++++++++++++++
 lib/msun/src/s_rsqrtl.c (new) | 203 ++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 654 insertions(+), 5 deletions(-)
Comment 5 Paul Zimmermann 2026-05-18 08:09:11 UTC
with attachment 270556 [details], I still get incorrect rounding for rsqrtf:
```
libm wrong by up to 5.01e-01 ulp(s) [1] for x=0x1.13e07p-129
rsqrt      gives 0x1.5cc0a8p+64
mpfr_rsqrt gives 0x1.5cc0aap+64
Total: errors=129 (0.00%) errors2=0 maxerr=5.01e-01 ulp(s)
```
(I copied the auxiliary functions from https://github.com/freebsd/freebsd-src.git.)
Comment 6 Paul Zimmermann 2026-05-18 08:23:53 UTC
I also get errors with the double version:
```
zimmerma@thym:~/svn/core-math$ CORE_MATH_CHECK_STD=true LIBM=/tmp/libm.a ./check.sh rsqrt
Running worst cases check in --rndn mode...
FAIL x=0x1.e60431782ff31p-1 ref=0x1.06c10c599fca7p+0 z=0x0.000002b344e03p-1022
```
but maybe I did a mistake in copying the code. How can I build a libm.a with just rsqrtf, rsqrt and rsqrtl?
Comment 7 Mark Linimon freebsd_committer freebsd_triage 2026-07-18 19:09:15 UTC
(In reply to Paul Zimmermann from comment #6)
Is this still a problem?
Comment 8 Steve Kargl freebsd_committer freebsd_triage 2026-07-18 19:31:22 UTC
(In reply to Mark Linimon from comment #7)
The code committed to head and stable-15 should be correct.
The bug report is still open as it is a candidate for a
merge into the stable-14 branch.

% ./tlibm rsqrt -fPED -s 0 -x min -X max
Interval tested for rsqrtf: [1.17549e-38,3.40282e+38]
1000000 calls, 0.011024 secs, 0.01102 usecs/call
      ulp <= 0.5: 100.000% 2130706431 | 100.000% 2130706431
Max ulp: 0.50000000 at 3.41806599e-38 0x1.7431c6p-125

% ./tlibm rsqrt -da 0x1.e60431782ff31p-1
   x =  9.4925074188356551e-01, /* 0x1.e60431782ff31p-1 */
libm =  1.0263831824120559e+00, /* 0x1.06c10c599fca7p+0 */
mpfr =  1.0263831824120559e+00, /* 0x1.06c10c599fca7p+0 */
 ULP = 0.000000000000020280
Comment 9 Robert Clausecker freebsd_committer freebsd_triage 2026-07-18 19:51:30 UTC
(In reply to Steve Kargl from comment #8)

Do you want to have it merged?  I can arrange for that to happen.
Comment 10 Steve Kargl freebsd_committer freebsd_triage 2026-07-18 20:53:23 UTC
(In reply to Robert Clausecker from comment #9)
I don't use stable-*, so I'll leave to whomever
finds the PR>

In lieu of committing to stable-14, I would rather that
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=295884
is committed to head.
Comment 11 commit-hook freebsd_committer freebsd_triage 2026-07-28 15:45:30 UTC
A commit in branch stable/14 references this bug:

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

commit e35696f0b30507e31c86ad80bd2f4ff770bbbe54
Author:     Steve Kargl <kargl@FreeBSD.org>
AuthorDate: 2026-05-08 14:06:08 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-07-28 15:44:58 +0000

    [libm] implementation of rsqrt, rsqrtf, and rsqrtl

    PR:     295089

    (cherry picked from commit 3085fc9d97bd83785ba3ba43e0378d7d67987d1f)
    (cherry picked from commit 0093bb670537f19e99fff2d46b4831d7b9d44b4c)

 lib/msun/Makefile             |   6 +-
 lib/msun/Symbol.map           |   7 ++
 lib/msun/man/sqrt.3           |  55 +++++++++++-
 lib/msun/src/math.h           |   3 +
 lib/msun/src/math_private.h   |  83 +++++++++++++++++
 lib/msun/src/s_rsqrt.c (new)  | 153 +++++++++++++++++++++++++++++++
 lib/msun/src/s_rsqrtf.c (new) | 155 ++++++++++++++++++++++++++++++++
 lib/msun/src/s_rsqrtl.c (new) | 203 ++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 658 insertions(+), 7 deletions(-)