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