155113Storek /*
2*63319Sbostic * Copyright (c) 1992, 1993
3*63319Sbostic * The Regents of the University of California. All rights reserved.
455113Storek *
555113Storek * This software was developed by the Computer Systems Engineering group
655113Storek * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
755113Storek * contributed to Berkeley.
855113Storek *
955500Sbostic * All advertising materials mentioning features or use of this software
1055500Sbostic * must display the following acknowledgement:
1155500Sbostic * This product includes software developed by the University of
1259195Storek * California, Lawrence Berkeley Laboratory.
1355500Sbostic *
1455113Storek * %sccs.include.redist.c%
1555113Storek *
16*63319Sbostic * @(#)fpu_implode.c 8.1 (Berkeley) 06/11/93
1755113Storek *
1859195Storek * from: $Header: fpu_implode.c,v 1.5 92/11/26 01:39:49 torek Exp $
1955113Storek */
2055113Storek
2155113Storek /*
2255113Storek * FPU subroutines: `implode' internal format numbers into the machine's
2355113Storek * `packed binary' format.
2455113Storek */
2555113Storek
2656537Sbostic #include <sys/types.h>
2755113Storek
2856537Sbostic #include <machine/ieee.h>
2956537Sbostic #include <machine/instr.h>
3056537Sbostic #include <machine/reg.h>
3155113Storek
3256537Sbostic #include <sparc/fpu/fpu_arith.h>
3356537Sbostic #include <sparc/fpu/fpu_emu.h>
3455113Storek
3555113Storek /*
3655113Storek * Round a number (algorithm from Motorola MC68882 manual, modified for
3755113Storek * our internal format). Set inexact exception if rounding is required.
3855113Storek * Return true iff we rounded up.
3955113Storek *
4055113Storek * After rounding, we discard the guard and round bits by shifting right
4155113Storek * 2 bits (a la fpu_shr(), but we do not bother with fp->fp_sticky).
4255113Storek * This saves effort later.
4355113Storek *
4455113Storek * Note that we may leave the value 2.0 in fp->fp_mant; it is the caller's
4555113Storek * responsibility to fix this if necessary.
4655113Storek */
4755113Storek static int
round(register struct fpemu * fe,register struct fpn * fp)4855113Storek round(register struct fpemu *fe, register struct fpn *fp)
4955113Storek {
5055113Storek register u_int m0, m1, m2, m3;
5155113Storek register int gr, s, ret;
5255113Storek
5355113Storek m0 = fp->fp_mant[0];
5455113Storek m1 = fp->fp_mant[1];
5555113Storek m2 = fp->fp_mant[2];
5655113Storek m3 = fp->fp_mant[3];
5755113Storek gr = m3 & 3;
5855113Storek s = fp->fp_sticky;
5955113Storek
6055113Storek /* mant >>= FP_NG */
6155113Storek m3 = (m3 >> FP_NG) | (m2 << (32 - FP_NG));
6255113Storek m2 = (m2 >> FP_NG) | (m1 << (32 - FP_NG));
6355113Storek m1 = (m1 >> FP_NG) | (m0 << (32 - FP_NG));
6455113Storek m0 >>= FP_NG;
6555113Storek
6655113Storek if ((gr | s) == 0) /* result is exact: no rounding needed */
6755113Storek goto rounddown;
6855113Storek
6955113Storek fe->fe_cx |= FSR_NX; /* inexact */
7055113Storek
7155113Storek /* Go to rounddown to round down; break to round up. */
7255113Storek switch ((fe->fe_fsr >> FSR_RD_SHIFT) & FSR_RD_MASK) {
7355113Storek
7455113Storek case FSR_RD_RN:
7555113Storek default:
7655113Storek /*
7755113Storek * Round only if guard is set (gr & 2). If guard is set,
7855113Storek * but round & sticky both clear, then we want to round
7955113Storek * but have a tie, so round to even, i.e., add 1 iff odd.
8055113Storek */
8155113Storek if ((gr & 2) == 0)
8255113Storek goto rounddown;
8355113Storek if ((gr & 1) || fp->fp_sticky || (m3 & 1))
8455113Storek break;
8555113Storek goto rounddown;
8655113Storek
8755113Storek case FSR_RD_RZ:
8855113Storek /* Round towards zero, i.e., down. */
8955113Storek goto rounddown;
9055113Storek
9155113Storek case FSR_RD_RM:
9255113Storek /* Round towards -Inf: up if negative, down if positive. */
9355113Storek if (fp->fp_sign)
9455113Storek break;
9555113Storek goto rounddown;
9655113Storek
9755113Storek case FSR_RD_RP:
9855113Storek /* Round towards +Inf: up if positive, down otherwise. */
9955113Storek if (!fp->fp_sign)
10055113Storek break;
10155113Storek goto rounddown;
10255113Storek }
10355113Storek
10455113Storek /* Bump low bit of mantissa, with carry. */
10555113Storek #ifdef sparc /* ``cheating'' (left out FPU_DECL_CARRY; know this is faster) */
10655113Storek FPU_ADDS(m3, m3, 1);
10755113Storek FPU_ADDCS(m2, m2, 0);
10855113Storek FPU_ADDCS(m1, m1, 0);
10955113Storek FPU_ADDC(m0, m0, 0);
11055113Storek #else
11155113Storek if (++m3 == 0 && ++m2 == 0 && ++m1 == 0)
11255113Storek m0++;
11355113Storek #endif
11455113Storek fp->fp_mant[0] = m0;
11555113Storek fp->fp_mant[1] = m1;
11655113Storek fp->fp_mant[2] = m2;
11755113Storek fp->fp_mant[3] = m3;
11855113Storek return (1);
11955113Storek
12055113Storek rounddown:
12155113Storek fp->fp_mant[0] = m0;
12255113Storek fp->fp_mant[1] = m1;
12355113Storek fp->fp_mant[2] = m2;
12455113Storek fp->fp_mant[3] = m3;
12555113Storek return (0);
12655113Storek }
12755113Storek
12855113Storek /*
12955113Storek * For overflow: return true if overflow is to go to +/-Inf, according
13055113Storek * to the sign of the overflowing result. If false, overflow is to go
13155113Storek * to the largest magnitude value instead.
13255113Storek */
13355113Storek static int
toinf(struct fpemu * fe,int sign)13455113Storek toinf(struct fpemu *fe, int sign)
13555113Storek {
13655113Storek int inf;
13755113Storek
13855113Storek /* look at rounding direction */
13955113Storek switch ((fe->fe_fsr >> FSR_RD_SHIFT) & FSR_RD_MASK) {
14055113Storek
14155113Storek default:
14255113Storek case FSR_RD_RN: /* the nearest value is always Inf */
14355113Storek inf = 1;
14455113Storek break;
14555113Storek
14655113Storek case FSR_RD_RZ: /* toward 0 => never towards Inf */
14755113Storek inf = 0;
14855113Storek break;
14955113Storek
15055113Storek case FSR_RD_RP: /* toward +Inf iff positive */
15155113Storek inf = sign == 0;
15255113Storek break;
15355113Storek
15455113Storek case FSR_RD_RM: /* toward -Inf iff negative */
15555113Storek inf = sign;
15655113Storek break;
15755113Storek }
15855113Storek return (inf);
15955113Storek }
16055113Storek
16155113Storek /*
16255113Storek * fpn -> int (int value returned as return value).
16355113Storek *
16455113Storek * N.B.: this conversion always rounds towards zero (this is a peculiarity
16555113Storek * of the SPARC instruction set).
16655113Storek */
16755113Storek u_int
fpu_ftoi(fe,fp)16855113Storek fpu_ftoi(fe, fp)
16955113Storek struct fpemu *fe;
17055113Storek register struct fpn *fp;
17155113Storek {
17255113Storek register u_int i;
17355113Storek register int sign, exp;
17455113Storek
17555113Storek sign = fp->fp_sign;
17655113Storek switch (fp->fp_class) {
17755113Storek
17855113Storek case FPC_ZERO:
17955113Storek return (0);
18055113Storek
18155113Storek case FPC_NUM:
18255113Storek /*
18355113Storek * If exp >= 2^32, overflow. Otherwise shift value right
18455113Storek * into last mantissa word (this will not exceed 0xffffffff),
18555113Storek * shifting any guard and round bits out into the sticky
18655113Storek * bit. Then ``round'' towards zero, i.e., just set an
18755113Storek * inexact exception if sticky is set (see round()).
18855113Storek * If the result is > 0x80000000, or is positive and equals
18955113Storek * 0x80000000, overflow; otherwise the last fraction word
19055113Storek * is the result.
19155113Storek */
19255113Storek if ((exp = fp->fp_exp) >= 32)
19355113Storek break;
19455113Storek /* NB: the following includes exp < 0 cases */
19555113Storek if (fpu_shr(fp, FP_NMANT - 1 - exp) != 0)
19655113Storek fe->fe_cx |= FSR_NX;
19755113Storek i = fp->fp_mant[3];
19855113Storek if (i >= ((u_int)0x80000000 + sign))
19955113Storek break;
20055113Storek return (sign ? -i : i);
20155113Storek
20255113Storek default: /* Inf, qNaN, sNaN */
20355113Storek break;
20455113Storek }
20555113Storek /* overflow: replace any inexact exception with invalid */
20655113Storek fe->fe_cx = (fe->fe_cx & ~FSR_NX) | FSR_NV;
20755113Storek return (0x7fffffff + sign);
20855113Storek }
20955113Storek
21055113Storek /*
21155113Storek * fpn -> single (32 bit single returned as return value).
21255113Storek * We assume <= 29 bits in a single-precision fraction (1.f part).
21355113Storek */
21455113Storek u_int
fpu_ftos(fe,fp)21555113Storek fpu_ftos(fe, fp)
21655113Storek struct fpemu *fe;
21755113Storek register struct fpn *fp;
21855113Storek {
21955113Storek register u_int sign = fp->fp_sign << 31;
22055113Storek register int exp;
22155113Storek
22255113Storek #define SNG_EXP(e) ((e) << SNG_FRACBITS) /* makes e an exponent */
22355113Storek #define SNG_MASK (SNG_EXP(1) - 1) /* mask for fraction */
22455113Storek
22555113Storek /* Take care of non-numbers first. */
22655113Storek if (ISNAN(fp)) {
22755113Storek /*
22855113Storek * Preserve upper bits of NaN, per SPARC V8 appendix N.
22955113Storek * Note that fp->fp_mant[0] has the quiet bit set,
23055113Storek * even if it is classified as a signalling NaN.
23155113Storek */
23255113Storek (void) fpu_shr(fp, FP_NMANT - 1 - SNG_FRACBITS);
23355113Storek exp = SNG_EXP_INFNAN;
23455113Storek goto done;
23555113Storek }
23655113Storek if (ISINF(fp))
23755113Storek return (sign | SNG_EXP(SNG_EXP_INFNAN));
23855113Storek if (ISZERO(fp))
23955113Storek return (sign);
24055113Storek
24155113Storek /*
24255113Storek * Normals (including subnormals). Drop all the fraction bits
24355113Storek * (including the explicit ``implied'' 1 bit) down into the
24455113Storek * single-precision range. If the number is subnormal, move
24555113Storek * the ``implied'' 1 into the explicit range as well, and shift
24655113Storek * right to introduce leading zeroes. Rounding then acts
24755113Storek * differently for normals and subnormals: the largest subnormal
24855113Storek * may round to the smallest normal (1.0 x 2^minexp), or may
24955113Storek * remain subnormal. In the latter case, signal an underflow
25055113Storek * if the result was inexact or if underflow traps are enabled.
25155113Storek *
25255113Storek * Rounding a normal, on the other hand, always produces another
25355113Storek * normal (although either way the result might be too big for
25455113Storek * single precision, and cause an overflow). If rounding a
25555113Storek * normal produces 2.0 in the fraction, we need not adjust that
25655113Storek * fraction at all, since both 1.0 and 2.0 are zero under the
25755113Storek * fraction mask.
25855113Storek *
25955113Storek * Note that the guard and round bits vanish from the number after
26055113Storek * rounding.
26155113Storek */
26255113Storek if ((exp = fp->fp_exp + SNG_EXP_BIAS) <= 0) { /* subnormal */
26355113Storek /* -NG for g,r; -SNG_FRACBITS-exp for fraction */
26455113Storek (void) fpu_shr(fp, FP_NMANT - FP_NG - SNG_FRACBITS - exp);
26555113Storek if (round(fe, fp) && fp->fp_mant[3] == SNG_EXP(1))
26655113Storek return (sign | SNG_EXP(1) | 0);
26755113Storek if ((fe->fe_cx & FSR_NX) ||
26855113Storek (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
26955113Storek fe->fe_cx |= FSR_UF;
27055113Storek return (sign | SNG_EXP(0) | fp->fp_mant[3]);
27155113Storek }
27255113Storek /* -FP_NG for g,r; -1 for implied 1; -SNG_FRACBITS for fraction */
27355113Storek (void) fpu_shr(fp, FP_NMANT - FP_NG - 1 - SNG_FRACBITS);
27455113Storek #ifdef DIAGNOSTIC
27555113Storek if ((fp->fp_mant[3] & SNG_EXP(1 << FP_NG)) == 0)
27655113Storek panic("fpu_ftos");
27755113Storek #endif
27855113Storek if (round(fe, fp) && fp->fp_mant[3] == SNG_EXP(2))
27955113Storek exp++;
28055113Storek if (exp >= SNG_EXP_INFNAN) {
28155113Storek /* overflow to inf or to max single */
28255113Storek fe->fe_cx |= FSR_OF | FSR_NX;
28355113Storek if (toinf(fe, sign))
28455113Storek return (sign | SNG_EXP(SNG_EXP_INFNAN));
28555113Storek return (sign | SNG_EXP(SNG_EXP_INFNAN - 1) | SNG_MASK);
28655113Storek }
28755113Storek done:
28855113Storek /* phew, made it */
28955113Storek return (sign | SNG_EXP(exp) | (fp->fp_mant[3] & SNG_MASK));
29055113Storek }
29155113Storek
29255113Storek /*
29355113Storek * fpn -> double (32 bit high-order result returned; 32-bit low order result
29455113Storek * left in res[1]). Assumes <= 61 bits in double precision fraction.
29555113Storek *
29655113Storek * This code mimics fpu_ftos; see it for comments.
29755113Storek */
29855113Storek u_int
fpu_ftod(fe,fp,res)29955113Storek fpu_ftod(fe, fp, res)
30055113Storek struct fpemu *fe;
30155113Storek register struct fpn *fp;
30255113Storek u_int *res;
30355113Storek {
30455113Storek register u_int sign = fp->fp_sign << 31;
30555113Storek register int exp;
30655113Storek
30755113Storek #define DBL_EXP(e) ((e) << (DBL_FRACBITS & 31))
30855113Storek #define DBL_MASK (DBL_EXP(1) - 1)
30955113Storek
31055113Storek if (ISNAN(fp)) {
31155113Storek (void) fpu_shr(fp, FP_NMANT - 1 - DBL_FRACBITS);
31255113Storek exp = DBL_EXP_INFNAN;
31355113Storek goto done;
31455113Storek }
31555113Storek if (ISINF(fp)) {
31655113Storek sign |= DBL_EXP(DBL_EXP_INFNAN);
31755113Storek goto zero;
31855113Storek }
31955113Storek if (ISZERO(fp)) {
32055113Storek zero: res[1] = 0;
32155113Storek return (sign);
32255113Storek }
32355113Storek
32455113Storek if ((exp = fp->fp_exp + DBL_EXP_BIAS) <= 0) {
32555113Storek (void) fpu_shr(fp, FP_NMANT - FP_NG - DBL_FRACBITS - exp);
32655113Storek if (round(fe, fp) && fp->fp_mant[2] == DBL_EXP(1)) {
32755113Storek res[1] = 0;
32855113Storek return (sign | DBL_EXP(1) | 0);
32955113Storek }
33055113Storek if ((fe->fe_cx & FSR_NX) ||
33155113Storek (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
33255113Storek fe->fe_cx |= FSR_UF;
33355113Storek exp = 0;
33455113Storek goto done;
33555113Storek }
33655113Storek (void) fpu_shr(fp, FP_NMANT - FP_NG - 1 - DBL_FRACBITS);
33755113Storek if (round(fe, fp) && fp->fp_mant[2] == DBL_EXP(2))
33855113Storek exp++;
33955113Storek if (exp >= DBL_EXP_INFNAN) {
34055113Storek fe->fe_cx |= FSR_OF | FSR_NX;
34155113Storek if (toinf(fe, sign)) {
34255113Storek res[1] = 0;
34355113Storek return (sign | DBL_EXP(DBL_EXP_INFNAN) | 0);
34455113Storek }
34555113Storek res[1] = ~0;
34655113Storek return (sign | DBL_EXP(DBL_EXP_INFNAN) | DBL_MASK);
34755113Storek }
34855113Storek done:
34955113Storek res[1] = fp->fp_mant[3];
35055113Storek return (sign | DBL_EXP(exp) | (fp->fp_mant[2] & DBL_MASK));
35155113Storek }
35255113Storek
35355113Storek /*
35455113Storek * fpn -> extended (32 bit high-order result returned; low-order fraction
35555113Storek * words left in res[1]..res[3]). Like ftod, which is like ftos ... but
35655113Storek * our internal format *is* extended precision, plus 2 bits for guard/round,
35755113Storek * so we can avoid a small bit of work.
35855113Storek */
35955113Storek u_int
fpu_ftox(fe,fp,res)36055113Storek fpu_ftox(fe, fp, res)
36155113Storek struct fpemu *fe;
36255113Storek register struct fpn *fp;
36355113Storek u_int *res;
36455113Storek {
36555113Storek register u_int sign = fp->fp_sign << 31;
36655113Storek register int exp;
36755113Storek
36855113Storek #define EXT_EXP(e) ((e) << (EXT_FRACBITS & 31))
36955113Storek #define EXT_MASK (EXT_EXP(1) - 1)
37055113Storek
37155113Storek if (ISNAN(fp)) {
37255113Storek (void) fpu_shr(fp, 2); /* since we are not rounding */
37355113Storek exp = EXT_EXP_INFNAN;
37455113Storek goto done;
37555113Storek }
37655113Storek if (ISINF(fp)) {
37755113Storek sign |= EXT_EXP(EXT_EXP_INFNAN);
37855113Storek goto zero;
37955113Storek }
38055113Storek if (ISZERO(fp)) {
38155113Storek zero: res[1] = res[2] = res[3] = 0;
38255113Storek return (sign);
38355113Storek }
38455113Storek
38555113Storek if ((exp = fp->fp_exp + EXT_EXP_BIAS) <= 0) {
38655113Storek (void) fpu_shr(fp, FP_NMANT - FP_NG - EXT_FRACBITS - exp);
38755113Storek if (round(fe, fp) && fp->fp_mant[0] == EXT_EXP(1)) {
38855113Storek res[1] = res[2] = res[3] = 0;
38955113Storek return (sign | EXT_EXP(1) | 0);
39055113Storek }
39155113Storek if ((fe->fe_cx & FSR_NX) ||
39255113Storek (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
39355113Storek fe->fe_cx |= FSR_UF;
39455113Storek exp = 0;
39555113Storek goto done;
39655113Storek }
39755113Storek /* Since internal == extended, no need to shift here. */
39855113Storek if (round(fe, fp) && fp->fp_mant[0] == EXT_EXP(2))
39955113Storek exp++;
40055113Storek if (exp >= EXT_EXP_INFNAN) {
40155113Storek fe->fe_cx |= FSR_OF | FSR_NX;
40255113Storek if (toinf(fe, sign)) {
40355113Storek res[1] = res[2] = res[3] = 0;
40455113Storek return (sign | EXT_EXP(EXT_EXP_INFNAN) | 0);
40555113Storek }
40655113Storek res[1] = res[2] = res[3] = ~0;
40755113Storek return (sign | EXT_EXP(EXT_EXP_INFNAN) | EXT_MASK);
40855113Storek }
40955113Storek done:
41055113Storek res[1] = fp->fp_mant[1];
41155113Storek res[2] = fp->fp_mant[2];
41255113Storek res[3] = fp->fp_mant[3];
41355113Storek return (sign | EXT_EXP(exp) | (fp->fp_mant[0] & EXT_MASK));
41455113Storek }
41555113Storek
41655113Storek /*
41755113Storek * Implode an fpn, writing the result into the given space.
41855113Storek */
41955113Storek void
fpu_implode(fe,fp,type,space)42055113Storek fpu_implode(fe, fp, type, space)
42155113Storek struct fpemu *fe;
42255113Storek register struct fpn *fp;
42355113Storek int type;
42455113Storek register u_int *space;
42555113Storek {
42655113Storek
42755113Storek switch (type) {
42855113Storek
42955113Storek case FTYPE_INT:
43055113Storek space[0] = fpu_ftoi(fe, fp);
43155113Storek break;
43255113Storek
43355113Storek case FTYPE_SNG:
43455113Storek space[0] = fpu_ftos(fe, fp);
43555113Storek break;
43655113Storek
43755113Storek case FTYPE_DBL:
43855113Storek space[0] = fpu_ftod(fe, fp, space);
43955113Storek break;
44055113Storek
44155113Storek case FTYPE_EXT:
44255113Storek /* funky rounding precision options ?? */
44355113Storek space[0] = fpu_ftox(fe, fp, space);
44455113Storek break;
44555113Storek
44655113Storek default:
44755113Storek panic("fpu_implode");
44855113Storek }
44955113Storek }
450