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 | */ |
5 | |
6 | /* |
7 | * ==================================================== |
8 | * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. |
9 | * |
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 | * ==================================================== |
15 | */ |
16 | |
17 | /* __ieee754_hypotl(x,y) |
18 | * |
19 | * Method : |
20 | * If (assume round-to-nearest) z=x*x+y*y |
21 | * has error less than sqrt(2)/2 ulp, than |
22 | * sqrt(z) has error less than 1 ulp (exercise). |
23 | * |
24 | * So, compute sqrt(x*x+y*y) with some care as |
25 | * follows to get the error below 1 ulp: |
26 | * |
27 | * Assume x>y>0; |
28 | * (if possible, set rounding to round-to-nearest) |
29 | * 1. if x > 2y use |
30 | * x1*x1+(y*y+(x2*(x+x1))) for x*x+y*y |
31 | * where x1 = x with lower 32 bits cleared, x2 = x-x1; else |
32 | * 2. if x <= 2y use |
33 | * t1*y1+((x-y)*(x-y)+(t1*y2+t2*y)) |
34 | * where t1 = 2x with lower 32 bits cleared, t2 = 2x-t1, |
35 | * y1= y with lower 32 bits chopped, y2 = y-y1. |
36 | * |
37 | * NOTE: scaling may be necessary if some argument is too |
38 | * large or too tiny |
39 | * |
40 | * Special cases: |
41 | * hypot(x,y) is INF if x or y is +INF or -INF; else |
42 | * hypot(x,y) is NAN if x or y is NAN. |
43 | * |
44 | * Accuracy: |
45 | * hypot(x,y) returns sqrt(x^2+y^2) with error less |
46 | * than 1 ulps (units in the last place) |
47 | */ |
48 | |
49 | #include <math.h> |
50 | #include <math_private.h> |
51 | #include <math-underflow.h> |
52 | #include <libm-alias-finite.h> |
53 | |
54 | long double __ieee754_hypotl(long double x, long double y) |
55 | { |
56 | long double a,b,t1,t2,y1,y2,w; |
57 | uint32_t j,k,ea,eb; |
58 | |
59 | GET_LDOUBLE_EXP(ea,x); |
60 | ea &= 0x7fff; |
61 | GET_LDOUBLE_EXP(eb,y); |
62 | eb &= 0x7fff; |
63 | if(eb > ea) {a=y;b=x;j=ea; ea=eb;eb=j;} else {a=x;b=y;} |
64 | SET_LDOUBLE_EXP(a,ea); /* a <- |a| */ |
65 | SET_LDOUBLE_EXP(b,eb); /* b <- |b| */ |
66 | if((ea-eb)>0x46) {return a+b;} /* x/y > 2**70 */ |
67 | k=0; |
68 | if(__builtin_expect(ea > 0x5f3f,0)) { /* a>2**8000 */ |
69 | if(ea == 0x7fff) { /* Inf or NaN */ |
70 | uint32_t exp __attribute__ ((unused)); |
71 | uint32_t high,low; |
72 | w = a+b; /* for sNaN */ |
73 | if (issignaling (a) || issignaling (b)) |
74 | return w; |
75 | GET_LDOUBLE_WORDS(exp,high,low,a); |
76 | if(((high&0x7fffffff)|low)==0) w = a; |
77 | GET_LDOUBLE_WORDS(exp,high,low,b); |
78 | if(((eb^0x7fff)|(high&0x7fffffff)|low)==0) w = b; |
79 | return w; |
80 | } |
81 | /* scale a and b by 2**-9600 */ |
82 | ea -= 0x2580; eb -= 0x2580; k += 9600; |
83 | SET_LDOUBLE_EXP(a,ea); |
84 | SET_LDOUBLE_EXP(b,eb); |
85 | } |
86 | if(__builtin_expect(eb < 0x20bf, 0)) { /* b < 2**-8000 */ |
87 | if(eb == 0) { /* subnormal b or 0 */ |
88 | uint32_t exp __attribute__ ((unused)); |
89 | uint32_t high,low; |
90 | GET_LDOUBLE_WORDS(exp,high,low,b); |
91 | if((high|low)==0) return a; |
92 | SET_LDOUBLE_WORDS(t1, 0x7ffd, 0x80000000, 0); /* t1=2^16382 */ |
93 | b *= t1; |
94 | a *= t1; |
95 | k -= 16382; |
96 | GET_LDOUBLE_EXP (ea, a); |
97 | GET_LDOUBLE_EXP (eb, b); |
98 | if (eb > ea) |
99 | { |
100 | t1 = a; |
101 | a = b; |
102 | b = t1; |
103 | j = ea; |
104 | ea = eb; |
105 | eb = j; |
106 | } |
107 | } else { /* scale a and b by 2^9600 */ |
108 | ea += 0x2580; /* a *= 2^9600 */ |
109 | eb += 0x2580; /* b *= 2^9600 */ |
110 | k -= 9600; |
111 | SET_LDOUBLE_EXP(a,ea); |
112 | SET_LDOUBLE_EXP(b,eb); |
113 | } |
114 | } |
115 | /* medium size a and b */ |
116 | w = a-b; |
117 | if (w>b) { |
118 | uint32_t high; |
119 | GET_LDOUBLE_MSW(high,a); |
120 | SET_LDOUBLE_WORDS(t1,ea,high,0); |
121 | t2 = a-t1; |
122 | w = sqrtl(t1*t1-(b*(-b)-t2*(a+t1))); |
123 | } else { |
124 | uint32_t high; |
125 | GET_LDOUBLE_MSW(high,b); |
126 | a = a+a; |
127 | SET_LDOUBLE_WORDS(y1,eb,high,0); |
128 | y2 = b - y1; |
129 | GET_LDOUBLE_MSW(high,a); |
130 | SET_LDOUBLE_WORDS(t1,ea+1,high,0); |
131 | t2 = a - t1; |
132 | w = sqrtl(t1*y1-(w*(-w)-(t1*y2+t2*b))); |
133 | } |
134 | if(k!=0) { |
135 | uint32_t exp; |
136 | t1 = 1.0; |
137 | GET_LDOUBLE_EXP(exp,t1); |
138 | SET_LDOUBLE_EXP(t1,exp+k); |
139 | w *= t1; |
140 | math_check_force_underflow_nonneg (w); |
141 | return w; |
142 | } else return w; |
143 | } |
144 | libm_alias_finite (__ieee754_hypotl, __hypotl) |
145 | |