1*4b64ca3eSmiod /* $OpenBSD: fpu_implode.c,v 1.11 2024/03/29 21:08:10 miod Exp $ */
2839f47eaSjason /* $NetBSD: fpu_implode.c,v 1.7 2000/08/03 18:32:08 eeh Exp $ */
3839f47eaSjason
4839f47eaSjason /*
5839f47eaSjason * Copyright (c) 1992, 1993
6839f47eaSjason * The Regents of the University of California. All rights reserved.
7839f47eaSjason *
8839f47eaSjason * This software was developed by the Computer Systems Engineering group
9839f47eaSjason * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10839f47eaSjason * contributed to Berkeley.
11839f47eaSjason *
12839f47eaSjason * All advertising materials mentioning features or use of this software
13839f47eaSjason * must display the following acknowledgement:
14839f47eaSjason * This product includes software developed by the University of
15839f47eaSjason * California, Lawrence Berkeley Laboratory.
16839f47eaSjason *
17839f47eaSjason * Redistribution and use in source and binary forms, with or without
18839f47eaSjason * modification, are permitted provided that the following conditions
19839f47eaSjason * are met:
20839f47eaSjason * 1. Redistributions of source code must retain the above copyright
21839f47eaSjason * notice, this list of conditions and the following disclaimer.
22839f47eaSjason * 2. Redistributions in binary form must reproduce the above copyright
23839f47eaSjason * notice, this list of conditions and the following disclaimer in the
24839f47eaSjason * documentation and/or other materials provided with the distribution.
2529295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
26839f47eaSjason * may be used to endorse or promote products derived from this software
27839f47eaSjason * without specific prior written permission.
28839f47eaSjason *
29839f47eaSjason * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30839f47eaSjason * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31839f47eaSjason * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32839f47eaSjason * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33839f47eaSjason * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34839f47eaSjason * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35839f47eaSjason * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36839f47eaSjason * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37839f47eaSjason * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38839f47eaSjason * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39839f47eaSjason * SUCH DAMAGE.
40839f47eaSjason *
41839f47eaSjason * @(#)fpu_implode.c 8.1 (Berkeley) 6/11/93
42839f47eaSjason */
43839f47eaSjason
44839f47eaSjason /*
45839f47eaSjason * FPU subroutines: `implode' internal format numbers into the machine's
46839f47eaSjason * `packed binary' format.
47839f47eaSjason */
48839f47eaSjason
49839f47eaSjason #include <sys/types.h>
50839f47eaSjason #include <sys/systm.h>
51839f47eaSjason
52*4b64ca3eSmiod #include <machine/fsr.h>
53839f47eaSjason #include <machine/ieee.h>
54839f47eaSjason #include <machine/instr.h>
55839f47eaSjason #include <machine/reg.h>
56839f47eaSjason
57839f47eaSjason #include <sparc64/fpu/fpu_arith.h>
58839f47eaSjason #include <sparc64/fpu/fpu_emu.h>
59839f47eaSjason #include <sparc64/fpu/fpu_extern.h>
60839f47eaSjason
612c7a42e9Smiod static int fpu_round(struct fpemu *, struct fpn *);
62c4071fd1Smillert static int toinf(struct fpemu *, int);
63839f47eaSjason
64839f47eaSjason /*
65839f47eaSjason * Round a number (algorithm from Motorola MC68882 manual, modified for
66839f47eaSjason * our internal format). Set inexact exception if rounding is required.
67839f47eaSjason * Return true iff we rounded up.
68839f47eaSjason *
69839f47eaSjason * After rounding, we discard the guard and round bits by shifting right
70839f47eaSjason * 2 bits (a la fpu_shr(), but we do not bother with fp->fp_sticky).
71839f47eaSjason * This saves effort later.
72839f47eaSjason *
73839f47eaSjason * Note that we may leave the value 2.0 in fp->fp_mant; it is the caller's
74839f47eaSjason * responsibility to fix this if necessary.
75839f47eaSjason */
76839f47eaSjason static int
fpu_round(struct fpemu * fe,struct fpn * fp)772c7a42e9Smiod fpu_round(struct fpemu *fe, struct fpn *fp)
78839f47eaSjason {
792c7a42e9Smiod u_int m0, m1, m2, m3;
802c7a42e9Smiod int gr, s;
81839f47eaSjason
82839f47eaSjason m0 = fp->fp_mant[0];
83839f47eaSjason m1 = fp->fp_mant[1];
84839f47eaSjason m2 = fp->fp_mant[2];
85839f47eaSjason m3 = fp->fp_mant[3];
86839f47eaSjason gr = m3 & 3;
87839f47eaSjason s = fp->fp_sticky;
88839f47eaSjason
89839f47eaSjason /* mant >>= FP_NG */
90839f47eaSjason m3 = (m3 >> FP_NG) | (m2 << (32 - FP_NG));
91839f47eaSjason m2 = (m2 >> FP_NG) | (m1 << (32 - FP_NG));
92839f47eaSjason m1 = (m1 >> FP_NG) | (m0 << (32 - FP_NG));
93839f47eaSjason m0 >>= FP_NG;
94839f47eaSjason
95839f47eaSjason if ((gr | s) == 0) /* result is exact: no rounding needed */
96839f47eaSjason goto rounddown;
97839f47eaSjason
98839f47eaSjason fe->fe_cx |= FSR_NX; /* inexact */
99839f47eaSjason
100839f47eaSjason /* Go to rounddown to round down; break to round up. */
101839f47eaSjason switch ((fe->fe_fsr >> FSR_RD_SHIFT) & FSR_RD_MASK) {
102839f47eaSjason
103839f47eaSjason case FSR_RD_RN:
104839f47eaSjason default:
105839f47eaSjason /*
106839f47eaSjason * Round only if guard is set (gr & 2). If guard is set,
107839f47eaSjason * but round & sticky both clear, then we want to round
108839f47eaSjason * but have a tie, so round to even, i.e., add 1 iff odd.
109839f47eaSjason */
110839f47eaSjason if ((gr & 2) == 0)
111839f47eaSjason goto rounddown;
112839f47eaSjason if ((gr & 1) || fp->fp_sticky || (m3 & 1))
113839f47eaSjason break;
114839f47eaSjason goto rounddown;
115839f47eaSjason
116839f47eaSjason case FSR_RD_RZ:
117839f47eaSjason /* Round towards zero, i.e., down. */
118839f47eaSjason goto rounddown;
119839f47eaSjason
120839f47eaSjason case FSR_RD_RM:
121839f47eaSjason /* Round towards -Inf: up if negative, down if positive. */
122839f47eaSjason if (fp->fp_sign)
123839f47eaSjason break;
124839f47eaSjason goto rounddown;
125839f47eaSjason
126839f47eaSjason case FSR_RD_RP:
127839f47eaSjason /* Round towards +Inf: up if positive, down otherwise. */
128839f47eaSjason if (!fp->fp_sign)
129839f47eaSjason break;
130839f47eaSjason goto rounddown;
131839f47eaSjason }
132839f47eaSjason
133839f47eaSjason /* Bump low bit of mantissa, with carry. */
134839f47eaSjason FPU_ADDS(m3, m3, 1);
135839f47eaSjason FPU_ADDCS(m2, m2, 0);
136839f47eaSjason FPU_ADDCS(m1, m1, 0);
137839f47eaSjason FPU_ADDC(m0, m0, 0);
138839f47eaSjason fp->fp_mant[0] = m0;
139839f47eaSjason fp->fp_mant[1] = m1;
140839f47eaSjason fp->fp_mant[2] = m2;
141839f47eaSjason fp->fp_mant[3] = m3;
142839f47eaSjason return (1);
143839f47eaSjason
144839f47eaSjason rounddown:
145839f47eaSjason fp->fp_mant[0] = m0;
146839f47eaSjason fp->fp_mant[1] = m1;
147839f47eaSjason fp->fp_mant[2] = m2;
148839f47eaSjason fp->fp_mant[3] = m3;
149839f47eaSjason return (0);
150839f47eaSjason }
151839f47eaSjason
152839f47eaSjason /*
153839f47eaSjason * For overflow: return true if overflow is to go to +/-Inf, according
154839f47eaSjason * to the sign of the overflowing result. If false, overflow is to go
155839f47eaSjason * to the largest magnitude value instead.
156839f47eaSjason */
157839f47eaSjason static int
toinf(struct fpemu * fe,int sign)158839f47eaSjason toinf(struct fpemu *fe, int sign)
159839f47eaSjason {
160839f47eaSjason int inf;
161839f47eaSjason
162839f47eaSjason /* look at rounding direction */
163839f47eaSjason switch ((fe->fe_fsr >> FSR_RD_SHIFT) & FSR_RD_MASK) {
164839f47eaSjason
165839f47eaSjason default:
166839f47eaSjason case FSR_RD_RN: /* the nearest value is always Inf */
167839f47eaSjason inf = 1;
168839f47eaSjason break;
169839f47eaSjason
170839f47eaSjason case FSR_RD_RZ: /* toward 0 => never towards Inf */
171839f47eaSjason inf = 0;
172839f47eaSjason break;
173839f47eaSjason
174839f47eaSjason case FSR_RD_RP: /* toward +Inf iff positive */
175839f47eaSjason inf = sign == 0;
176839f47eaSjason break;
177839f47eaSjason
178839f47eaSjason case FSR_RD_RM: /* toward -Inf iff negative */
179839f47eaSjason inf = sign;
180839f47eaSjason break;
181839f47eaSjason }
182839f47eaSjason return (inf);
183839f47eaSjason }
184839f47eaSjason
185839f47eaSjason /*
186839f47eaSjason * fpn -> int (int value returned as return value).
187839f47eaSjason *
188839f47eaSjason * N.B.: this conversion always rounds towards zero (this is a peculiarity
189839f47eaSjason * of the SPARC instruction set).
190839f47eaSjason */
191839f47eaSjason u_int
fpu_ftoi(struct fpemu * fe,struct fpn * fp)1922c7a42e9Smiod fpu_ftoi(struct fpemu *fe, struct fpn *fp)
193839f47eaSjason {
1942c7a42e9Smiod u_int i;
1952c7a42e9Smiod int sign, exp;
196839f47eaSjason
197839f47eaSjason sign = fp->fp_sign;
198839f47eaSjason switch (fp->fp_class) {
199839f47eaSjason
200839f47eaSjason case FPC_ZERO:
201839f47eaSjason return (0);
202839f47eaSjason
203839f47eaSjason case FPC_NUM:
204839f47eaSjason /*
205839f47eaSjason * If exp >= 2^32, overflow. Otherwise shift value right
206839f47eaSjason * into last mantissa word (this will not exceed 0xffffffff),
207839f47eaSjason * shifting any guard and round bits out into the sticky
208839f47eaSjason * bit. Then ``round'' towards zero, i.e., just set an
209f85e1829Skettenis * inexact exception if sticky is set (see fpu_round()).
210839f47eaSjason * If the result is > 0x80000000, or is positive and equals
211839f47eaSjason * 0x80000000, overflow; otherwise the last fraction word
212839f47eaSjason * is the result.
213839f47eaSjason */
214839f47eaSjason if ((exp = fp->fp_exp) >= 32)
215839f47eaSjason break;
216839f47eaSjason /* NB: the following includes exp < 0 cases */
217839f47eaSjason if (fpu_shr(fp, FP_NMANT - 1 - exp) != 0)
218839f47eaSjason fe->fe_cx |= FSR_NX;
219839f47eaSjason i = fp->fp_mant[3];
220839f47eaSjason if (i >= ((u_int)0x80000000 + sign))
221839f47eaSjason break;
222839f47eaSjason return (sign ? -i : i);
223839f47eaSjason
224839f47eaSjason default: /* Inf, qNaN, sNaN */
225839f47eaSjason break;
226839f47eaSjason }
227839f47eaSjason /* overflow: replace any inexact exception with invalid */
228839f47eaSjason fe->fe_cx = (fe->fe_cx & ~FSR_NX) | FSR_NV;
229839f47eaSjason return (0x7fffffff + sign);
230839f47eaSjason }
231839f47eaSjason
232839f47eaSjason /*
233839f47eaSjason * fpn -> extended int (high bits of int value returned as return value).
234839f47eaSjason *
235839f47eaSjason * N.B.: this conversion always rounds towards zero (this is a peculiarity
236839f47eaSjason * of the SPARC instruction set).
237839f47eaSjason */
238839f47eaSjason u_int
fpu_ftox(struct fpemu * fe,struct fpn * fp,u_int * res)2392c7a42e9Smiod fpu_ftox(struct fpemu *fe, struct fpn *fp, u_int *res)
240839f47eaSjason {
2412c7a42e9Smiod u_int64_t i;
2422c7a42e9Smiod int sign, exp;
243839f47eaSjason
244839f47eaSjason sign = fp->fp_sign;
245839f47eaSjason switch (fp->fp_class) {
246839f47eaSjason
247839f47eaSjason case FPC_ZERO:
2485dc24c7aSjason i = 0;
2495dc24c7aSjason goto out;
250839f47eaSjason
251839f47eaSjason case FPC_NUM:
252839f47eaSjason /*
253839f47eaSjason * If exp >= 2^64, overflow. Otherwise shift value right
254839f47eaSjason * into last mantissa word (this will not exceed 0xffffffffffffffff),
255839f47eaSjason * shifting any guard and round bits out into the sticky
256839f47eaSjason * bit. Then ``round'' towards zero, i.e., just set an
257f85e1829Skettenis * inexact exception if sticky is set (see fpu_round()).
258839f47eaSjason * If the result is > 0x8000000000000000, or is positive and equals
259839f47eaSjason * 0x8000000000000000, overflow; otherwise the last fraction word
260839f47eaSjason * is the result.
261839f47eaSjason */
262839f47eaSjason if ((exp = fp->fp_exp) >= 64)
263839f47eaSjason break;
264839f47eaSjason /* NB: the following includes exp < 0 cases */
265839f47eaSjason if (fpu_shr(fp, FP_NMANT - 1 - exp) != 0)
266839f47eaSjason fe->fe_cx |= FSR_NX;
267839f47eaSjason i = ((u_int64_t)fp->fp_mant[2]<<32)|fp->fp_mant[3];
268839f47eaSjason if (i >= ((u_int64_t)0x8000000000000000LL + sign))
269839f47eaSjason break;
2705dc24c7aSjason if (sign)
2715dc24c7aSjason i = -i;
2725dc24c7aSjason goto out;
273839f47eaSjason
274839f47eaSjason default: /* Inf, qNaN, sNaN */
275839f47eaSjason break;
276839f47eaSjason }
277839f47eaSjason /* overflow: replace any inexact exception with invalid */
278839f47eaSjason fe->fe_cx = (fe->fe_cx & ~FSR_NX) | FSR_NV;
2795dc24c7aSjason i = 0x7fffffffffffffffLL + sign;
2805dc24c7aSjason out:
2815dc24c7aSjason res[1] = i & 0xffffffff;
2825dc24c7aSjason return (i >> 32);
283839f47eaSjason }
284839f47eaSjason
285839f47eaSjason /*
286839f47eaSjason * fpn -> single (32 bit single returned as return value).
287839f47eaSjason * We assume <= 29 bits in a single-precision fraction (1.f part).
288839f47eaSjason */
289839f47eaSjason u_int
fpu_ftos(struct fpemu * fe,struct fpn * fp)2902c7a42e9Smiod fpu_ftos(struct fpemu *fe, struct fpn *fp)
291839f47eaSjason {
2922c7a42e9Smiod u_int sign = fp->fp_sign << 31;
2932c7a42e9Smiod int exp;
294839f47eaSjason
295839f47eaSjason #define SNG_EXP(e) ((e) << SNG_FRACBITS) /* makes e an exponent */
296839f47eaSjason #define SNG_MASK (SNG_EXP(1) - 1) /* mask for fraction */
297839f47eaSjason
298839f47eaSjason /* Take care of non-numbers first. */
299839f47eaSjason if (ISNAN(fp)) {
300839f47eaSjason /*
301839f47eaSjason * Preserve upper bits of NaN, per SPARC V8 appendix N.
302839f47eaSjason * Note that fp->fp_mant[0] has the quiet bit set,
303839f47eaSjason * even if it is classified as a signalling NaN.
304839f47eaSjason */
305839f47eaSjason (void) fpu_shr(fp, FP_NMANT - 1 - SNG_FRACBITS);
306839f47eaSjason exp = SNG_EXP_INFNAN;
307839f47eaSjason goto done;
308839f47eaSjason }
309839f47eaSjason if (ISINF(fp))
310839f47eaSjason return (sign | SNG_EXP(SNG_EXP_INFNAN));
311839f47eaSjason if (ISZERO(fp))
312839f47eaSjason return (sign);
313839f47eaSjason
314839f47eaSjason /*
315839f47eaSjason * Normals (including subnormals). Drop all the fraction bits
316839f47eaSjason * (including the explicit ``implied'' 1 bit) down into the
317839f47eaSjason * single-precision range. If the number is subnormal, move
318839f47eaSjason * the ``implied'' 1 into the explicit range as well, and shift
319839f47eaSjason * right to introduce leading zeroes. Rounding then acts
320839f47eaSjason * differently for normals and subnormals: the largest subnormal
321839f47eaSjason * may round to the smallest normal (1.0 x 2^minexp), or may
322839f47eaSjason * remain subnormal. In the latter case, signal an underflow
323839f47eaSjason * if the result was inexact or if underflow traps are enabled.
324839f47eaSjason *
325839f47eaSjason * Rounding a normal, on the other hand, always produces another
326839f47eaSjason * normal (although either way the result might be too big for
327839f47eaSjason * single precision, and cause an overflow). If rounding a
328839f47eaSjason * normal produces 2.0 in the fraction, we need not adjust that
329839f47eaSjason * fraction at all, since both 1.0 and 2.0 are zero under the
330839f47eaSjason * fraction mask.
331839f47eaSjason *
332839f47eaSjason * Note that the guard and round bits vanish from the number after
333839f47eaSjason * rounding.
334839f47eaSjason */
335839f47eaSjason if ((exp = fp->fp_exp + SNG_EXP_BIAS) <= 0) { /* subnormal */
336839f47eaSjason /* -NG for g,r; -SNG_FRACBITS-exp for fraction */
337839f47eaSjason (void) fpu_shr(fp, FP_NMANT - FP_NG - SNG_FRACBITS - exp);
33862832e9aSkettenis if (fpu_round(fe, fp) && fp->fp_mant[3] == SNG_EXP(1))
339839f47eaSjason return (sign | SNG_EXP(1) | 0);
340839f47eaSjason if ((fe->fe_cx & FSR_NX) ||
341839f47eaSjason (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
342839f47eaSjason fe->fe_cx |= FSR_UF;
343839f47eaSjason return (sign | SNG_EXP(0) | fp->fp_mant[3]);
344839f47eaSjason }
345839f47eaSjason /* -FP_NG for g,r; -1 for implied 1; -SNG_FRACBITS for fraction */
346839f47eaSjason (void) fpu_shr(fp, FP_NMANT - FP_NG - 1 - SNG_FRACBITS);
347839f47eaSjason #ifdef DIAGNOSTIC
348839f47eaSjason if ((fp->fp_mant[3] & SNG_EXP(1 << FP_NG)) == 0)
349839f47eaSjason panic("fpu_ftos");
350839f47eaSjason #endif
35162832e9aSkettenis if (fpu_round(fe, fp) && fp->fp_mant[3] == SNG_EXP(2))
352839f47eaSjason exp++;
353839f47eaSjason if (exp >= SNG_EXP_INFNAN) {
354839f47eaSjason /* overflow to inf or to max single */
355839f47eaSjason fe->fe_cx |= FSR_OF | FSR_NX;
356839f47eaSjason if (toinf(fe, sign))
357839f47eaSjason return (sign | SNG_EXP(SNG_EXP_INFNAN));
358839f47eaSjason return (sign | SNG_EXP(SNG_EXP_INFNAN - 1) | SNG_MASK);
359839f47eaSjason }
360839f47eaSjason done:
361839f47eaSjason /* phew, made it */
362839f47eaSjason return (sign | SNG_EXP(exp) | (fp->fp_mant[3] & SNG_MASK));
363839f47eaSjason }
364839f47eaSjason
365839f47eaSjason /*
366839f47eaSjason * fpn -> double (32 bit high-order result returned; 32-bit low order result
367839f47eaSjason * left in res[1]). Assumes <= 61 bits in double precision fraction.
368839f47eaSjason *
369839f47eaSjason * This code mimics fpu_ftos; see it for comments.
370839f47eaSjason */
371839f47eaSjason u_int
fpu_ftod(struct fpemu * fe,struct fpn * fp,u_int * res)3722c7a42e9Smiod fpu_ftod(struct fpemu *fe, struct fpn *fp, u_int *res)
373839f47eaSjason {
3742c7a42e9Smiod u_int sign = fp->fp_sign << 31;
3752c7a42e9Smiod int exp;
376839f47eaSjason
377839f47eaSjason #define DBL_EXP(e) ((e) << (DBL_FRACBITS & 31))
378839f47eaSjason #define DBL_MASK (DBL_EXP(1) - 1)
379839f47eaSjason
380839f47eaSjason if (ISNAN(fp)) {
381839f47eaSjason (void) fpu_shr(fp, FP_NMANT - 1 - DBL_FRACBITS);
382839f47eaSjason exp = DBL_EXP_INFNAN;
383839f47eaSjason goto done;
384839f47eaSjason }
385839f47eaSjason if (ISINF(fp)) {
386839f47eaSjason sign |= DBL_EXP(DBL_EXP_INFNAN);
387839f47eaSjason goto zero;
388839f47eaSjason }
389839f47eaSjason if (ISZERO(fp)) {
390839f47eaSjason zero: res[1] = 0;
391839f47eaSjason return (sign);
392839f47eaSjason }
393839f47eaSjason
394839f47eaSjason if ((exp = fp->fp_exp + DBL_EXP_BIAS) <= 0) {
395839f47eaSjason (void) fpu_shr(fp, FP_NMANT - FP_NG - DBL_FRACBITS - exp);
39662832e9aSkettenis if (fpu_round(fe, fp) && fp->fp_mant[2] == DBL_EXP(1)) {
397839f47eaSjason res[1] = 0;
398839f47eaSjason return (sign | DBL_EXP(1) | 0);
399839f47eaSjason }
400839f47eaSjason if ((fe->fe_cx & FSR_NX) ||
401839f47eaSjason (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
402839f47eaSjason fe->fe_cx |= FSR_UF;
403839f47eaSjason exp = 0;
404839f47eaSjason goto done;
405839f47eaSjason }
406839f47eaSjason (void) fpu_shr(fp, FP_NMANT - FP_NG - 1 - DBL_FRACBITS);
40762832e9aSkettenis if (fpu_round(fe, fp) && fp->fp_mant[2] == DBL_EXP(2))
408839f47eaSjason exp++;
409839f47eaSjason if (exp >= DBL_EXP_INFNAN) {
410839f47eaSjason fe->fe_cx |= FSR_OF | FSR_NX;
411839f47eaSjason if (toinf(fe, sign)) {
412839f47eaSjason res[1] = 0;
413839f47eaSjason return (sign | DBL_EXP(DBL_EXP_INFNAN) | 0);
414839f47eaSjason }
415839f47eaSjason res[1] = ~0;
416839f47eaSjason return (sign | DBL_EXP(DBL_EXP_INFNAN) | DBL_MASK);
417839f47eaSjason }
418839f47eaSjason done:
419839f47eaSjason res[1] = fp->fp_mant[3];
420839f47eaSjason return (sign | DBL_EXP(exp) | (fp->fp_mant[2] & DBL_MASK));
421839f47eaSjason }
422839f47eaSjason
423839f47eaSjason /*
424839f47eaSjason * fpn -> extended (32 bit high-order result returned; low-order fraction
425839f47eaSjason * words left in res[1]..res[3]). Like ftod, which is like ftos ... but
426839f47eaSjason * our internal format *is* extended precision, plus 2 bits for guard/round,
427839f47eaSjason * so we can avoid a small bit of work.
428839f47eaSjason */
429839f47eaSjason u_int
fpu_ftoq(struct fpemu * fe,struct fpn * fp,u_int * res)4302c7a42e9Smiod fpu_ftoq(struct fpemu *fe, struct fpn *fp, u_int *res)
431839f47eaSjason {
4322c7a42e9Smiod u_int sign = fp->fp_sign << 31;
4332c7a42e9Smiod int exp;
434839f47eaSjason
435839f47eaSjason #define EXT_EXP(e) ((e) << (EXT_FRACBITS & 31))
436839f47eaSjason #define EXT_MASK (EXT_EXP(1) - 1)
437839f47eaSjason
438839f47eaSjason if (ISNAN(fp)) {
439839f47eaSjason (void) fpu_shr(fp, 2); /* since we are not rounding */
440839f47eaSjason exp = EXT_EXP_INFNAN;
441839f47eaSjason goto done;
442839f47eaSjason }
443839f47eaSjason if (ISINF(fp)) {
444839f47eaSjason sign |= EXT_EXP(EXT_EXP_INFNAN);
445839f47eaSjason goto zero;
446839f47eaSjason }
447839f47eaSjason if (ISZERO(fp)) {
448839f47eaSjason zero: res[1] = res[2] = res[3] = 0;
449839f47eaSjason return (sign);
450839f47eaSjason }
451839f47eaSjason
452839f47eaSjason if ((exp = fp->fp_exp + EXT_EXP_BIAS) <= 0) {
453839f47eaSjason (void) fpu_shr(fp, FP_NMANT - FP_NG - EXT_FRACBITS - exp);
45462832e9aSkettenis if (fpu_round(fe, fp) && fp->fp_mant[0] == EXT_EXP(1)) {
455839f47eaSjason res[1] = res[2] = res[3] = 0;
456839f47eaSjason return (sign | EXT_EXP(1) | 0);
457839f47eaSjason }
458839f47eaSjason if ((fe->fe_cx & FSR_NX) ||
459839f47eaSjason (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
460839f47eaSjason fe->fe_cx |= FSR_UF;
461839f47eaSjason exp = 0;
462839f47eaSjason goto done;
463839f47eaSjason }
464839f47eaSjason /* Since internal == extended, no need to shift here. */
46562832e9aSkettenis if (fpu_round(fe, fp) && fp->fp_mant[0] == EXT_EXP(2))
466839f47eaSjason exp++;
467839f47eaSjason if (exp >= EXT_EXP_INFNAN) {
468839f47eaSjason fe->fe_cx |= FSR_OF | FSR_NX;
469839f47eaSjason if (toinf(fe, sign)) {
470839f47eaSjason res[1] = res[2] = res[3] = 0;
471839f47eaSjason return (sign | EXT_EXP(EXT_EXP_INFNAN) | 0);
472839f47eaSjason }
473839f47eaSjason res[1] = res[2] = res[3] = ~0;
474839f47eaSjason return (sign | EXT_EXP(EXT_EXP_INFNAN) | EXT_MASK);
475839f47eaSjason }
476839f47eaSjason done:
477839f47eaSjason res[1] = fp->fp_mant[1];
478839f47eaSjason res[2] = fp->fp_mant[2];
479839f47eaSjason res[3] = fp->fp_mant[3];
480839f47eaSjason return (sign | EXT_EXP(exp) | (fp->fp_mant[0] & EXT_MASK));
481839f47eaSjason }
482839f47eaSjason
483839f47eaSjason /*
484839f47eaSjason * Implode an fpn, writing the result into the given space.
485839f47eaSjason */
486839f47eaSjason void
fpu_implode(struct fpemu * fe,struct fpn * fp,int type,u_int * space)4872c7a42e9Smiod fpu_implode(struct fpemu *fe, struct fpn *fp, int type, u_int *space)
488839f47eaSjason {
4895ebf65afSjason DPRINTF(FPE_INSN, ("fpu_implode: "));
490839f47eaSjason switch (type) {
491839f47eaSjason
492839f47eaSjason case FTYPE_LNG:
493839f47eaSjason space[0] = fpu_ftox(fe, fp, space);
4945ebf65afSjason DPRINTF(FPE_INSN, ("LNG %x %x\n", space[0], space[1]));
495839f47eaSjason break;
496839f47eaSjason
497839f47eaSjason case FTYPE_INT:
498839f47eaSjason space[0] = fpu_ftoi(fe, fp);
4995ebf65afSjason DPRINTF(FPE_INSN, ("INT %x\n", space[0]));
500839f47eaSjason break;
501839f47eaSjason
502839f47eaSjason case FTYPE_SNG:
503839f47eaSjason space[0] = fpu_ftos(fe, fp);
5045ebf65afSjason DPRINTF(FPE_INSN, ("SNG %x\n", space[0]));
505839f47eaSjason break;
506839f47eaSjason
507839f47eaSjason case FTYPE_DBL:
508839f47eaSjason space[0] = fpu_ftod(fe, fp, space);
5095ebf65afSjason DPRINTF(FPE_INSN, ("DBL %x %x\n", space[0], space[1]));
510839f47eaSjason break;
511839f47eaSjason
512839f47eaSjason case FTYPE_EXT:
513839f47eaSjason /* funky rounding precision options ?? */
514839f47eaSjason space[0] = fpu_ftoq(fe, fp, space);
5155ebf65afSjason DPRINTF(FPE_INSN, ("EXT %x %x %x %x\n", space[0], space[1],
5165ebf65afSjason space[2], space[3]));
517839f47eaSjason break;
518839f47eaSjason
519839f47eaSjason default:
520839f47eaSjason panic("fpu_implode");
521839f47eaSjason }
522839f47eaSjason }
523