[PATCH] problems in hypotf math function

Bob Wilson bwilson@tensilica.com
Mon Aug 1 21:08:00 GMT 2005


The hypotf function in sysdeps/ieee754/flt-32/e_hypotf.c has problems with 
scaling small input values.

Values that are not denormalized are scaled by directly incrementing the 
exponent.  The increment value in this case is not a floating-point value and 
must not include the exponent bias.  The current code for hypotf has this wrong 
-- it includes the exponent bias in the increment.  Richard Sandiford fixed this 
in newlib (http://sources.redhat.com/ml/newlib-cvs/2002-q1/msg00048.html) when 
he changed it to scale by 2^68 instead of 2^60.  I don't know which scaling 
factor (60 vs. 68) is better, but the exponent bias needs to be removed from the 
increment in either case.

For denormal values, the scaling requires a floating-point multiplication to 
normalize the mantissa.  The 2^126 scale factor in this case is a floating-point 
value and must include the exponent bias.  It currently does not.  I just 
submitted a patch for this in newlib.  The test program below will demonstrate 
the problem when run on a platform using this version of hypotf (e.g., RedHat 
Enterprise 3 on x86_64) -- without the patch, the value returned by hypotf will 
be zero.

#include <math.h>
unsigned int ta = 0x80000002;
main ()
{
   float a = *(float *)&ta;
   float c = hypotf(a, a);
   float d = hypot(a, a);
   printf("a = %e hypotf(a,a) = %e hypot(a,a) = %e\n", a, c, d);
}

The attached patch combines my fix for denormal values and Richard Sandiford's 
change from newlib.  Presumably he had a good reason for changing the scaling 
factor to 2^68.  (I'm including Richard in the ChangeLog entry because I'm using 
his newlib patch.)

2005-08-01  Bob Wilson  <bob.wilson@acm.org>
	    Richard Sandiford  <richard@codesourcery.com>

	* sysdeps/ieee754/flt-32/e_hypotf.c (__ieee754_hypotf): Add missing
	exponent bias to the value for 2^126.  Change 2^60 scaling factor to
	2^68 and remove the exponent bias from the increment value.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: hypotf-denorm.patch
Type: text/x-patch
Size: 1256 bytes
Desc: not available
URL: <http://sourceware.org/pipermail/libc-alpha/attachments/20050801/6f488113/attachment.bin>


More information about the Libc-alpha mailing list