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