From 2eafcc78dfe6d356a8d6debe439c8aa9839b1dbe Mon Sep 17 00:00:00 2001 From: Keith Packard via Newlib Date: Tue, 4 Aug 2020 15:22:21 -0700 Subject: [PATCH] libm/math: Make yx functions set errno=ERANGE for x=0 The y0, y1 and yn functions need separate conditions when x is zero as that returns ERANGE instead of EDOM. Also stop adjusting the return value from the __ieee754_y* functions as that is already correct and we were just breaking it. Signed-off-by: Keith Packard --- newlib/libm/math/w_j0.c | 14 ++++++++------ newlib/libm/math/w_j1.c | 14 ++++++++------ newlib/libm/math/w_jn.c | 14 ++++++++------ newlib/libm/math/wf_j0.c | 19 ++++++++++--------- newlib/libm/math/wf_j1.c | 14 ++++++++------ newlib/libm/math/wf_jn.c | 19 ++++++++++--------- 6 files changed, 52 insertions(+), 42 deletions(-) diff --git a/newlib/libm/math/w_j0.c b/newlib/libm/math/w_j0.c index b537ef406..47063ff6f 100644 --- a/newlib/libm/math/w_j0.c +++ b/newlib/libm/math/w_j0.c @@ -128,17 +128,19 @@ None of the Bessel functions are in ANSI C. double z; z = __ieee754_y0(x); if(_LIB_VERSION == _IEEE_ || isnan(x) ) return z; - if(x <= 0.0){ - /* y0(0) = -inf or y0(x<0) = NaN */ + if(x < 0.0){ + /* y0(x<0) = NaN */ errno = EDOM; - return -HUGE_VAL; + } + if(x == 0.0){ + /* y0(0) = -inf */ + errno = ERANGE; } if(x>X_TLOSS) { /* y0(x>X_TLOSS) */ errno = ERANGE; - return 0.0; - } else - return z; + } + return z; #endif } diff --git a/newlib/libm/math/w_j1.c b/newlib/libm/math/w_j1.c index 6f25fea27..0912b6776 100644 --- a/newlib/libm/math/w_j1.c +++ b/newlib/libm/math/w_j1.c @@ -55,17 +55,19 @@ double z; z = __ieee754_y1(x); if(_LIB_VERSION == _IEEE_ || isnan(x) ) return z; - if(x <= 0.0){ - /* y1(0) = -inf or y1(x<0) = NaN */ + if(x < 0.0){ + /* y1(x<0) = NaN */ errno = EDOM; - return -HUGE_VAL; + } + if(x == 0.0){ + /* y1(0) = -inf */ + errno = ERANGE; } if(x>X_TLOSS) { /* y1(x>X_TLOSS) */ errno = ERANGE; - return 0.0; - } else - return z; + } + return z; #endif } diff --git a/newlib/libm/math/w_jn.c b/newlib/libm/math/w_jn.c index 28a9abc07..4e770ea5c 100644 --- a/newlib/libm/math/w_jn.c +++ b/newlib/libm/math/w_jn.c @@ -77,17 +77,19 @@ double z; z = __ieee754_yn(n,x); if(_LIB_VERSION == _IEEE_ || isnan(x) ) return z; - if(x <= 0.0){ - /* yn(n,0) = -inf or yn(x<0) = NaN */ + if(x < 0.0){ + /* yn(x<0) = NaN */ errno = EDOM; - return -HUGE_VAL; + } + if(x == 0.0){ + /* yn(0) = -inf */ + errno = ERANGE; } if(x>X_TLOSS) { /* yn(x>X_TLOSS) */ errno = ERANGE; - return 0.0; - } else - return z; + } + return z; #endif } diff --git a/newlib/libm/math/wf_j0.c b/newlib/libm/math/wf_j0.c index 4d125db79..ed21a01f5 100644 --- a/newlib/libm/math/wf_j0.c +++ b/newlib/libm/math/wf_j0.c @@ -35,9 +35,8 @@ if(fabsf(x)>(float)X_TLOSS) { /* j0f(|x|>X_TLOSS) */ errno = ERANGE; - return 0.0f; - } else - return z; + } + return z; #endif } @@ -54,17 +53,19 @@ float z; z = __ieee754_y0f(x); if(_LIB_VERSION == _IEEE_ || isnan(x) ) return z; - if(x <= (float)0.0){ - /* y0f(0) = -inf or y0f(x<0) = NaN */ + if(x < 0.0f){ + /* y0f(x<0) = NaN */ errno = EDOM; - return -HUGE_VALF; } + if (x == 0.0f){ + /* y0f(n,0) = -inf */ + errno = ERANGE; + } if(x>(float)X_TLOSS) { /* y0f(x>X_TLOSS) */ errno = ERANGE; - return 0.0f; - } else - return z; + } + return z; #endif } diff --git a/newlib/libm/math/wf_j1.c b/newlib/libm/math/wf_j1.c index e178273c5..a4609ba28 100644 --- a/newlib/libm/math/wf_j1.c +++ b/newlib/libm/math/wf_j1.c @@ -56,17 +56,19 @@ float z; z = __ieee754_y1f(x); if(_LIB_VERSION == _IEEE_ || isnan(x) ) return z; - if(x <= 0.0f){ - /* y1f(0) = -inf or y1f(x<0) = NaN */ + if(x < 0.0f){ + /* y1f(x<0) = NaN */ errno = EDOM; - return -HUGE_VALF; } + if (x == 0.0f){ + /* y1f(n,0) = -inf */ + errno = ERANGE; + } if(x>(float)X_TLOSS) { /* y1f(x>X_TLOSS) */ errno = ERANGE; - return 0.0f; - } else - return z; + } + return z; #endif } diff --git a/newlib/libm/math/wf_jn.c b/newlib/libm/math/wf_jn.c index 3e4632ead..b82346d79 100644 --- a/newlib/libm/math/wf_jn.c +++ b/newlib/libm/math/wf_jn.c @@ -33,9 +33,8 @@ if(fabsf(x)>(float)X_TLOSS) { /* jnf(|x|>X_TLOSS) */ errno = ERANGE; - return 0.0f; - } else - return z; + } + return z; #endif } @@ -52,17 +51,19 @@ float z; z = __ieee754_ynf(n,x); if(_LIB_VERSION == _IEEE_ || isnan(x) ) return z; - if(x <= 0.0f){ - /* ynf(n,0) = -inf or ynf(x<0) = NaN */ + if(x < 0.0f){ + /* ynf(x<0) = NaN */ errno = EDOM; - return -HUGE_VALF; } + if (x == 0.0f){ + /* ynf(n,0) = -inf */ + errno = ERANGE; + } if(x>(float)X_TLOSS) { /* ynf(x>X_TLOSS) */ errno = ERANGE; - return 0.0f; - } else - return z; + } + return z; #endif } -- 2.43.5