1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
23*0Sstevel@tonic-gate
24*0Sstevel@tonic-gate /*
25*0Sstevel@tonic-gate * Copyright (c) 1988 by Sun Microsystems, Inc.
26*0Sstevel@tonic-gate */
27*0Sstevel@tonic-gate
28*0Sstevel@tonic-gate #include "_Qquad.h"
29*0Sstevel@tonic-gate #include "_Qglobals.h"
30*0Sstevel@tonic-gate
31*0Sstevel@tonic-gate void
_fp_div(px,py,pz)32*0Sstevel@tonic-gate _fp_div(px, py, pz)
33*0Sstevel@tonic-gate unpacked *px, *py, *pz;
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate {
36*0Sstevel@tonic-gate unsigned r[4],*y,q,c;
37*0Sstevel@tonic-gate int n;
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gate *pz = *px;
40*0Sstevel@tonic-gate pz->sign = px->sign ^ py->sign;
41*0Sstevel@tonic-gate
42*0Sstevel@tonic-gate if ((py->fpclass == fp_quiet) || (py->fpclass == fp_signaling)) {
43*0Sstevel@tonic-gate *pz = *py;
44*0Sstevel@tonic-gate return;
45*0Sstevel@tonic-gate }
46*0Sstevel@tonic-gate switch (px->fpclass) {
47*0Sstevel@tonic-gate case fp_quiet:
48*0Sstevel@tonic-gate case fp_signaling:
49*0Sstevel@tonic-gate return;
50*0Sstevel@tonic-gate case fp_zero:
51*0Sstevel@tonic-gate case fp_infinity:
52*0Sstevel@tonic-gate if (px->fpclass == py->fpclass) { /* 0/0 or inf/inf */
53*0Sstevel@tonic-gate fpu_error_nan(pz);
54*0Sstevel@tonic-gate pz->fpclass = fp_quiet;
55*0Sstevel@tonic-gate }
56*0Sstevel@tonic-gate return;
57*0Sstevel@tonic-gate case fp_normal:
58*0Sstevel@tonic-gate switch (py->fpclass) {
59*0Sstevel@tonic-gate case fp_zero: /* number/0 */
60*0Sstevel@tonic-gate fpu_set_exception(fp_division);
61*0Sstevel@tonic-gate pz->fpclass = fp_infinity;
62*0Sstevel@tonic-gate return;
63*0Sstevel@tonic-gate case fp_infinity: /* number/inf */
64*0Sstevel@tonic-gate pz->fpclass = fp_zero;
65*0Sstevel@tonic-gate return;
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate }
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate /* Now x and y are both normal or subnormal. */
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate r[0] = px->significand[0];
72*0Sstevel@tonic-gate r[1] = px->significand[1];
73*0Sstevel@tonic-gate r[2] = px->significand[2];
74*0Sstevel@tonic-gate r[3] = px->significand[3];
75*0Sstevel@tonic-gate y = py->significand;
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gate if(fpu_cmpli(r,y,4)>=0)
78*0Sstevel@tonic-gate pz->exponent = px->exponent - py->exponent;
79*0Sstevel@tonic-gate else
80*0Sstevel@tonic-gate pz->exponent = px->exponent - py->exponent - 1;
81*0Sstevel@tonic-gate
82*0Sstevel@tonic-gate q=0;
83*0Sstevel@tonic-gate while(q<0x10000) { /* generate quo[0] */
84*0Sstevel@tonic-gate q<<=1;
85*0Sstevel@tonic-gate if(fpu_cmpli(r,y,4)>=0) {
86*0Sstevel@tonic-gate q += 1; /* if r>y do r-=y and q+=1 */
87*0Sstevel@tonic-gate c = 0;
88*0Sstevel@tonic-gate c = fpu_sub3wc(&r[3],r[3],y[3],c);
89*0Sstevel@tonic-gate c = fpu_sub3wc(&r[2],r[2],y[2],c);
90*0Sstevel@tonic-gate c = fpu_sub3wc(&r[1],r[1],y[1],c);
91*0Sstevel@tonic-gate c = fpu_sub3wc(&r[0],r[0],y[0],c);
92*0Sstevel@tonic-gate }
93*0Sstevel@tonic-gate r[0] = (r[0]<<1)|((r[1]&0x80000000)>>31); /* r << 1 */
94*0Sstevel@tonic-gate r[1] = (r[1]<<1)|((r[2]&0x80000000)>>31);
95*0Sstevel@tonic-gate r[2] = (r[2]<<1)|((r[3]&0x80000000)>>31);
96*0Sstevel@tonic-gate r[3] = (r[3]<<1);
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate pz->significand[0]=q;
99*0Sstevel@tonic-gate q=0; /* generate quo[1] */
100*0Sstevel@tonic-gate n = 32;
101*0Sstevel@tonic-gate while(n--) {
102*0Sstevel@tonic-gate q<<=1;
103*0Sstevel@tonic-gate if(fpu_cmpli(r,y,4)>=0) {
104*0Sstevel@tonic-gate q += 1; /* if r>y do r-=y and q+=1 */
105*0Sstevel@tonic-gate c = 0;
106*0Sstevel@tonic-gate c = fpu_sub3wc(&r[3],r[3],y[3],c);
107*0Sstevel@tonic-gate c = fpu_sub3wc(&r[2],r[2],y[2],c);
108*0Sstevel@tonic-gate c = fpu_sub3wc(&r[1],r[1],y[1],c);
109*0Sstevel@tonic-gate c = fpu_sub3wc(&r[0],r[0],y[0],c);
110*0Sstevel@tonic-gate }
111*0Sstevel@tonic-gate r[0] = (r[0]<<1)|((r[1]&0x80000000)>>31); /* r << 1 */
112*0Sstevel@tonic-gate r[1] = (r[1]<<1)|((r[2]&0x80000000)>>31);
113*0Sstevel@tonic-gate r[2] = (r[2]<<1)|((r[3]&0x80000000)>>31);
114*0Sstevel@tonic-gate r[3] = (r[3]<<1);
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate pz->significand[1] = q;
117*0Sstevel@tonic-gate q=0; /* generate quo[2] */
118*0Sstevel@tonic-gate n = 32;
119*0Sstevel@tonic-gate while(n--) {
120*0Sstevel@tonic-gate q<<=1;
121*0Sstevel@tonic-gate if(fpu_cmpli(r,y,4)>=0) {
122*0Sstevel@tonic-gate q += 1; /* if r>y do r-=y and q+=1 */
123*0Sstevel@tonic-gate c = 0;
124*0Sstevel@tonic-gate c = fpu_sub3wc(&r[3],r[3],y[3],c);
125*0Sstevel@tonic-gate c = fpu_sub3wc(&r[2],r[2],y[2],c);
126*0Sstevel@tonic-gate c = fpu_sub3wc(&r[1],r[1],y[1],c);
127*0Sstevel@tonic-gate c = fpu_sub3wc(&r[0],r[0],y[0],c);
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate r[0] = (r[0]<<1)|((r[1]&0x80000000)>>31); /* r << 1 */
130*0Sstevel@tonic-gate r[1] = (r[1]<<1)|((r[2]&0x80000000)>>31);
131*0Sstevel@tonic-gate r[2] = (r[2]<<1)|((r[3]&0x80000000)>>31);
132*0Sstevel@tonic-gate r[3] = (r[3]<<1);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate pz->significand[2] = q;
135*0Sstevel@tonic-gate q=0; /* generate quo[3] */
136*0Sstevel@tonic-gate n = 32;
137*0Sstevel@tonic-gate while(n--) {
138*0Sstevel@tonic-gate q<<=1;
139*0Sstevel@tonic-gate if(fpu_cmpli(r,y,4)>=0) {
140*0Sstevel@tonic-gate q += 1; /* if r>y do r-=y and q+=1 */
141*0Sstevel@tonic-gate c = 0;
142*0Sstevel@tonic-gate c = fpu_sub3wc(&r[3],r[3],y[3],c);
143*0Sstevel@tonic-gate c = fpu_sub3wc(&r[2],r[2],y[2],c);
144*0Sstevel@tonic-gate c = fpu_sub3wc(&r[1],r[1],y[1],c);
145*0Sstevel@tonic-gate c = fpu_sub3wc(&r[0],r[0],y[0],c);
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate r[0] = (r[0]<<1)|((r[1]&0x80000000)>>31); /* r << 1 */
148*0Sstevel@tonic-gate r[1] = (r[1]<<1)|((r[2]&0x80000000)>>31);
149*0Sstevel@tonic-gate r[2] = (r[2]<<1)|((r[3]&0x80000000)>>31);
150*0Sstevel@tonic-gate r[3] = (r[3]<<1);
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate pz->significand[3] = q;
153*0Sstevel@tonic-gate if((r[0]|r[1]|r[2]|r[3])==0) pz->sticky = pz->rounded = 0;
154*0Sstevel@tonic-gate else {
155*0Sstevel@tonic-gate pz->sticky = 1; /* half way case won't occur */
156*0Sstevel@tonic-gate if(fpu_cmpli(r,y,4)>=0) pz->rounded = 1;
157*0Sstevel@tonic-gate }
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gate void
_fp_sqrt(px,pz)161*0Sstevel@tonic-gate _fp_sqrt(px, pz)
162*0Sstevel@tonic-gate unpacked *px, *pz;
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gate { /* *pz gets sqrt(*px) */
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate unsigned *x,r,c,q,t[4],s[4];
167*0Sstevel@tonic-gate *pz = *px;
168*0Sstevel@tonic-gate switch (px->fpclass) {
169*0Sstevel@tonic-gate case fp_quiet:
170*0Sstevel@tonic-gate case fp_signaling:
171*0Sstevel@tonic-gate case fp_zero:
172*0Sstevel@tonic-gate return;
173*0Sstevel@tonic-gate case fp_infinity:
174*0Sstevel@tonic-gate if (px->sign == 1) { /* sqrt(-inf) */
175*0Sstevel@tonic-gate fpu_error_nan(pz);
176*0Sstevel@tonic-gate pz->fpclass = fp_quiet;
177*0Sstevel@tonic-gate }
178*0Sstevel@tonic-gate return;
179*0Sstevel@tonic-gate case fp_normal:
180*0Sstevel@tonic-gate if (px->sign == 1) { /* sqrt(-norm) */
181*0Sstevel@tonic-gate fpu_error_nan(pz);
182*0Sstevel@tonic-gate pz->fpclass = fp_quiet;
183*0Sstevel@tonic-gate return;
184*0Sstevel@tonic-gate }
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate /* Now x is normal. */
188*0Sstevel@tonic-gate x = px->significand;
189*0Sstevel@tonic-gate if (px->exponent & 1) { /* sqrt(1.f * 2**odd) = sqrt (2.+2f) *
190*0Sstevel@tonic-gate * 2**(odd-1)/2 */
191*0Sstevel@tonic-gate pz->exponent = (px->exponent - 1) / 2;
192*0Sstevel@tonic-gate x[0] = (x[0]<<1)|((x[1]&0x80000000)>>31); /* x<<1 */
193*0Sstevel@tonic-gate x[1] = (x[1]<<1)|((x[2]&0x80000000)>>31);
194*0Sstevel@tonic-gate x[2] = (x[2]<<1)|((x[3]&0x80000000)>>31);
195*0Sstevel@tonic-gate x[3] = (x[3]<<1);
196*0Sstevel@tonic-gate } else { /* sqrt(1.f * 2**even) = sqrt (1.f) *
197*0Sstevel@tonic-gate * 2**(even)/2 */
198*0Sstevel@tonic-gate pz->exponent = px->exponent / 2;
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate s[0]=s[1]=s[2]=s[3]=t[0]=t[1]=t[2]=t[3]=0;
201*0Sstevel@tonic-gate q = 0;
202*0Sstevel@tonic-gate r = 0x00010000;
203*0Sstevel@tonic-gate while(r!=0) { /* compute sqrt[0] */
204*0Sstevel@tonic-gate t[0] = s[0]+r;
205*0Sstevel@tonic-gate if(t[0]<=x[0]) {
206*0Sstevel@tonic-gate s[0] = t[0]+r;
207*0Sstevel@tonic-gate x[0] -= t[0];
208*0Sstevel@tonic-gate q += r;
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate x[0] = (x[0]<<1)|((x[1]&0x80000000)>>31); /* x<<1 */
211*0Sstevel@tonic-gate x[1] = (x[1]<<1)|((x[2]&0x80000000)>>31);
212*0Sstevel@tonic-gate x[2] = (x[2]<<1)|((x[3]&0x80000000)>>31);
213*0Sstevel@tonic-gate x[3] = (x[3]<<1);
214*0Sstevel@tonic-gate r>>=1;
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate pz->significand[0] = q;
217*0Sstevel@tonic-gate q = 0;
218*0Sstevel@tonic-gate r = 0x80000000;
219*0Sstevel@tonic-gate while(r!=0) { /* compute sqrt[1] */
220*0Sstevel@tonic-gate t[1] = s[1]+r; /* no carry */
221*0Sstevel@tonic-gate t[0] = s[0];
222*0Sstevel@tonic-gate if(fpu_cmpli(t,x,2)<=0) {
223*0Sstevel@tonic-gate c = 0;
224*0Sstevel@tonic-gate c = fpu_add3wc(&s[1],t[1],r,c);
225*0Sstevel@tonic-gate c = fpu_add3wc(&s[0],t[0],0,c);
226*0Sstevel@tonic-gate c = 0;
227*0Sstevel@tonic-gate c = fpu_sub3wc(&x[1],x[1],t[1],c);
228*0Sstevel@tonic-gate c = fpu_sub3wc(&x[0],x[0],t[0],c);
229*0Sstevel@tonic-gate q += r;
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate x[0] = (x[0]<<1)|((x[1]&0x80000000)>>31); /* x<<1 */
232*0Sstevel@tonic-gate x[1] = (x[1]<<1)|((x[2]&0x80000000)>>31);
233*0Sstevel@tonic-gate x[2] = (x[2]<<1)|((x[3]&0x80000000)>>31);
234*0Sstevel@tonic-gate x[3] = (x[3]<<1);
235*0Sstevel@tonic-gate r>>=1;
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate pz->significand[1] = q;
238*0Sstevel@tonic-gate q = 0;
239*0Sstevel@tonic-gate r = 0x80000000;
240*0Sstevel@tonic-gate while(r!=0) { /* compute sqrt[2] */
241*0Sstevel@tonic-gate t[2] = s[2]+r; /* no carry */
242*0Sstevel@tonic-gate t[1] = s[1];
243*0Sstevel@tonic-gate t[0] = s[0];
244*0Sstevel@tonic-gate if(fpu_cmpli(t,x,3)<=0) {
245*0Sstevel@tonic-gate c = 0;
246*0Sstevel@tonic-gate c = fpu_add3wc(&s[2],t[2],r,c);
247*0Sstevel@tonic-gate c = fpu_add3wc(&s[1],t[1],0,c);
248*0Sstevel@tonic-gate c = fpu_add3wc(&s[0],t[0],0,c);
249*0Sstevel@tonic-gate c = 0;
250*0Sstevel@tonic-gate c = fpu_sub3wc(&x[2],x[2],t[2],c);
251*0Sstevel@tonic-gate c = fpu_sub3wc(&x[1],x[1],t[1],c);
252*0Sstevel@tonic-gate c = fpu_sub3wc(&x[0],x[0],t[0],c);
253*0Sstevel@tonic-gate q += r;
254*0Sstevel@tonic-gate }
255*0Sstevel@tonic-gate x[0] = (x[0]<<1)|((x[1]&0x80000000)>>31); /* x<<1 */
256*0Sstevel@tonic-gate x[1] = (x[1]<<1)|((x[2]&0x80000000)>>31);
257*0Sstevel@tonic-gate x[2] = (x[2]<<1)|((x[3]&0x80000000)>>31);
258*0Sstevel@tonic-gate x[3] = (x[3]<<1);
259*0Sstevel@tonic-gate r>>=1;
260*0Sstevel@tonic-gate }
261*0Sstevel@tonic-gate pz->significand[2] = q;
262*0Sstevel@tonic-gate q = 0;
263*0Sstevel@tonic-gate r = 0x80000000;
264*0Sstevel@tonic-gate while(r!=0) { /* compute sqrt[3] */
265*0Sstevel@tonic-gate t[3] = s[3]+r; /* no carry */
266*0Sstevel@tonic-gate t[2] = s[2];
267*0Sstevel@tonic-gate t[1] = s[1];
268*0Sstevel@tonic-gate t[0] = s[0];
269*0Sstevel@tonic-gate if(fpu_cmpli(t,x,4)<=0) {
270*0Sstevel@tonic-gate c = 0;
271*0Sstevel@tonic-gate c = fpu_add3wc(&s[3],t[3],r,c);
272*0Sstevel@tonic-gate c = fpu_add3wc(&s[2],t[2],0,c);
273*0Sstevel@tonic-gate c = fpu_add3wc(&s[1],t[1],0,c);
274*0Sstevel@tonic-gate c = fpu_add3wc(&s[0],t[0],0,c);
275*0Sstevel@tonic-gate c = 0;
276*0Sstevel@tonic-gate c = fpu_sub3wc(&x[3],x[3],t[3],c);
277*0Sstevel@tonic-gate c = fpu_sub3wc(&x[2],x[2],t[2],c);
278*0Sstevel@tonic-gate c = fpu_sub3wc(&x[1],x[1],t[1],c);
279*0Sstevel@tonic-gate c = fpu_sub3wc(&x[0],x[0],t[0],c);
280*0Sstevel@tonic-gate q += r;
281*0Sstevel@tonic-gate }
282*0Sstevel@tonic-gate x[0] = (x[0]<<1)|((x[1]&0x80000000)>>31); /* x<<1 */
283*0Sstevel@tonic-gate x[1] = (x[1]<<1)|((x[2]&0x80000000)>>31);
284*0Sstevel@tonic-gate x[2] = (x[2]<<1)|((x[3]&0x80000000)>>31);
285*0Sstevel@tonic-gate x[3] = (x[3]<<1);
286*0Sstevel@tonic-gate r>>=1;
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate pz->significand[3] = q;
289*0Sstevel@tonic-gate if((x[0]|x[1]|x[2]|x[3])==0) {
290*0Sstevel@tonic-gate pz->sticky = pz->rounded = 0;
291*0Sstevel@tonic-gate } else {
292*0Sstevel@tonic-gate pz->sticky = 1;
293*0Sstevel@tonic-gate if(fpu_cmpli(s,x,4)<0) pz->rounded=1; else pz->rounded = 0;
294*0Sstevel@tonic-gate }
295*0Sstevel@tonic-gate }
296