1 | /* @(#)e_hypotl.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 | /* __ieee754_hypotl(x,y) |
14 | * |
15 | * Method : |
16 | * If (assume round-to-nearest) z=x*x+y*y |
17 | * has error less than sqrtl(2)/2 ulp, than |
18 | * sqrtl(z) has error less than 1 ulp (exercise). |
19 | * |
20 | * So, compute sqrtl(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 53 bits cleared, x2 = x-x1; else |
28 | * 2. if x <= 2y use |
29 | * t1*y1+((x-y)*(x-y)+(t1*y2+t2*y)) |
30 | * where t1 = 2x with lower 53 bits cleared, t2 = 2x-t1, |
31 | * y1= y with lower 53 bits chopped, y2 = y-y1. |
32 | * |
33 | * NOTE: scaling may be necessary if some argument is too |
34 | * large or too tiny |
35 | * |
36 | * Special cases: |
37 | * hypotl(x,y) is INF if x or y is +INF or -INF; else |
38 | * hypotl(x,y) is NAN if x or y is NAN. |
39 | * |
40 | * Accuracy: |
41 | * hypotl(x,y) returns sqrtl(x^2+y^2) with error less |
42 | * than 1 ulps (units in the last place) |
43 | */ |
44 | |
45 | #include <math.h> |
46 | #include <math_private.h> |
47 | #include <math-underflow.h> |
48 | #include <libm-alias-finite.h> |
49 | |
50 | long double |
51 | __ieee754_hypotl(long double x, long double y) |
52 | { |
53 | long double a,b,a1,a2,b1,b2,w,kld; |
54 | int64_t j,k,ha,hb; |
55 | double xhi, yhi, hi, lo; |
56 | |
57 | xhi = ldbl_high (x); |
58 | EXTRACT_WORDS64 (ha, xhi); |
59 | yhi = ldbl_high (y); |
60 | EXTRACT_WORDS64 (hb, yhi); |
61 | ha &= 0x7fffffffffffffffLL; |
62 | hb &= 0x7fffffffffffffffLL; |
63 | if(hb > ha) {a=y;b=x;j=ha; ha=hb;hb=j;} else {a=x;b=y;} |
64 | a = fabsl(x: a); /* a <- |a| */ |
65 | b = fabsl(x: b); /* b <- |b| */ |
66 | if((ha-hb)>0x0780000000000000LL) {return a+b;} /* x/y > 2**120 */ |
67 | k=0; |
68 | kld = 1.0L; |
69 | if(ha > 0x5f30000000000000LL) { /* a>2**500 */ |
70 | if(ha >= 0x7ff0000000000000LL) { /* Inf or NaN */ |
71 | w = a+b; /* for sNaN */ |
72 | if (issignaling (a) || issignaling (b)) |
73 | return w; |
74 | if(ha == 0x7ff0000000000000LL) |
75 | w = a; |
76 | if(hb == 0x7ff0000000000000LL) |
77 | w = b; |
78 | return w; |
79 | } |
80 | /* scale a and b by 2**-600 */ |
81 | a *= 0x1p-600L; |
82 | b *= 0x1p-600L; |
83 | k = 600; |
84 | kld = 0x1p+600L; |
85 | } |
86 | else if(hb < 0x23d0000000000000LL) { /* b < 2**-450 */ |
87 | if(hb <= 0x000fffffffffffffLL) { /* subnormal b or 0 */ |
88 | if(hb==0) return a; |
89 | a *= 0x1p+1022L; |
90 | b *= 0x1p+1022L; |
91 | k = -1022; |
92 | kld = 0x1p-1022L; |
93 | } else { /* scale a and b by 2^600 */ |
94 | a *= 0x1p+600L; |
95 | b *= 0x1p+600L; |
96 | k = -600; |
97 | kld = 0x1p-600L; |
98 | } |
99 | } |
100 | /* medium size a and b */ |
101 | w = a-b; |
102 | if (w>b) { |
103 | ldbl_unpack (a, &hi, &lo); |
104 | a1 = hi; |
105 | a2 = lo; |
106 | /* a*a + b*b |
107 | = (a1+a2)*a + b*b |
108 | = a1*a + a2*a + b*b |
109 | = a1*(a1+a2) + a2*a + b*b |
110 | = a1*a1 + a1*a2 + a2*a + b*b |
111 | = a1*a1 + a2*(a+a1) + b*b */ |
112 | w = sqrtl(a1*a1-(b*(-b)-a2*(a+a1))); |
113 | } else { |
114 | a = a+a; |
115 | ldbl_unpack (b, &hi, &lo); |
116 | b1 = hi; |
117 | b2 = lo; |
118 | ldbl_unpack (a, &hi, &lo); |
119 | a1 = hi; |
120 | a2 = lo; |
121 | /* a*a + b*b |
122 | = a*a + (a-b)*(a-b) - (a-b)*(a-b) + b*b |
123 | = a*a + w*w - (a*a - 2*a*b + b*b) + b*b |
124 | = w*w + 2*a*b |
125 | = w*w + (a1+a2)*b |
126 | = w*w + a1*b + a2*b |
127 | = w*w + a1*(b1+b2) + a2*b |
128 | = w*w + a1*b1 + a1*b2 + a2*b */ |
129 | w = sqrtl(a1*b1-(w*(-w)-(a1*b2+a2*b))); |
130 | } |
131 | if(k!=0) |
132 | { |
133 | w *= kld; |
134 | math_check_force_underflow_nonneg (w); |
135 | return w; |
136 | } |
137 | else |
138 | return w; |
139 | } |
140 | libm_alias_finite (__ieee754_hypotl, __hypotl) |
141 | |