mirror of
https://github.com/adtools/clib2.git
synced 2025-12-08 14:59:05 +00:00
- Updated math_hypot.c to a newer (e_hypot.c 1.3 95/01/18) version from
fdlibm which uses macros for manipulating the high and low words of a double, like the rest of fdlibm functions in clib2. The previous version would give bogus results when compiled with -O3 in clib2 which lead to "interesting" results (and lots of fun while searching for the problem) in Ghostscript. Wish I managed to track this down yesterday for 1.196 release... <aantonijevic> git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@15043 87f5fb63-7c3d-0410-a384-fd976d0f7a62
This commit is contained in:
@ -1,3 +1,11 @@
|
||||
- Updated math_hypot.c to a newer (e_hypot.c 1.3 95/01/18) version from
|
||||
fdlibm which uses macros for manipulating the high and low words of a
|
||||
double, like the rest of fdlibm functions in clib2. The previous version
|
||||
would give bogus results when compiled with -O3 in clib2 which lead to
|
||||
"interesting" results (and lots of fun while searching for the problem)
|
||||
in Ghostscript. Wish I managed to track this down yesterday for 1.196
|
||||
release... <aantonijevic>
|
||||
|
||||
c.lib 1.196 (11.10.2005)
|
||||
|
||||
- Removed the various workarounds associated with <unistd.h>, required
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* $Id: math_hypot.c,v 1.3 2005-02-25 10:14:21 obarthel Exp $
|
||||
* $Id: math_hypot.c,v 1.4 2005-10-11 19:05:18 theantony Exp $
|
||||
*
|
||||
* :ts=4
|
||||
*
|
||||
@ -88,35 +88,40 @@ static const double one = 1.0;
|
||||
INLINE STATIC const double
|
||||
__hypot(double x,double y)
|
||||
{
|
||||
int n0;
|
||||
double a=x,b=y,t1,t2,y1,y2,w;
|
||||
int j,k,ha,hb;
|
||||
|
||||
n0 = ((*(int*)&one)>>29)^1; /* high word index */
|
||||
ha = *(n0+(int*)&x)&0x7fffffff; /* high word of x */
|
||||
hb = *(n0+(int*)&y)&0x7fffffff; /* high word of y */
|
||||
GET_HIGH_WORD(ha,x);
|
||||
ha &= 0x7fffffff;
|
||||
GET_HIGH_WORD(hb,y);
|
||||
hb &= 0x7fffffff;
|
||||
if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
|
||||
*(n0+(int*)&a) = ha; /* a <- |a| */
|
||||
*(n0+(int*)&b) = hb; /* b <- |b| */
|
||||
SET_HIGH_WORD(a,ha); /* a <- |a| */
|
||||
SET_HIGH_WORD(b,hb); /* b <- |b| */
|
||||
if((ha-hb)>0x3c00000) {return a+b;} /* x/y > 2**60 */
|
||||
k=0;
|
||||
if(ha > 0x5f300000) { /* a>2**500 */
|
||||
if(ha >= 0x7ff00000) { /* Inf or NaN */
|
||||
unsigned int low;
|
||||
w = a+b; /* for sNaN */
|
||||
if(((ha&0xfffff)|*(1-n0+(int*)&a))==0) w = a;
|
||||
if(((hb^0x7ff00000)|*(1-n0+(int*)&b))==0) w = b;
|
||||
GET_LOW_WORD(low,a);
|
||||
if(((ha&0xfffff)|low)==0) w = a;
|
||||
GET_LOW_WORD(low,b);
|
||||
if(((hb^0x7ff00000)|low)==0) w = b;
|
||||
return w;
|
||||
}
|
||||
/* scale a and b by 2**-600 */
|
||||
ha -= 0x25800000; hb -= 0x25800000; k += 600;
|
||||
*(n0+(int*)&a) = ha;
|
||||
*(n0+(int*)&b) = hb;
|
||||
SET_HIGH_WORD(a,ha);
|
||||
SET_HIGH_WORD(b,hb);
|
||||
}
|
||||
if(hb < 0x20b00000) { /* b < 2**-500 */
|
||||
if(hb <= 0x000fffff) { /* subnormal b or 0 */
|
||||
if((hb|(*(1-n0+(int*)&b)))==0) return a;
|
||||
if(hb <= 0x000fffff) { /* subnormal b or 0 */
|
||||
unsigned int low;
|
||||
GET_LOW_WORD(low,b);
|
||||
if((hb|low)==0) return a;
|
||||
t1=0;
|
||||
*(n0+(int*)&t1) = 0x7fd00000; /* t1=2^1022 */
|
||||
SET_HIGH_WORD(t1,0x7fd00000); /* t1=2^1022 */
|
||||
b *= t1;
|
||||
a *= t1;
|
||||
k -= 1022;
|
||||
@ -124,30 +129,32 @@ __hypot(double x,double y)
|
||||
ha += 0x25800000; /* a *= 2^600 */
|
||||
hb += 0x25800000; /* b *= 2^600 */
|
||||
k -= 600;
|
||||
*(n0+(int*)&a) = ha;
|
||||
*(n0+(int*)&b) = hb;
|
||||
SET_HIGH_WORD(a,ha);
|
||||
SET_HIGH_WORD(b,hb);
|
||||
}
|
||||
}
|
||||
/* medium size a and b */
|
||||
w = a-b;
|
||||
if (w>b) {
|
||||
t1 = 0;
|
||||
*(n0+(int*)&t1) = ha;
|
||||
SET_HIGH_WORD(t1,ha);
|
||||
t2 = a-t1;
|
||||
w = sqrt(t1*t1-(b*(-b)-t2*(a+t1)));
|
||||
} else {
|
||||
a = a+a;
|
||||
y1 = 0;
|
||||
*(n0+(int*)&y1) = hb;
|
||||
SET_HIGH_WORD(y1,hb);
|
||||
y2 = b - y1;
|
||||
t1 = 0;
|
||||
*(n0+(int*)&t1) = ha+0x00100000;
|
||||
SET_HIGH_WORD(t1,ha+0x00100000);
|
||||
t2 = a - t1;
|
||||
w = sqrt(t1*y1-(w*(-w)-(t1*y2+t2*b)));
|
||||
}
|
||||
if(k!=0) {
|
||||
unsigned int high;
|
||||
t1 = 1.0;
|
||||
*(n0+(int*)&t1) += (k<<20);
|
||||
GET_HIGH_WORD(high,t1);
|
||||
SET_HIGH_WORD(t1,high+(k<<20));
|
||||
return t1*w;
|
||||
} else return w;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user