Bug 294719 - lib/msun: Added fmaximum_mag_num
Summary: lib/msun: Added fmaximum_mag_num
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: standards (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Only Me
Assignee: Robert Clausecker
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2026-04-23 07:11 UTC by Jesús Cipriano Blázquez Martínez
Modified: 2026-06-01 22:35 UTC (History)
2 users (show)

See Also:


Attachments
Differential for the commit (67.27 KB, patch)
2026-04-23 07:11 UTC, Jesús Cipriano Blázquez Martínez
no flags Details | Diff
New differential with newer pattern to force raising the exception (84.36 KB, patch)
2026-04-27 16:52 UTC, Jesús Cipriano Blázquez Martínez
no flags Details | Diff
bit-twiddling implementation and test program (4.48 KB, text/plain)
2026-04-29 16:04 UTC, Steve Kargl
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jesús Cipriano Blázquez Martínez 2026-04-23 07:11:50 UTC
Created attachment 270018 [details]
Differential for the commit

Addition of the fmaximum_mag_num and fminimum_mag_num families, with man page and tests.
Comment 1 Jesús Cipriano Blázquez Martínez 2026-04-23 07:12:03 UTC
I have a question regarding the correctness of my implementation. Isn't it possible that an optimization will turn:

return ((x + y) != 0.0 ? y : y);

into 

return y;

If that's the case, maybe it is better to do:

volatile double force_except = x + y;
(void)force_except;
Comment 2 Robert Clausecker freebsd_committer freebsd_triage 2026-04-23 09:16:48 UTC
If that's the case, maybe it is better to do:

> volatile double force_except = x + y;
> (void)force_except;

Yes, I think that's a good pattern.  Not sure if the void cast to avoid the unused warning is actually needed (check if the compiler complains).
Comment 3 Jesús Cipriano Blázquez Martínez 2026-04-27 16:52:28 UTC
Created attachment 270183 [details]
New differential with newer pattern to force raising the exception

This differentials includes two commits, the first one implements _mag_num with the newer pattern (volatile variable instead of ternary) and the second one updates _num to use the newer one.
Comment 4 Robert Clausecker freebsd_committer freebsd_triage 2026-04-28 13:24:30 UTC
(In reply to Jesús Cipriano Blázquez Martínez from comment #3)

Ok, I guess I was unclear about what I meant by "Not sure if the void cast to avoid the unused warning is actually needed".

I meant that perhaps the whole (void)force_except; statement can be removed.
If it is present, then yes, you should cast (void) to avoid a “statement without effect” warning.  But I think it can be removed entirely.

I'll check which case it is and copy-edit your patch before landing it.

I do also wonder if we can do better in the final tie break.  How about this for fmaximum_mag*()?

    return (signbit(x) ? y : x);

and conversely for fminimum_mag*()?

    return (signbit(x) ? x : y);

This would also apply to the versions with _num() and would allow us to avoid unpacking the datum entirely.

Unfortunately our signbit() macro resolves in such a way that the compiler does not recognise it as such and generates really bad code.  That should be fixed long term.  Maybe short term the current approach is thus better.

kargl, what do you think?
Comment 5 Jesús Cipriano Blázquez Martínez 2026-04-28 14:21:24 UTC
(In reply to Robert Clausecker from comment #4)

My bad. Not having (void) doesn't raise warnings because reading a volatile variable does have side effects, I thought that's what you meant. 

Testing around, as you suspected, deleting the entire line doesn't raise warnings with regular make either (unless it is compiled with -Wunused-variable). It also keeps raising exceptions as expected when deleting the line, meaning the compiler won't ignore the line. 

I can update the patch if it makes your life easier
Comment 6 Robert Clausecker freebsd_committer freebsd_triage 2026-04-28 17:51:15 UTC
(In reply to Jesús Cipriano Blázquez Martínez from comment #5)

No need.  I'm waiting for kargl to have a look at the patch and would then proceed to commit.  If he is fast, we may be able to get this one into 15.1.
Comment 7 Steve Kargl freebsd_committer freebsd_triage 2026-04-28 19:31:45 UTC
(In reply to Robert Clausecker from comment #4)
I don't know the internals for libm's signbit().  Naively,
I would have thought that both gcc and clang provide 
__builtin_signbit() with optimal implementations if
one uses signbit().  I would follow the 'make it
work, make it correct, and then make it fast' maxim.

I haven't figured out what Jesus is trying to do
with the force_except.  Reading the description of
fmaximum_num etc, I would try something like

if (nan_x) {
   if (nan_y)
      return (x + y);

   if (issignaling(x))
      feraiseexcept(FE_INVALID);

   return (y);
}

if (nan_y) {

   if (issignaling(y))
      feraiseexcept(FE_INVALID);

   return (x);
}
Comment 8 Steve Kargl freebsd_committer freebsd_triage 2026-04-28 20:28:57 UTC
(In reply to Steve Kargl from comment #7)
After a bit of code reading, it seems FreeBSD currently
does not have the issignaling() macro.  A quick test
also suggests that neither clang nor gcc15 have a builtin
for issignaling().  Need to some bit twiddling to do
the equivalent.
Comment 9 Robert Clausecker freebsd_committer freebsd_triage 2026-04-28 20:40:55 UTC
(In reply to Steve Kargl from comment #8)

The problem is that our signbit() macro resolves to a __signbit() function, which gcc and clang do not recognise as being the signbit() macro.  This is probably fixable by checking if the compiler has a built-in implementation and then resolving signbit() to __builtin_signbit(), but let's do that another time.

> I haven't figured out what Jesus is trying to do
> with the force_except.  Reading the description of
> fmaximum_num etc, I would try something like

The idea is that we force the compiler to perform an addition on the NaN values.
If either is a signalling NaN, this raises FE_INVALID as usual, avoiding a costly branch and sign-bit check.

We do similar things in other variants of the fmax/fmin family.  Whenever
return (x + y) is used to return a NaN, that too raises FE_INVALID if either is a signalling NaN.

> Need to some bit twiddling to do the equivalent.

I've checked the assembly of a the functions and the bitfield-union approach we use is surprisingly inefficient.  It's probably a good idea to try to avoid doing this sort of stuff if we can help it.
Comment 10 Steve Kargl freebsd_committer freebsd_triage 2026-04-29 16:04:11 UTC
Created attachment 270243 [details]
bit-twiddling implementation and test program

(In reply to Robert Clausecker from comment #9)

I thought I replies yesterday.  Anyway, here's the
bit twiddling for fmaxium_numf().  AFAICT, it meets
the 'make it work.  make it correct.' criteria.
A full self-contained test code, which works on
x86_64, is attached as h4.c.


/*
 * Return the number if one argument is a number and the other is
 * a qNaN or sNaN.  If both arguments are NaNs, a quiet NaN is returned.
 * If an argument is a sNaN, the "invalid" floating-point exception
 * is raised (even though the function returns the number when the other
 * argument is a number.
 */

float
fmaximum_numf(float x, float y)
{
   uint32_t hx, hy, ux, uy;

   GET_FLOAT_WORD(hx, x);
   ux = hx & 0x7fffffff;

   GET_FLOAT_WORD(hy, y);
   uy = hy & 0x7fffffff;

   if (ux > 0x7f800000) {              /* x is NaN. */
      if (uy > 0x7f800000)             /* y is NaN. */
         return (x + y);               /* Return qNaN, raise FE_INVALID. */

      if (ux < 0x7fc00000)             /* x is sNaN. */
         feraiseexcept(FE_INVALID);

      return (y);
   }

   if (uy > 0x7f800000) {             /* y is NaN */
      if (uy < 0x7fc00000)            /* y is sNaN */
         feraiseexcept(FE_INVALID);
      return (x);
   }

   return (((hx >> 31) < (hy >> 31)) || y < x ? x : y);
}
Comment 11 Robert Clausecker freebsd_committer freebsd_triage 2026-05-01 16:33:27 UTC
(In reply to Steve Kargl from comment #10)

Interesting idea.

That said, do you approve of the patch set?  I would like to land it soon.
Comment 12 Steve Kargl freebsd_committer freebsd_triage 2026-05-01 17:59:04 UTC
(In reply to Robert Clausecker from comment #11)
Sure, go ahead.  Commit the changes.

I'll note that I'm not a big fan of the proliferation of
USE_BUILTIN_* in the code.  libm should contain implementations
that do the job without *explicitly* using __builtin_*.  If
a compiler supports a particular builtin, it will use that
builtin when a user compiles code that includes math.h (unless
the user specifically disables that feature with a compiler
option).

Are we really going to have CFLAGS look like

CFLAGS += -DUSE_BUILTIN_FMINIMUM_NUM -DUSE_BUILTIN_FMINIMUM_NUMF \
          -DUSE_BUILTIN_FMINIMUM_MAG_NUM -DUSE_BUILTIN_FMINIMUM_MAG_NUMF \
          -DUSE_BUILTIN_FMINIMUM_MAG -DUSE_BUILTIN_FMINIMUM_MAGF \
          -DUSE_BUILTIN_FMAXIMUM_NUM -DUSE_BUILTIN_FMAXIMUM_NUMF \
          -DUSE_BUILTIN_FMAXIMUM_MAG_NUM -DUSE_BUILTIN_FMAXIMUM_MAG_NUMF \
          -DUSE_BUILTIN_FMAXIMUM_MAG -DUSE_BUILTIN_FMAXIMUM_MAGF


If you're going to support the above, then why are the
long double builtins missing? 
          -DUSE_BUILTIN_FMAXIMUM_MAGL \
          -DUSE_BUILTIN_FMINIMUM_NUML \
          -DUSE_BUILTIN_FMINIMUM_MAG_NUML \
          -DUSE_BUILTIN_FMINIMUM_MAGL \
          -DUSE_BUILTIN_FMAXIMUM_NUML \
          -DUSE_BUILTIN_FMAXIMUM_MAG_NUML

gcc.info does not list fmaximum* and fminimum* as 
having builtins.  I cannot locate a list of math
builtins for clang.  It seems pointless to clutter
the code.  If you're going to insist on the use
of builtins, then make it an all or nothing macro
-DUSE_BUILTINS.
Comment 13 commit-hook freebsd_committer freebsd_triage 2026-05-02 14:56:51 UTC
A commit in branch main references this bug:

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

commit 7c20e15592a07ea457cacb0d6706948815c8420e
Author:     Jesús Blázquez <jesuscblazquez@gmail.com>
AuthorDate: 2026-04-27 16:43:41 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-05-02 14:50:49 +0000

    lib/msun: Replaced pattern to force exception in _num families

    Replaced the old pattern of using a ternary to force addition
    (raising exceptions for sNaN's) with a new one using a volatile
    variable. The _mag_num family was already implemented with this pattern

    PR:             294719
    Reviewed by:    fuz, kargl
    MFC after:      1 month

 lib/msun/src/s_fmaximum_num.c  | 14 ++++++++++----
 lib/msun/src/s_fmaximum_numf.c | 14 ++++++++++----
 lib/msun/src/s_fmaximum_numl.c | 14 ++++++++++----
 lib/msun/src/s_fminimum_num.c  | 14 ++++++++++----
 lib/msun/src/s_fminimum_numf.c | 14 ++++++++++----
 lib/msun/src/s_fminimum_numl.c | 14 ++++++++++----
 6 files changed, 60 insertions(+), 24 deletions(-)
Comment 14 commit-hook freebsd_committer freebsd_triage 2026-05-02 14:56:57 UTC
A commit in branch main references this bug:

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

commit f62d826a6f5b9022b0cedfe22a698998ad9cb7f4
Author:     Jesús Blázquez <jesuscblazquez@gmail.com>
AuthorDate: 2026-04-21 17:17:00 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-05-02 14:50:13 +0000

    lib/msun: fmaximum_mag_num family. Tests and man page

    Added the fmaximum_mag_num{,f,l} and fminimum_mag_num{,f,l} functions.

    PR:             294719
    Reviewed by:    fuz, kargl
    MFC after:      1 month

 lib/msun/Makefile                        |  18 +++--
 lib/msun/Symbol.map                      |  18 +++--
 lib/msun/man/fmaximum_mag.3              |   2 +-
 lib/msun/man/fmaximum_mag_num.3 (new)    | 109 +++++++++++++++++++++++++++++++
 lib/msun/src/math.h                      |  18 +++--
 lib/msun/src/s_fmaximum_mag_num.c (new)  |  88 +++++++++++++++++++++++++
 lib/msun/src/s_fmaximum_mag_numf.c (new) |  84 ++++++++++++++++++++++++
 lib/msun/src/s_fmaximum_mag_numl.c (new) |  77 ++++++++++++++++++++++
 lib/msun/src/s_fminimum_mag_num.c (new)  |  90 +++++++++++++++++++++++++
 lib/msun/src/s_fminimum_mag_numf.c (new) |  85 ++++++++++++++++++++++++
 lib/msun/src/s_fminimum_mag_numl.c (new) |  79 ++++++++++++++++++++++
 lib/msun/tests/fmaximum_fminimum_test.c  |  28 +++++++-
 12 files changed, 677 insertions(+), 19 deletions(-)
Comment 15 commit-hook freebsd_committer freebsd_triage 2026-05-02 14:57:02 UTC
A commit in branch main references this bug:

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

commit 9f98195ff615417f7af875b65cdaf220239206db
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2026-05-02 14:53:08 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-05-02 14:53:08 +0000

    man/math.3: mention fmaximum_mag_num, fminimum_mag_num

    PR:             294719
    MFC after:      1 month

 lib/msun/man/math.3 | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Comment 16 Robert Clausecker freebsd_committer freebsd_triage 2026-05-02 14:57:46 UTC
Thank you for your contribution.
Comment 17 commit-hook freebsd_committer freebsd_triage 2026-06-01 22:35:08 UTC
A commit in branch stable/15 references this bug:

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

commit f8a72f48951fc9f156f122ef6ba5181df85a12e6
Author:     Jesús Blázquez <jesuscblazquez@gmail.com>
AuthorDate: 2026-04-27 16:43:41 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-06-01 22:33:34 +0000

    lib/msun: Replaced pattern to force exception in _num families

    Replaced the old pattern of using a ternary to force addition
    (raising exceptions for sNaN's) with a new one using a volatile
    variable. The _mag_num family was already implemented with this pattern

    PR:             294719
    Reviewed by:    fuz, kargl
    MFC after:      1 month

    (cherry picked from commit 7c20e15592a07ea457cacb0d6706948815c8420e)

 lib/msun/src/s_fmaximum_num.c  | 14 ++++++++++----
 lib/msun/src/s_fmaximum_numf.c | 14 ++++++++++----
 lib/msun/src/s_fmaximum_numl.c | 14 ++++++++++----
 lib/msun/src/s_fminimum_num.c  | 14 ++++++++++----
 lib/msun/src/s_fminimum_numf.c | 14 ++++++++++----
 lib/msun/src/s_fminimum_numl.c | 14 ++++++++++----
 6 files changed, 60 insertions(+), 24 deletions(-)
Comment 18 commit-hook freebsd_committer freebsd_triage 2026-06-01 22:35:08 UTC
A commit in branch stable/15 references this bug:

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

commit c485a0b8388904d115ce10bbd68fa33486cb23e3
Author:     Robert Clausecker <fuz@FreeBSD.org>
AuthorDate: 2026-05-02 14:53:08 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-06-01 22:33:34 +0000

    man/math.3: mention fmaximum_mag_num, fminimum_mag_num

    PR:             294719
    MFC after:      1 month

    (cherry picked from commit 9f98195ff615417f7af875b65cdaf220239206db)

 lib/msun/man/math.3 | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
Comment 19 commit-hook freebsd_committer freebsd_triage 2026-06-01 22:35:09 UTC
A commit in branch stable/15 references this bug:

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

commit 9bba4a22504180974bfc009f25502032d7a3232f
Author:     Jesús Blázquez <jesuscblazquez@gmail.com>
AuthorDate: 2026-04-21 17:17:00 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-06-01 22:33:34 +0000

    lib/msun: fmaximum_mag_num family. Tests and man page

    Added the fmaximum_mag_num{,f,l} and fminimum_mag_num{,f,l} functions.

    PR:             294719
    Reviewed by:    fuz, kargl
    MFC after:      1 month

    (cherry picked from commit f62d826a6f5b9022b0cedfe22a698998ad9cb7f4)

 lib/msun/Makefile                        |  18 +++--
 lib/msun/Symbol.map                      |  18 +++--
 lib/msun/man/fmaximum_mag.3              |   2 +-
 lib/msun/man/fmaximum_mag_num.3 (new)    | 109 +++++++++++++++++++++++++++++++
 lib/msun/src/math.h                      |  18 +++--
 lib/msun/src/s_fmaximum_mag_num.c (new)  |  88 +++++++++++++++++++++++++
 lib/msun/src/s_fmaximum_mag_numf.c (new) |  84 ++++++++++++++++++++++++
 lib/msun/src/s_fmaximum_mag_numl.c (new) |  77 ++++++++++++++++++++++
 lib/msun/src/s_fminimum_mag_num.c (new)  |  90 +++++++++++++++++++++++++
 lib/msun/src/s_fminimum_mag_numf.c (new) |  85 ++++++++++++++++++++++++
 lib/msun/src/s_fminimum_mag_numl.c (new) |  79 ++++++++++++++++++++++
 lib/msun/tests/fmaximum_fminimum_test.c  |  28 +++++++-
 12 files changed, 677 insertions(+), 19 deletions(-)