xref: /illumos-gate/usr/src/lib/libc/sparc/fp/_Q_sqrt.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include "quad.h"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate static const double C[] = {
30*7c478bd9Sstevel@tonic-gate 	0.0,
31*7c478bd9Sstevel@tonic-gate 	0.5,
32*7c478bd9Sstevel@tonic-gate 	1.0,
33*7c478bd9Sstevel@tonic-gate 	68719476736.0,
34*7c478bd9Sstevel@tonic-gate 	536870912.0,
35*7c478bd9Sstevel@tonic-gate 	48.0,
36*7c478bd9Sstevel@tonic-gate 	16.0,
37*7c478bd9Sstevel@tonic-gate 	1.52587890625000000000e-05,
38*7c478bd9Sstevel@tonic-gate 	2.86102294921875000000e-06,
39*7c478bd9Sstevel@tonic-gate 	5.96046447753906250000e-08,
40*7c478bd9Sstevel@tonic-gate 	3.72529029846191406250e-09,
41*7c478bd9Sstevel@tonic-gate 	1.70530256582424044609e-13,
42*7c478bd9Sstevel@tonic-gate 	7.10542735760100185871e-15,
43*7c478bd9Sstevel@tonic-gate 	8.67361737988403547206e-19,
44*7c478bd9Sstevel@tonic-gate 	2.16840434497100886801e-19,
45*7c478bd9Sstevel@tonic-gate 	1.27054942088145050860e-21,
46*7c478bd9Sstevel@tonic-gate 	1.21169035041947413311e-27,
47*7c478bd9Sstevel@tonic-gate 	9.62964972193617926528e-35,
48*7c478bd9Sstevel@tonic-gate 	4.70197740328915003187e-38
49*7c478bd9Sstevel@tonic-gate };
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #define	zero		C[0]
52*7c478bd9Sstevel@tonic-gate #define	half		C[1]
53*7c478bd9Sstevel@tonic-gate #define	one		C[2]
54*7c478bd9Sstevel@tonic-gate #define	two36		C[3]
55*7c478bd9Sstevel@tonic-gate #define	two29		C[4]
56*7c478bd9Sstevel@tonic-gate #define	three2p4	C[5]
57*7c478bd9Sstevel@tonic-gate #define	two4		C[6]
58*7c478bd9Sstevel@tonic-gate #define	twom16		C[7]
59*7c478bd9Sstevel@tonic-gate #define	three2m20	C[8]
60*7c478bd9Sstevel@tonic-gate #define	twom24		C[9]
61*7c478bd9Sstevel@tonic-gate #define	twom28		C[10]
62*7c478bd9Sstevel@tonic-gate #define	three2m44	C[11]
63*7c478bd9Sstevel@tonic-gate #define	twom47		C[12]
64*7c478bd9Sstevel@tonic-gate #define	twom60		C[13]
65*7c478bd9Sstevel@tonic-gate #define	twom62		C[14]
66*7c478bd9Sstevel@tonic-gate #define	three2m71	C[15]
67*7c478bd9Sstevel@tonic-gate #define	three2m91	C[16]
68*7c478bd9Sstevel@tonic-gate #define	twom113		C[17]
69*7c478bd9Sstevel@tonic-gate #define	twom124		C[18]
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate static const unsigned
72*7c478bd9Sstevel@tonic-gate 	fsr_re = 0x00000000u,
73*7c478bd9Sstevel@tonic-gate 	fsr_rn = 0xc0000000u;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate #ifdef __sparcv9
76*7c478bd9Sstevel@tonic-gate 
77*7c478bd9Sstevel@tonic-gate /*
78*7c478bd9Sstevel@tonic-gate  * _Qp_sqrt(pz, x) sets *pz = sqrt(*x).
79*7c478bd9Sstevel@tonic-gate  */
80*7c478bd9Sstevel@tonic-gate void
_Qp_sqrt(union longdouble * pz,const union longdouble * x)81*7c478bd9Sstevel@tonic-gate _Qp_sqrt(union longdouble *pz, const union longdouble *x)
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate #else
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate /*
86*7c478bd9Sstevel@tonic-gate  * _Q_sqrt(x) returns sqrt(*x).
87*7c478bd9Sstevel@tonic-gate  */
88*7c478bd9Sstevel@tonic-gate union longdouble
89*7c478bd9Sstevel@tonic-gate _Q_sqrt(const union longdouble *x)
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate #endif	/* __sparcv9 */
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate {
94*7c478bd9Sstevel@tonic-gate 	union longdouble	z;
95*7c478bd9Sstevel@tonic-gate 	union xdouble		u;
96*7c478bd9Sstevel@tonic-gate 	double			c, d, rr, r[2], tt[3], xx[4], zz[5];
97*7c478bd9Sstevel@tonic-gate 	unsigned int		xm, fsr, lx, wx[3];
98*7c478bd9Sstevel@tonic-gate 	unsigned int		msw, frac2, frac3, frac4, rm;
99*7c478bd9Sstevel@tonic-gate 	int			ex, ez;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	if (QUAD_ISZERO(*x)) {
102*7c478bd9Sstevel@tonic-gate 		Z = *x;
103*7c478bd9Sstevel@tonic-gate 		QUAD_RETURN(Z);
104*7c478bd9Sstevel@tonic-gate 	}
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	xm = x->l.msw;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	__quad_getfsrp(&fsr);
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	/* handle nan and inf cases */
111*7c478bd9Sstevel@tonic-gate 	if ((xm & 0x7fffffff) >= 0x7fff0000) {
112*7c478bd9Sstevel@tonic-gate 		if ((x->l.msw & 0xffff) | x->l.frac2 | x->l.frac3 |
113*7c478bd9Sstevel@tonic-gate 		    x->l.frac4) {
114*7c478bd9Sstevel@tonic-gate 			if (!(x->l.msw & 0x8000)) {
115*7c478bd9Sstevel@tonic-gate 				/* snan, signal invalid */
116*7c478bd9Sstevel@tonic-gate 				if (fsr & FSR_NVM) {
117*7c478bd9Sstevel@tonic-gate 					__quad_fsqrtq(x, &Z);
118*7c478bd9Sstevel@tonic-gate 				} else {
119*7c478bd9Sstevel@tonic-gate 					Z = *x;
120*7c478bd9Sstevel@tonic-gate 					Z.l.msw |= 0x8000;
121*7c478bd9Sstevel@tonic-gate 					fsr = (fsr & ~FSR_CEXC) | FSR_NVA |
122*7c478bd9Sstevel@tonic-gate 					    FSR_NVC;
123*7c478bd9Sstevel@tonic-gate 					__quad_setfsrp(&fsr);
124*7c478bd9Sstevel@tonic-gate 				}
125*7c478bd9Sstevel@tonic-gate 				QUAD_RETURN(Z);
126*7c478bd9Sstevel@tonic-gate 			}
127*7c478bd9Sstevel@tonic-gate 			Z = *x;
128*7c478bd9Sstevel@tonic-gate 			QUAD_RETURN(Z);
129*7c478bd9Sstevel@tonic-gate 		}
130*7c478bd9Sstevel@tonic-gate 		if (x->l.msw & 0x80000000) {
131*7c478bd9Sstevel@tonic-gate 			/* sqrt(-inf), signal invalid */
132*7c478bd9Sstevel@tonic-gate 			if (fsr & FSR_NVM) {
133*7c478bd9Sstevel@tonic-gate 				__quad_fsqrtq(x, &Z);
134*7c478bd9Sstevel@tonic-gate 			} else {
135*7c478bd9Sstevel@tonic-gate 				Z.l.msw = 0x7fffffff;
136*7c478bd9Sstevel@tonic-gate 				Z.l.frac2 = Z.l.frac3 = Z.l.frac4 = 0xffffffff;
137*7c478bd9Sstevel@tonic-gate 				fsr = (fsr & ~FSR_CEXC) | FSR_NVA | FSR_NVC;
138*7c478bd9Sstevel@tonic-gate 				__quad_setfsrp(&fsr);
139*7c478bd9Sstevel@tonic-gate 			}
140*7c478bd9Sstevel@tonic-gate 			QUAD_RETURN(Z);
141*7c478bd9Sstevel@tonic-gate 		}
142*7c478bd9Sstevel@tonic-gate 		/* sqrt(inf), return inf */
143*7c478bd9Sstevel@tonic-gate 		Z = *x;
144*7c478bd9Sstevel@tonic-gate 		QUAD_RETURN(Z);
145*7c478bd9Sstevel@tonic-gate 	}
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	/* handle negative numbers */
148*7c478bd9Sstevel@tonic-gate 	if (xm & 0x80000000) {
149*7c478bd9Sstevel@tonic-gate 		if (fsr & FSR_NVM) {
150*7c478bd9Sstevel@tonic-gate 			__quad_fsqrtq(x, &Z);
151*7c478bd9Sstevel@tonic-gate 		} else {
152*7c478bd9Sstevel@tonic-gate 			Z.l.msw = 0x7fffffff;
153*7c478bd9Sstevel@tonic-gate 			Z.l.frac2 = Z.l.frac3 = Z.l.frac4 = 0xffffffff;
154*7c478bd9Sstevel@tonic-gate 			fsr = (fsr & ~FSR_CEXC) | FSR_NVA | FSR_NVC;
155*7c478bd9Sstevel@tonic-gate 			__quad_setfsrp(&fsr);
156*7c478bd9Sstevel@tonic-gate 		}
157*7c478bd9Sstevel@tonic-gate 		QUAD_RETURN(Z);
158*7c478bd9Sstevel@tonic-gate 	}
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	/* now x is finite, positive */
161*7c478bd9Sstevel@tonic-gate 	__quad_setfsrp((unsigned *)&fsr_re);
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	/* get the normalized significand and exponent */
164*7c478bd9Sstevel@tonic-gate 	ex = (int)(xm >> 16);
165*7c478bd9Sstevel@tonic-gate 	lx = xm & 0xffff;
166*7c478bd9Sstevel@tonic-gate 	if (ex) {
167*7c478bd9Sstevel@tonic-gate 		lx |= 0x10000;
168*7c478bd9Sstevel@tonic-gate 		wx[0] = x->l.frac2;
169*7c478bd9Sstevel@tonic-gate 		wx[1] = x->l.frac3;
170*7c478bd9Sstevel@tonic-gate 		wx[2] = x->l.frac4;
171*7c478bd9Sstevel@tonic-gate 	} else {
172*7c478bd9Sstevel@tonic-gate 		if (lx | (x->l.frac2 & 0xfffe0000)) {
173*7c478bd9Sstevel@tonic-gate 			wx[0] = x->l.frac2;
174*7c478bd9Sstevel@tonic-gate 			wx[1] = x->l.frac3;
175*7c478bd9Sstevel@tonic-gate 			wx[2] = x->l.frac4;
176*7c478bd9Sstevel@tonic-gate 			ex = 1;
177*7c478bd9Sstevel@tonic-gate 		} else if (x->l.frac2 | (x->l.frac3 & 0xfffe0000)) {
178*7c478bd9Sstevel@tonic-gate 			lx = x->l.frac2;
179*7c478bd9Sstevel@tonic-gate 			wx[0] = x->l.frac3;
180*7c478bd9Sstevel@tonic-gate 			wx[1] = x->l.frac4;
181*7c478bd9Sstevel@tonic-gate 			wx[2] = 0;
182*7c478bd9Sstevel@tonic-gate 			ex = -31;
183*7c478bd9Sstevel@tonic-gate 		} else if (x->l.frac3 | (x->l.frac4 & 0xfffe0000)) {
184*7c478bd9Sstevel@tonic-gate 			lx = x->l.frac3;
185*7c478bd9Sstevel@tonic-gate 			wx[0] = x->l.frac4;
186*7c478bd9Sstevel@tonic-gate 			wx[1] = wx[2] = 0;
187*7c478bd9Sstevel@tonic-gate 			ex = -63;
188*7c478bd9Sstevel@tonic-gate 		} else {
189*7c478bd9Sstevel@tonic-gate 			lx = x->l.frac4;
190*7c478bd9Sstevel@tonic-gate 			wx[0] = wx[1] = wx[2] = 0;
191*7c478bd9Sstevel@tonic-gate 			ex = -95;
192*7c478bd9Sstevel@tonic-gate 		}
193*7c478bd9Sstevel@tonic-gate 		while ((lx & 0x10000) == 0) {
194*7c478bd9Sstevel@tonic-gate 			lx = (lx << 1) | (wx[0] >> 31);
195*7c478bd9Sstevel@tonic-gate 			wx[0] = (wx[0] << 1) | (wx[1] >> 31);
196*7c478bd9Sstevel@tonic-gate 			wx[1] = (wx[1] << 1) | (wx[2] >> 31);
197*7c478bd9Sstevel@tonic-gate 			wx[2] <<= 1;
198*7c478bd9Sstevel@tonic-gate 			ex--;
199*7c478bd9Sstevel@tonic-gate 		}
200*7c478bd9Sstevel@tonic-gate 	}
201*7c478bd9Sstevel@tonic-gate 	ez = ex - 0x3fff;
202*7c478bd9Sstevel@tonic-gate 	if (ez & 1) {
203*7c478bd9Sstevel@tonic-gate 		/* make exponent even */
204*7c478bd9Sstevel@tonic-gate 		lx = (lx << 1) | (wx[0] >> 31);
205*7c478bd9Sstevel@tonic-gate 		wx[0] = (wx[0] << 1) | (wx[1] >> 31);
206*7c478bd9Sstevel@tonic-gate 		wx[1] = (wx[1] << 1) | (wx[2] >> 31);
207*7c478bd9Sstevel@tonic-gate 		wx[2] <<= 1;
208*7c478bd9Sstevel@tonic-gate 		ez--;
209*7c478bd9Sstevel@tonic-gate 	}
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	/* extract the significands into doubles */
212*7c478bd9Sstevel@tonic-gate 	c = twom16;
213*7c478bd9Sstevel@tonic-gate 	xx[0] = (double)((int)lx) * c;
214*7c478bd9Sstevel@tonic-gate 
215*7c478bd9Sstevel@tonic-gate 	c *= twom24;
216*7c478bd9Sstevel@tonic-gate 	xx[0] += (double)((int)(wx[0] >> 8)) * c;
217*7c478bd9Sstevel@tonic-gate 
218*7c478bd9Sstevel@tonic-gate 	c *= twom24;
219*7c478bd9Sstevel@tonic-gate 	xx[1] = (double)((int)(((wx[0] << 16) | (wx[1] >> 16)) &
220*7c478bd9Sstevel@tonic-gate 	    0xffffff)) * c;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	c *= twom24;
223*7c478bd9Sstevel@tonic-gate 	xx[2] = (double)((int)(((wx[1] << 8) | (wx[2] >> 24)) &
224*7c478bd9Sstevel@tonic-gate 	    0xffffff)) * c;
225*7c478bd9Sstevel@tonic-gate 
226*7c478bd9Sstevel@tonic-gate 	c *= twom24;
227*7c478bd9Sstevel@tonic-gate 	xx[3] = (double)((int)(wx[2] & 0xffffff)) * c;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 	/* approximate the divisor for the Newton iteration */
230*7c478bd9Sstevel@tonic-gate 	c = xx[0] + xx[1];
231*7c478bd9Sstevel@tonic-gate 	c = __quad_dp_sqrt(&c);
232*7c478bd9Sstevel@tonic-gate 	rr = half / c;
233*7c478bd9Sstevel@tonic-gate 
234*7c478bd9Sstevel@tonic-gate 	/* compute the first five "digits" of the square root */
235*7c478bd9Sstevel@tonic-gate 	zz[0] = (c + two29) - two29;
236*7c478bd9Sstevel@tonic-gate 	tt[0] = zz[0] + zz[0];
237*7c478bd9Sstevel@tonic-gate 	r[0] = (xx[0] - zz[0] * zz[0]) + xx[1];
238*7c478bd9Sstevel@tonic-gate 
239*7c478bd9Sstevel@tonic-gate 	zz[1] = (rr * (r[0] + xx[2]) + three2p4) - three2p4;
240*7c478bd9Sstevel@tonic-gate 	tt[1] = zz[1] + zz[1];
241*7c478bd9Sstevel@tonic-gate 	r[0] -= tt[0] * zz[1];
242*7c478bd9Sstevel@tonic-gate 	r[1] = xx[2] - zz[1] * zz[1];
243*7c478bd9Sstevel@tonic-gate 	c = (r[1] + three2m20) - three2m20;
244*7c478bd9Sstevel@tonic-gate 	r[0] += c;
245*7c478bd9Sstevel@tonic-gate 	r[1] = (r[1] - c) + xx[3];
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	zz[2] = (rr * (r[0] + r[1]) + three2m20) - three2m20;
248*7c478bd9Sstevel@tonic-gate 	tt[2] = zz[2] + zz[2];
249*7c478bd9Sstevel@tonic-gate 	r[0] -= tt[0] * zz[2];
250*7c478bd9Sstevel@tonic-gate 	r[1] -= tt[1] * zz[2];
251*7c478bd9Sstevel@tonic-gate 	c = (r[1] + three2m44) - three2m44;
252*7c478bd9Sstevel@tonic-gate 	r[0] += c;
253*7c478bd9Sstevel@tonic-gate 	r[1] = (r[1] - c) - zz[2] * zz[2];
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate 	zz[3] = (rr * (r[0] + r[1]) + three2m44) - three2m44;
256*7c478bd9Sstevel@tonic-gate 	r[0] = ((r[0] - tt[0] * zz[3]) + r[1]) - tt[1] * zz[3];
257*7c478bd9Sstevel@tonic-gate 	r[1] = -tt[2] * zz[3];
258*7c478bd9Sstevel@tonic-gate 	c = (r[1] + three2m91) - three2m91;
259*7c478bd9Sstevel@tonic-gate 	r[0] += c;
260*7c478bd9Sstevel@tonic-gate 	r[1] = (r[1] - c) - zz[3] * zz[3];
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	zz[4] = (rr * (r[0] + r[1]) + three2m71) - three2m71;
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	/* reduce to three doubles, making sure zz[1] is positive */
265*7c478bd9Sstevel@tonic-gate 	zz[0] += zz[1] - twom47;
266*7c478bd9Sstevel@tonic-gate 	zz[1] = twom47 + zz[2] + zz[3];
267*7c478bd9Sstevel@tonic-gate 	zz[2] = zz[4];
268*7c478bd9Sstevel@tonic-gate 
269*7c478bd9Sstevel@tonic-gate 	/* if the third term might lie on a rounding boundary, perturb it */
270*7c478bd9Sstevel@tonic-gate 	if (zz[2] == (twom62 + zz[2]) - twom62) {
271*7c478bd9Sstevel@tonic-gate 		/* here we just need to get the sign of the remainder */
272*7c478bd9Sstevel@tonic-gate 		c = (((((r[0] - tt[0] * zz[4]) - tt[1] * zz[4]) + r[1])
273*7c478bd9Sstevel@tonic-gate 		    - tt[2] * zz[4]) - (zz[3] + zz[3]) * zz[4]) - zz[4] * zz[4];
274*7c478bd9Sstevel@tonic-gate 		if (c < zero)
275*7c478bd9Sstevel@tonic-gate 			zz[2] -= twom124;
276*7c478bd9Sstevel@tonic-gate 		else if (c > zero)
277*7c478bd9Sstevel@tonic-gate 			zz[2] += twom124;
278*7c478bd9Sstevel@tonic-gate 	}
279*7c478bd9Sstevel@tonic-gate 
280*7c478bd9Sstevel@tonic-gate 	/*
281*7c478bd9Sstevel@tonic-gate 	 * propagate carries/borrows, using round-to-negative-infinity mode
282*7c478bd9Sstevel@tonic-gate 	 * to make all terms nonnegative (note that we can't encounter a
283*7c478bd9Sstevel@tonic-gate 	 * borrow so large that the roundoff is unrepresentable because
284*7c478bd9Sstevel@tonic-gate 	 * we took care to make zz[1] positive above)
285*7c478bd9Sstevel@tonic-gate 	 */
286*7c478bd9Sstevel@tonic-gate 	__quad_setfsrp(&fsr_rn);
287*7c478bd9Sstevel@tonic-gate 	c = zz[1] + zz[2];
288*7c478bd9Sstevel@tonic-gate 	zz[2] += (zz[1] - c);
289*7c478bd9Sstevel@tonic-gate 	zz[1] = c;
290*7c478bd9Sstevel@tonic-gate 	c = zz[0] + zz[1];
291*7c478bd9Sstevel@tonic-gate 	zz[1] += (zz[0] - c);
292*7c478bd9Sstevel@tonic-gate 	zz[0] = c;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	/* adjust exponent and strip off integer bit */
295*7c478bd9Sstevel@tonic-gate 	ez = (ez >> 1) + 0x3fff;
296*7c478bd9Sstevel@tonic-gate 	zz[0] -= one;
297*7c478bd9Sstevel@tonic-gate 
298*7c478bd9Sstevel@tonic-gate 	/* the first 48 bits of fraction come from zz[0] */
299*7c478bd9Sstevel@tonic-gate 	u.d = d = two36 + zz[0];
300*7c478bd9Sstevel@tonic-gate 	msw = u.l.lo;
301*7c478bd9Sstevel@tonic-gate 	zz[0] -= (d - two36);
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	u.d = d = two4 + zz[0];
304*7c478bd9Sstevel@tonic-gate 	frac2 = u.l.lo;
305*7c478bd9Sstevel@tonic-gate 	zz[0] -= (d - two4);
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate 	/* the next 32 come from zz[0] and zz[1] */
308*7c478bd9Sstevel@tonic-gate 	u.d = d = twom28 + (zz[0] + zz[1]);
309*7c478bd9Sstevel@tonic-gate 	frac3 = u.l.lo;
310*7c478bd9Sstevel@tonic-gate 	zz[0] -= (d - twom28);
311*7c478bd9Sstevel@tonic-gate 
312*7c478bd9Sstevel@tonic-gate 	/* condense the remaining fraction; errors here won't matter */
313*7c478bd9Sstevel@tonic-gate 	c = zz[0] + zz[1];
314*7c478bd9Sstevel@tonic-gate 	zz[1] = ((zz[0] - c) + zz[1]) + zz[2];
315*7c478bd9Sstevel@tonic-gate 	zz[0] = c;
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 	/* get the last word of fraction */
318*7c478bd9Sstevel@tonic-gate 	u.d = d = twom60 + (zz[0] + zz[1]);
319*7c478bd9Sstevel@tonic-gate 	frac4 = u.l.lo;
320*7c478bd9Sstevel@tonic-gate 	zz[0] -= (d - twom60);
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate 	/* keep track of what's left for rounding; note that the error */
323*7c478bd9Sstevel@tonic-gate 	/* in computing c will be non-negative due to rounding mode */
324*7c478bd9Sstevel@tonic-gate 	c = zz[0] + zz[1];
325*7c478bd9Sstevel@tonic-gate 
326*7c478bd9Sstevel@tonic-gate 	/* get the rounding mode */
327*7c478bd9Sstevel@tonic-gate 	rm = fsr >> 30;
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	/* round and raise exceptions */
330*7c478bd9Sstevel@tonic-gate 	fsr &= ~FSR_CEXC;
331*7c478bd9Sstevel@tonic-gate 	if (c != zero) {
332*7c478bd9Sstevel@tonic-gate 		fsr |= FSR_NXC;
333*7c478bd9Sstevel@tonic-gate 
334*7c478bd9Sstevel@tonic-gate 		/* decide whether to round the fraction up */
335*7c478bd9Sstevel@tonic-gate 		if (rm == FSR_RP || (rm == FSR_RN && (c > twom113 ||
336*7c478bd9Sstevel@tonic-gate 		    (c == twom113 && ((frac4 & 1) || (c - zz[0] != zz[1])))))) {
337*7c478bd9Sstevel@tonic-gate 			/* round up and renormalize if necessary */
338*7c478bd9Sstevel@tonic-gate 			if (++frac4 == 0)
339*7c478bd9Sstevel@tonic-gate 				if (++frac3 == 0)
340*7c478bd9Sstevel@tonic-gate 					if (++frac2 == 0)
341*7c478bd9Sstevel@tonic-gate 						if (++msw == 0x10000) {
342*7c478bd9Sstevel@tonic-gate 							msw = 0;
343*7c478bd9Sstevel@tonic-gate 							ez++;
344*7c478bd9Sstevel@tonic-gate 						}
345*7c478bd9Sstevel@tonic-gate 		}
346*7c478bd9Sstevel@tonic-gate 	}
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	/* stow the result */
349*7c478bd9Sstevel@tonic-gate 	z.l.msw = (ez << 16) | msw;
350*7c478bd9Sstevel@tonic-gate 	z.l.frac2 = frac2;
351*7c478bd9Sstevel@tonic-gate 	z.l.frac3 = frac3;
352*7c478bd9Sstevel@tonic-gate 	z.l.frac4 = frac4;
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 	if ((fsr & FSR_CEXC) & (fsr >> 23)) {
355*7c478bd9Sstevel@tonic-gate 		__quad_setfsrp(&fsr);
356*7c478bd9Sstevel@tonic-gate 		__quad_fsqrtq(x, &Z);
357*7c478bd9Sstevel@tonic-gate 	} else {
358*7c478bd9Sstevel@tonic-gate 		Z = z;
359*7c478bd9Sstevel@tonic-gate 		fsr |= (fsr & 0x1f) << 5;
360*7c478bd9Sstevel@tonic-gate 		__quad_setfsrp(&fsr);
361*7c478bd9Sstevel@tonic-gate 	}
362*7c478bd9Sstevel@tonic-gate 	QUAD_RETURN(Z);
363*7c478bd9Sstevel@tonic-gate }
364