Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 15 Feb 2021 18:13:27 GMT
From:      Dimitry Andric <dim@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 720600023287 - stable/13 - Fix incorrect powf(3) result with x near 1 and |y| much larger than 1
Message-ID:  <202102151813.11FIDR9h093485@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/13 has been updated by dim:

URL: https://cgit.FreeBSD.org/src/commit/?id=720600023287cd029894b79477fb2bd4a5183ba4

commit 720600023287cd029894b79477fb2bd4a5183ba4
Author:     Steve Kargl <sgk@troutmask.apl.washington.edu>
AuthorDate: 2021-02-08 19:45:30 +0000
Commit:     Dimitry Andric <dim@FreeBSD.org>
CommitDate: 2021-02-15 18:08:20 +0000

    Fix incorrect powf(3) result with x near 1 and |y| much larger than 1
    
    This adjusts the check to trigger overflow/underflow to a slightly lower
    value.
    
    Before: powf(9.999995e-01, -1.342177e+08) -> inf
    After:  powf(9.999995e-01, -1.342177e+08) -> 1.858724e+31
    
    (cherry picked from commit 93fc67896550548f91b307dbe3053f11db5d4a8a)
    
    Add test case for 93fc67896550 (incorrect powf(3) result)
    
    This adds the test case to contrib/netbsd-tests/lib/libm/t_pow.c, as it
    is currently the only place testing pow(3) and friends.
    
    (cherry picked from commit 51af03328755c9095e94d20858a8d10acfe412ae)
---
 contrib/netbsd-tests/lib/libm/t_pow.c | 22 ++++++++++++++++++++++
 lib/msun/src/e_powf.c                 |  2 +-
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/contrib/netbsd-tests/lib/libm/t_pow.c b/contrib/netbsd-tests/lib/libm/t_pow.c
index 7afee9f6dffa..09e72be7311b 100644
--- a/contrib/netbsd-tests/lib/libm/t_pow.c
+++ b/contrib/netbsd-tests/lib/libm/t_pow.c
@@ -637,6 +637,27 @@ ATF_TC_BODY(powf_zero_y, tc)
 	}
 }
 
+ATF_TC(powf_near_one_x_huge_y);
+ATF_TC_HEAD(powf_near_one_x_huge_y, tc)
+{
+	atf_tc_set_md_var(tc, "descr", "Test powf(->1, huge) != inf");
+}
+
+ATF_TC_BODY(powf_near_one_x_huge_y, tc)
+{
+	const float x =  0x1.ffffeep-1f;   /*  9.999995e-01f */
+	const float y = -0x1.000002p+27f;  /* -1.342177e+08f */
+	const float e =  0x1.d53532p+103f; /*  1.858724e+31f */
+	const float ulp = __FLT_EPSILON__;
+	float z;
+
+	z = powf(x, y);
+
+	ATF_CHECK(isinf(z) == 0);
+	ATF_CHECK(fabsf(z - e) <= 2 * ulp);
+}
+
+
 ATF_TP_ADD_TCS(tp)
 {
 
@@ -661,6 +682,7 @@ ATF_TP_ADD_TCS(tp)
 	ATF_TP_ADD_TC(tp, powf_one_pos_x);
 	ATF_TP_ADD_TC(tp, powf_zero_x);
 	ATF_TP_ADD_TC(tp, powf_zero_y);
+	ATF_TP_ADD_TC(tp, powf_near_one_x_huge_y);
 
 	return atf_no_error();
 }
diff --git a/lib/msun/src/e_powf.c b/lib/msun/src/e_powf.c
index 53f1d37d6bec..33eedad50b16 100644
--- a/lib/msun/src/e_powf.c
+++ b/lib/msun/src/e_powf.c
@@ -136,7 +136,7 @@ __ieee754_powf(float x, float y)
     /* |y| is huge */
 	if(iy>0x4d000000) { /* if |y| > 2**27 */
 	/* over/underflow if x is not close to one */
-	    if(ix<0x3f7ffff8) return (hy<0)? sn*huge*huge:sn*tiny*tiny;
+	    if(ix<0x3f7ffff7) return (hy<0)? sn*huge*huge:sn*tiny*tiny;
 	    if(ix>0x3f800007) return (hy>0)? sn*huge*huge:sn*tiny*tiny;
 	/* now |1-x| is tiny <= 2**-20, suffice to compute
 	   log(x) by x-x^2/2+x^3/3-x^4/4 */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202102151813.11FIDR9h093485>