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