BUG in libm's powf

From: Steve Kargl <sgk_at_troutmask.apl.washington.edu>
Date: Mon, 6 Sep 2021 08:28:36 -0700
Paul Zimmermann has identified a bug in Openlibm's powf(),
which is identical to FreeBSD's libm.  Both derived from
fdlibm. https://github.com/JuliaMath/openlibm/issues/212.

Consider

% cat h.c
#include <math.h>
#include <stdio.h>
int
main(void)
{
   float x, y, z;
   x =  0x1.ffffecp-1F;
   y = -0x1.000002p+27F;
   z =  0x1.557a86p115F;
   printf("%e %e %e <-- should be %e\n", x, y, powf(x,y), z);
   return 0;
}

% cc -o h -fno-builtin h.c -lm && ./h
9.999994e-01 -1.342177e+08 inf <-- should be 5.540807e+34

Note, clang seems to have a builtin for powf(), but one cannot
count of clang being the only consumer of libm.  With the patch
at the end of this email, I get

% cc -o h -fno-builtin h.c -L/home/kargl/trunk/math/libm/msun -lmath && ./h
9.999994e-01 -1.342177e+08 5.540807e+34 <-- should be 5.540807e+34

Watch for copy and paste whitespace corruption.

--- /usr/src/lib/msun/src/e_powf.c	2021-02-21 03:29:00.956878000 -0800
+++ src/e_powf.c	2021-09-06 08:17:09.800008000 -0700
_at__at_ -136,7 +136,7 _at__at_
     /* |y| is huge */
 	if(iy>0x4d000000) { /* if |y| > 2**27 */
 	/* over/underflow if x is not close to one */
-	    if(ix<0x3f7ffff7) return (hy<0)? sn*huge*huge:sn*tiny*tiny;
+	    if(ix<0x3f7ffff6) 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 */


-- 
Steve
Received on Mon Sep 06 2021 - 15:28:36 UTC

Original text of this message