newlib: libm: nexttoward: test the sign bit for the direction

Message ID 20261029221005.172399-1-bernie@codewiz.org
State New
Headers
Series newlib: libm: nexttoward: test the sign bit for the direction |

Commit Message

Bernie Innocenti Oct. 29, 2026, 10:10 p.m. UTC
  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(-)
  

Patch

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 */