xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/_Qfutility.c (revision 0:68f95e015346)
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 /* Utility functions for Sparc FPU simulator. */
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate #include "_Qquad.h"
31*0Sstevel@tonic-gate #include "_Qglobals.h"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate void
fpu_normalize(pu)35*0Sstevel@tonic-gate fpu_normalize(pu)
36*0Sstevel@tonic-gate 	unpacked       *pu;
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /* Normalize a number.  Does not affect zeros, infs, or NaNs. */
39*0Sstevel@tonic-gate /* The number will be normalized to 113 bit extended:
40*0Sstevel@tonic-gate  * 		0x0001####,0x########,0x########,0x########.
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate {
44*0Sstevel@tonic-gate 	unsigned u,u0,u1,u2,u3,m,n,k;
45*0Sstevel@tonic-gate 	u0 = pu->significand[0];
46*0Sstevel@tonic-gate 	u1 = pu->significand[1];
47*0Sstevel@tonic-gate 	u2 = pu->significand[2];
48*0Sstevel@tonic-gate 	u3 = pu->significand[3];
49*0Sstevel@tonic-gate 	if ((*pu).fpclass == fp_normal) {
50*0Sstevel@tonic-gate 		if ((u0|u1|u2|u3)==0) {
51*0Sstevel@tonic-gate 			(*pu).fpclass = fp_zero;
52*0Sstevel@tonic-gate 			return;
53*0Sstevel@tonic-gate 		}
54*0Sstevel@tonic-gate 		while (u0 == 0) {
55*0Sstevel@tonic-gate 			u0 = u1; u1=u2; u2=u3; u3=0;
56*0Sstevel@tonic-gate 			(*pu).exponent = (*pu).exponent - 32;
57*0Sstevel@tonic-gate 		}
58*0Sstevel@tonic-gate 		if (u0>=0x20000) { 	/* u3 should be zero */
59*0Sstevel@tonic-gate 			n=1; u = u0>>1;
60*0Sstevel@tonic-gate 			while(u>=0x20000) {u >>= 1; n += 1;}
61*0Sstevel@tonic-gate 			m = (1<<n)-1;
62*0Sstevel@tonic-gate 			k = 32-n;
63*0Sstevel@tonic-gate 			(*pu).exponent += n;
64*0Sstevel@tonic-gate 			u3 = ((u2&m)<<k)|(u3>>n);
65*0Sstevel@tonic-gate 			u2 = ((u1&m)<<k)|(u2>>n);
66*0Sstevel@tonic-gate 			u1 = ((u0&m)<<k)|(u1>>n);
67*0Sstevel@tonic-gate 			u0 = u;
68*0Sstevel@tonic-gate 		} else if(u0<0x10000) {
69*0Sstevel@tonic-gate 			n=1; u = u0<<1;
70*0Sstevel@tonic-gate 			while(u<0x10000) {u <<= 1; n += 1;}
71*0Sstevel@tonic-gate 			k = 32-n;
72*0Sstevel@tonic-gate 			m = -(1<<k);
73*0Sstevel@tonic-gate 			(*pu).exponent -= n;
74*0Sstevel@tonic-gate 			u0 = (u0<<n)|((u1&m)>>k);
75*0Sstevel@tonic-gate 			u1 = (u1<<n)|((u2&m)>>k);
76*0Sstevel@tonic-gate 			u2 = (u2<<n)|((u3&m)>>k);
77*0Sstevel@tonic-gate 			u3 = (u3<<n);
78*0Sstevel@tonic-gate 		}
79*0Sstevel@tonic-gate 		pu->significand[0] = u0;
80*0Sstevel@tonic-gate 		pu->significand[1] = u1;
81*0Sstevel@tonic-gate 		pu->significand[2] = u2;
82*0Sstevel@tonic-gate 		pu->significand[3] = u3;
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate void
fpu_rightshift(pu,n)87*0Sstevel@tonic-gate fpu_rightshift(pu, n)
88*0Sstevel@tonic-gate 	unpacked       *pu;
89*0Sstevel@tonic-gate 	int             n;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /* Right shift significand sticky by n bits.  */
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate {
94*0Sstevel@tonic-gate 	unsigned m,k,j,u0,u1,u2,u3;
95*0Sstevel@tonic-gate 	if (n > 113) {		/* drastic */
96*0Sstevel@tonic-gate 		if (((*pu).significand[0] | (*pu).significand[1]
97*0Sstevel@tonic-gate 			| (*pu).significand[2] | (*pu).significand[3]) == 0){
98*0Sstevel@tonic-gate 						/* really zero */
99*0Sstevel@tonic-gate 			pu->fpclass = fp_zero;
100*0Sstevel@tonic-gate 			return;
101*0Sstevel@tonic-gate 		} else {
102*0Sstevel@tonic-gate 			pu->rounded = 0;
103*0Sstevel@tonic-gate 			pu->sticky  = 1;
104*0Sstevel@tonic-gate 			pu->significand[3] = 0;
105*0Sstevel@tonic-gate 			pu->significand[2] = 0;
106*0Sstevel@tonic-gate 			pu->significand[1] = 0;
107*0Sstevel@tonic-gate 			pu->significand[0] = 0;
108*0Sstevel@tonic-gate 			return;
109*0Sstevel@tonic-gate 		}
110*0Sstevel@tonic-gate 	}
111*0Sstevel@tonic-gate 	while (n >= 32) {	/* big shift */
112*0Sstevel@tonic-gate 		pu->sticky  |= pu->rounded | (pu->significand[3]&0x7fffffff);
113*0Sstevel@tonic-gate 		pu->rounded  = (*pu).significand[3]>>31;
114*0Sstevel@tonic-gate 		(*pu).significand[3] = (*pu).significand[2];
115*0Sstevel@tonic-gate 		(*pu).significand[2] = (*pu).significand[1];
116*0Sstevel@tonic-gate 		(*pu).significand[1] = (*pu).significand[0];
117*0Sstevel@tonic-gate 		(*pu).significand[0] = 0;
118*0Sstevel@tonic-gate 		n -= 32;
119*0Sstevel@tonic-gate 	}
120*0Sstevel@tonic-gate 	if (n > 0) {		/* small shift */
121*0Sstevel@tonic-gate 		u0 = pu->significand[0];
122*0Sstevel@tonic-gate 		u1 = pu->significand[1];
123*0Sstevel@tonic-gate 		u2 = pu->significand[2];
124*0Sstevel@tonic-gate 		u3 = pu->significand[3];
125*0Sstevel@tonic-gate 		m = (1<<n)-1;
126*0Sstevel@tonic-gate 		k = 32 - n;
127*0Sstevel@tonic-gate 		j = (1<<(n-1))-1;
128*0Sstevel@tonic-gate 		pu->sticky |= pu->rounded | (u3&j);
129*0Sstevel@tonic-gate 		pu->rounded = (u3&m)>>(n-1);
130*0Sstevel@tonic-gate 		pu->significand[3] = ((u2&m)<<k)|(u3>>n);
131*0Sstevel@tonic-gate 		pu->significand[2] = ((u1&m)<<k)|(u2>>n);
132*0Sstevel@tonic-gate 		pu->significand[1] = ((u0&m)<<k)|(u1>>n);
133*0Sstevel@tonic-gate 		pu->significand[0] = u0>>n;
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate void
fpu_set_exception(ex)138*0Sstevel@tonic-gate fpu_set_exception(ex)
139*0Sstevel@tonic-gate 	enum fp_exception_type ex;
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate /* Set the exception bit in the current exception register. */
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	_fp_current_exceptions |= 1 << (int) ex;
145*0Sstevel@tonic-gate }
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate void
fpu_error_nan(pu)148*0Sstevel@tonic-gate fpu_error_nan(pu)
149*0Sstevel@tonic-gate 	unpacked       *pu;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate {				/* Set invalid exception and error nan in *pu */
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	fpu_set_exception(fp_invalid);
154*0Sstevel@tonic-gate 	pu->significand[0] = 0x7fffffff|((pu->sign)<<31);
155*0Sstevel@tonic-gate 	pu->significand[1] = 0xffffffff;
156*0Sstevel@tonic-gate 	pu->significand[2] = 0xffffffff;
157*0Sstevel@tonic-gate 	pu->significand[3] = 0xffffffff;
158*0Sstevel@tonic-gate }
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate /* the following fpu_add3wc should be inlined as
161*0Sstevel@tonic-gate  *	.inline	_fpu_add3wc,3
162*0Sstevel@tonic-gate  *	ld	[%o1],%o4		! sum = x
163*0Sstevel@tonic-gate  *	addcc	-1,%o3,%g0		! restore last carry in cc reg
164*0Sstevel@tonic-gate  *	addxcc	%o4,%o2,%o4		! sum = sum + y + last carry
165*0Sstevel@tonic-gate  *	st	%o4,[%o0]		! *z  = sum
166*0Sstevel@tonic-gate  *	addx	%g0,%g0,%o0		! return new carry
167*0Sstevel@tonic-gate  *	.end
168*0Sstevel@tonic-gate  */
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate unsigned
fpu_add3wc(z,x,y,carry)171*0Sstevel@tonic-gate fpu_add3wc(z,x,y,carry)
172*0Sstevel@tonic-gate 	unsigned *z,x,y,carry;
173*0Sstevel@tonic-gate {				/*  *z = x + y + carry, set carry; */
174*0Sstevel@tonic-gate 	if(carry==0) {
175*0Sstevel@tonic-gate 		*z = x+y;
176*0Sstevel@tonic-gate 		return (*z<y);
177*0Sstevel@tonic-gate 	} else {
178*0Sstevel@tonic-gate 		*z = x+y+1;
179*0Sstevel@tonic-gate 		return (*z<=y);
180*0Sstevel@tonic-gate 	}
181*0Sstevel@tonic-gate }
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate /* the following fpu_sub3wc should be inlined as
184*0Sstevel@tonic-gate  *	.inline	_fpu_sub3wc,3
185*0Sstevel@tonic-gate  *	ld	[%o1],%o4		! sum = *x
186*0Sstevel@tonic-gate  *	addcc	-1,%o3,%g0		! restore last carry in cc reg
187*0Sstevel@tonic-gate  *	subxcc	%o4,%o2,%o4		! sum = sum - y - last carry
188*0Sstevel@tonic-gate  *	st	%o4,[%o0]		! *x  = sum
189*0Sstevel@tonic-gate  *	addx	%g0,%g0,%o0		! return new carry
190*0Sstevel@tonic-gate  *	.end
191*0Sstevel@tonic-gate  */
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate unsigned
fpu_sub3wc(z,x,y,carry)194*0Sstevel@tonic-gate fpu_sub3wc(z,x,y,carry)
195*0Sstevel@tonic-gate 	unsigned *z,x,y,carry;
196*0Sstevel@tonic-gate {				/*  *z = x - y - carry, set carry; */
197*0Sstevel@tonic-gate 	if(carry==0) {
198*0Sstevel@tonic-gate 		*z = x-y;
199*0Sstevel@tonic-gate 		return (*z>x);
200*0Sstevel@tonic-gate 	} else {
201*0Sstevel@tonic-gate 		*z = x-y-1;
202*0Sstevel@tonic-gate 		return (*z>=x);
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate /* the following fpu_neg2wc should be inlined as
207*0Sstevel@tonic-gate  *	.inline	_fpu_neg2wc,2
208*0Sstevel@tonic-gate  *	ld	[%o1],%o3		! tmp = *x
209*0Sstevel@tonic-gate  *	addcc	-1,%o2,%g0		! restore last carry in cc reg
210*0Sstevel@tonic-gate  *	subxcc	%g0,%o3,%o3		! sum = 0 - tmp - last carry
211*0Sstevel@tonic-gate  *	st	%o3,[%o0]		! *x  = sum
212*0Sstevel@tonic-gate  *	addx	%g0,%g0,%o0		! return new carry
213*0Sstevel@tonic-gate  *	.end
214*0Sstevel@tonic-gate  */
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate unsigned
fpu_neg2wc(z,x,carry)217*0Sstevel@tonic-gate fpu_neg2wc(z,x,carry)
218*0Sstevel@tonic-gate 	unsigned *z,x,carry;
219*0Sstevel@tonic-gate {				/*  *x = 0 - *x - carry, set carry; */
220*0Sstevel@tonic-gate 	if(carry==0) {
221*0Sstevel@tonic-gate 		*z = -x;
222*0Sstevel@tonic-gate 		return ((*z)!=0);
223*0Sstevel@tonic-gate 	} else {
224*0Sstevel@tonic-gate 		*z = -x-1;
225*0Sstevel@tonic-gate 		return 1;
226*0Sstevel@tonic-gate 	}
227*0Sstevel@tonic-gate }
228*0Sstevel@tonic-gate 
229*0Sstevel@tonic-gate int
fpu_cmpli(x,y,n)230*0Sstevel@tonic-gate fpu_cmpli(x,y,n)
231*0Sstevel@tonic-gate 	unsigned x[],y[]; int n;
232*0Sstevel@tonic-gate {				/* compare two unsigned array */
233*0Sstevel@tonic-gate 	int i;
234*0Sstevel@tonic-gate 	i=0;
235*0Sstevel@tonic-gate 	while(i<n)  {
236*0Sstevel@tonic-gate 		if(x[i]>y[i]) return 1;
237*0Sstevel@tonic-gate 		else if(x[i]<y[i]) return -1;
238*0Sstevel@tonic-gate 		i++;
239*0Sstevel@tonic-gate 	}
240*0Sstevel@tonic-gate 	return 0;
241*0Sstevel@tonic-gate }
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate #ifdef DEBUG
244*0Sstevel@tonic-gate void
display_unpacked(pu)245*0Sstevel@tonic-gate display_unpacked(pu)
246*0Sstevel@tonic-gate 	unpacked       *pu;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate /* Print out unpacked record.	 */
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate {
251*0Sstevel@tonic-gate 	(void) printf(" unpacked ");
252*0Sstevel@tonic-gate 	if (pu->sign)
253*0Sstevel@tonic-gate 		(void) printf("-");
254*0Sstevel@tonic-gate 	else
255*0Sstevel@tonic-gate 		(void) printf("+");
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 	switch (pu->fpclass) {
258*0Sstevel@tonic-gate 	case fp_zero:
259*0Sstevel@tonic-gate 		(void) printf("0     ");
260*0Sstevel@tonic-gate 		break;
261*0Sstevel@tonic-gate 	case fp_normal:
262*0Sstevel@tonic-gate 		(void) printf("normal");
263*0Sstevel@tonic-gate 		break;
264*0Sstevel@tonic-gate 	case fp_infinity:
265*0Sstevel@tonic-gate 		(void) printf("Inf   ");
266*0Sstevel@tonic-gate 		break;
267*0Sstevel@tonic-gate 	case fp_quiet:
268*0Sstevel@tonic-gate 	case fp_signaling:
269*0Sstevel@tonic-gate 		(void) printf("nan   ");
270*0Sstevel@tonic-gate 		break;
271*0Sstevel@tonic-gate 	}
272*0Sstevel@tonic-gate 	(void) printf(" %X %X %X %X (%X,%X) exponent %X \n",
273*0Sstevel@tonic-gate 		pu->significand[0], pu->significand[1],pu->significand[2],
274*0Sstevel@tonic-gate 		pu->significand[3], (pu->rounded!=0),
275*0Sstevel@tonic-gate 		(pu->sticky!=0),pu->exponent);
276*0Sstevel@tonic-gate }
277*0Sstevel@tonic-gate #endif
278*0Sstevel@tonic-gate 
279