Subject: lib/14508: ppc-specific ldexp uses inauspicious variable name
To: None <gnats-bugs@gnats.netbsd.org>
From: seebs <seebs@ged.plethora.net>
List: netbsd-bugs
Date: 11/08/2001 22:15:31
>Number:         14508
>Category:       lib
>Synopsis:       ldexp.c uses "exp" as a variable name, producing a warning
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Nov 08 21:23:01 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     seebs
>Release:        NetBSD 1.5Y
>Organization:
	
>Environment:
System: NetBSD ged.plethora.net 1.5Y NetBSD 1.5Y (GED) #0: Tue Oct 30 10:56:38 CST 2001 seebs@ged.plethora.net:/usr/src/sys/arch/i386/compile/GED i386
Architecture: i386
Machine: i386
... but I'm cross-compiling for ppc.
>Description:
	The ppc version of ldexp uses a variable named "exp", which causes
	gcc to emit Dire Warnings, and the compilation to abort.  (The warning
	is "local variable exp shadows global declaration")
>How-To-Repeat:
	build.sh (blah blah blah)
>Fix:

in: /usr/src/lib/libc/arch/powerpc/gen/ldexp.c
*** ldexp.c.orig	Thu Nov  8 22:10:57 2001
--- ldexp.c	Thu Nov  8 22:12:37 2001
***************
*** 47,58 ****
  #include <math.h>
  
  /*
!  * Multiply the given value by 2^exp.
   */
  double
! ldexp(val, exp)
  	double val;
! 	int exp;
  {
  	register int oldexp, newexp;
  	union {
--- 47,58 ----
  #include <math.h>
  
  /*
!  * Multiply the given value by 2^exponent.
   */
  double
! ldexp(val, exponent)
  	double val;
! 	int exponent;
  {
  	register int oldexp, newexp;
  	union {
***************
*** 74,90 ****
  		 * u.v is denormal.  We must adjust it so that the exponent
  		 * arithmetic below will work.
  		 */
! 		if (exp <= DBL_EXP_BIAS) {
  			/*
  			 * Optimization: if the scaling can be done in a single
  			 * multiply, or underflows, just do it now.
  			 */
! 			if (exp <= -DBL_FRACBITS) {
  				errno = ERANGE;
  				return (0.0);
  			}
  			mul.v = 0.0;
! 			mul.s.dbl_exp = exp + DBL_EXP_BIAS;
  			u.v *= mul.v;
  			if (u.v == 0.0) {
  				errno = ERANGE;
--- 74,90 ----
  		 * u.v is denormal.  We must adjust it so that the exponent
  		 * arithmetic below will work.
  		 */
! 		if (exponent <= DBL_EXP_BIAS) {
  			/*
  			 * Optimization: if the scaling can be done in a single
  			 * multiply, or underflows, just do it now.
  			 */
! 			if (exponent <= -DBL_FRACBITS) {
  				errno = ERANGE;
  				return (0.0);
  			}
  			mul.v = 0.0;
! 			mul.s.dbl_exp = exponent + DBL_EXP_BIAS;
  			u.v *= mul.v;
  			if (u.v == 0.0) {
  				errno = ERANGE;
***************
*** 93,106 ****
  			return (u.v);
  		} else {
  			/*
! 			 * We know that exp is very large, and therefore the
! 			 * result cannot be denormal (though it may be Inf).
! 			 * Shift u.v by just enough to make it normal.
  			 */
  			mul.v = 0.0;
  			mul.s.dbl_exp = DBL_FRACBITS + DBL_EXP_BIAS;
  			u.v *= mul.v;
! 			exp -= DBL_FRACBITS;
  			oldexp = u.s.dbl_exp;
  		}
  	}
--- 93,106 ----
  			return (u.v);
  		} else {
  			/*
! 			 * We know that exponent is very large, and therefore
! 			 * the result cannot be denormal (though it may be
! 			 * Inf).  Shift u.v by just enough to make it normal.
  			 */
  			mul.v = 0.0;
  			mul.s.dbl_exp = DBL_FRACBITS + DBL_EXP_BIAS;
  			u.v *= mul.v;
! 			exponent -= DBL_FRACBITS;
  			oldexp = u.s.dbl_exp;
  		}
  	}
***************
*** 109,115 ****
  	 * u.v is now normalized and oldexp has been adjusted if necessary.
  	 * Calculate the new exponent and check for underflow and overflow.
  	 */
! 	newexp = oldexp + exp;
  
  	if (newexp <= 0) {
  		/*
--- 109,115 ----
  	 * u.v is now normalized and oldexp has been adjusted if necessary.
  	 * Calculate the new exponent and check for underflow and overflow.
  	 */
! 	newexp = oldexp + exponent;
  
  	if (newexp <= 0) {
  		/*
***************
*** 121,137 ****
  			return (0.0);
  		}
  		/*
! 		 * Denormalize the result.  We do this with a multiply.  If exp
! 		 * is very large, it won't fit in a double, so we have to
! 		 * adjust the exponent first.  This is safe because we know
! 		 * that u.v is normal at this point.
  		 */
! 		if (exp <= -DBL_EXP_BIAS) {
  			u.s.dbl_exp = 1;
! 			exp += oldexp - 1;
  		}
  		mul.v = 0.0;
! 		mul.s.dbl_exp = exp + DBL_EXP_BIAS;
  		u.v *= mul.v;
  		return (u.v);
  	} else if (newexp >= DBL_EXP_INFNAN) {
--- 121,137 ----
  			return (0.0);
  		}
  		/*
! 		 * Denormalize the result.  We do this with a multiply.  If
! 		 * exponent is very large, it won't fit in a double, so we
! 		 * have to adjust the exponent first.  This is safe because we
! 		 * know that u.v is normal at this point.
  		 */
! 		if (exponent <= -DBL_EXP_BIAS) {
  			u.s.dbl_exp = 1;
! 			exponent += oldexp - 1;
  		}
  		mul.v = 0.0;
! 		mul.s.dbl_exp = exponent + DBL_EXP_BIAS;
  		u.v *= mul.v;
  		return (u.v);
  	} else if (newexp >= DBL_EXP_INFNAN) {
>Release-Note:
>Audit-Trail:
>Unformatted:
 	Today's source, actually.