Fix modf/f for NaN input

Message ID 20200318131820.1269-1-fabian.schriever@gtd-gmbh.de
State Accepted
Commit 4ad9ba42fc3dd116bad8b9cb89d434256d3431fb
Headers show
Series
  • Fix modf/f for NaN input
Related show

Commit Message

Fabian Schriever March 18, 2020, 1:18 p.m.
For NaN input the modf/f procedures should return NaN instead of zero
with the sign of the input.
---
 newlib/libm/common/s_modf.c  | 1 +
 newlib/libm/common/sf_modf.c | 1 +
 2 files changed, 2 insertions(+)

-- 
2.24.1.windows.2

Comments

Keith Packard via Newlib March 19, 2020, 3:43 p.m. | #1
On Mar 18 14:18, Fabian Schriever wrote:
> For NaN input the modf/f procedures should return NaN instead of zero

> with the sign of the input.

> ---

>  newlib/libm/common/s_modf.c  | 1 +

>  newlib/libm/common/sf_modf.c | 1 +

>  2 files changed, 2 insertions(+)

> 

> diff --git a/newlib/libm/common/s_modf.c b/newlib/libm/common/s_modf.c

> index 8551a99e4..c948b8525 100644

> --- a/newlib/libm/common/s_modf.c

> +++ b/newlib/libm/common/s_modf.c

> @@ -100,6 +100,7 @@ static double one = 1.0;

>  	} else if (j0>51) {		/* no fraction part */

>  	    __uint32_t high;

>  	    *iptr = x*one;

> +	    if (__fpclassifyd(x) == FP_NAN) return x+x; /* x is NaN, return NaN */

>  	    GET_HIGH_WORD(high,x);

>  	    INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */

>  	    return x;

> diff --git a/newlib/libm/common/sf_modf.c b/newlib/libm/common/sf_modf.c

> index 6c64e3fa0..ae970762b 100644

> --- a/newlib/libm/common/sf_modf.c

> +++ b/newlib/libm/common/sf_modf.c

> @@ -52,6 +52,7 @@ static float one = 1.0;

>  	} else {			/* no fraction part */

>  	    __uint32_t ix;

>  	    *iptr = x*one;

> +	    if (__fpclassifyf(x) == FP_NAN) return x+x; /* x is NaN, return NaN */

>  	    GET_FLOAT_WORD(ix,x);

>  	    SET_FLOAT_WORD(x,ix&0x80000000);	/* return +-0 */

>  	    return x;

> -- 

> 2.24.1.windows.2

> 


Pushed.


Thanks,
Corinna

-- 
Corinna Vinschen
Cygwin Maintainer
Red Hat

Patch

diff --git a/newlib/libm/common/s_modf.c b/newlib/libm/common/s_modf.c
index 8551a99e4..c948b8525 100644
--- a/newlib/libm/common/s_modf.c
+++ b/newlib/libm/common/s_modf.c
@@ -100,6 +100,7 @@  static double one = 1.0;
 	} else if (j0>51) {		/* no fraction part */
 	    __uint32_t high;
 	    *iptr = x*one;
+	    if (__fpclassifyd(x) == FP_NAN) return x+x; /* x is NaN, return NaN */
 	    GET_HIGH_WORD(high,x);
 	    INSERT_WORDS(x,high&0x80000000,0);	/* return +-0 */
 	    return x;
diff --git a/newlib/libm/common/sf_modf.c b/newlib/libm/common/sf_modf.c
index 6c64e3fa0..ae970762b 100644
--- a/newlib/libm/common/sf_modf.c
+++ b/newlib/libm/common/sf_modf.c
@@ -52,6 +52,7 @@  static float one = 1.0;
 	} else {			/* no fraction part */
 	    __uint32_t ix;
 	    *iptr = x*one;
+	    if (__fpclassifyf(x) == FP_NAN) return x+x; /* x is NaN, return NaN */
 	    GET_FLOAT_WORD(ix,x);
 	    SET_FLOAT_WORD(x,ix&0x80000000);	/* return +-0 */
 	    return x;