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

Message ID 20241205083956.1644399-1-kito.cheng@sifive.com
State New
Headers
Series libm/common: Fix nextafter and nextafterf when x == y |

Commit Message

Kito Cheng Dec. 5, 2024, 8:39 a.m. UTC
  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(-)
  

Comments

Corinna Vinschen Dec. 5, 2024, 10:58 a.m. UTC | #1
Pushed.

Thanks,
Corinna


On Dec  5 16:39, Kito Cheng wrote:
> 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
  

Patch

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;