[PATCH] libm/common: Fix nextafter and nextafterf when x == y

Kito Cheng <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
That according to C99/POSIX, nextafter(x,y) should return y if x==y.

[1] NetBSD fix for this: https://github.com/IIJ-NetBSD/netbsd-src/commit/3bc685224189d2b7dfb68da52d9725a256a667bd
[2] glibc fix for this: https://github.com/bminor/glibc/commit/bc9f6000f6752153e5e1902259d5f491a88a1ae5#diff-bcc0628a39c3c2003047dcb5a40a8b50c00f01a74b1c8c1100d770a8e48b1ce2
[3] Linux man page: https://man7.org/linux/man-pages/man3/nextafter.3.html
---
 newlib/libm/common/s_nextafter.c  | 2 +-
 newlib/libm/common/sf_nextafter.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/newlib/libm/common/s_nextafter.c b/newlib/libm/common/s_nextafter.c
index 9453a0a00..356c79f3d 100644
--- a/newlib/libm/common/s_nextafter.c
+++ b/newlib/libm/common/s_nextafter.c
@@ -70,7 +70,7 @@ PORTABILITY
 	if(((ix>=0x7ff00000)&&((ix-0x7ff00000)|lx)!=0) ||   /* x is nan */ 
 	   ((iy>=0x7ff00000)&&((iy-0x7ff00000)|ly)!=0))     /* y is nan */ 
 	   return x+y;				
-	if(x==y) return x;		/* x=y, return x */
+	if(x==y) return y;		/* x=y, return y */
 	if((ix|lx)==0) {			/* x == 0 */
 	    INSERT_WORDS(x,hy&0x80000000,1);	/* return +-minsubnormal */
 	    y = x*x;
diff --git a/newlib/libm/common/sf_nextafter.c b/newlib/libm/common/sf_nextafter.c
index cea4da58d..cdc7c663d 100644
--- a/newlib/libm/common/sf_nextafter.c
+++ b/newlib/libm/common/sf_nextafter.c
@@ -32,7 +32,7 @@
 	if(FLT_UWORD_IS_NAN(ix) ||
 	   FLT_UWORD_IS_NAN(iy))
 	   return x+y;
-	if(x==y) return x;		/* x=y, return x */
+	if(x==y) return y;		/* x=y, return y */
 	if(FLT_UWORD_IS_ZERO(ix)) {		/* x == 0 */
 	    SET_FLOAT_WORD(x,(hy&0x80000000)|FLT_UWORD_MIN);
 	    y = x*x;
-- 
2.34.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.