|
Packit Service |
82fcde |
/* @(#)e_hypot.c 5.1 93/09/24 */
|
|
Packit Service |
82fcde |
/*
|
|
Packit Service |
82fcde |
* ====================================================
|
|
Packit Service |
82fcde |
* Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
|
|
Packit Service |
82fcde |
*
|
|
Packit Service |
82fcde |
* Developed at SunPro, a Sun Microsystems, Inc. business.
|
|
Packit Service |
82fcde |
* Permission to use, copy, modify, and distribute this
|
|
Packit Service |
82fcde |
* software is freely granted, provided that this notice
|
|
Packit Service |
82fcde |
* is preserved.
|
|
Packit Service |
82fcde |
* ====================================================
|
|
Packit Service |
82fcde |
*/
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
/* __ieee754_hypot(x,y)
|
|
Packit Service |
82fcde |
*
|
|
Packit Service |
82fcde |
* Method :
|
|
Packit Service |
82fcde |
* If (assume round-to-nearest) z=x*x+y*y
|
|
Packit Service |
82fcde |
* has error less than sqrt(2)/2 ulp, than
|
|
Packit Service |
82fcde |
* sqrt(z) has error less than 1 ulp (exercise).
|
|
Packit Service |
82fcde |
*
|
|
Packit Service |
82fcde |
* So, compute sqrt(x*x+y*y) with some care as
|
|
Packit Service |
82fcde |
* follows to get the error below 1 ulp:
|
|
Packit Service |
82fcde |
*
|
|
Packit Service |
82fcde |
* Assume x>y>0;
|
|
Packit Service |
82fcde |
* (if possible, set rounding to round-to-nearest)
|
|
Packit Service |
82fcde |
* 1. if x > 2y use
|
|
Packit Service |
82fcde |
* x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
|
|
Packit Service |
82fcde |
* where x1 = x with lower 32 bits cleared, x2 = x-x1; else
|
|
Packit Service |
82fcde |
* 2. if x <= 2y use
|
|
Packit Service |
82fcde |
* t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
|
|
Packit Service |
82fcde |
* where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
|
|
Packit Service |
82fcde |
* y1= y with lower 32 bits chopped, y2 = y-y1.
|
|
Packit Service |
82fcde |
*
|
|
Packit Service |
82fcde |
* NOTE: scaling may be necessary if some argument is too
|
|
Packit Service |
82fcde |
* large or too tiny
|
|
Packit Service |
82fcde |
*
|
|
Packit Service |
82fcde |
* Special cases:
|
|
Packit Service |
82fcde |
* hypot(x,y) is INF if x or y is +INF or -INF; else
|
|
Packit Service |
82fcde |
* hypot(x,y) is NAN if x or y is NAN.
|
|
Packit Service |
82fcde |
*
|
|
Packit Service |
82fcde |
* Accuracy:
|
|
Packit Service |
82fcde |
* hypot(x,y) returns sqrt(x^2+y^2) with error less
|
|
Packit Service |
82fcde |
* than 1 ulps (units in the last place)
|
|
Packit Service |
82fcde |
*/
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
#include <math.h>
|
|
Packit Service |
82fcde |
#include <math_private.h>
|
|
Packit Service |
82fcde |
#include <math-underflow.h>
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
double
|
|
Packit Service |
82fcde |
__ieee754_hypot (double x, double y)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
double a, b, t1, t2, y1, y2, w;
|
|
Packit Service |
82fcde |
int32_t j, k, ha, hb;
|
|
Packit Service |
82fcde |
|
|
Packit Service |
82fcde |
GET_HIGH_WORD (ha, x);
|
|
Packit Service |
82fcde |
ha &= 0x7fffffff;
|
|
Packit Service |
82fcde |
GET_HIGH_WORD (hb, y);
|
|
Packit Service |
82fcde |
hb &= 0x7fffffff;
|
|
Packit Service |
82fcde |
if (hb > ha)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
a = y; b = x; j = ha; ha = hb; hb = j;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
a = x; b = y;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
SET_HIGH_WORD (a, ha); /* a <- |a| */
|
|
Packit Service |
82fcde |
SET_HIGH_WORD (b, hb); /* b <- |b| */
|
|
Packit Service |
82fcde |
if ((ha - hb) > 0x3c00000)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
return a + b;
|
|
Packit Service |
82fcde |
} /* x/y > 2**60 */
|
|
Packit Service |
82fcde |
k = 0;
|
|
Packit Service |
82fcde |
if (__glibc_unlikely (ha > 0x5f300000)) /* a>2**500 */
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (ha >= 0x7ff00000) /* Inf or NaN */
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
uint32_t low;
|
|
Packit Service |
82fcde |
w = a + b; /* for sNaN */
|
|
Packit Service |
82fcde |
if (issignaling (a) || issignaling (b))
|
|
Packit Service |
82fcde |
return w;
|
|
Packit Service |
82fcde |
GET_LOW_WORD (low, a);
|
|
Packit Service |
82fcde |
if (((ha & 0xfffff) | low) == 0)
|
|
Packit Service |
82fcde |
w = a;
|
|
Packit Service |
82fcde |
GET_LOW_WORD (low, b);
|
|
Packit Service |
82fcde |
if (((hb ^ 0x7ff00000) | low) == 0)
|
|
Packit Service |
82fcde |
w = b;
|
|
Packit Service |
82fcde |
return w;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
/* scale a and b by 2**-600 */
|
|
Packit Service |
82fcde |
ha -= 0x25800000; hb -= 0x25800000; k += 600;
|
|
Packit Service |
82fcde |
SET_HIGH_WORD (a, ha);
|
|
Packit Service |
82fcde |
SET_HIGH_WORD (b, hb);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (__builtin_expect (hb < 0x23d00000, 0)) /* b < 2**-450 */
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
if (hb <= 0x000fffff) /* subnormal b or 0 */
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
uint32_t low;
|
|
Packit Service |
82fcde |
GET_LOW_WORD (low, b);
|
|
Packit Service |
82fcde |
if ((hb | low) == 0)
|
|
Packit Service |
82fcde |
return a;
|
|
Packit Service |
82fcde |
t1 = 0;
|
|
Packit Service |
82fcde |
SET_HIGH_WORD (t1, 0x7fd00000); /* t1=2^1022 */
|
|
Packit Service |
82fcde |
b *= t1;
|
|
Packit Service |
82fcde |
a *= t1;
|
|
Packit Service |
82fcde |
k -= 1022;
|
|
Packit Service |
82fcde |
GET_HIGH_WORD (ha, a);
|
|
Packit Service |
82fcde |
GET_HIGH_WORD (hb, b);
|
|
Packit Service |
82fcde |
if (hb > ha)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
t1 = a;
|
|
Packit Service |
82fcde |
a = b;
|
|
Packit Service |
82fcde |
b = t1;
|
|
Packit Service |
82fcde |
j = ha;
|
|
Packit Service |
82fcde |
ha = hb;
|
|
Packit Service |
82fcde |
hb = j;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else /* scale a and b by 2^600 */
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
ha += 0x25800000; /* a *= 2^600 */
|
|
Packit Service |
82fcde |
hb += 0x25800000; /* b *= 2^600 */
|
|
Packit Service |
82fcde |
k -= 600;
|
|
Packit Service |
82fcde |
SET_HIGH_WORD (a, ha);
|
|
Packit Service |
82fcde |
SET_HIGH_WORD (b, hb);
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
/* medium size a and b */
|
|
Packit Service |
82fcde |
w = a - b;
|
|
Packit Service |
82fcde |
if (w > b)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
t1 = 0;
|
|
Packit Service |
82fcde |
SET_HIGH_WORD (t1, ha);
|
|
Packit Service |
82fcde |
t2 = a - t1;
|
|
Packit Service |
82fcde |
w = sqrt (t1 * t1 - (b * (-b) - t2 * (a + t1)));
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
a = a + a;
|
|
Packit Service |
82fcde |
y1 = 0;
|
|
Packit Service |
82fcde |
SET_HIGH_WORD (y1, hb);
|
|
Packit Service |
82fcde |
y2 = b - y1;
|
|
Packit Service |
82fcde |
t1 = 0;
|
|
Packit Service |
82fcde |
SET_HIGH_WORD (t1, ha + 0x00100000);
|
|
Packit Service |
82fcde |
t2 = a - t1;
|
|
Packit Service |
82fcde |
w = sqrt (t1 * y1 - (w * (-w) - (t1 * y2 + t2 * b)));
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
if (k != 0)
|
|
Packit Service |
82fcde |
{
|
|
Packit Service |
82fcde |
uint32_t high;
|
|
Packit Service |
82fcde |
t1 = 1.0;
|
|
Packit Service |
82fcde |
GET_HIGH_WORD (high, t1);
|
|
Packit Service |
82fcde |
SET_HIGH_WORD (t1, high + (k << 20));
|
|
Packit Service |
82fcde |
w *= t1;
|
|
Packit Service |
82fcde |
math_check_force_underflow_nonneg (w);
|
|
Packit Service |
82fcde |
return w;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
else
|
|
Packit Service |
82fcde |
return w;
|
|
Packit Service |
82fcde |
}
|
|
Packit Service |
82fcde |
strong_alias (__ieee754_hypot, __hypot_finite)
|