Print this page
5261 libm should stop using synonyms.h
5298 fabs is 0-sized, confuses dis(1) and others
Reviewed by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
Approved by: Gordon Ross <gwr@nexenta.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libm/common/complex/cacos.c
+++ new/usr/src/lib/libm/common/complex/cacos.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
↓ open down ↓ |
19 lines elided |
↑ open up ↑ |
20 20 */
21 21
22 22 /*
23 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24 24 */
25 25 /*
26 26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 27 * Use is subject to license terms.
28 28 */
29 29
30 -#pragma weak cacos = __cacos
30 +#pragma weak __cacos = cacos
31 31
32 32 /* INDENT OFF */
33 33 /*
34 34 * dcomplex cacos(dcomplex z);
35 35 *
36 36 * Alogrithm
37 37 * (based on T.E.Hull, Thomas F. Fairgrieve and Ping Tak Peter Tang's
38 38 * paper "Implementing the Complex Arcsine and Arccosine Functins Using
39 39 * Exception Handling", ACM TOMS, Vol 23, pp 299-335)
40 40 *
41 41 * The principal value of complex inverse cosine function cacos(z),
42 42 * where z = x+iy, can be defined by
43 43 *
44 44 * cacos(z) = acos(B) - i sign(y) log (A + sqrt(A*A-1)),
45 45 *
46 46 * where the log function is the natural log, and
47 47 * ____________ ____________
48 48 * 1 / 2 2 1 / 2 2
49 49 * A = --- / (x+1) + y + --- / (x-1) + y
50 50 * 2 \/ 2 \/
51 51 * ____________ ____________
52 52 * 1 / 2 2 1 / 2 2
53 53 * B = --- / (x+1) + y - --- / (x-1) + y .
54 54 * 2 \/ 2 \/
55 55 *
56 56 * The Branch cuts are on the real line from -inf to -1 and from 1 to inf.
57 57 * The real and imaginary parts are based on Abramowitz and Stegun
58 58 * [Handbook of Mathematic Functions, 1972]. The sign of the imaginary
59 59 * part is chosen to be the generally considered the principal value of
60 60 * this function.
61 61 *
62 62 * Notes:1. A is the average of the distances from z to the points (1,0)
63 63 * and (-1,0) in the complex z-plane, and in particular A>=1.
64 64 * 2. B is in [-1,1], and A*B = x
65 65 *
66 66 * Basic relations
67 67 * cacos(conj(z)) = conj(cacos(z))
68 68 * cacos(-z) = pi - cacos(z)
69 69 * cacos( z) = pi/2 - casin(z)
70 70 *
71 71 * Special cases (conform to ISO/IEC 9899:1999(E)):
72 72 * cacos(+-0 + i y ) = pi/2 - i y for y is +-0, +-inf, NaN
73 73 * cacos( x + i inf) = pi/2 - i inf for all x
74 74 * cacos( x + i NaN) = NaN + i NaN with invalid for non-zero finite x
75 75 * cacos(-inf + i y ) = pi - i inf for finite +y
76 76 * cacos( inf + i y ) = 0 - i inf for finite +y
77 77 * cacos(-inf + i inf) = 3pi/4- i inf
78 78 * cacos( inf + i inf) = pi/4 - i inf
79 79 * cacos(+-inf+ i NaN) = NaN - i inf (sign of imaginary is unspecified)
80 80 * cacos(NaN + i y ) = NaN + i NaN with invalid for finite y
81 81 * cacos(NaN + i inf) = NaN - i inf
82 82 * cacos(NaN + i NaN) = NaN + i NaN
83 83 *
84 84 * Special Regions (better formula for accuracy and for avoiding spurious
85 85 * overflow or underflow) (all x and y are assumed nonnegative):
86 86 * case 1: y = 0
87 87 * case 2: tiny y relative to x-1: y <= ulp(0.5)*|x-1|
88 88 * case 3: tiny y: y < 4 sqrt(u), where u = minimum normal number
89 89 * case 4: huge y relative to x+1: y >= (1+x)/ulp(0.5)
90 90 * case 5: huge x and y: x and y >= sqrt(M)/8, where M = maximum normal number
91 91 * case 6: tiny x: x < 4 sqrt(u)
92 92 * --------
93 93 * case 1 & 2. y=0 or y/|x-1| is tiny. We have
94 94 * ____________ _____________
95 95 * / 2 2 / y 2
96 96 * / (x+-1) + y = |x+-1| / 1 + (------)
97 97 * \/ \/ |x+-1|
98 98 *
99 99 * 1 y 2
100 100 * ~ |x+-1| ( 1 + --- (------) )
101 101 * 2 |x+-1|
102 102 *
103 103 * 2
104 104 * y
105 105 * = |x+-1| + --------.
106 106 * 2|x+-1|
107 107 *
108 108 * Consequently, it is not difficult to see that
109 109 * 2
110 110 * y
111 111 * [ 1 + ------------ , if x < 1,
112 112 * [ 2(1+x)(1-x)
113 113 * [
114 114 * [
115 115 * [ x, if x = 1 (y = 0),
116 116 * [
117 117 * A ~= [ 2
118 118 * [ x * y
119 119 * [ x + ------------ ~ x, if x > 1
120 120 * [ 2(x+1)(x-1)
121 121 *
122 122 * and hence
123 123 * ______ 2
124 124 * / 2 y y
125 125 * A + \/ A - 1 ~ 1 + ---------------- + -----------, if x < 1,
126 126 * sqrt((x+1)(1-x)) 2(x+1)(1-x)
127 127 *
128 128 *
129 129 * ~ x + sqrt((x-1)*(x+1)), if x >= 1.
130 130 *
131 131 * 2
132 132 * y
133 133 * [ x(1 - -----------) ~ x, if x < 1,
134 134 * [ 2(1+x)(1-x)
135 135 * B = x/A ~ [
136 136 * [ 1, if x = 1,
137 137 * [
138 138 * [ 2
139 139 * [ y
140 140 * [ 1 - ------------ , if x > 1,
141 141 * [ 2(x+1)(x-1)
142 142 * Thus
143 143 * [ acos(x) - i y/sqrt((x-1)*(x+1)), if x < 1,
144 144 * [
145 145 * cacos(x+i*y)~ [ 0 - i 0, if x = 1,
146 146 * [
147 147 * [ y/sqrt(x*x-1) - i log(x+sqrt(x*x-1)), if x > 1.
148 148 *
149 149 * Note: y/sqrt(x*x-1) ~ y/x when x >= 2**26.
150 150 * case 3. y < 4 sqrt(u), where u = minimum normal x.
151 151 * After case 1 and 2, this will only occurs when x=1. When x=1, we have
152 152 * A = (sqrt(4+y*y)+y)/2 ~ 1 + y/2 + y^2/8 + ...
153 153 * and
154 154 * B = 1/A = 1 - y/2 + y^2/8 + ...
155 155 * Since
156 156 * cos(sqrt(y)) ~ 1 - y/2 + ...
157 157 * we have, for the real part,
158 158 * acos(B) ~ acos(1 - y/2) ~ sqrt(y)
159 159 * For the imaginary part,
160 160 * log(A+sqrt(A*A-1)) ~ log(1+y/2+sqrt(2*y/2))
161 161 * = log(1+y/2+sqrt(y))
162 162 * = (y/2+sqrt(y)) - (y/2+sqrt(y))^2/2 + ...
163 163 * ~ sqrt(y) - y*(sqrt(y)+y/2)/2
164 164 * ~ sqrt(y)
165 165 *
166 166 * case 4. y >= (x+1)/ulp(0.5). In this case, A ~ y and B ~ x/y. Thus
167 167 * real part = acos(B) ~ pi/2
168 168 * and
169 169 * imag part = log(y+sqrt(y*y-one))
170 170 *
171 171 * case 5. Both x and y are large: x and y > sqrt(M)/8, where M = maximum x
172 172 * In this case,
173 173 * A ~ sqrt(x*x+y*y)
174 174 * B ~ x/sqrt(x*x+y*y).
175 175 * Thus
176 176 * real part = acos(B) = atan(y/x),
177 177 * imag part = log(A+sqrt(A*A-1)) ~ log(2A)
178 178 * = log(2) + 0.5*log(x*x+y*y)
179 179 * = log(2) + log(y) + 0.5*log(1+(x/y)^2)
180 180 *
181 181 * case 6. x < 4 sqrt(u). In this case, we have
182 182 * A ~ sqrt(1+y*y), B = x/sqrt(1+y*y).
183 183 * Since B is tiny, we have
184 184 * real part = acos(B) ~ pi/2
185 185 * imag part = log(A+sqrt(A*A-1)) = log (A+sqrt(y*y))
186 186 * = log(y+sqrt(1+y*y))
187 187 * = 0.5*log(y^2+2ysqrt(1+y^2)+1+y^2)
188 188 * = 0.5*log(1+2y(y+sqrt(1+y^2)));
189 189 * = 0.5*log1p(2y(y+A));
190 190 *
191 191 * cacos(z) = acos(B) - i sign(y) log (A + sqrt(A*A-1)),
192 192 */
193 193 /* INDENT ON */
194 194
195 195 #include "libm.h"
196 196 #include "complex_wrapper.h"
197 197
198 198 /* INDENT OFF */
199 199 static const double
200 200 zero = 0.0,
201 201 one = 1.0,
202 202 E = 1.11022302462515654042e-16, /* 2**-53 */
203 203 ln2 = 6.93147180559945286227e-01,
204 204 pi = 3.1415926535897931159979634685,
205 205 pi_l = 1.224646799147353177e-16,
206 206 pi_2 = 1.570796326794896558e+00,
207 207 pi_2_l = 6.123233995736765886e-17,
208 208 pi_4 = 0.78539816339744827899949,
209 209 pi_4_l = 3.061616997868382943e-17,
210 210 pi3_4 = 2.356194490192344836998,
211 211 pi3_4_l = 9.184850993605148829195e-17,
212 212 Foursqrtu = 5.96667258496016539463e-154, /* 2**(-509) */
213 213 Acrossover = 1.5,
214 214 Bcrossover = 0.6417,
215 215 half = 0.5;
216 216 /* INDENT ON */
217 217
218 218 dcomplex
219 219 cacos(dcomplex z) {
220 220 double x, y, t, R, S, A, Am1, B, y2, xm1, xp1, Apx;
221 221 int ix, iy, hx, hy;
222 222 unsigned lx, ly;
223 223 dcomplex ans;
224 224
225 225 x = D_RE(z);
226 226 y = D_IM(z);
227 227 hx = HI_WORD(x);
228 228 lx = LO_WORD(x);
229 229 hy = HI_WORD(y);
230 230 ly = LO_WORD(y);
231 231 ix = hx & 0x7fffffff;
232 232 iy = hy & 0x7fffffff;
233 233
234 234 /* x is 0 */
235 235 if ((ix | lx) == 0) {
236 236 if (((iy | ly) == 0) || (iy >= 0x7ff00000)) {
237 237 D_RE(ans) = pi_2;
238 238 D_IM(ans) = -y;
239 239 return (ans);
240 240 }
241 241 }
242 242
243 243 /* |y| is inf or NaN */
244 244 if (iy >= 0x7ff00000) {
245 245 if (ISINF(iy, ly)) { /* cacos(x + i inf) = pi/2 - i inf */
246 246 D_IM(ans) = -y;
247 247 if (ix < 0x7ff00000) {
248 248 D_RE(ans) = pi_2 + pi_2_l;
249 249 } else if (ISINF(ix, lx)) {
250 250 if (hx >= 0)
251 251 D_RE(ans) = pi_4 + pi_4_l;
252 252 else
253 253 D_RE(ans) = pi3_4 + pi3_4_l;
254 254 } else {
255 255 D_RE(ans) = x;
256 256 }
257 257 } else { /* cacos(x + i NaN) = NaN + i NaN */
258 258 D_RE(ans) = y + x;
259 259 if (ISINF(ix, lx))
260 260 D_IM(ans) = -fabs(x);
261 261 else
262 262 D_IM(ans) = y;
263 263 }
264 264 return (ans);
265 265 }
266 266
267 267 x = fabs(x);
268 268 y = fabs(y);
269 269
270 270 /* x is inf or NaN */
271 271 if (ix >= 0x7ff00000) { /* x is inf or NaN */
272 272 if (ISINF(ix, lx)) { /* x is INF */
273 273 D_IM(ans) = -x;
274 274 if (iy >= 0x7ff00000) {
275 275 if (ISINF(iy, ly)) {
276 276 /* INDENT OFF */
277 277 /* cacos(inf + i inf) = pi/4 - i inf */
278 278 /* cacos(-inf+ i inf) =3pi/4 - i inf */
279 279 /* INDENT ON */
280 280 if (hx >= 0)
281 281 D_RE(ans) = pi_4 + pi_4_l;
282 282 else
283 283 D_RE(ans) = pi3_4 + pi3_4_l;
284 284 } else
285 285 /* INDENT OFF */
286 286 /* cacos(inf + i NaN) = NaN - i inf */
287 287 /* INDENT ON */
288 288 D_RE(ans) = y + y;
289 289 } else
290 290 /* INDENT OFF */
291 291 /* cacos(inf + iy ) = 0 - i inf */
292 292 /* cacos(-inf+ iy ) = pi - i inf */
293 293 /* INDENT ON */
294 294 if (hx >= 0)
295 295 D_RE(ans) = zero;
296 296 else
297 297 D_RE(ans) = pi + pi_l;
298 298 } else { /* x is NaN */
299 299 /* INDENT OFF */
300 300 /*
301 301 * cacos(NaN + i inf) = NaN - i inf
302 302 * cacos(NaN + i y ) = NaN + i NaN
303 303 * cacos(NaN + i NaN) = NaN + i NaN
304 304 */
305 305 /* INDENT ON */
306 306 D_RE(ans) = x + y;
307 307 if (iy >= 0x7ff00000) {
308 308 D_IM(ans) = -y;
309 309 } else {
310 310 D_IM(ans) = x;
311 311 }
312 312 }
313 313 if (hy < 0)
314 314 D_IM(ans) = -D_IM(ans);
315 315 return (ans);
316 316 }
317 317
318 318 if ((iy | ly) == 0) { /* region 1: y=0 */
319 319 if (ix < 0x3ff00000) { /* |x| < 1 */
320 320 D_RE(ans) = acos(x);
321 321 D_IM(ans) = zero;
322 322 } else {
323 323 D_RE(ans) = zero;
324 324 if (ix >= 0x43500000) /* |x| >= 2**54 */
325 325 D_IM(ans) = ln2 + log(x);
326 326 else if (ix >= 0x3ff80000) /* x > Acrossover */
327 327 D_IM(ans) = log(x + sqrt((x - one) * (x +
328 328 one)));
329 329 else {
330 330 xm1 = x - one;
331 331 D_IM(ans) = log1p(xm1 + sqrt(xm1 * (x + one)));
332 332 }
333 333 }
334 334 } else if (y <= E * fabs(x - one)) { /* region 2: y < tiny*|x-1| */
335 335 if (ix < 0x3ff00000) { /* x < 1 */
336 336 D_RE(ans) = acos(x);
337 337 D_IM(ans) = y / sqrt((one + x) * (one - x));
338 338 } else if (ix >= 0x43500000) { /* |x| >= 2**54 */
339 339 D_RE(ans) = y / x;
340 340 D_IM(ans) = ln2 + log(x);
341 341 } else {
342 342 t = sqrt((x - one) * (x + one));
343 343 D_RE(ans) = y / t;
344 344 if (ix >= 0x3ff80000) /* x > Acrossover */
345 345 D_IM(ans) = log(x + t);
346 346 else
347 347 D_IM(ans) = log1p((x - one) + t);
348 348 }
349 349 } else if (y < Foursqrtu) { /* region 3 */
350 350 t = sqrt(y);
351 351 D_RE(ans) = t;
352 352 D_IM(ans) = t;
353 353 } else if (E * y - one >= x) { /* region 4 */
354 354 D_RE(ans) = pi_2;
355 355 D_IM(ans) = ln2 + log(y);
356 356 } else if (ix >= 0x5fc00000 || iy >= 0x5fc00000) { /* x,y>2**509 */
357 357 /* region 5: x+1 or y is very large (>= sqrt(max)/8) */
358 358 t = x / y;
359 359 D_RE(ans) = atan(y / x);
360 360 D_IM(ans) = ln2 + log(y) + half * log1p(t * t);
361 361 } else if (x < Foursqrtu) {
362 362 /* region 6: x is very small, < 4sqrt(min) */
363 363 D_RE(ans) = pi_2;
364 364 A = sqrt(one + y * y);
365 365 if (iy >= 0x3ff80000) /* if y > Acrossover */
366 366 D_IM(ans) = log(y + A);
367 367 else
368 368 D_IM(ans) = half * log1p((y + y) * (y + A));
369 369 } else { /* safe region */
370 370 y2 = y * y;
371 371 xp1 = x + one;
372 372 xm1 = x - one;
373 373 R = sqrt(xp1 * xp1 + y2);
374 374 S = sqrt(xm1 * xm1 + y2);
375 375 A = half * (R + S);
376 376 B = x / A;
377 377 if (B <= Bcrossover)
378 378 D_RE(ans) = acos(B);
379 379 else { /* use atan and an accurate approx to a-x */
380 380 Apx = A + x;
381 381 if (x <= one)
382 382 D_RE(ans) = atan(sqrt(half * Apx * (y2 / (R +
383 383 xp1) + (S - xm1))) / x);
384 384 else
385 385 D_RE(ans) = atan((y * sqrt(half * (Apx / (R +
386 386 xp1) + Apx / (S + xm1)))) / x);
387 387 }
388 388 if (A <= Acrossover) {
389 389 /* use log1p and an accurate approx to A-1 */
390 390 if (x < one)
391 391 Am1 = half * (y2 / (R + xp1) + y2 / (S - xm1));
392 392 else
393 393 Am1 = half * (y2 / (R + xp1) + (S + xm1));
394 394 D_IM(ans) = log1p(Am1 + sqrt(Am1 * (A + one)));
395 395 } else {
396 396 D_IM(ans) = log(A + sqrt(A * A - one));
397 397 }
398 398 }
399 399 if (hx < 0)
400 400 D_RE(ans) = pi - D_RE(ans);
401 401 if (hy >= 0)
402 402 D_IM(ans) = -D_IM(ans);
403 403 return (ans);
404 404 }
↓ open down ↓ |
364 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX