[PATCH] newlib: libm: nexttoward: test the sign bit for the direction

Bernie Innocenti bernie@codewiz.org
Thu Oct 29 22:10:05 GMT 2026


hx > 0 is false for the smallest subnormals, whose high word is 0, so
nexttoward() stepped them away from y: nexttoward(DBL_TRUE_MIN, 0) gave
2 * DBL_TRUE_MIN.  x == 0 is handled before, so hx >= 0 means x > 0.

Signed-off-by: Bernie Innocenti <bernie@codewiz.org>
Assisted-By: Claude Opus 5 <noreply@anthropic.com>
---
 newlib/libm/ld/s_nexttoward.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/newlib/libm/ld/s_nexttoward.c b/newlib/libm/ld/s_nexttoward.c
index b2a50d313..36ff1de3d 100644
--- a/newlib/libm/ld/s_nexttoward.c
+++ b/newlib/libm/ld/s_nexttoward.c
@@ -51,7 +51,7 @@ nexttoward(double x, long double y)
 	    t = x*x;
 	    if(t==x) return t; else return x;	/* raise underflow flag */
 	}
-	if(hx>0.0 ^ x < y) {			/* x -= ulp */
+	if((hx>=0) ^ (x < y)) {			/* x -= ulp */
 	    if(lx==0) hx -= 1;
 	    lx -= 1;
 	} else {				/* x += ulp */
-- 
2.56.0



More information about the Newlib mailing list