xref: /onnv-gate/usr/src/uts/sparc/fpu/unpack.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 /*
23*0Sstevel@tonic-gate  * Copyright 1987, 2000, 2003 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SunOS-4.1 */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /* Unpack procedures for Sparc FPU simulator. */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <sys/fpu/fpu_simulator.h>
32*0Sstevel@tonic-gate #include <sys/fpu/globals.h>
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate static void
unpackint32(unpacked * pu,int32_t x)35*0Sstevel@tonic-gate unpackint32(
36*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked result */
37*0Sstevel@tonic-gate 	int32_t		x)	/* packed int32_t */
38*0Sstevel@tonic-gate {
39*0Sstevel@tonic-gate 	uint_t ux;
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate 	pu->sticky = pu->rounded = 0;
42*0Sstevel@tonic-gate 	if (x == 0) {
43*0Sstevel@tonic-gate 		pu->sign = 0;
44*0Sstevel@tonic-gate 		pu->fpclass = fp_zero;
45*0Sstevel@tonic-gate 	} else {
46*0Sstevel@tonic-gate 		(*pu).sign = x < 0;
47*0Sstevel@tonic-gate 		(*pu).fpclass = fp_normal;
48*0Sstevel@tonic-gate 		(*pu).exponent = INTEGER_BIAS;
49*0Sstevel@tonic-gate 		if (x < 0) ux = -x; else ux = x;
50*0Sstevel@tonic-gate 		(*pu).significand[0] = ux>>15;
51*0Sstevel@tonic-gate 		(*pu).significand[1] = (ux&0x7fff)<<17;
52*0Sstevel@tonic-gate 		(*pu).significand[2] = 0;
53*0Sstevel@tonic-gate 		(*pu).significand[3] = 0;
54*0Sstevel@tonic-gate 		fpu_normalize(pu);
55*0Sstevel@tonic-gate 	}
56*0Sstevel@tonic-gate }
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * Workaround for bug 4287443--- we have to convince the compiler
61*0Sstevel@tonic-gate  * that unpackint64 isn't a leaf routine.
62*0Sstevel@tonic-gate  */
63*0Sstevel@tonic-gate static void
subroutine(void)64*0Sstevel@tonic-gate subroutine(void)
65*0Sstevel@tonic-gate {
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate static void
unpackint64(unpacked * pu,int64_t x)69*0Sstevel@tonic-gate unpackint64(
70*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked result */
71*0Sstevel@tonic-gate 	int64_t		x)	/* packed int64_t */
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate 	union {
74*0Sstevel@tonic-gate 		uint64_t ll;
75*0Sstevel@tonic-gate 		uint32_t i[2];
76*0Sstevel@tonic-gate 	} ux;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	subroutine();
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	pu->sticky = pu->rounded = 0;
81*0Sstevel@tonic-gate 	if (x == 0) {
82*0Sstevel@tonic-gate 		pu->sign = 0;
83*0Sstevel@tonic-gate 		pu->fpclass = fp_zero;
84*0Sstevel@tonic-gate 	} else {
85*0Sstevel@tonic-gate 		(*pu).sign = x < 0;
86*0Sstevel@tonic-gate 		(*pu).fpclass = fp_normal;
87*0Sstevel@tonic-gate 		(*pu).exponent = LONGLONG_BIAS;
88*0Sstevel@tonic-gate 		if (x < 0) ux.ll = -x; else ux.ll = x;
89*0Sstevel@tonic-gate 		(*pu).significand[0] = ux.i[0]>>15;
90*0Sstevel@tonic-gate 		(*pu).significand[1] = (((ux.i[0]&0x7fff)<<17) | (ux.i[1]>>15));
91*0Sstevel@tonic-gate 		(*pu).significand[2] = (ux.i[1]&0x7fff)<<17;
92*0Sstevel@tonic-gate 		(*pu).significand[3] = 0;
93*0Sstevel@tonic-gate 		fpu_normalize(pu);
94*0Sstevel@tonic-gate 	}
95*0Sstevel@tonic-gate }
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate void
unpacksingle(fp_simd_type * pfpsd,unpacked * pu,single_type x)98*0Sstevel@tonic-gate unpacksingle(
99*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* simulator data */
100*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked result */
101*0Sstevel@tonic-gate 	single_type	x)	/* packed single */
102*0Sstevel@tonic-gate {
103*0Sstevel@tonic-gate 	uint_t U;
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	pu->sticky = pu->rounded = 0;
106*0Sstevel@tonic-gate 	U = x.significand;
107*0Sstevel@tonic-gate 	(*pu).sign = x.sign;
108*0Sstevel@tonic-gate 	pu->significand[1] = 0;
109*0Sstevel@tonic-gate 	pu->significand[2] = 0;
110*0Sstevel@tonic-gate 	pu->significand[3] = 0;
111*0Sstevel@tonic-gate 	if (x.exponent == 0) {				/* zero or sub */
112*0Sstevel@tonic-gate 		if (x.significand == 0) {		/* zero */
113*0Sstevel@tonic-gate 			pu->fpclass = fp_zero;
114*0Sstevel@tonic-gate 			return;
115*0Sstevel@tonic-gate 		} else {				/* subnormal */
116*0Sstevel@tonic-gate 			pu->fpclass = fp_normal;
117*0Sstevel@tonic-gate 			pu->exponent = -SINGLE_BIAS-6;
118*0Sstevel@tonic-gate 			pu->significand[0] = U;
119*0Sstevel@tonic-gate 			fpu_normalize(pu);
120*0Sstevel@tonic-gate 			return;
121*0Sstevel@tonic-gate 		}
122*0Sstevel@tonic-gate 	} else if (x.exponent == 0xff) {		/* inf or nan */
123*0Sstevel@tonic-gate 		if (x.significand == 0) {		/* inf */
124*0Sstevel@tonic-gate 			pu->fpclass = fp_infinity;
125*0Sstevel@tonic-gate 			return;
126*0Sstevel@tonic-gate 		} else {				/* nan */
127*0Sstevel@tonic-gate 			if ((U & 0x400000) != 0) {	/* quiet */
128*0Sstevel@tonic-gate 				pu->fpclass = fp_quiet;
129*0Sstevel@tonic-gate 			} else {			/* signaling */
130*0Sstevel@tonic-gate 				pu->fpclass = fp_signaling;
131*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_invalid);
132*0Sstevel@tonic-gate 			}
133*0Sstevel@tonic-gate 			pu->significand[0] = 0x18000 | (U >> 7);
134*0Sstevel@tonic-gate 			(*pu).significand[1] = ((U&0x7f)<<25);
135*0Sstevel@tonic-gate 			return;
136*0Sstevel@tonic-gate 		}
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 	(*pu).exponent = x.exponent - SINGLE_BIAS;
139*0Sstevel@tonic-gate 	(*pu).fpclass = fp_normal;
140*0Sstevel@tonic-gate 	(*pu).significand[0] = 0x10000|(U>>7);
141*0Sstevel@tonic-gate 	(*pu).significand[1] = ((U&0x7f)<<25);
142*0Sstevel@tonic-gate }
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate void
unpackdouble(fp_simd_type * pfpsd,unpacked * pu,double_type x,uint_t y)145*0Sstevel@tonic-gate unpackdouble(
146*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* simulator data */
147*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked result */
148*0Sstevel@tonic-gate 	double_type	x,	/* packed double, sign/exponent/upper 20 bits */
149*0Sstevel@tonic-gate 	uint_t		y)	/* and the lower 32 bits of the significand */
150*0Sstevel@tonic-gate {
151*0Sstevel@tonic-gate 	uint_t U;
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	pu->sticky = pu->rounded = 0;
154*0Sstevel@tonic-gate 	U = x.significand;
155*0Sstevel@tonic-gate 	(*pu).sign = x.sign;
156*0Sstevel@tonic-gate 	pu->significand[1] = y;
157*0Sstevel@tonic-gate 	pu->significand[2] = 0;
158*0Sstevel@tonic-gate 	pu->significand[3] = 0;
159*0Sstevel@tonic-gate 	if (x.exponent == 0) {				/* zero or sub */
160*0Sstevel@tonic-gate 		if ((x.significand == 0) && (y == 0)) {	/* zero */
161*0Sstevel@tonic-gate 			pu->fpclass = fp_zero;
162*0Sstevel@tonic-gate 			return;
163*0Sstevel@tonic-gate 		} else {				/* subnormal */
164*0Sstevel@tonic-gate 			pu->fpclass = fp_normal;
165*0Sstevel@tonic-gate 			pu->exponent = -DOUBLE_BIAS-3;
166*0Sstevel@tonic-gate 			pu->significand[0] = U;
167*0Sstevel@tonic-gate 			fpu_normalize(pu);
168*0Sstevel@tonic-gate 			return;
169*0Sstevel@tonic-gate 		}
170*0Sstevel@tonic-gate 	} else if (x.exponent == 0x7ff) {		/* inf or nan */
171*0Sstevel@tonic-gate 		if ((U|y) == 0) {			/* inf */
172*0Sstevel@tonic-gate 			pu->fpclass = fp_infinity;
173*0Sstevel@tonic-gate 			return;
174*0Sstevel@tonic-gate 		} else {				/* nan */
175*0Sstevel@tonic-gate 			if ((U & 0x80000) != 0) {	/* quiet */
176*0Sstevel@tonic-gate 				pu->fpclass = fp_quiet;
177*0Sstevel@tonic-gate 			} else {			/* signaling */
178*0Sstevel@tonic-gate 				pu->fpclass = fp_signaling;
179*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_invalid);
180*0Sstevel@tonic-gate 			}
181*0Sstevel@tonic-gate 			pu->significand[0] = 0x18000 | (U >> 4);
182*0Sstevel@tonic-gate 			(*pu).significand[1] = ((U&0xf)<<28)|(y>>4);
183*0Sstevel@tonic-gate 			(*pu).significand[2] = ((y&0xf)<<28);
184*0Sstevel@tonic-gate 			return;
185*0Sstevel@tonic-gate 		}
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 	(*pu).exponent = x.exponent - DOUBLE_BIAS;
188*0Sstevel@tonic-gate 	(*pu).fpclass = fp_normal;
189*0Sstevel@tonic-gate 	(*pu).significand[0] = 0x10000|(U>>4);
190*0Sstevel@tonic-gate 	(*pu).significand[1] = ((U&0xf)<<28)|(y>>4);
191*0Sstevel@tonic-gate 	(*pu).significand[2] = ((y&0xf)<<28);
192*0Sstevel@tonic-gate }
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate static void
unpackextended(fp_simd_type * pfpsd,unpacked * pu,extended_type x,uint32_t y,uint32_t z,uint32_t w)195*0Sstevel@tonic-gate unpackextended(
196*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* simulator data */
197*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked result */
198*0Sstevel@tonic-gate 	extended_type	x,	/* packed extended, sign/exponent/16 bits */
199*0Sstevel@tonic-gate 	uint32_t	y,	/* 2nd word of extended significand */
200*0Sstevel@tonic-gate 	uint32_t	z,	/* 3rd word of extended significand */
201*0Sstevel@tonic-gate 	uint32_t	w)	/* 4th word of extended significand */
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	uint_t U;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 	pu->sticky = pu->rounded = 0;
206*0Sstevel@tonic-gate 	U = x.significand;
207*0Sstevel@tonic-gate 	(*pu).sign = x.sign;
208*0Sstevel@tonic-gate 	(*pu).fpclass = fp_normal;
209*0Sstevel@tonic-gate 	(*pu).exponent = x.exponent - EXTENDED_BIAS;
210*0Sstevel@tonic-gate 	(*pu).significand[0] = (x.exponent == 0) ? U : 0x10000|U;
211*0Sstevel@tonic-gate 	(*pu).significand[1] = y;
212*0Sstevel@tonic-gate 	(*pu).significand[2] = z;
213*0Sstevel@tonic-gate 	(*pu).significand[3] = w;
214*0Sstevel@tonic-gate 	if (x.exponent < 0x7fff) {	/* zero, normal, or subnormal */
215*0Sstevel@tonic-gate 		if ((z|y|w|pu->significand[0]) == 0) {	/* zero */
216*0Sstevel@tonic-gate 			pu->fpclass = fp_zero;
217*0Sstevel@tonic-gate 			return;
218*0Sstevel@tonic-gate 		} else {			/* normal or subnormal */
219*0Sstevel@tonic-gate 			if (x.exponent == 0) {
220*0Sstevel@tonic-gate 				fpu_normalize(pu);
221*0Sstevel@tonic-gate 				pu->exponent += 1;
222*0Sstevel@tonic-gate 			}
223*0Sstevel@tonic-gate 			return;
224*0Sstevel@tonic-gate 		}
225*0Sstevel@tonic-gate 	} else {					/* inf or nan */
226*0Sstevel@tonic-gate 		if ((U|z|y|w) == 0) {			/* inf */
227*0Sstevel@tonic-gate 			pu->fpclass = fp_infinity;
228*0Sstevel@tonic-gate 			return;
229*0Sstevel@tonic-gate 		} else {				/* nan */
230*0Sstevel@tonic-gate 			if ((U & 0x00008000) != 0) {	/* quiet */
231*0Sstevel@tonic-gate 				pu->fpclass = fp_quiet;
232*0Sstevel@tonic-gate 			} else {			/* signaling */
233*0Sstevel@tonic-gate 				pu->fpclass = fp_signaling;
234*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_invalid);
235*0Sstevel@tonic-gate 			}
236*0Sstevel@tonic-gate 			pu->significand[0] |= 0x8000;	/* make quiet */
237*0Sstevel@tonic-gate 			return;
238*0Sstevel@tonic-gate 		}
239*0Sstevel@tonic-gate 	}
240*0Sstevel@tonic-gate }
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate void
_fp_unpack(fp_simd_type * pfpsd,unpacked * pu,uint_t n,enum fp_op_type dtype)243*0Sstevel@tonic-gate _fp_unpack(
244*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* simulator data */
245*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked result */
246*0Sstevel@tonic-gate 	uint_t		n,	/* register where data starts */
247*0Sstevel@tonic-gate 	enum fp_op_type	dtype)	/* type of datum */
248*0Sstevel@tonic-gate {
249*0Sstevel@tonic-gate 	freg_type	f;
250*0Sstevel@tonic-gate 	union {
251*0Sstevel@tonic-gate 		uint32_t	y[4];
252*0Sstevel@tonic-gate 		uint64_t	ll[2];
253*0Sstevel@tonic-gate 		freg_type	f;
254*0Sstevel@tonic-gate 	} fp;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	switch (dtype) {
257*0Sstevel@tonic-gate 	case fp_op_int32:
258*0Sstevel@tonic-gate 		pfpsd->fp_current_read_freg(&f, n, pfpsd);
259*0Sstevel@tonic-gate 		unpackint32(pu, f.int32_reg);
260*0Sstevel@tonic-gate 		break;
261*0Sstevel@tonic-gate 	case fp_op_int64:
262*0Sstevel@tonic-gate 
263*0Sstevel@tonic-gate 		if ((n & 0x1) == 1)	/* fix register encoding */
264*0Sstevel@tonic-gate 			n = (n & 0x1e) | 0x20;
265*0Sstevel@tonic-gate 		pfpsd->fp_current_read_dreg(&fp.ll[0], DOUBLE(n), pfpsd);
266*0Sstevel@tonic-gate 		unpackint64(pu, fp.f.int64_reg);
267*0Sstevel@tonic-gate 		break;
268*0Sstevel@tonic-gate 	case fp_op_single:
269*0Sstevel@tonic-gate 		pfpsd->fp_current_read_freg(&f, n, pfpsd);
270*0Sstevel@tonic-gate 		unpacksingle(pfpsd, pu, f.single_reg);
271*0Sstevel@tonic-gate 		break;
272*0Sstevel@tonic-gate 	case fp_op_double:
273*0Sstevel@tonic-gate 		if ((n & 0x1) == 1)	/* fix register encoding */
274*0Sstevel@tonic-gate 			n = (n & 0x1e) | 0x20;
275*0Sstevel@tonic-gate 		pfpsd->fp_current_read_dreg(&fp.ll[0], DOUBLE(n), pfpsd);
276*0Sstevel@tonic-gate 		unpackdouble(pfpsd, pu, fp.f.double_reg, fp.y[1]);
277*0Sstevel@tonic-gate 		break;
278*0Sstevel@tonic-gate 	case fp_op_extended:
279*0Sstevel@tonic-gate 		if ((n & 0x1) == 1)	/* fix register encoding */
280*0Sstevel@tonic-gate 			n = (n & 0x1e) | 0x20;
281*0Sstevel@tonic-gate 		pfpsd->fp_current_read_dreg(&fp.ll[0], QUAD_E(n), pfpsd);
282*0Sstevel@tonic-gate 		pfpsd->fp_current_read_dreg(&fp.ll[1], QUAD_F(n), pfpsd);
283*0Sstevel@tonic-gate 		unpackextended(pfpsd, pu, fp.f.extended_reg, fp.y[1],
284*0Sstevel@tonic-gate 					fp.y[2], fp.y[3]);
285*0Sstevel@tonic-gate 		break;
286*0Sstevel@tonic-gate 	}
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate void
_fp_unpack_word(fp_simd_type * pfpsd,uint32_t * pu,uint_t n)290*0Sstevel@tonic-gate _fp_unpack_word(
291*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* simulator data */
292*0Sstevel@tonic-gate 	uint32_t	*pu,	/* unpacked result */
293*0Sstevel@tonic-gate 	uint_t		n)	/* register where data starts */
294*0Sstevel@tonic-gate {
295*0Sstevel@tonic-gate 	pfpsd->fp_current_read_freg(pu, n, pfpsd);
296*0Sstevel@tonic-gate }
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate void
_fp_unpack_extword(fp_simd_type * pfpsd,uint64_t * pu,uint_t n)299*0Sstevel@tonic-gate _fp_unpack_extword(
300*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* simulator data */
301*0Sstevel@tonic-gate 	uint64_t	*pu,	/* unpacked result */
302*0Sstevel@tonic-gate 	uint_t		n)	/* register where data starts */
303*0Sstevel@tonic-gate {
304*0Sstevel@tonic-gate 	if ((n & 0x1) == 1)	/* fix register encoding */
305*0Sstevel@tonic-gate 		n = (n & 0x1e) | 0x20;
306*0Sstevel@tonic-gate 	pfpsd->fp_current_read_dreg(pu, DOUBLE(n), pfpsd);
307*0Sstevel@tonic-gate }
308