git: 6ccf7433398c - stable/14 - msun: fix behaviour of nextafter*() and nexttoward*() for MAX value

From: Robert Clausecker <fuz_at_FreeBSD.org>
Date: Fri, 06 Nov 2026 10:50:39 UTC
The branch stable/14 has been updated by fuz:

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

commit 6ccf7433398c7dea803a9c1dbc6fc8d77d638403
Author:     Steve Kargl <kargl@FreeBSD.org>
AuthorDate: 2026-10-31 10:18:06 +0000
Commit:     Robert Clausecker <fuz@FreeBSD.org>
CommitDate: 2026-11-06 10:50:31 +0000

    msun: fix behaviour of nextafter*() and nexttoward*() for MAX value
    
    For an edge case at an extreme, e.g., nextafter(-DBL_MAX,-INFINITY)
    do not return x+x.  Instead, evaluate x+x and assign it to a volatile
    to raise an exception, and then fall through to return the correct
    result in current rounding mode.
    
    PR:             299389
    MFC after:      6 days (for inclusion into releng/15.2)
    
    (cherry picked from commit 1fb5d8265bc4bbbf1f5bc79cc1d2a7f280b446b2)
---
 lib/msun/src/s_nextafter.c   | 2 +-
 lib/msun/src/s_nextafterf.c  | 2 +-
 lib/msun/src/s_nextafterl.c  | 2 +-
 lib/msun/src/s_nexttoward.c  | 2 +-
 lib/msun/src/s_nexttowardf.c | 2 +-
 5 files changed, 5 insertions(+), 5 deletions(-)

diff --git a/lib/msun/src/s_nextafter.c b/lib/msun/src/s_nextafter.c
index 8316ba41d78c..5a142c121b97 100644
--- a/lib/msun/src/s_nextafter.c
+++ b/lib/msun/src/s_nextafter.c
@@ -61,7 +61,7 @@ nextafter(double x, double y)
 	    }
 	}
 	hy = hx&0x7ff00000;
-	if(hy>=0x7ff00000) return x+x;	/* overflow  */
+	if(hy>=0x7ff00000) t = x+x;	/* overflow  */
 	if(hy<0x00100000) {		/* underflow */
 	    t = x*x;
 	    if(t!=x) {		/* raise underflow flag */
diff --git a/lib/msun/src/s_nextafterf.c b/lib/msun/src/s_nextafterf.c
index 418b126a27ab..379d00f5f390 100644
--- a/lib/msun/src/s_nextafterf.c
+++ b/lib/msun/src/s_nextafterf.c
@@ -50,7 +50,7 @@ nextafterf(float x, float y)
 	    }
 	}
 	hy = hx&0x7f800000;
-	if(hy>=0x7f800000) return x+x;	/* overflow  */
+	if(hy>=0x7f800000) t = x+x;	/* overflow  */
 	if(hy<0x00800000) {		/* underflow */
 	    t = x*x;
 	    if(t!=x) {		/* raise underflow flag */
diff --git a/lib/msun/src/s_nextafterl.c b/lib/msun/src/s_nextafterl.c
index 7df6c008910d..1abd1e103df2 100644
--- a/lib/msun/src/s_nextafterl.c
+++ b/lib/msun/src/s_nextafterl.c
@@ -64,7 +64,7 @@ nextafterl(long double x, long double y)
 		    ux.bits.exp += 1;
 	    }
 	}
-	if(ux.bits.exp==0x7fff) return x+x;	/* overflow  */
+	if(ux.bits.exp==0x7fff) t = x+x;	/* overflow  */
 	if(ux.bits.exp==0) {			/* underflow */
 	    mask_nbit_l(ux);
 	    t = ux.e * ux.e;
diff --git a/lib/msun/src/s_nexttoward.c b/lib/msun/src/s_nexttoward.c
index ef4da47f6408..253d440d31b9 100644
--- a/lib/msun/src/s_nexttoward.c
+++ b/lib/msun/src/s_nexttoward.c
@@ -56,7 +56,7 @@ nexttoward(double x, long double y)
 	    if(lx==0) hx += 1;
 	}
 	ix = hx&0x7ff00000;
-	if(ix>=0x7ff00000) return x+x;	/* overflow  */
+	if(ix>=0x7ff00000) t = x+x;	/* overflow  */
 	if(ix<0x00100000) {		/* underflow */
 	    t = x*x;
 	    if(t!=x) {		/* raise underflow flag */
diff --git a/lib/msun/src/s_nexttowardf.c b/lib/msun/src/s_nexttowardf.c
index 05c89f4554ec..50325be8c184 100644
--- a/lib/msun/src/s_nexttowardf.c
+++ b/lib/msun/src/s_nexttowardf.c
@@ -43,7 +43,7 @@ nexttowardf(float x, long double y)
 	else					/* x += ulp */
 	    hx += 1;
 	ix = hx&0x7f800000;
-	if(ix>=0x7f800000) return x+x;	/* overflow  */
+	if(ix>=0x7f800000)t = x+x;	/* overflow  */
 	if(ix<0x00800000) {		/* underflow */
 	    t = x*x;
 	    if(t!=x) {		/* raise underflow flag */