xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/_Qfunpack.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 /* Unpack procedures 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 PRIVATE void
unpackinteger(pu,x)34*0Sstevel@tonic-gate unpackinteger(pu, x)
35*0Sstevel@tonic-gate 	unpacked       *pu;	/* unpacked result */
36*0Sstevel@tonic-gate 	int             x;	/* packed integer */
37*0Sstevel@tonic-gate {
38*0Sstevel@tonic-gate 	unsigned ux;
39*0Sstevel@tonic-gate 	pu->sticky = pu->rounded = 0;
40*0Sstevel@tonic-gate 	if (x == 0) {
41*0Sstevel@tonic-gate 		pu->sign = 0;
42*0Sstevel@tonic-gate 		pu->fpclass = fp_zero;
43*0Sstevel@tonic-gate 	} else {
44*0Sstevel@tonic-gate 		(*pu).sign = x < 0;
45*0Sstevel@tonic-gate 		(*pu).fpclass = fp_normal;
46*0Sstevel@tonic-gate 		(*pu).exponent = INTEGER_BIAS;
47*0Sstevel@tonic-gate 		if(x<0) ux = -x; else ux = x;
48*0Sstevel@tonic-gate 		(*pu).significand[0] = ux>>15;
49*0Sstevel@tonic-gate 		(*pu).significand[1] = (ux&0x7fff)<<17;
50*0Sstevel@tonic-gate 		(*pu).significand[2] = 0;
51*0Sstevel@tonic-gate 		(*pu).significand[3] = 0;
52*0Sstevel@tonic-gate 		fpu_normalize(pu);
53*0Sstevel@tonic-gate 	}
54*0Sstevel@tonic-gate }
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate void
unpacksingle(pu,x)57*0Sstevel@tonic-gate unpacksingle(pu, x)
58*0Sstevel@tonic-gate 	unpacked       *pu;	/* unpacked result */
59*0Sstevel@tonic-gate 	single_type     x;	/* packed single */
60*0Sstevel@tonic-gate {
61*0Sstevel@tonic-gate 	unsigned u;
62*0Sstevel@tonic-gate 	pu->sticky = pu->rounded = 0;
63*0Sstevel@tonic-gate 	u = x.significand;
64*0Sstevel@tonic-gate 	(*pu).sign = x.sign;
65*0Sstevel@tonic-gate 	pu->significand[1] = 0;
66*0Sstevel@tonic-gate 	pu->significand[2] = 0;
67*0Sstevel@tonic-gate 	pu->significand[3] = 0;
68*0Sstevel@tonic-gate 	if (x.exponent == 0) {	/* zero or sub */
69*0Sstevel@tonic-gate 		if (x.significand == 0) {	/* zero */
70*0Sstevel@tonic-gate 			pu->fpclass = fp_zero;
71*0Sstevel@tonic-gate 			return;
72*0Sstevel@tonic-gate 		} else {	/* subnormal */
73*0Sstevel@tonic-gate 			pu->fpclass = fp_normal;
74*0Sstevel@tonic-gate 			pu->exponent = -SINGLE_BIAS-6;
75*0Sstevel@tonic-gate 			pu->significand[0]=u;
76*0Sstevel@tonic-gate 			fpu_normalize(pu);
77*0Sstevel@tonic-gate 			return;
78*0Sstevel@tonic-gate 		}
79*0Sstevel@tonic-gate 	} else if (x.exponent == 0xff) {	/* inf or nan */
80*0Sstevel@tonic-gate 		if (x.significand == 0) {	/* inf */
81*0Sstevel@tonic-gate 			pu->fpclass = fp_infinity;
82*0Sstevel@tonic-gate 			return;
83*0Sstevel@tonic-gate 		} else {	/* nan */
84*0Sstevel@tonic-gate 			if ((u & 0x400000) != 0) {	/* quiet */
85*0Sstevel@tonic-gate 				pu->fpclass = fp_quiet;
86*0Sstevel@tonic-gate 			} else {/* signaling */
87*0Sstevel@tonic-gate 				pu->fpclass = fp_signaling;
88*0Sstevel@tonic-gate 				fpu_set_exception(fp_invalid);
89*0Sstevel@tonic-gate 			}
90*0Sstevel@tonic-gate 			pu->significand[0] = 0x18000 | (u >> 7);
91*0Sstevel@tonic-gate 			(*pu).significand[1]=((u&0x7f)<<25);
92*0Sstevel@tonic-gate 			return;
93*0Sstevel@tonic-gate 		}
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate 	(*pu).exponent = x.exponent - SINGLE_BIAS;
96*0Sstevel@tonic-gate 	(*pu).fpclass = fp_normal;
97*0Sstevel@tonic-gate 	(*pu).significand[0]=0x10000|(u>>7);
98*0Sstevel@tonic-gate 	(*pu).significand[1]=((u&0x7f)<<25);
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate void
unpackdouble(pu,x,y)102*0Sstevel@tonic-gate unpackdouble(pu, x, y)
103*0Sstevel@tonic-gate 	unpacked       *pu;	/* unpacked result */
104*0Sstevel@tonic-gate 	double_type     x;	/* packed double */
105*0Sstevel@tonic-gate 	unsigned        y;
106*0Sstevel@tonic-gate {
107*0Sstevel@tonic-gate 	unsigned u;
108*0Sstevel@tonic-gate 	pu->sticky = pu->rounded = 0;
109*0Sstevel@tonic-gate 	u = x.significand;
110*0Sstevel@tonic-gate 	(*pu).sign = x.sign;
111*0Sstevel@tonic-gate 	pu->significand[1] = y;
112*0Sstevel@tonic-gate 	pu->significand[2] = 0;
113*0Sstevel@tonic-gate 	pu->significand[3] = 0;
114*0Sstevel@tonic-gate 	if (x.exponent == 0) {	/* zero or sub */
115*0Sstevel@tonic-gate 		if ((x.significand == 0) && (y == 0)) {	/* zero */
116*0Sstevel@tonic-gate 			pu->fpclass = fp_zero;
117*0Sstevel@tonic-gate 			return;
118*0Sstevel@tonic-gate 		} else {	/* subnormal */
119*0Sstevel@tonic-gate 			pu->fpclass = fp_normal;
120*0Sstevel@tonic-gate 			pu->exponent = -DOUBLE_BIAS-3;
121*0Sstevel@tonic-gate 			pu->significand[0] = u;
122*0Sstevel@tonic-gate 			fpu_normalize(pu);
123*0Sstevel@tonic-gate 			return;
124*0Sstevel@tonic-gate 		}
125*0Sstevel@tonic-gate 	} else if (x.exponent == 0x7ff) {	/* inf or nan */
126*0Sstevel@tonic-gate 		if ((u|y) == 0) {	/* inf */
127*0Sstevel@tonic-gate 			pu->fpclass = fp_infinity;
128*0Sstevel@tonic-gate 			return;
129*0Sstevel@tonic-gate 		} else {	/* nan */
130*0Sstevel@tonic-gate 			if ((u & 0x80000) != 0) {	/* quiet */
131*0Sstevel@tonic-gate 				pu->fpclass = fp_quiet;
132*0Sstevel@tonic-gate 			} else {/* signaling */
133*0Sstevel@tonic-gate 				pu->fpclass = fp_signaling;
134*0Sstevel@tonic-gate 				fpu_set_exception(fp_invalid);
135*0Sstevel@tonic-gate 			}
136*0Sstevel@tonic-gate 			pu->significand[0] = 0x18000 | (u >> 4);
137*0Sstevel@tonic-gate 			(*pu).significand[1]=((u&0xf)<<28)|(y>>4);
138*0Sstevel@tonic-gate 			(*pu).significand[2]=((y&0xf)<<28);
139*0Sstevel@tonic-gate 			return;
140*0Sstevel@tonic-gate 		}
141*0Sstevel@tonic-gate 	}
142*0Sstevel@tonic-gate 	(*pu).exponent = x.exponent - DOUBLE_BIAS;
143*0Sstevel@tonic-gate 	(*pu).fpclass = fp_normal;
144*0Sstevel@tonic-gate 	(*pu).significand[0]=0x10000|(u>>4);
145*0Sstevel@tonic-gate 	(*pu).significand[1]=((u&0xf)<<28)|(y>>4);
146*0Sstevel@tonic-gate 	(*pu).significand[2]=((y&0xf)<<28);
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate PRIVATE void
unpackextended(pu,x,y,z,w)150*0Sstevel@tonic-gate unpackextended(pu, x, y, z, w)
151*0Sstevel@tonic-gate 	unpacked       *pu;	/* unpacked result */
152*0Sstevel@tonic-gate 	extended_type   x;	/* packed extended */
153*0Sstevel@tonic-gate 	unsigned        y, z, w;
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	unsigned u;
156*0Sstevel@tonic-gate 	pu->sticky = pu->rounded = 0;
157*0Sstevel@tonic-gate 	u = x.significand;
158*0Sstevel@tonic-gate 	(*pu).sign = x.sign;
159*0Sstevel@tonic-gate 	(*pu).fpclass = fp_normal;
160*0Sstevel@tonic-gate 	(*pu).exponent = x.exponent - EXTENDED_BIAS;
161*0Sstevel@tonic-gate 	(*pu).significand[0] = (x.exponent==0)? u:0x10000|u;
162*0Sstevel@tonic-gate 	(*pu).significand[1] = y;
163*0Sstevel@tonic-gate 	(*pu).significand[2] = z;
164*0Sstevel@tonic-gate 	(*pu).significand[3] = w;
165*0Sstevel@tonic-gate 	if (x.exponent < 0x7fff) {	/* zero, normal, or subnormal */
166*0Sstevel@tonic-gate 		if ((z|y|w|pu->significand[0]) == 0) {	/* zero */
167*0Sstevel@tonic-gate 			pu->fpclass = fp_zero;
168*0Sstevel@tonic-gate 			return;
169*0Sstevel@tonic-gate 		} else {	/* normal or subnormal */
170*0Sstevel@tonic-gate 			if(x.exponent==0) {
171*0Sstevel@tonic-gate 				fpu_normalize(pu);
172*0Sstevel@tonic-gate 				pu->exponent += 1;
173*0Sstevel@tonic-gate 			}
174*0Sstevel@tonic-gate 			return;
175*0Sstevel@tonic-gate 		}
176*0Sstevel@tonic-gate 	} else {	/* inf or nan */
177*0Sstevel@tonic-gate 		if ((u|z|y|w) == 0) {	/* inf */
178*0Sstevel@tonic-gate 			pu->fpclass = fp_infinity;
179*0Sstevel@tonic-gate 			return;
180*0Sstevel@tonic-gate 		} else {	/* nan */
181*0Sstevel@tonic-gate 			if ((u & 0x00008000) != 0) {	/* quiet */
182*0Sstevel@tonic-gate 				pu->fpclass = fp_quiet;
183*0Sstevel@tonic-gate 			} else {/* signaling */
184*0Sstevel@tonic-gate 				pu->fpclass = fp_signaling;
185*0Sstevel@tonic-gate 				fpu_set_exception(fp_invalid);
186*0Sstevel@tonic-gate 			}
187*0Sstevel@tonic-gate 			pu->significand[0] |= 0x8000; /* make quiet */
188*0Sstevel@tonic-gate 			return;
189*0Sstevel@tonic-gate 		}
190*0Sstevel@tonic-gate }
191*0Sstevel@tonic-gate }
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate void
_fp_unpack(pu,n,dtype)194*0Sstevel@tonic-gate _fp_unpack(pu, n, dtype)
195*0Sstevel@tonic-gate 	unpacked       *pu;	/* unpacked result */
196*0Sstevel@tonic-gate 	int        	*n;	/* input array */
197*0Sstevel@tonic-gate 	enum fp_op_type dtype;	/* type of datum */
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate {
200*0Sstevel@tonic-gate 	switch ((int) dtype) {
201*0Sstevel@tonic-gate 	case fp_op_integer:
202*0Sstevel@tonic-gate 		unpackinteger(pu, n[0]);
203*0Sstevel@tonic-gate 		break;
204*0Sstevel@tonic-gate 	case fp_op_single:
205*0Sstevel@tonic-gate 		{
206*0Sstevel@tonic-gate 			single_type x;
207*0Sstevel@tonic-gate 			*(int*)&x = n[0];
208*0Sstevel@tonic-gate 			unpacksingle(pu, x);
209*0Sstevel@tonic-gate 			break;
210*0Sstevel@tonic-gate 		}
211*0Sstevel@tonic-gate 	case fp_op_double:
212*0Sstevel@tonic-gate 		{
213*0Sstevel@tonic-gate 			double_type x;
214*0Sstevel@tonic-gate 			double t=1.0; int i0,i1;
215*0Sstevel@tonic-gate 			if((*(int*)&t)!=0) {i0=0;i1=1;} else {i0=1;i1=0;}
216*0Sstevel@tonic-gate 			*(int*)&x = n[i0];
217*0Sstevel@tonic-gate 			unpackdouble(pu, x, n[i1]);
218*0Sstevel@tonic-gate 			break;
219*0Sstevel@tonic-gate 		}
220*0Sstevel@tonic-gate 	case fp_op_extended:
221*0Sstevel@tonic-gate 		{
222*0Sstevel@tonic-gate 			extended_type x;
223*0Sstevel@tonic-gate 			double t=1.0; int i0,i1,i2,i3;
224*0Sstevel@tonic-gate 			if((*(int*)&t)!=0) {i0=0;i1=1;i2=2;i3=3;}
225*0Sstevel@tonic-gate 			else {i0=3;i1=2;i2=1;i3=0;}
226*0Sstevel@tonic-gate 			*(int*)&x = n[i0];
227*0Sstevel@tonic-gate 			unpackextended(pu, x, n[i1], n[i2], n[i3]);
228*0Sstevel@tonic-gate 			break;
229*0Sstevel@tonic-gate 		}
230*0Sstevel@tonic-gate 	}
231*0Sstevel@tonic-gate }
232