xref: /haiku/src/system/libroot/os/arch/sparc/fpu_implode.c (revision 803a4704e82749bce36c05ee56792dad62576144)
1*803a4704SPulkoMandy /*
2*803a4704SPulkoMandy  * Copyright (c) 1992, 1993
3*803a4704SPulkoMandy  *	The Regents of the University of California.  All rights reserved.
4*803a4704SPulkoMandy  *
5*803a4704SPulkoMandy  * This software was developed by the Computer Systems Engineering group
6*803a4704SPulkoMandy  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
7*803a4704SPulkoMandy  * contributed to Berkeley.
8*803a4704SPulkoMandy  *
9*803a4704SPulkoMandy  * All advertising materials mentioning features or use of this software
10*803a4704SPulkoMandy  * must display the following acknowledgement:
11*803a4704SPulkoMandy  *	This product includes software developed by the University of
12*803a4704SPulkoMandy  *	California, Lawrence Berkeley Laboratory.
13*803a4704SPulkoMandy  *
14*803a4704SPulkoMandy  * Redistribution and use in source and binary forms, with or without
15*803a4704SPulkoMandy  * modification, are permitted provided that the following conditions
16*803a4704SPulkoMandy  * are met:
17*803a4704SPulkoMandy  * 1. Redistributions of source code must retain the above copyright
18*803a4704SPulkoMandy  *    notice, this list of conditions and the following disclaimer.
19*803a4704SPulkoMandy  * 2. Redistributions in binary form must reproduce the above copyright
20*803a4704SPulkoMandy  *    notice, this list of conditions and the following disclaimer in the
21*803a4704SPulkoMandy  *    documentation and/or other materials provided with the distribution.
22*803a4704SPulkoMandy  * 3. Neither the name of the University nor the names of its contributors
23*803a4704SPulkoMandy  *    may be used to endorse or promote products derived from this software
24*803a4704SPulkoMandy  *    without specific prior written permission.
25*803a4704SPulkoMandy  *
26*803a4704SPulkoMandy  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27*803a4704SPulkoMandy  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28*803a4704SPulkoMandy  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29*803a4704SPulkoMandy  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30*803a4704SPulkoMandy  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31*803a4704SPulkoMandy  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32*803a4704SPulkoMandy  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33*803a4704SPulkoMandy  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34*803a4704SPulkoMandy  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35*803a4704SPulkoMandy  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36*803a4704SPulkoMandy  * SUCH DAMAGE.
37*803a4704SPulkoMandy  *
38*803a4704SPulkoMandy  *	@(#)fpu_implode.c	8.1 (Berkeley) 6/11/93
39*803a4704SPulkoMandy  *	$NetBSD: fpu_implode.c,v 1.8 2001/08/26 05:44:46 eeh Exp $
40*803a4704SPulkoMandy  */
41*803a4704SPulkoMandy 
42*803a4704SPulkoMandy #include <sys/cdefs.h>
43*803a4704SPulkoMandy 
44*803a4704SPulkoMandy /*
45*803a4704SPulkoMandy  * FPU subroutines: `implode' internal format numbers into the machine's
46*803a4704SPulkoMandy  * `packed binary' format.
47*803a4704SPulkoMandy  */
48*803a4704SPulkoMandy 
49*803a4704SPulkoMandy #include <sys/param.h>
50*803a4704SPulkoMandy #include <stdint.h>
51*803a4704SPulkoMandy 
52*803a4704SPulkoMandy #ifdef FPU_DEBUG
53*803a4704SPulkoMandy #include <stdio.h>
54*803a4704SPulkoMandy #endif
55*803a4704SPulkoMandy 
56*803a4704SPulkoMandy #include "fsr.h"
57*803a4704SPulkoMandy #include "ieee.h"
58*803a4704SPulkoMandy #include "instr.h"
59*803a4704SPulkoMandy 
60*803a4704SPulkoMandy #include "fpu_arith.h"
61*803a4704SPulkoMandy #include "fpu_emu.h"
62*803a4704SPulkoMandy #include "fpu_extern.h"
63*803a4704SPulkoMandy 
64*803a4704SPulkoMandy static int fpround(struct fpemu *, struct fpn *);
65*803a4704SPulkoMandy static int toinf(struct fpemu *, int);
66*803a4704SPulkoMandy 
67*803a4704SPulkoMandy #ifdef _KERNEL_MODE
68*803a4704SPulkoMandy extern void panic(const char*, ...);
69*803a4704SPulkoMandy #else
70*803a4704SPulkoMandy #include <OS.h>
71*803a4704SPulkoMandy #endif
72*803a4704SPulkoMandy 
73*803a4704SPulkoMandy /*
74*803a4704SPulkoMandy  * Round a number (algorithm from Motorola MC68882 manual, modified for
75*803a4704SPulkoMandy  * our internal format).  Set inexact exception if rounding is required.
76*803a4704SPulkoMandy  * Return true iff we rounded up.
77*803a4704SPulkoMandy  *
78*803a4704SPulkoMandy  * After rounding, we discard the guard and round bits by shifting right
79*803a4704SPulkoMandy  * 2 bits (a la fpu_shr(), but we do not bother with fp->fp_sticky).
80*803a4704SPulkoMandy  * This saves effort later.
81*803a4704SPulkoMandy  *
82*803a4704SPulkoMandy  * Note that we may leave the value 2.0 in fp->fp_mant; it is the caller's
83*803a4704SPulkoMandy  * responsibility to fix this if necessary.
84*803a4704SPulkoMandy  */
85*803a4704SPulkoMandy static int
fpround(struct fpemu * fe,struct fpn * fp)86*803a4704SPulkoMandy fpround(struct fpemu *fe, struct fpn *fp)
87*803a4704SPulkoMandy {
88*803a4704SPulkoMandy 	uint32_t m0, m1, m2, m3;
89*803a4704SPulkoMandy 	int gr, s;
90*803a4704SPulkoMandy 
91*803a4704SPulkoMandy 	m0 = fp->fp_mant[0];
92*803a4704SPulkoMandy 	m1 = fp->fp_mant[1];
93*803a4704SPulkoMandy 	m2 = fp->fp_mant[2];
94*803a4704SPulkoMandy 	m3 = fp->fp_mant[3];
95*803a4704SPulkoMandy 	gr = m3 & 3;
96*803a4704SPulkoMandy 	s = fp->fp_sticky;
97*803a4704SPulkoMandy 
98*803a4704SPulkoMandy 	/* mant >>= FP_NG */
99*803a4704SPulkoMandy 	m3 = (m3 >> FP_NG) | (m2 << (32 - FP_NG));
100*803a4704SPulkoMandy 	m2 = (m2 >> FP_NG) | (m1 << (32 - FP_NG));
101*803a4704SPulkoMandy 	m1 = (m1 >> FP_NG) | (m0 << (32 - FP_NG));
102*803a4704SPulkoMandy 	m0 >>= FP_NG;
103*803a4704SPulkoMandy 
104*803a4704SPulkoMandy 	if ((gr | s) == 0)	/* result is exact: no rounding needed */
105*803a4704SPulkoMandy 		goto rounddown;
106*803a4704SPulkoMandy 
107*803a4704SPulkoMandy 	fe->fe_cx |= FSR_NX;	/* inexact */
108*803a4704SPulkoMandy 
109*803a4704SPulkoMandy 	/* Go to rounddown to round down; break to round up. */
110*803a4704SPulkoMandy 	switch (FSR_GET_RD(fe->fe_fsr)) {
111*803a4704SPulkoMandy 	case FSR_RD_N:
112*803a4704SPulkoMandy 	default:
113*803a4704SPulkoMandy 		/*
114*803a4704SPulkoMandy 		 * Round only if guard is set (gr & 2).  If guard is set,
115*803a4704SPulkoMandy 		 * but round & sticky both clear, then we want to round
116*803a4704SPulkoMandy 		 * but have a tie, so round to even, i.e., add 1 iff odd.
117*803a4704SPulkoMandy 		 */
118*803a4704SPulkoMandy 		if ((gr & 2) == 0)
119*803a4704SPulkoMandy 			goto rounddown;
120*803a4704SPulkoMandy 		if ((gr & 1) || fp->fp_sticky || (m3 & 1))
121*803a4704SPulkoMandy 			break;
122*803a4704SPulkoMandy 		goto rounddown;
123*803a4704SPulkoMandy 
124*803a4704SPulkoMandy 	case FSR_RD_Z:
125*803a4704SPulkoMandy 		/* Round towards zero, i.e., down. */
126*803a4704SPulkoMandy 		goto rounddown;
127*803a4704SPulkoMandy 
128*803a4704SPulkoMandy 	case FSR_RD_NINF:
129*803a4704SPulkoMandy 		/* Round towards -Inf: up if negative, down if positive. */
130*803a4704SPulkoMandy 		if (fp->fp_sign)
131*803a4704SPulkoMandy 			break;
132*803a4704SPulkoMandy 		goto rounddown;
133*803a4704SPulkoMandy 
134*803a4704SPulkoMandy 	case FSR_RD_PINF:
135*803a4704SPulkoMandy 		/* Round towards +Inf: up if positive, down otherwise. */
136*803a4704SPulkoMandy 		if (!fp->fp_sign)
137*803a4704SPulkoMandy 			break;
138*803a4704SPulkoMandy 		goto rounddown;
139*803a4704SPulkoMandy 	}
140*803a4704SPulkoMandy 
141*803a4704SPulkoMandy 	/* Bump low bit of mantissa, with carry. */
142*803a4704SPulkoMandy 	FPU_ADDS(m3, m3, 1);
143*803a4704SPulkoMandy 	FPU_ADDCS(m2, m2, 0);
144*803a4704SPulkoMandy 	FPU_ADDCS(m1, m1, 0);
145*803a4704SPulkoMandy 	FPU_ADDC(m0, m0, 0);
146*803a4704SPulkoMandy 	fp->fp_mant[0] = m0;
147*803a4704SPulkoMandy 	fp->fp_mant[1] = m1;
148*803a4704SPulkoMandy 	fp->fp_mant[2] = m2;
149*803a4704SPulkoMandy 	fp->fp_mant[3] = m3;
150*803a4704SPulkoMandy 	return (1);
151*803a4704SPulkoMandy 
152*803a4704SPulkoMandy rounddown:
153*803a4704SPulkoMandy 	fp->fp_mant[0] = m0;
154*803a4704SPulkoMandy 	fp->fp_mant[1] = m1;
155*803a4704SPulkoMandy 	fp->fp_mant[2] = m2;
156*803a4704SPulkoMandy 	fp->fp_mant[3] = m3;
157*803a4704SPulkoMandy 	return (0);
158*803a4704SPulkoMandy }
159*803a4704SPulkoMandy 
160*803a4704SPulkoMandy /*
161*803a4704SPulkoMandy  * For overflow: return true if overflow is to go to +/-Inf, according
162*803a4704SPulkoMandy  * to the sign of the overflowing result.  If false, overflow is to go
163*803a4704SPulkoMandy  * to the largest magnitude value instead.
164*803a4704SPulkoMandy  */
165*803a4704SPulkoMandy static int
toinf(struct fpemu * fe,int sign)166*803a4704SPulkoMandy toinf(struct fpemu *fe, int sign)
167*803a4704SPulkoMandy {
168*803a4704SPulkoMandy 	int inf;
169*803a4704SPulkoMandy 
170*803a4704SPulkoMandy 	/* look at rounding direction */
171*803a4704SPulkoMandy 	switch (FSR_GET_RD(fe->fe_fsr)) {
172*803a4704SPulkoMandy 	default:
173*803a4704SPulkoMandy 	case FSR_RD_N:		/* the nearest value is always Inf */
174*803a4704SPulkoMandy 		inf = 1;
175*803a4704SPulkoMandy 		break;
176*803a4704SPulkoMandy 
177*803a4704SPulkoMandy 	case FSR_RD_Z:		/* toward 0 => never towards Inf */
178*803a4704SPulkoMandy 		inf = 0;
179*803a4704SPulkoMandy 		break;
180*803a4704SPulkoMandy 
181*803a4704SPulkoMandy 	case FSR_RD_PINF:	/* toward +Inf iff positive */
182*803a4704SPulkoMandy 		inf = sign == 0;
183*803a4704SPulkoMandy 		break;
184*803a4704SPulkoMandy 
185*803a4704SPulkoMandy 	case FSR_RD_NINF:	/* toward -Inf iff negative */
186*803a4704SPulkoMandy 		inf = sign;
187*803a4704SPulkoMandy 		break;
188*803a4704SPulkoMandy 	}
189*803a4704SPulkoMandy 	return (inf);
190*803a4704SPulkoMandy }
191*803a4704SPulkoMandy 
192*803a4704SPulkoMandy /*
193*803a4704SPulkoMandy  * fpn -> int (int value returned as return value).
194*803a4704SPulkoMandy  *
195*803a4704SPulkoMandy  * N.B.: this conversion always rounds towards zero (this is a peculiarity
196*803a4704SPulkoMandy  * of the SPARC instruction set).
197*803a4704SPulkoMandy  */
198*803a4704SPulkoMandy uint32_t
__fpu_ftoi(fe,fp)199*803a4704SPulkoMandy __fpu_ftoi(fe, fp)
200*803a4704SPulkoMandy 	struct fpemu *fe;
201*803a4704SPulkoMandy 	struct fpn *fp;
202*803a4704SPulkoMandy {
203*803a4704SPulkoMandy 	uint32_t i;
204*803a4704SPulkoMandy 	int sign, exp;
205*803a4704SPulkoMandy 
206*803a4704SPulkoMandy 	sign = fp->fp_sign;
207*803a4704SPulkoMandy 	switch (fp->fp_class) {
208*803a4704SPulkoMandy 	case FPC_ZERO:
209*803a4704SPulkoMandy 		return (0);
210*803a4704SPulkoMandy 
211*803a4704SPulkoMandy 	case FPC_NUM:
212*803a4704SPulkoMandy 		/*
213*803a4704SPulkoMandy 		 * If exp >= 2^32, overflow.  Otherwise shift value right
214*803a4704SPulkoMandy 		 * into last mantissa word (this will not exceed 0xffffffff),
215*803a4704SPulkoMandy 		 * shifting any guard and round bits out into the sticky
216*803a4704SPulkoMandy 		 * bit.  Then ``round'' towards zero, i.e., just set an
217*803a4704SPulkoMandy 		 * inexact exception if sticky is set (see round()).
218*803a4704SPulkoMandy 		 * If the result is > 0x80000000, or is positive and equals
219*803a4704SPulkoMandy 		 * 0x80000000, overflow; otherwise the last fraction word
220*803a4704SPulkoMandy 		 * is the result.
221*803a4704SPulkoMandy 		 */
222*803a4704SPulkoMandy 		if ((exp = fp->fp_exp) >= 32)
223*803a4704SPulkoMandy 			break;
224*803a4704SPulkoMandy 		/* NB: the following includes exp < 0 cases */
225*803a4704SPulkoMandy 		if (__fpu_shr(fp, FP_NMANT - 1 - exp) != 0)
226*803a4704SPulkoMandy 			fe->fe_cx |= FSR_NX;
227*803a4704SPulkoMandy 		i = fp->fp_mant[3];
228*803a4704SPulkoMandy 		if (i >= ((uint32_t)0x80000000 + sign))
229*803a4704SPulkoMandy 			break;
230*803a4704SPulkoMandy 		return (sign ? -i : i);
231*803a4704SPulkoMandy 
232*803a4704SPulkoMandy 	default:		/* Inf, qNaN, sNaN */
233*803a4704SPulkoMandy 		break;
234*803a4704SPulkoMandy 	}
235*803a4704SPulkoMandy 	/* overflow: replace any inexact exception with invalid */
236*803a4704SPulkoMandy 	fe->fe_cx = (fe->fe_cx & ~FSR_NX) | FSR_NV;
237*803a4704SPulkoMandy 	return (0x7fffffff + sign);
238*803a4704SPulkoMandy }
239*803a4704SPulkoMandy 
240*803a4704SPulkoMandy /*
241*803a4704SPulkoMandy  * fpn -> extended int (high bits of int value returned as return value).
242*803a4704SPulkoMandy  *
243*803a4704SPulkoMandy  * N.B.: this conversion always rounds towards zero (this is a peculiarity
244*803a4704SPulkoMandy  * of the SPARC instruction set).
245*803a4704SPulkoMandy  */
246*803a4704SPulkoMandy uint32_t
__fpu_ftox(fe,fp,res)247*803a4704SPulkoMandy __fpu_ftox(fe, fp, res)
248*803a4704SPulkoMandy 	struct fpemu *fe;
249*803a4704SPulkoMandy 	struct fpn *fp;
250*803a4704SPulkoMandy 	uint32_t *res;
251*803a4704SPulkoMandy {
252*803a4704SPulkoMandy 	uint64_t i;
253*803a4704SPulkoMandy 	int sign, exp;
254*803a4704SPulkoMandy 
255*803a4704SPulkoMandy 	sign = fp->fp_sign;
256*803a4704SPulkoMandy 	switch (fp->fp_class) {
257*803a4704SPulkoMandy 	case FPC_ZERO:
258*803a4704SPulkoMandy 		i = 0;
259*803a4704SPulkoMandy 		goto done;
260*803a4704SPulkoMandy 
261*803a4704SPulkoMandy 	case FPC_NUM:
262*803a4704SPulkoMandy 		/*
263*803a4704SPulkoMandy 		 * If exp >= 2^64, overflow.  Otherwise shift value
264*803a4704SPulkoMandy 		 * right into last mantissa word (this will not exceed
265*803a4704SPulkoMandy 		 * 0xffffffffffffffff), shifting any guard and round
266*803a4704SPulkoMandy 		 * bits out into the sticky bit.  Then ``round'' towards
267*803a4704SPulkoMandy 		 * zero, i.e., just set an inexact exception if sticky
268*803a4704SPulkoMandy 		 * is set (see round()).
269*803a4704SPulkoMandy 		 * If the result is > 0x8000000000000000, or is positive
270*803a4704SPulkoMandy 		 * and equals 0x8000000000000000, overflow; otherwise
271*803a4704SPulkoMandy 		 * the last fraction word is the result.
272*803a4704SPulkoMandy 		 */
273*803a4704SPulkoMandy 		if ((exp = fp->fp_exp) >= 64)
274*803a4704SPulkoMandy 			break;
275*803a4704SPulkoMandy 		/* NB: the following includes exp < 0 cases */
276*803a4704SPulkoMandy 		if (__fpu_shr(fp, FP_NMANT - 1 - exp) != 0)
277*803a4704SPulkoMandy 			fe->fe_cx |= FSR_NX;
278*803a4704SPulkoMandy 		i = ((uint64_t)fp->fp_mant[2]<<32)|fp->fp_mant[3];
279*803a4704SPulkoMandy 		if (i >= ((uint64_t)0x8000000000000000LL + sign))
280*803a4704SPulkoMandy 			break;
281*803a4704SPulkoMandy 		if (sign)
282*803a4704SPulkoMandy 			i = -i;
283*803a4704SPulkoMandy 		goto done;
284*803a4704SPulkoMandy 
285*803a4704SPulkoMandy 	default:		/* Inf, qNaN, sNaN */
286*803a4704SPulkoMandy 		break;
287*803a4704SPulkoMandy 	}
288*803a4704SPulkoMandy 	/* overflow: replace any inexact exception with invalid */
289*803a4704SPulkoMandy 	fe->fe_cx = (fe->fe_cx & ~FSR_NX) | FSR_NV;
290*803a4704SPulkoMandy 	i = 0x7fffffffffffffffLL + sign;
291*803a4704SPulkoMandy done:
292*803a4704SPulkoMandy 	res[1] = i & 0xffffffff;
293*803a4704SPulkoMandy 	return (i >> 32);
294*803a4704SPulkoMandy }
295*803a4704SPulkoMandy 
296*803a4704SPulkoMandy /*
297*803a4704SPulkoMandy  * fpn -> single (32 bit single returned as return value).
298*803a4704SPulkoMandy  * We assume <= 29 bits in a single-precision fraction (1.f part).
299*803a4704SPulkoMandy  */
300*803a4704SPulkoMandy uint32_t
__fpu_ftos(fe,fp)301*803a4704SPulkoMandy __fpu_ftos(fe, fp)
302*803a4704SPulkoMandy 	struct fpemu *fe;
303*803a4704SPulkoMandy 	struct fpn *fp;
304*803a4704SPulkoMandy {
305*803a4704SPulkoMandy 	uint32_t sign = fp->fp_sign << 31;
306*803a4704SPulkoMandy 	int exp;
307*803a4704SPulkoMandy 
308*803a4704SPulkoMandy #define	SNG_EXP(e)	((e) << SNG_FRACBITS)	/* makes e an exponent */
309*803a4704SPulkoMandy #define	SNG_MASK	(SNG_EXP(1) - 1)	/* mask for fraction */
310*803a4704SPulkoMandy 
311*803a4704SPulkoMandy 	/* Take care of non-numbers first. */
312*803a4704SPulkoMandy 	if (ISNAN(fp)) {
313*803a4704SPulkoMandy 		/*
314*803a4704SPulkoMandy 		 * Preserve upper bits of NaN, per SPARC V8 appendix N.
315*803a4704SPulkoMandy 		 * Note that fp->fp_mant[0] has the quiet bit set,
316*803a4704SPulkoMandy 		 * even if it is classified as a signalling NaN.
317*803a4704SPulkoMandy 		 */
318*803a4704SPulkoMandy 		(void) __fpu_shr(fp, FP_NMANT - 1 - SNG_FRACBITS);
319*803a4704SPulkoMandy 		exp = SNG_EXP_INFNAN;
320*803a4704SPulkoMandy 		goto done;
321*803a4704SPulkoMandy 	}
322*803a4704SPulkoMandy 	if (ISINF(fp))
323*803a4704SPulkoMandy 		return (sign | SNG_EXP(SNG_EXP_INFNAN));
324*803a4704SPulkoMandy 	if (ISZERO(fp))
325*803a4704SPulkoMandy 		return (sign);
326*803a4704SPulkoMandy 
327*803a4704SPulkoMandy 	/*
328*803a4704SPulkoMandy 	 * Normals (including subnormals).  Drop all the fraction bits
329*803a4704SPulkoMandy 	 * (including the explicit ``implied'' 1 bit) down into the
330*803a4704SPulkoMandy 	 * single-precision range.  If the number is subnormal, move
331*803a4704SPulkoMandy 	 * the ``implied'' 1 into the explicit range as well, and shift
332*803a4704SPulkoMandy 	 * right to introduce leading zeroes.  Rounding then acts
333*803a4704SPulkoMandy 	 * differently for normals and subnormals: the largest subnormal
334*803a4704SPulkoMandy 	 * may round to the smallest normal (1.0 x 2^minexp), or may
335*803a4704SPulkoMandy 	 * remain subnormal.  A number that is subnormal before rounding
336*803a4704SPulkoMandy 	 * will signal an underflow if the result is inexact or if underflow
337*803a4704SPulkoMandy 	 * traps are enabled.
338*803a4704SPulkoMandy 	 *
339*803a4704SPulkoMandy 	 * Rounding a normal, on the other hand, always produces another
340*803a4704SPulkoMandy 	 * normal (although either way the result might be too big for
341*803a4704SPulkoMandy 	 * single precision, and cause an overflow).  If rounding a
342*803a4704SPulkoMandy 	 * normal produces 2.0 in the fraction, we need not adjust that
343*803a4704SPulkoMandy 	 * fraction at all, since both 1.0 and 2.0 are zero under the
344*803a4704SPulkoMandy 	 * fraction mask.
345*803a4704SPulkoMandy 	 *
346*803a4704SPulkoMandy 	 * Note that the guard and round bits vanish from the number after
347*803a4704SPulkoMandy 	 * rounding.
348*803a4704SPulkoMandy 	 */
349*803a4704SPulkoMandy 	if ((exp = fp->fp_exp + SNG_EXP_BIAS) <= 0) {	/* subnormal */
350*803a4704SPulkoMandy 		/* -NG for g,r; -SNG_FRACBITS-exp for fraction */
351*803a4704SPulkoMandy 		(void) __fpu_shr(fp, FP_NMANT - FP_NG - SNG_FRACBITS - exp);
352*803a4704SPulkoMandy 		if (fpround(fe, fp) && fp->fp_mant[3] == SNG_EXP(1)) {
353*803a4704SPulkoMandy 			fe->fe_cx |= FSR_UF;
354*803a4704SPulkoMandy 			return (sign | SNG_EXP(1) | 0);
355*803a4704SPulkoMandy 		}
356*803a4704SPulkoMandy 		if ((fe->fe_cx & FSR_NX) ||
357*803a4704SPulkoMandy 		    (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
358*803a4704SPulkoMandy 			fe->fe_cx |= FSR_UF;
359*803a4704SPulkoMandy 		return (sign | SNG_EXP(0) | fp->fp_mant[3]);
360*803a4704SPulkoMandy 	}
361*803a4704SPulkoMandy 	/* -FP_NG for g,r; -1 for implied 1; -SNG_FRACBITS for fraction */
362*803a4704SPulkoMandy 	(void) __fpu_shr(fp, FP_NMANT - FP_NG - 1 - SNG_FRACBITS);
363*803a4704SPulkoMandy #ifdef DIAGNOSTIC
364*803a4704SPulkoMandy 	if ((fp->fp_mant[3] & SNG_EXP(1 << FP_NG)) == 0)
365*803a4704SPulkoMandy 		__utrap_panic("fpu_ftos");
366*803a4704SPulkoMandy #endif
367*803a4704SPulkoMandy 	if (fpround(fe, fp) && fp->fp_mant[3] == SNG_EXP(2))
368*803a4704SPulkoMandy 		exp++;
369*803a4704SPulkoMandy 	if (exp >= SNG_EXP_INFNAN) {
370*803a4704SPulkoMandy 		/* overflow to inf or to max single */
371*803a4704SPulkoMandy 		fe->fe_cx |= FSR_OF | FSR_NX;
372*803a4704SPulkoMandy 		if (toinf(fe, sign))
373*803a4704SPulkoMandy 			return (sign | SNG_EXP(SNG_EXP_INFNAN));
374*803a4704SPulkoMandy 		return (sign | SNG_EXP(SNG_EXP_INFNAN - 1) | SNG_MASK);
375*803a4704SPulkoMandy 	}
376*803a4704SPulkoMandy done:
377*803a4704SPulkoMandy 	/* phew, made it */
378*803a4704SPulkoMandy 	return (sign | SNG_EXP(exp) | (fp->fp_mant[3] & SNG_MASK));
379*803a4704SPulkoMandy }
380*803a4704SPulkoMandy 
381*803a4704SPulkoMandy /*
382*803a4704SPulkoMandy  * fpn -> double (32 bit high-order result returned; 32-bit low order result
383*803a4704SPulkoMandy  * left in res[1]).  Assumes <= 61 bits in double precision fraction.
384*803a4704SPulkoMandy  *
385*803a4704SPulkoMandy  * This code mimics fpu_ftos; see it for comments.
386*803a4704SPulkoMandy  */
387*803a4704SPulkoMandy uint32_t
__fpu_ftod(fe,fp,res)388*803a4704SPulkoMandy __fpu_ftod(fe, fp, res)
389*803a4704SPulkoMandy 	struct fpemu *fe;
390*803a4704SPulkoMandy 	struct fpn *fp;
391*803a4704SPulkoMandy 	uint32_t *res;
392*803a4704SPulkoMandy {
393*803a4704SPulkoMandy 	uint32_t sign = fp->fp_sign << 31;
394*803a4704SPulkoMandy 	int exp;
395*803a4704SPulkoMandy 
396*803a4704SPulkoMandy #define	DBL_EXP(e)	((e) << (DBL_FRACBITS & 31))
397*803a4704SPulkoMandy #define	DBL_MASK	(DBL_EXP(1) - 1)
398*803a4704SPulkoMandy 
399*803a4704SPulkoMandy 	if (ISNAN(fp)) {
400*803a4704SPulkoMandy 		(void) __fpu_shr(fp, FP_NMANT - 1 - DBL_FRACBITS);
401*803a4704SPulkoMandy 		exp = DBL_EXP_INFNAN;
402*803a4704SPulkoMandy 		goto done;
403*803a4704SPulkoMandy 	}
404*803a4704SPulkoMandy 	if (ISINF(fp)) {
405*803a4704SPulkoMandy 		sign |= DBL_EXP(DBL_EXP_INFNAN);
406*803a4704SPulkoMandy 		goto zero;
407*803a4704SPulkoMandy 	}
408*803a4704SPulkoMandy 	if (ISZERO(fp)) {
409*803a4704SPulkoMandy zero:		res[1] = 0;
410*803a4704SPulkoMandy 		return (sign);
411*803a4704SPulkoMandy 	}
412*803a4704SPulkoMandy 
413*803a4704SPulkoMandy 	if ((exp = fp->fp_exp + DBL_EXP_BIAS) <= 0) {
414*803a4704SPulkoMandy 		(void) __fpu_shr(fp, FP_NMANT - FP_NG - DBL_FRACBITS - exp);
415*803a4704SPulkoMandy 		if (fpround(fe, fp) && fp->fp_mant[2] == DBL_EXP(1)) {
416*803a4704SPulkoMandy 			fe->fe_cx |= FSR_UF;
417*803a4704SPulkoMandy 			res[1] = 0;
418*803a4704SPulkoMandy 			return (sign | DBL_EXP(1) | 0);
419*803a4704SPulkoMandy 		}
420*803a4704SPulkoMandy 		if ((fe->fe_cx & FSR_NX) ||
421*803a4704SPulkoMandy 		    (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
422*803a4704SPulkoMandy 			fe->fe_cx |= FSR_UF;
423*803a4704SPulkoMandy 		exp = 0;
424*803a4704SPulkoMandy 		goto done;
425*803a4704SPulkoMandy 	}
426*803a4704SPulkoMandy 	(void) __fpu_shr(fp, FP_NMANT - FP_NG - 1 - DBL_FRACBITS);
427*803a4704SPulkoMandy 	if (fpround(fe, fp) && fp->fp_mant[2] == DBL_EXP(2))
428*803a4704SPulkoMandy 		exp++;
429*803a4704SPulkoMandy 	if (exp >= DBL_EXP_INFNAN) {
430*803a4704SPulkoMandy 		fe->fe_cx |= FSR_OF | FSR_NX;
431*803a4704SPulkoMandy 		if (toinf(fe, sign)) {
432*803a4704SPulkoMandy 			res[1] = 0;
433*803a4704SPulkoMandy 			return (sign | DBL_EXP(DBL_EXP_INFNAN) | 0);
434*803a4704SPulkoMandy 		}
435*803a4704SPulkoMandy 		res[1] = ~0;
436*803a4704SPulkoMandy 		return (sign | DBL_EXP(DBL_EXP_INFNAN - 1) | DBL_MASK);
437*803a4704SPulkoMandy 	}
438*803a4704SPulkoMandy done:
439*803a4704SPulkoMandy 	res[1] = fp->fp_mant[3];
440*803a4704SPulkoMandy 	return (sign | DBL_EXP(exp) | (fp->fp_mant[2] & DBL_MASK));
441*803a4704SPulkoMandy }
442*803a4704SPulkoMandy 
443*803a4704SPulkoMandy /*
444*803a4704SPulkoMandy  * fpn -> extended (32 bit high-order result returned; low-order fraction
445*803a4704SPulkoMandy  * words left in res[1]..res[3]).  Like ftod, which is like ftos ... but
446*803a4704SPulkoMandy  * our internal format *is* extended precision, plus 2 bits for guard/round,
447*803a4704SPulkoMandy  * so we can avoid a small bit of work.
448*803a4704SPulkoMandy  */
449*803a4704SPulkoMandy uint32_t
__fpu_ftoq(fe,fp,res)450*803a4704SPulkoMandy __fpu_ftoq(fe, fp, res)
451*803a4704SPulkoMandy 	struct fpemu *fe;
452*803a4704SPulkoMandy 	struct fpn *fp;
453*803a4704SPulkoMandy 	uint32_t *res;
454*803a4704SPulkoMandy {
455*803a4704SPulkoMandy 	uint32_t sign = fp->fp_sign << 31;
456*803a4704SPulkoMandy 	int exp;
457*803a4704SPulkoMandy 
458*803a4704SPulkoMandy #define	EXT_EXP(e)	((e) << (EXT_FRACBITS & 31))
459*803a4704SPulkoMandy #define	EXT_MASK	(EXT_EXP(1) - 1)
460*803a4704SPulkoMandy 
461*803a4704SPulkoMandy 	if (ISNAN(fp)) {
462*803a4704SPulkoMandy 		(void) __fpu_shr(fp, 2);	/* since we are not rounding */
463*803a4704SPulkoMandy 		exp = EXT_EXP_INFNAN;
464*803a4704SPulkoMandy 		goto done;
465*803a4704SPulkoMandy 	}
466*803a4704SPulkoMandy 	if (ISINF(fp)) {
467*803a4704SPulkoMandy 		sign |= EXT_EXP(EXT_EXP_INFNAN);
468*803a4704SPulkoMandy 		goto zero;
469*803a4704SPulkoMandy 	}
470*803a4704SPulkoMandy 	if (ISZERO(fp)) {
471*803a4704SPulkoMandy zero:		res[1] = res[2] = res[3] = 0;
472*803a4704SPulkoMandy 		return (sign);
473*803a4704SPulkoMandy 	}
474*803a4704SPulkoMandy 
475*803a4704SPulkoMandy 	if ((exp = fp->fp_exp + EXT_EXP_BIAS) <= 0) {
476*803a4704SPulkoMandy 		(void) __fpu_shr(fp, FP_NMANT - FP_NG - EXT_FRACBITS - exp);
477*803a4704SPulkoMandy 		if (fpround(fe, fp) && fp->fp_mant[0] == EXT_EXP(1)) {
478*803a4704SPulkoMandy 			fe->fe_cx |= FSR_UF;
479*803a4704SPulkoMandy 			res[1] = res[2] = res[3] = 0;
480*803a4704SPulkoMandy 			return (sign | EXT_EXP(1) | 0);
481*803a4704SPulkoMandy 		}
482*803a4704SPulkoMandy 		if ((fe->fe_cx & FSR_NX) ||
483*803a4704SPulkoMandy 		    (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
484*803a4704SPulkoMandy 			fe->fe_cx |= FSR_UF;
485*803a4704SPulkoMandy 		exp = 0;
486*803a4704SPulkoMandy 		goto done;
487*803a4704SPulkoMandy 	}
488*803a4704SPulkoMandy 	/* Since internal == extended, no need to shift here. */
489*803a4704SPulkoMandy 	if (fpround(fe, fp) && fp->fp_mant[0] == EXT_EXP(2))
490*803a4704SPulkoMandy 		exp++;
491*803a4704SPulkoMandy 	if (exp >= EXT_EXP_INFNAN) {
492*803a4704SPulkoMandy 		fe->fe_cx |= FSR_OF | FSR_NX;
493*803a4704SPulkoMandy 		if (toinf(fe, sign)) {
494*803a4704SPulkoMandy 			res[1] = res[2] = res[3] = 0;
495*803a4704SPulkoMandy 			return (sign | EXT_EXP(EXT_EXP_INFNAN) | 0);
496*803a4704SPulkoMandy 		}
497*803a4704SPulkoMandy 		res[1] = res[2] = res[3] = ~0;
498*803a4704SPulkoMandy 		return (sign | EXT_EXP(EXT_EXP_INFNAN - 1) | EXT_MASK);
499*803a4704SPulkoMandy 	}
500*803a4704SPulkoMandy done:
501*803a4704SPulkoMandy 	res[1] = fp->fp_mant[1];
502*803a4704SPulkoMandy 	res[2] = fp->fp_mant[2];
503*803a4704SPulkoMandy 	res[3] = fp->fp_mant[3];
504*803a4704SPulkoMandy 	return (sign | EXT_EXP(exp) | (fp->fp_mant[0] & EXT_MASK));
505*803a4704SPulkoMandy }
506*803a4704SPulkoMandy 
507*803a4704SPulkoMandy /*
508*803a4704SPulkoMandy  * Implode an fpn, writing the result into the given space.
509*803a4704SPulkoMandy  */
510*803a4704SPulkoMandy void
__fpu_implode(fe,fp,type,space)511*803a4704SPulkoMandy __fpu_implode(fe, fp, type, space)
512*803a4704SPulkoMandy 	struct fpemu *fe;
513*803a4704SPulkoMandy 	struct fpn *fp;
514*803a4704SPulkoMandy 	int type;
515*803a4704SPulkoMandy 	uint32_t *space;
516*803a4704SPulkoMandy {
517*803a4704SPulkoMandy 
518*803a4704SPulkoMandy 	switch (type) {
519*803a4704SPulkoMandy 	case FTYPE_LNG:
520*803a4704SPulkoMandy 		space[0] = __fpu_ftox(fe, fp, space);
521*803a4704SPulkoMandy 		break;
522*803a4704SPulkoMandy 
523*803a4704SPulkoMandy 	case FTYPE_INT:
524*803a4704SPulkoMandy 		space[0] = __fpu_ftoi(fe, fp);
525*803a4704SPulkoMandy 		break;
526*803a4704SPulkoMandy 
527*803a4704SPulkoMandy 	case FTYPE_SNG:
528*803a4704SPulkoMandy 		space[0] = __fpu_ftos(fe, fp);
529*803a4704SPulkoMandy 		break;
530*803a4704SPulkoMandy 
531*803a4704SPulkoMandy 	case FTYPE_DBL:
532*803a4704SPulkoMandy 		space[0] = __fpu_ftod(fe, fp, space);
533*803a4704SPulkoMandy 		break;
534*803a4704SPulkoMandy 
535*803a4704SPulkoMandy 	case FTYPE_EXT:
536*803a4704SPulkoMandy 		/* funky rounding precision options ?? */
537*803a4704SPulkoMandy 		space[0] = __fpu_ftoq(fe, fp, space);
538*803a4704SPulkoMandy 		break;
539*803a4704SPulkoMandy 
540*803a4704SPulkoMandy 	default:
541*803a4704SPulkoMandy #ifdef _KERNEL_MODE
542*803a4704SPulkoMandy 		panic("fpu_implode");
543*803a4704SPulkoMandy #else
544*803a4704SPulkoMandy 		debugger("fpu_implode");
545*803a4704SPulkoMandy #endif
546*803a4704SPulkoMandy 	}
547*803a4704SPulkoMandy 	DPRINTF(FPE_REG, ("fpu_implode: %x %x %x %x\n",
548*803a4704SPulkoMandy 		space[0], space[1], space[2], space[3]));
549*803a4704SPulkoMandy }
550