2.9
[glibc/nacl-glibc.git] / sysdeps / ieee754 / ldbl-96 / e_hypotl.c
blob1a40c556dcf1741058a2f2ef41b0fbb27f5cb347
1 /* e_hypotl.c -- long double version of e_hypot.c.
2 * Conversion to long double by Ulrich Drepper,
3 * Cygnus Support, drepper@cygnus.com.
4 */
6 /*
7 * ====================================================
8 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
10 * Developed at SunPro, a Sun Microsystems, Inc. business.
11 * Permission to use, copy, modify, and distribute this
12 * software is freely granted, provided that this notice
13 * is preserved.
14 * ====================================================
17 #if defined(LIBM_SCCS) && !defined(lint)
18 static char rcsid[] = "$NetBSD: $";
19 #endif
21 /* __ieee754_hypotl(x,y)
23 * Method :
24 * If (assume round-to-nearest) z=x*x+y*y
25 * has error less than sqrt(2)/2 ulp, than
26 * sqrt(z) has error less than 1 ulp (exercise).
28 * So, compute sqrt(x*x+y*y) with some care as
29 * follows to get the error below 1 ulp:
31 * Assume x>y>0;
32 * (if possible, set rounding to round-to-nearest)
33 * 1. if x > 2y use
34 * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
35 * where x1 = x with lower 32 bits cleared, x2 = x-x1; else
36 * 2. if x <= 2y use
37 * t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
38 * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1,
39 * y1= y with lower 32 bits chopped, y2 = y-y1.
41 * NOTE: scaling may be necessary if some argument is too
42 * large or too tiny
44 * Special cases:
45 * hypot(x,y) is INF if x or y is +INF or -INF; else
46 * hypot(x,y) is NAN if x or y is NAN.
48 * Accuracy:
49 * hypot(x,y) returns sqrt(x^2+y^2) with error less
50 * than 1 ulps (units in the last place)
53 #include "math.h"
54 #include "math_private.h"
56 #ifdef __STDC__
57 long double __ieee754_hypotl(long double x, long double y)
58 #else
59 long double __ieee754_hypotl(x,y)
60 long double x, y;
61 #endif
63 long double a,b,t1,t2,y1,y2,w;
64 u_int32_t j,k,ea,eb;
66 GET_LDOUBLE_EXP(ea,x);
67 ea &= 0x7fff;
68 GET_LDOUBLE_EXP(eb,y);
69 eb &= 0x7fff;
70 if(eb > ea) {a=y;b=x;j=ea; ea=eb;eb=j;} else {a=x;b=y;}
71 SET_LDOUBLE_EXP(a,ea); /* a <- |a| */
72 SET_LDOUBLE_EXP(b,eb); /* b <- |b| */
73 if((ea-eb)>0x46) {return a+b;} /* x/y > 2**70 */
74 k=0;
75 if(ea > 0x5f3f) { /* a>2**8000 */
76 if(ea == 0x7fff) { /* Inf or NaN */
77 u_int32_t exp,high,low;
78 w = a+b; /* for sNaN */
79 GET_LDOUBLE_WORDS(exp,high,low,a);
80 if(((high&0x7fffffff)|low)==0) w = a;
81 GET_LDOUBLE_WORDS(exp,high,low,b);
82 if(((eb^0x7fff)|(high&0x7fffffff)|low)==0) w = b;
83 return w;
85 /* scale a and b by 2**-9600 */
86 ea -= 0x2580; eb -= 0x2580; k += 9600;
87 SET_LDOUBLE_EXP(a,ea);
88 SET_LDOUBLE_EXP(b,eb);
90 if(eb < 0x20bf) { /* b < 2**-8000 */
91 if(eb == 0) { /* subnormal b or 0 */
92 u_int32_t exp,high,low;
93 GET_LDOUBLE_WORDS(exp,high,low,b);
94 if((high|low)==0) return a;
95 SET_LDOUBLE_WORDS(t1, 0x7ffd, 0, 0); /* t1=2^16382 */
96 b *= t1;
97 a *= t1;
98 k -= 16382;
99 } else { /* scale a and b by 2^9600 */
100 ea += 0x2580; /* a *= 2^9600 */
101 eb += 0x2580; /* b *= 2^9600 */
102 k -= 9600;
103 SET_LDOUBLE_EXP(a,ea);
104 SET_LDOUBLE_EXP(b,eb);
107 /* medium size a and b */
108 w = a-b;
109 if (w>b) {
110 u_int32_t high;
111 GET_LDOUBLE_MSW(high,a);
112 SET_LDOUBLE_WORDS(t1,ea,high,0);
113 t2 = a-t1;
114 w = __ieee754_sqrtl(t1*t1-(b*(-b)-t2*(a+t1)));
115 } else {
116 u_int32_t high;
117 GET_LDOUBLE_MSW(high,b);
118 a = a+a;
119 SET_LDOUBLE_WORDS(y1,eb,high,0);
120 y2 = b - y1;
121 GET_LDOUBLE_MSW(high,a);
122 SET_LDOUBLE_WORDS(t1,ea+1,high,0);
123 t2 = a - t1;
124 w = __ieee754_sqrtl(t1*y1-(w*(-w)-(t1*y2+t2*b)));
126 if(k!=0) {
127 u_int32_t exp;
128 t1 = 1.0;
129 GET_LDOUBLE_EXP(exp,t1);
130 SET_LDOUBLE_EXP(t1,exp+k);
131 return t1*w;
132 } else return w;