1 /* @(#)e_hypot.c 5.1 93/09/24 */
2 /*
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5 *
6 * Developed at SunPro, a Sun Microsystems, Inc. business.
7 * Permission to use, copy, modify, and distribute this
8 * software is freely granted, provided that this notice
9 * is preserved.
10 * ====================================================
11 */
12
13 /* hypotl(x,y)
14 *
15 * Method :
16 * If (assume round-to-nearest) z=x*x+y*y
17 * has error less than sqrt(2)/2 ulp, than
18 * sqrt(z) has error less than 1 ulp (exercise).
19 *
20 * So, compute sqrt(x*x+y*y) with some care as
21 * follows to get the error below 1 ulp:
22 *
23 * Assume x>y>0;
24 * (if possible, set rounding to round-to-nearest)
25 * 1. if x > 2y use
26 * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
27 * where x1 = x with lower 32 bits cleared, x2 = x-x1; else
28 * 2. if x <= 2y use
29 * t1*yy1+((x-y)*(x-y)+(t1*y2+t2*y))
30 * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
31 * yy1= y with lower 32 bits chopped, y2 = y-yy1.
32 *
33 * NOTE: scaling may be necessary if some argument is too
34 * large or too tiny
35 *
36 * Special cases:
37 * hypot(x,y) is INF if x or y is +INF or -INF; else
38 * hypot(x,y) is NAN if x or y is NAN.
39 *
40 * Accuracy:
41 * hypot(x,y) returns sqrt(x^2+y^2) with error less
42 * than 1 ulps (units in the last place)
43 */
44
45
46
47 long double
hypotl(long double x,long double y)48 hypotl(long double x, long double y)
49 {
50 long double a,b,t1,t2,yy1,y2,w;
51 u_int32_t j,k,ea,eb;
52
53 GET_LDOUBLE_EXP(ea,x);
54 ea &= 0x7fff;
55 GET_LDOUBLE_EXP(eb,y);
56 eb &= 0x7fff;
57 if(eb > ea) {a=y;b=x;j=ea; ea=eb;eb=j;} else {a=x;b=y;}
58 SET_LDOUBLE_EXP(a,ea); /* a <- |a| */
59 SET_LDOUBLE_EXP(b,eb); /* b <- |b| */
60 if((ea-eb)>0x46) {return a+b;} /* x/y > 2**70 */
61 k=0;
62 if(ea > 0x5f3f) { /* a>2**8000 */
63 if(ea == 0x7fff) { /* Inf or NaN */
64 u_int32_t es,high,low;
65 w = a+b; /* for sNaN */
66 GET_LDOUBLE_WORDS(es,high,low,a);
67 (void) es;
68 if(((high&0x7fffffff)|low)==0 && !issignalingl_inline(b))
69 w = a;
70 GET_LDOUBLE_WORDS(es,high,low,b);
71 if(((eb^0x7fff)|(high&0x7fffffff)|low)==0 && !issignalingl_inline(a))
72 w = b;
73 return w;
74 }
75 /* scale a and b by 2**-9600 */
76 ea -= 0x2580; eb -= 0x2580; k += 9600;
77 SET_LDOUBLE_EXP(a,ea);
78 SET_LDOUBLE_EXP(b,eb);
79 }
80 if(eb < 0x20bf) { /* b < 2**-8000 */
81 if(eb == 0) { /* subnormal b or 0 */
82 u_int32_t es,high,low;
83 GET_LDOUBLE_WORDS(es,high,low,b);
84 (void) es;
85 if((high|low)==0) return a;
86 SET_LDOUBLE_WORDS(t1, 0x7ffd, 0, 0); /* t1=2^16382 */
87 b *= t1;
88 a *= t1;
89 k -= 16382;
90 } else { /* scale a and b by 2^9600 */
91 ea += 0x2580; /* a *= 2^9600 */
92 eb += 0x2580; /* b *= 2^9600 */
93 k -= 9600;
94 SET_LDOUBLE_EXP(a,ea);
95 SET_LDOUBLE_EXP(b,eb);
96 }
97 }
98 /* medium size a and b */
99 w = a-b;
100 if (w>b) {
101 u_int32_t high;
102 GET_LDOUBLE_MSW(high,a);
103 SET_LDOUBLE_WORDS(t1,ea,high,0);
104 t2 = a-t1;
105 w = sqrtl(t1*t1-(b*(-b)-t2*(a+t1)));
106 } else {
107 u_int32_t high;
108 GET_LDOUBLE_MSW(high,b);
109 a = a+a;
110 SET_LDOUBLE_WORDS(yy1,eb,high,0);
111 y2 = b - yy1;
112 GET_LDOUBLE_MSW(high,a);
113 SET_LDOUBLE_WORDS(t1,ea+1,high,0);
114 t2 = a - t1;
115 w = sqrtl(t1*yy1-(w*(-w)-(t1*y2+t2*b)));
116 }
117 if(k!=0) {
118 u_int32_t es;
119 t1 = 1.0L;
120 GET_LDOUBLE_EXP(es,t1);
121 SET_LDOUBLE_EXP(t1,es+k);
122 return check_oflowl(t1*w);
123 } else return w;
124 }
125