xref: /onnv-gate/usr/src/uts/sparc/fpu/pack.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 1988,1995-1996,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"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /* Pack 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 /*
35*0Sstevel@tonic-gate  * Returns 1 if overflow should go to infinity, 0 if to max finite.
36*0Sstevel@tonic-gate  */
37*0Sstevel@tonic-gate static int
overflow_to_infinity(fp_simd_type * pfpsd,int sign)38*0Sstevel@tonic-gate overflow_to_infinity(
39*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,		/* Pointer to simulator data */
40*0Sstevel@tonic-gate 	int		sign)		/* negative or positive */
41*0Sstevel@tonic-gate {
42*0Sstevel@tonic-gate 	int		inf;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 	switch (pfpsd->fp_direction) {
45*0Sstevel@tonic-gate 	case fp_nearest:
46*0Sstevel@tonic-gate 		inf = 1;
47*0Sstevel@tonic-gate 		break;
48*0Sstevel@tonic-gate 	case fp_tozero:
49*0Sstevel@tonic-gate 		inf = 0;
50*0Sstevel@tonic-gate 		break;
51*0Sstevel@tonic-gate 	case fp_positive:
52*0Sstevel@tonic-gate 		inf = !sign;
53*0Sstevel@tonic-gate 		break;
54*0Sstevel@tonic-gate 	case fp_negative:
55*0Sstevel@tonic-gate 		inf = sign;
56*0Sstevel@tonic-gate 		break;
57*0Sstevel@tonic-gate 	}
58*0Sstevel@tonic-gate 	return (inf);
59*0Sstevel@tonic-gate }
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate /*
62*0Sstevel@tonic-gate  * Round according to current rounding mode.
63*0Sstevel@tonic-gate  */
64*0Sstevel@tonic-gate static void
round(fp_simd_type * pfpsd,unpacked * pu)65*0Sstevel@tonic-gate round(
66*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,		/* Pointer to simulator data */
67*0Sstevel@tonic-gate 	unpacked	*pu)		/* unpacked result */
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate 	int		increment;	/* boolean to indicate round up */
70*0Sstevel@tonic-gate 	int		sr;
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 	sr = pu->sticky|pu->rounded;
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	if (sr == 0)
75*0Sstevel@tonic-gate 		return;
76*0Sstevel@tonic-gate 	fpu_set_exception(pfpsd, fp_inexact);
77*0Sstevel@tonic-gate 	switch (pfpsd->fp_direction) {
78*0Sstevel@tonic-gate 	case fp_nearest:
79*0Sstevel@tonic-gate 		increment = pu->rounded;
80*0Sstevel@tonic-gate 		break;
81*0Sstevel@tonic-gate 	case fp_tozero:
82*0Sstevel@tonic-gate 		increment = 0;
83*0Sstevel@tonic-gate 		break;
84*0Sstevel@tonic-gate 	case fp_positive:
85*0Sstevel@tonic-gate 		increment = (pu->sign == 0) & (sr != 0);
86*0Sstevel@tonic-gate 		break;
87*0Sstevel@tonic-gate 	case fp_negative:
88*0Sstevel@tonic-gate 		increment = (pu->sign != 0) & (sr != 0);
89*0Sstevel@tonic-gate 		break;
90*0Sstevel@tonic-gate 	}
91*0Sstevel@tonic-gate 	if (increment) {
92*0Sstevel@tonic-gate 	    pu->significand[3]++;
93*0Sstevel@tonic-gate 	    if (pu->significand[3] == 0) {
94*0Sstevel@tonic-gate 		pu->significand[2]++;
95*0Sstevel@tonic-gate 		if (pu->significand[2] == 0) {
96*0Sstevel@tonic-gate 		    pu->significand[1]++;
97*0Sstevel@tonic-gate 		    if (pu->significand[1] == 0) {
98*0Sstevel@tonic-gate 			pu->significand[0]++;	/* rounding carried out */
99*0Sstevel@tonic-gate 			if (pu->significand[0] == 0x20000) {
100*0Sstevel@tonic-gate 			    pu->exponent++;
101*0Sstevel@tonic-gate 			    pu->significand[0] = 0x10000;
102*0Sstevel@tonic-gate 			}
103*0Sstevel@tonic-gate 		    }
104*0Sstevel@tonic-gate 		}
105*0Sstevel@tonic-gate 	    }
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 	if ((pfpsd->fp_direction == fp_nearest) &&
108*0Sstevel@tonic-gate 	    (pu->sticky == 0) && increment != 0) {	/* ambiguous case */
109*0Sstevel@tonic-gate 		pu->significand[3] &= 0xfffffffe; /* force round to even */
110*0Sstevel@tonic-gate 	}
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate static void
packint32(fp_simd_type * pfpsd,unpacked * pu,int32_t * px)114*0Sstevel@tonic-gate packint32(
115*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
116*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked result */
117*0Sstevel@tonic-gate 	int32_t		*px)	/* packed int32_t */
118*0Sstevel@tonic-gate {
119*0Sstevel@tonic-gate 	switch (pu->fpclass) {
120*0Sstevel@tonic-gate 	case fp_zero:
121*0Sstevel@tonic-gate 		*px = 0;
122*0Sstevel@tonic-gate 		break;
123*0Sstevel@tonic-gate 	case fp_normal:
124*0Sstevel@tonic-gate 		if (pu->exponent >= 32)
125*0Sstevel@tonic-gate 			goto overflow;
126*0Sstevel@tonic-gate 		fpu_rightshift(pu, 112 - pu->exponent);
127*0Sstevel@tonic-gate 		round(pfpsd, pu);
128*0Sstevel@tonic-gate 		if (pu->significand[3] >= 0x80000000)
129*0Sstevel@tonic-gate 			if ((pu->sign == 0)||(pu->significand[3] > 0x80000000))
130*0Sstevel@tonic-gate 				goto overflow;
131*0Sstevel@tonic-gate 		*px = pu->significand[3];
132*0Sstevel@tonic-gate 		if (pu->sign)
133*0Sstevel@tonic-gate 			*px = -*px;
134*0Sstevel@tonic-gate 		break;
135*0Sstevel@tonic-gate 	case fp_infinity:
136*0Sstevel@tonic-gate 	case fp_quiet:
137*0Sstevel@tonic-gate 	case fp_signaling:
138*0Sstevel@tonic-gate overflow:
139*0Sstevel@tonic-gate 		if (pu->sign)
140*0Sstevel@tonic-gate 			*px = 0x80000000;
141*0Sstevel@tonic-gate 		else
142*0Sstevel@tonic-gate 			*px = 0x7fffffff;
143*0Sstevel@tonic-gate 		pfpsd->fp_current_exceptions &= ~(1 << (int)fp_inexact);
144*0Sstevel@tonic-gate 		fpu_set_exception(pfpsd, fp_invalid);
145*0Sstevel@tonic-gate 		break;
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate static void
packint64(fp_simd_type * pfpsd,unpacked * pu,int64_t * px)150*0Sstevel@tonic-gate packint64(
151*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
152*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked result */
153*0Sstevel@tonic-gate 	int64_t		*px)	/* packed int64_t */
154*0Sstevel@tonic-gate {
155*0Sstevel@tonic-gate 	union {
156*0Sstevel@tonic-gate 		uint64_t ll;
157*0Sstevel@tonic-gate 		uint32_t i[2];
158*0Sstevel@tonic-gate 	} x;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	switch (pu->fpclass) {
161*0Sstevel@tonic-gate 	case fp_zero:
162*0Sstevel@tonic-gate 		*px = 0;
163*0Sstevel@tonic-gate 		break;
164*0Sstevel@tonic-gate 	case fp_normal:
165*0Sstevel@tonic-gate 		if (pu->exponent >= 64)
166*0Sstevel@tonic-gate 			goto overflow;
167*0Sstevel@tonic-gate 		fpu_rightshift(pu, 112 - pu->exponent);
168*0Sstevel@tonic-gate 		round(pfpsd, pu);
169*0Sstevel@tonic-gate 		if (pu->significand[2] >= 0x80000000)
170*0Sstevel@tonic-gate 			if ((pu->sign == 0) ||
171*0Sstevel@tonic-gate 			    (pu->significand[2] > 0x80000000) ||
172*0Sstevel@tonic-gate 			    (((pu->significand[2] == 0x80000000) &&
173*0Sstevel@tonic-gate 				(pu->significand[3] > 0))))
174*0Sstevel@tonic-gate 				goto overflow;
175*0Sstevel@tonic-gate 		x.i[0] = pu->significand[2];
176*0Sstevel@tonic-gate 		x.i[1] = pu->significand[3];
177*0Sstevel@tonic-gate 		*px = x.ll;
178*0Sstevel@tonic-gate 		if (pu->sign)
179*0Sstevel@tonic-gate 			*px = -*px;
180*0Sstevel@tonic-gate 		break;
181*0Sstevel@tonic-gate 	case fp_infinity:
182*0Sstevel@tonic-gate 	case fp_quiet:
183*0Sstevel@tonic-gate 	case fp_signaling:
184*0Sstevel@tonic-gate overflow:
185*0Sstevel@tonic-gate 		if (pu->sign)
186*0Sstevel@tonic-gate 			*px = (int64_t)0x8000000000000000;
187*0Sstevel@tonic-gate 		else
188*0Sstevel@tonic-gate 			*px = (int64_t)0x7fffffffffffffff;
189*0Sstevel@tonic-gate 		pfpsd->fp_current_exceptions &= ~(1 << (int)fp_inexact);
190*0Sstevel@tonic-gate 		fpu_set_exception(pfpsd, fp_invalid);
191*0Sstevel@tonic-gate 		break;
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate }
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate static void
packsingle(fp_simd_type * pfpsd,unpacked * pu,single_type * px)196*0Sstevel@tonic-gate packsingle(
197*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
198*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked result */
199*0Sstevel@tonic-gate 	single_type	*px)	/* packed single */
200*0Sstevel@tonic-gate {
201*0Sstevel@tonic-gate 	px->sign = pu->sign;
202*0Sstevel@tonic-gate 	switch (pu->fpclass) {
203*0Sstevel@tonic-gate 	case fp_zero:
204*0Sstevel@tonic-gate 		px->exponent = 0;
205*0Sstevel@tonic-gate 		px->significand = 0;
206*0Sstevel@tonic-gate 		break;
207*0Sstevel@tonic-gate 	case fp_infinity:
208*0Sstevel@tonic-gate infinity:
209*0Sstevel@tonic-gate 		px->exponent = 0xff;
210*0Sstevel@tonic-gate 		px->significand = 0;
211*0Sstevel@tonic-gate 		break;
212*0Sstevel@tonic-gate 	case fp_quiet:
213*0Sstevel@tonic-gate 	case fp_signaling:
214*0Sstevel@tonic-gate 		fpu_rightshift(pu, 113-24);
215*0Sstevel@tonic-gate 		px->exponent = 0xff;
216*0Sstevel@tonic-gate 		px->significand = 0x400000|(0x3fffff&pu->significand[3]);
217*0Sstevel@tonic-gate 		break;
218*0Sstevel@tonic-gate 	case fp_normal:
219*0Sstevel@tonic-gate 		fpu_rightshift(pu, 113-24);
220*0Sstevel@tonic-gate 		pu->exponent += SINGLE_BIAS;
221*0Sstevel@tonic-gate 		if (pu->exponent <= 0) {
222*0Sstevel@tonic-gate 			px->exponent = 0;
223*0Sstevel@tonic-gate 			fpu_rightshift(pu, 1 - pu->exponent);
224*0Sstevel@tonic-gate 			round(pfpsd, pu);
225*0Sstevel@tonic-gate 			if (pu->significand[3] == 0x800000) {
226*0Sstevel@tonic-gate 								/*
227*0Sstevel@tonic-gate 								 * rounded
228*0Sstevel@tonic-gate 								 * back up to
229*0Sstevel@tonic-gate 								 * normal
230*0Sstevel@tonic-gate 								 */
231*0Sstevel@tonic-gate 				px->exponent = 1;
232*0Sstevel@tonic-gate 				px->significand = 0;
233*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_inexact);
234*0Sstevel@tonic-gate 			} else
235*0Sstevel@tonic-gate 				px->significand = 0x7fffff & pu->significand[3];
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 			if (pfpsd->fp_current_exceptions & (1 << fp_inexact))
238*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_underflow);
239*0Sstevel@tonic-gate 			if (pfpsd->fp_fsrtem & (1<<fp_underflow)) {
240*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_underflow);
241*0Sstevel@tonic-gate 				pfpsd->fp_current_exceptions &=
242*0Sstevel@tonic-gate 						~(1 << (int)fp_inexact);
243*0Sstevel@tonic-gate 			}
244*0Sstevel@tonic-gate 			return;
245*0Sstevel@tonic-gate 		}
246*0Sstevel@tonic-gate 		round(pfpsd, pu);
247*0Sstevel@tonic-gate 		if (pu->significand[3] == 0x1000000) {	/* rounding overflow */
248*0Sstevel@tonic-gate 			pu->significand[3] = 0x800000;
249*0Sstevel@tonic-gate 			pu->exponent += 1;
250*0Sstevel@tonic-gate 		}
251*0Sstevel@tonic-gate 		if (pu->exponent >= 0xff) {
252*0Sstevel@tonic-gate 			fpu_set_exception(pfpsd, fp_overflow);
253*0Sstevel@tonic-gate 			fpu_set_exception(pfpsd, fp_inexact);
254*0Sstevel@tonic-gate 			if (pfpsd->fp_fsrtem & (1<<fp_overflow)) {
255*0Sstevel@tonic-gate 				pfpsd->fp_current_exceptions &=
256*0Sstevel@tonic-gate 						~(1 << (int)fp_inexact);
257*0Sstevel@tonic-gate 			}
258*0Sstevel@tonic-gate 			if (overflow_to_infinity(pfpsd, pu->sign))
259*0Sstevel@tonic-gate 				goto infinity;
260*0Sstevel@tonic-gate 			px->exponent = 0xfe;
261*0Sstevel@tonic-gate 			px->significand = 0x7fffff;
262*0Sstevel@tonic-gate 			return;
263*0Sstevel@tonic-gate 		}
264*0Sstevel@tonic-gate 		px->exponent = pu->exponent;
265*0Sstevel@tonic-gate 		px->significand = 0x7fffff & pu->significand[3];
266*0Sstevel@tonic-gate 	}
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate static void
packdouble(fp_simd_type * pfpsd,unpacked * pu,double_type * px,uint_t * py)270*0Sstevel@tonic-gate packdouble(
271*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
272*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked result */
273*0Sstevel@tonic-gate 	double_type	*px,	/* packed double, sign/exponent/upper 20 bits */
274*0Sstevel@tonic-gate 	uint_t		*py)	/* and the lower 32 bits of the significand */
275*0Sstevel@tonic-gate {
276*0Sstevel@tonic-gate 	px->sign = pu->sign;
277*0Sstevel@tonic-gate 	switch (pu->fpclass) {
278*0Sstevel@tonic-gate 	case fp_zero:
279*0Sstevel@tonic-gate 		px->exponent = 0;
280*0Sstevel@tonic-gate 		px->significand = 0;
281*0Sstevel@tonic-gate 		*py = 0;
282*0Sstevel@tonic-gate 		break;
283*0Sstevel@tonic-gate 	case fp_infinity:
284*0Sstevel@tonic-gate infinity:
285*0Sstevel@tonic-gate 		px->exponent = 0x7ff;
286*0Sstevel@tonic-gate 		px->significand = 0;
287*0Sstevel@tonic-gate 		*py = 0;
288*0Sstevel@tonic-gate 		break;
289*0Sstevel@tonic-gate 	case fp_quiet:
290*0Sstevel@tonic-gate 	case fp_signaling:
291*0Sstevel@tonic-gate 		fpu_rightshift(pu, 113-53);
292*0Sstevel@tonic-gate 		px->exponent = 0x7ff;
293*0Sstevel@tonic-gate 		px->significand = 0x80000 | (0x7ffff & pu->significand[2]);
294*0Sstevel@tonic-gate 		*py = pu->significand[3];
295*0Sstevel@tonic-gate 		break;
296*0Sstevel@tonic-gate 	case fp_normal:
297*0Sstevel@tonic-gate 		fpu_rightshift(pu, 113-53);
298*0Sstevel@tonic-gate 		pu->exponent += DOUBLE_BIAS;
299*0Sstevel@tonic-gate 		if (pu->exponent <= 0) {	/* underflow */
300*0Sstevel@tonic-gate 			px->exponent = 0;
301*0Sstevel@tonic-gate 			fpu_rightshift(pu, 1 - pu->exponent);
302*0Sstevel@tonic-gate 			round(pfpsd, pu);
303*0Sstevel@tonic-gate 			if (pu->significand[2] == 0x100000) {
304*0Sstevel@tonic-gate 								/*
305*0Sstevel@tonic-gate 								 * rounded
306*0Sstevel@tonic-gate 								 * back up to
307*0Sstevel@tonic-gate 								 * normal
308*0Sstevel@tonic-gate 								 */
309*0Sstevel@tonic-gate 				px->exponent = 1;
310*0Sstevel@tonic-gate 				px->significand = 0;
311*0Sstevel@tonic-gate 				*py = 0;
312*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_inexact);
313*0Sstevel@tonic-gate 			} else {
314*0Sstevel@tonic-gate 				px->exponent = 0;
315*0Sstevel@tonic-gate 				px->significand = 0xfffff & pu->significand[2];
316*0Sstevel@tonic-gate 				*py = pu->significand[3];
317*0Sstevel@tonic-gate 			}
318*0Sstevel@tonic-gate 			if (pfpsd->fp_current_exceptions & (1 << fp_inexact))
319*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_underflow);
320*0Sstevel@tonic-gate 			if (pfpsd->fp_fsrtem & (1<<fp_underflow)) {
321*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_underflow);
322*0Sstevel@tonic-gate 				pfpsd->fp_current_exceptions &=
323*0Sstevel@tonic-gate 						~(1 << (int)fp_inexact);
324*0Sstevel@tonic-gate 			}
325*0Sstevel@tonic-gate 			return;
326*0Sstevel@tonic-gate 		}
327*0Sstevel@tonic-gate 		round(pfpsd, pu);
328*0Sstevel@tonic-gate 		if (pu->significand[2] == 0x200000) {	/* rounding overflow */
329*0Sstevel@tonic-gate 			pu->significand[2] = 0x100000;
330*0Sstevel@tonic-gate 			pu->exponent += 1;
331*0Sstevel@tonic-gate 		}
332*0Sstevel@tonic-gate 		if (pu->exponent >= 0x7ff) {	/* overflow */
333*0Sstevel@tonic-gate 			fpu_set_exception(pfpsd, fp_overflow);
334*0Sstevel@tonic-gate 			fpu_set_exception(pfpsd, fp_inexact);
335*0Sstevel@tonic-gate 			if (pfpsd->fp_fsrtem & (1<<fp_overflow)) {
336*0Sstevel@tonic-gate 				pfpsd->fp_current_exceptions &=
337*0Sstevel@tonic-gate 						~(1 << (int)fp_inexact);
338*0Sstevel@tonic-gate 			}
339*0Sstevel@tonic-gate 			if (overflow_to_infinity(pfpsd, pu->sign))
340*0Sstevel@tonic-gate 				goto infinity;
341*0Sstevel@tonic-gate 			px->exponent = 0x7fe;
342*0Sstevel@tonic-gate 			px->significand = 0xfffff;
343*0Sstevel@tonic-gate 			*py = 0xffffffffU;
344*0Sstevel@tonic-gate 			return;
345*0Sstevel@tonic-gate 		}
346*0Sstevel@tonic-gate 		px->exponent = pu->exponent;
347*0Sstevel@tonic-gate 		px->significand = 0xfffff & pu->significand[2];
348*0Sstevel@tonic-gate 		*py = pu->significand[3];
349*0Sstevel@tonic-gate 		break;
350*0Sstevel@tonic-gate 	}
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate static void
packextended(fp_simd_type * pfpsd,unpacked * pu,extended_type * px,uint_t * py,uint_t * pz,uint_t * pw)354*0Sstevel@tonic-gate packextended(
355*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
356*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked result */
357*0Sstevel@tonic-gate 	extended_type	*px,	/* packed extended, sign/exponent/16 bits */
358*0Sstevel@tonic-gate 	uint_t		*py,	/* 2nd word of extended significand */
359*0Sstevel@tonic-gate 	uint_t		*pz,	/* 3rd word of extended significand */
360*0Sstevel@tonic-gate 	uint_t		*pw)	/* 4th word of extended significand */
361*0Sstevel@tonic-gate {
362*0Sstevel@tonic-gate 	px->sign = pu->sign;
363*0Sstevel@tonic-gate 	switch (pu->fpclass) {
364*0Sstevel@tonic-gate 	case fp_zero:
365*0Sstevel@tonic-gate 		px->exponent = 0;
366*0Sstevel@tonic-gate 		px->significand = 0;
367*0Sstevel@tonic-gate 		*pz = 0;
368*0Sstevel@tonic-gate 		*py = 0;
369*0Sstevel@tonic-gate 		*pw = 0;
370*0Sstevel@tonic-gate 		break;
371*0Sstevel@tonic-gate 	case fp_infinity:
372*0Sstevel@tonic-gate infinity:
373*0Sstevel@tonic-gate 		px->exponent = 0x7fff;
374*0Sstevel@tonic-gate 		px->significand = 0;
375*0Sstevel@tonic-gate 		*pz = 0;
376*0Sstevel@tonic-gate 		*py = 0;
377*0Sstevel@tonic-gate 		*pw = 0;
378*0Sstevel@tonic-gate 		break;
379*0Sstevel@tonic-gate 	case fp_quiet:
380*0Sstevel@tonic-gate 	case fp_signaling:
381*0Sstevel@tonic-gate 		px->exponent = 0x7fff;
382*0Sstevel@tonic-gate 		px->significand = 0x8000 | pu->significand[0];
383*0Sstevel@tonic-gate 								/*
384*0Sstevel@tonic-gate 								 * Insure quiet
385*0Sstevel@tonic-gate 								 * nan.
386*0Sstevel@tonic-gate 								 */
387*0Sstevel@tonic-gate 		*py = pu->significand[1];
388*0Sstevel@tonic-gate 		*pz = pu->significand[2];
389*0Sstevel@tonic-gate 		*pw = pu->significand[3];
390*0Sstevel@tonic-gate 		break;
391*0Sstevel@tonic-gate 	case fp_normal:
392*0Sstevel@tonic-gate 		pu->exponent += EXTENDED_BIAS;
393*0Sstevel@tonic-gate 		if (pu->exponent <= 0) {	/* underflow */
394*0Sstevel@tonic-gate 			fpu_rightshift(pu, 1-pu->exponent);
395*0Sstevel@tonic-gate 			round(pfpsd, pu);
396*0Sstevel@tonic-gate 			if (pu->significand[0] < 0x00010000) {
397*0Sstevel@tonic-gate 								/*
398*0Sstevel@tonic-gate 								 * not rounded
399*0Sstevel@tonic-gate 								 * back up
400*0Sstevel@tonic-gate 								 * to normal
401*0Sstevel@tonic-gate 								 */
402*0Sstevel@tonic-gate 				px->exponent = 0;
403*0Sstevel@tonic-gate 			} else {
404*0Sstevel@tonic-gate 				px->exponent = 1;
405*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_inexact);
406*0Sstevel@tonic-gate 			}
407*0Sstevel@tonic-gate 			if (pfpsd->fp_current_exceptions & (1 << fp_inexact))
408*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_underflow);
409*0Sstevel@tonic-gate 			if (pfpsd->fp_fsrtem & (1<<fp_underflow)) {
410*0Sstevel@tonic-gate 				fpu_set_exception(pfpsd, fp_underflow);
411*0Sstevel@tonic-gate 				pfpsd->fp_current_exceptions &=
412*0Sstevel@tonic-gate 						~(1 << (int)fp_inexact);
413*0Sstevel@tonic-gate 			}
414*0Sstevel@tonic-gate 			px->significand = pu->significand[0];
415*0Sstevel@tonic-gate 			*py = pu->significand[1];
416*0Sstevel@tonic-gate 			*pz = pu->significand[2];
417*0Sstevel@tonic-gate 			*pw = pu->significand[3];
418*0Sstevel@tonic-gate 			return;
419*0Sstevel@tonic-gate 		}
420*0Sstevel@tonic-gate 		round(pfpsd, pu); /* rounding overflow handled in round() */
421*0Sstevel@tonic-gate 		if (pu->exponent >= 0x7fff) {	/* overflow */
422*0Sstevel@tonic-gate 			fpu_set_exception(pfpsd, fp_overflow);
423*0Sstevel@tonic-gate 			fpu_set_exception(pfpsd, fp_inexact);
424*0Sstevel@tonic-gate 			if (pfpsd->fp_fsrtem & (1<<fp_overflow)) {
425*0Sstevel@tonic-gate 				pfpsd->fp_current_exceptions &=
426*0Sstevel@tonic-gate 						~(1 << (int)fp_inexact);
427*0Sstevel@tonic-gate 			}
428*0Sstevel@tonic-gate 			if (overflow_to_infinity(pfpsd, pu->sign))
429*0Sstevel@tonic-gate 				goto infinity;
430*0Sstevel@tonic-gate 			px->exponent = 0x7ffe;	/* overflow to max norm */
431*0Sstevel@tonic-gate 			px->significand = 0xffff;
432*0Sstevel@tonic-gate 			*py = 0xffffffffU;
433*0Sstevel@tonic-gate 			*pz = 0xffffffffU;
434*0Sstevel@tonic-gate 			*pw = 0xffffffffU;
435*0Sstevel@tonic-gate 			return;
436*0Sstevel@tonic-gate 		}
437*0Sstevel@tonic-gate 		px->exponent = pu->exponent;
438*0Sstevel@tonic-gate 		px->significand = pu->significand[0];
439*0Sstevel@tonic-gate 		*py = pu->significand[1];
440*0Sstevel@tonic-gate 		*pz = pu->significand[2];
441*0Sstevel@tonic-gate 		*pw = pu->significand[3];
442*0Sstevel@tonic-gate 		break;
443*0Sstevel@tonic-gate 	}
444*0Sstevel@tonic-gate }
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate void
_fp_pack(fp_simd_type * pfpsd,unpacked * pu,uint_t n,enum fp_op_type type)447*0Sstevel@tonic-gate _fp_pack(
448*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
449*0Sstevel@tonic-gate 	unpacked	*pu,	/* unpacked operand */
450*0Sstevel@tonic-gate 	uint_t		n,	/* register where datum starts */
451*0Sstevel@tonic-gate 	enum fp_op_type type)	/* type of datum */
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate {
454*0Sstevel@tonic-gate 	switch (type) {
455*0Sstevel@tonic-gate 	case fp_op_int32:
456*0Sstevel@tonic-gate 		{
457*0Sstevel@tonic-gate 			int32_t		x;
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 			packint32(pfpsd, pu, &x);
460*0Sstevel@tonic-gate 			if (!(pfpsd->fp_current_exceptions & pfpsd->fp_fsrtem))
461*0Sstevel@tonic-gate 				pfpsd->fp_current_write_freg(&x, n, pfpsd);
462*0Sstevel@tonic-gate 			break;
463*0Sstevel@tonic-gate 		}
464*0Sstevel@tonic-gate 	case fp_op_int64:
465*0Sstevel@tonic-gate 		{
466*0Sstevel@tonic-gate 			int64_t		x;
467*0Sstevel@tonic-gate 
468*0Sstevel@tonic-gate 			packint64(pfpsd, pu, &x);
469*0Sstevel@tonic-gate 			if ((n & 0x1) == 1)	/* fix register encoding */
470*0Sstevel@tonic-gate 				n = (n & 0x1e) | 0x20;
471*0Sstevel@tonic-gate 			if (!(pfpsd->fp_current_exceptions & pfpsd->fp_fsrtem))
472*0Sstevel@tonic-gate 			    pfpsd->fp_current_write_dreg(&x, DOUBLE(n), pfpsd);
473*0Sstevel@tonic-gate 			break;
474*0Sstevel@tonic-gate 		}
475*0Sstevel@tonic-gate 	case fp_op_single:
476*0Sstevel@tonic-gate 		{
477*0Sstevel@tonic-gate 			single_type	x;
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 			packsingle(pfpsd, pu, &x);
480*0Sstevel@tonic-gate 			if (!(pfpsd->fp_current_exceptions & pfpsd->fp_fsrtem))
481*0Sstevel@tonic-gate 				pfpsd->fp_current_write_freg(&x, n, pfpsd);
482*0Sstevel@tonic-gate 			break;
483*0Sstevel@tonic-gate 		}
484*0Sstevel@tonic-gate 	case fp_op_double:
485*0Sstevel@tonic-gate 		{
486*0Sstevel@tonic-gate 			union {
487*0Sstevel@tonic-gate 				double_type	x[2];
488*0Sstevel@tonic-gate 				uint32_t	y[2];
489*0Sstevel@tonic-gate 				uint64_t	ll;
490*0Sstevel@tonic-gate 			} db;
491*0Sstevel@tonic-gate 
492*0Sstevel@tonic-gate 			packdouble(pfpsd, pu, &db.x[0], &db.y[1]);
493*0Sstevel@tonic-gate 			if (!(pfpsd->fp_current_exceptions &
494*0Sstevel@tonic-gate 			    pfpsd->fp_fsrtem)) {
495*0Sstevel@tonic-gate 				if ((n & 0x1) == 1) /* fix register encoding */
496*0Sstevel@tonic-gate 					n = (n & 0x1e) | 0x20;
497*0Sstevel@tonic-gate 				pfpsd->fp_current_write_dreg(&db.ll, DOUBLE(n),
498*0Sstevel@tonic-gate 					pfpsd);
499*0Sstevel@tonic-gate 			}
500*0Sstevel@tonic-gate 			break;
501*0Sstevel@tonic-gate 		}
502*0Sstevel@tonic-gate 	case fp_op_extended:
503*0Sstevel@tonic-gate 		{
504*0Sstevel@tonic-gate 			union {
505*0Sstevel@tonic-gate 				extended_type	x;
506*0Sstevel@tonic-gate 				uint32_t	y[4];
507*0Sstevel@tonic-gate 				uint64_t	ll[2];
508*0Sstevel@tonic-gate 			} ex;
509*0Sstevel@tonic-gate 			unpacked	U;
510*0Sstevel@tonic-gate 			int		k;
511*0Sstevel@tonic-gate 			switch (pfpsd->fp_precision) {
512*0Sstevel@tonic-gate 							/*
513*0Sstevel@tonic-gate 							 * Implement extended
514*0Sstevel@tonic-gate 							 * rounding precision
515*0Sstevel@tonic-gate 							 * mode.
516*0Sstevel@tonic-gate 							 */
517*0Sstevel@tonic-gate 			case fp_single:
518*0Sstevel@tonic-gate 				{
519*0Sstevel@tonic-gate 					single_type	tx;
520*0Sstevel@tonic-gate 
521*0Sstevel@tonic-gate 					packsingle(pfpsd, pu, &tx);
522*0Sstevel@tonic-gate 					pu = &U;
523*0Sstevel@tonic-gate 					unpacksingle(pfpsd, pu, tx);
524*0Sstevel@tonic-gate 					break;
525*0Sstevel@tonic-gate 				}
526*0Sstevel@tonic-gate 			case fp_double:
527*0Sstevel@tonic-gate 				{
528*0Sstevel@tonic-gate 					double_type	tx;
529*0Sstevel@tonic-gate 					uint_t		ty;
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 					packdouble(pfpsd, pu, &tx, &ty);
532*0Sstevel@tonic-gate 					pu = &U;
533*0Sstevel@tonic-gate 					unpackdouble(pfpsd, pu, tx, ty);
534*0Sstevel@tonic-gate 					break;
535*0Sstevel@tonic-gate 				}
536*0Sstevel@tonic-gate 			case fp_precision_3:	/* rounded to 64 bits */
537*0Sstevel@tonic-gate 				{
538*0Sstevel@tonic-gate 					k = pu->exponent + EXTENDED_BIAS;
539*0Sstevel@tonic-gate 					if (k >= 0) k = 113-64;
540*0Sstevel@tonic-gate 					else	k = 113-64-k;
541*0Sstevel@tonic-gate 					fpu_rightshift(pu, 113-64);
542*0Sstevel@tonic-gate 					round(pfpsd, pu);
543*0Sstevel@tonic-gate 					pu->sticky = pu->rounded = 0;
544*0Sstevel@tonic-gate 					pu->exponent += k;
545*0Sstevel@tonic-gate 					fpu_normalize(pu);
546*0Sstevel@tonic-gate 					break;
547*0Sstevel@tonic-gate 				}
548*0Sstevel@tonic-gate 			}
549*0Sstevel@tonic-gate 			packextended(pfpsd, pu, &ex.x, &ex.y[1],
550*0Sstevel@tonic-gate 						&ex.y[2], &ex.y[3]);
551*0Sstevel@tonic-gate 			if (!(pfpsd->fp_current_exceptions &
552*0Sstevel@tonic-gate 			    pfpsd->fp_fsrtem)) {
553*0Sstevel@tonic-gate 				if ((n & 0x1) == 1) /* fix register encoding */
554*0Sstevel@tonic-gate 					n = (n & 0x1e) | 0x20;
555*0Sstevel@tonic-gate 				pfpsd->fp_current_write_dreg(&ex.ll[0],
556*0Sstevel@tonic-gate 							QUAD_E(n), pfpsd);
557*0Sstevel@tonic-gate 				pfpsd->fp_current_write_dreg(&ex.ll[1],
558*0Sstevel@tonic-gate 							QUAD_F(n), pfpsd);
559*0Sstevel@tonic-gate 			}
560*0Sstevel@tonic-gate 
561*0Sstevel@tonic-gate 			break;
562*0Sstevel@tonic-gate 		}
563*0Sstevel@tonic-gate 	}
564*0Sstevel@tonic-gate }
565*0Sstevel@tonic-gate 
566*0Sstevel@tonic-gate void
_fp_pack_word(fp_simd_type * pfpsd,uint32_t * pu,uint_t n)567*0Sstevel@tonic-gate _fp_pack_word(
568*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
569*0Sstevel@tonic-gate 	uint32_t	*pu,	/* unpacked operand */
570*0Sstevel@tonic-gate 	uint_t		n)	/* register where datum starts */
571*0Sstevel@tonic-gate {
572*0Sstevel@tonic-gate 	pfpsd->fp_current_write_freg(pu, n, pfpsd);
573*0Sstevel@tonic-gate }
574*0Sstevel@tonic-gate 
575*0Sstevel@tonic-gate void
_fp_pack_extword(fp_simd_type * pfpsd,uint64_t * pu,uint_t n)576*0Sstevel@tonic-gate _fp_pack_extword(
577*0Sstevel@tonic-gate 	fp_simd_type	*pfpsd,	/* Pointer to simulator data */
578*0Sstevel@tonic-gate 	uint64_t	*pu,	/* unpacked operand */
579*0Sstevel@tonic-gate 	uint_t		n)	/* register where datum starts */
580*0Sstevel@tonic-gate {
581*0Sstevel@tonic-gate 	if ((n & 1) == 1)	/* fix register encoding */
582*0Sstevel@tonic-gate 		n = (n & 0x1e) | 0x20;
583*0Sstevel@tonic-gate 	pfpsd->fp_current_write_dreg(pu, DOUBLE(n), pfpsd);
584*0Sstevel@tonic-gate }
585