1 /* @(#)e_hypotl.c 5.1 93/09/24 */
3 * ====================================================
4 * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
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
10 * ====================================================
13 #if defined(LIBM_SCCS) && !defined(lint)
14 static char rcsid[] = "$NetBSD: e_hypotl.c,v 1.9 1995/05/12 04:57:27 jtc Exp $";
17 /* __ieee754_hypotl(x,y)
20 * If (assume round-to-nearest) z=x*x+y*y
21 * has error less than sqrtl(2)/2 ulp, than
22 * sqrtl(z) has error less than 1 ulp (exercise).
24 * So, compute sqrtl(x*x+y*y) with some care as
25 * follows to get the error below 1 ulp:
28 * (if possible, set rounding to round-to-nearest)
30 * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y
31 * where x1 = x with lower 53 bits cleared, x2 = x-x1; else
33 * t1*y1+((x-y)*(x-y)+(t1*y2+t2*y))
34 * where t1 = 2x with lower 53 bits cleared, t2 = 2x-t1,
35 * y1= y with lower 53 bits chopped, y2 = y-y1.
37 * NOTE: scaling may be necessary if some argument is too
41 * hypotl(x,y) is INF if x or y is +INF or -INF; else
42 * hypotl(x,y) is NAN if x or y is NAN.
45 * hypotl(x,y) returns sqrtl(x^2+y^2) with error less
46 * than 1 ulps (units in the last place)
50 #include "math_private.h"
52 static const long double two600 = 0x1.0p+600L;
53 static const long double two1022 = 0x1.0p+1022L;
56 long double __ieee754_hypotl(long double x, long double y)
58 long double __ieee754_hypotl(x,y)
62 long double a,b,t1,t2,y1,y2,w,kld;
65 GET_LDOUBLE_MSW64(ha,x);
66 ha &= 0x7fffffffffffffffLL;
67 GET_LDOUBLE_MSW64(hb,y);
68 hb &= 0x7fffffffffffffffLL;
69 if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;}
70 a = fabsl(a); /* a <- |a| */
71 b = fabsl(b); /* b <- |b| */
72 if((ha-hb)>0x3c0000000000000LL) {return a+b;} /* x/y > 2**60 */
75 if(ha > 0x5f30000000000000LL) { /* a>2**500 */
76 if(ha >= 0x7ff0000000000000LL) { /* Inf or NaN */
78 w = a+b; /* for sNaN */
79 GET_LDOUBLE_LSW64(low,a);
80 if(((ha&0xfffffffffffffLL)|(low&0x7fffffffffffffffLL))==0)
82 GET_LDOUBLE_LSW64(low,b);
83 if(((hb^0x7ff0000000000000LL)|(low&0x7fffffffffffffffLL))==0)
87 /* scale a and b by 2**-600 */
88 ha -= 0x2580000000000000LL; hb -= 0x2580000000000000LL; k += 600;
94 if(hb < 0x20b0000000000000LL) { /* b < 2**-500 */
95 if(hb <= 0x000fffffffffffffLL) { /* subnormal b or 0 */
97 GET_LDOUBLE_LSW64(low,b);
98 if((hb|(low&0x7fffffffffffffffLL))==0) return a;
99 t1=two1022; /* t1=2^1022 */
104 } else { /* scale a and b by 2^600 */
105 ha += 0x2580000000000000LL; /* a *= 2^600 */
106 hb += 0x2580000000000000LL; /* b *= 2^600 */
113 /* medium size a and b */
116 SET_LDOUBLE_WORDS64(t1,ha,0);
118 w = __ieee754_sqrtl(t1*t1-(b*(-b)-t2*(a+t1)));
121 SET_LDOUBLE_WORDS64(y1,hb,0);
123 SET_LDOUBLE_WORDS64(t1,ha+0x0010000000000000LL,0);
125 w = __ieee754_sqrtl(t1*y1-(w*(-w)-(t1*y2+t2*b)));