xref: /titanic_51/usr/src/lib/libm/common/complex/cabs.c (revision b59c4a48daf5a1863ecac763711b497b2f8321e4)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
23  */
24 /*
25  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #pragma weak cabs = __cabs
30 
31 #include "libm_synonyms.h"
32 #include <math.h>
33 #include "complex_wrapper.h"
34 
35 /*
36  * If C were the only standard we cared about, cabs could just call
37  * hypot.  Unfortunately, various other standards say that hypot must
38  * call matherr and/or set errno to ERANGE when the result overflows.
39  * Since cabs should do neither of these things, we have to either
40  * make hypot a wrapper on another internal function or duplicate
41  * the hypot implementation here.  I've chosen to do the latter.
42  */
43 
44 static const double
45 	zero = 0.0,
46 	onep1u = 1.00000000000000022204e+00,	/* 0x3ff00000 1 = 1+2**-52 */
47 	twom53 = 1.11022302462515654042e-16,	/* 0x3ca00000 0 = 2**-53 */
48 	twom768 = 6.441148769597133308e-232,	/* 2^-768 */
49 	two768  = 1.552518092300708935e+231;	/* 2^768 */
50 
51 double
52 cabs(dcomplex z)
53 {
54 	double		x, y, xh, yh, w, ax, ay;
55 	int		i, j, nx, ny, ix, iy, iscale = 0;
56 	unsigned	lx, ly;
57 
58 	x = D_RE(z);
59 	y = D_IM(z);
60 
61 	ix = ((int *)&x)[HIWORD] & ~0x80000000;
62 	lx = ((int *)&x)[LOWORD];
63 	iy = ((int *)&y)[HIWORD] & ~0x80000000;
64 	ly = ((int *)&y)[LOWORD];
65 
66 	/* force ax = |x| ~>~ ay = |y| */
67 	if (iy > ix) {
68 		ax = fabs(y);
69 		ay = fabs(x);
70 		i = ix;
71 		ix = iy;
72 		iy = i;
73 		i = lx;
74 		lx = ly;
75 		ly = i;
76 	} else {
77 		ax = fabs(x);
78 		ay = fabs(y);
79 	}
80 	nx = ix >> 20;
81 	ny = iy >> 20;
82 	j  = nx - ny;
83 
84 	if (nx >= 0x5f3) {
85 		/* x >= 2^500 (x*x or y*y may overflow) */
86 		if (nx == 0x7ff) {
87 			/* inf or NaN, signal of sNaN */
88 			if (((ix - 0x7ff00000) | lx) == 0)
89 				return ((ax == ay)? ay : ax);
90 			else if (((iy - 0x7ff00000) | ly) == 0)
91 				return ((ay == ax)? ax : ay);
92 			else
93 				return (ax * ay);
94 		} else if (j > 32) {
95 			/* x >> y */
96 			if (j <= 53)
97 				ay *= twom53;
98 			ax += ay;
99 			return (ax);
100 		}
101 		ax *= twom768;
102 		ay *= twom768;
103 		iscale = 2;
104 		ix -= 768 << 20;
105 		iy -= 768 << 20;
106 	} else if (ny < 0x23d) {
107 		/* y < 2^-450 (x*x or y*y may underflow) */
108 		if ((ix | lx) == 0)
109 			return (ay);
110 		if ((iy | ly) == 0)
111 			return (ax);
112 		if (j > 53) 		/* x >> y */
113 			return (ax + ay);
114 		iscale = 1;
115 		ax *= two768;
116 		ay *= two768;
117 		if (nx == 0) {
118 			if (ax == zero)	/* guard subnormal flush to zero */
119 				return (ax);
120 			ix = ((int *)&ax)[HIWORD];
121 		} else {
122 			ix += 768 << 20;
123 		}
124 		if (ny == 0) {
125 			if (ay == zero)	/* guard subnormal flush to zero */
126 				return (ax * twom768);
127 			iy = ((int *)&ay)[HIWORD];
128 		} else {
129 			iy += 768 << 20;
130 		}
131 		j = (ix >> 20) - (iy >> 20);
132 		if (j > 32) {
133 			/* x >> y */
134 			if (j <= 53)
135 				ay *= twom53;
136 			return ((ax + ay) * twom768);
137 		}
138 	} else if (j > 32) {
139 		/* x >> y */
140 		if (j <= 53)
141 			ay *= twom53;
142 		return (ax + ay);
143 	}
144 
145 	/*
146 	 * Medium range ax and ay with max{|ax/ay|,|ay/ax|} bounded by 2^32.
147 	 * First check rounding mode by comparing onep1u*onep1u with onep1u
148 	 * + twom53.  Make sure the computation is done at run-time.
149 	 */
150 	if (((lx | ly) << 5) == 0) {
151 		ay = ay * ay;
152 		ax += ay / (ax + sqrt(ax * ax + ay));
153 	} else if (onep1u * onep1u != onep1u + twom53) {
154 		/* round-to-zero, positive, negative mode */
155 		/* magic formula with less than an ulp error */
156 		w = sqrt(ax * ax + ay * ay);
157 		ax += ay / ((ax + w) / ay);
158 	} else {
159 		/* round-to-nearest mode */
160 		w = ax - ay;
161 		if (w > ay) {
162 			((int *)&xh)[HIWORD] = ix;
163 			((int *)&xh)[LOWORD] = 0;
164 			ay = ay * ay + (ax - xh) * (ax + xh);
165 			ax = sqrt(xh * xh + ay);
166 		} else {
167 			ax = ax + ax;
168 			((int *)&xh)[HIWORD] = ix + 0x00100000;
169 			((int *)&xh)[LOWORD] = 0;
170 			((int *)&yh)[HIWORD] = iy;
171 			((int *)&yh)[LOWORD] = 0;
172 			ay = w * w + ((ax - xh) * yh + (ay - yh) * ax);
173 			ax = sqrt(xh * yh + ay);
174 		}
175 	}
176 	if (iscale > 0) {
177 		if (iscale == 1)
178 			ax *= twom768;
179 		else
180 			ax *= two768;	/* must generate side effect here */
181 	}
182 	return (ax);
183 }
184