1*6244ddccSmiod /* $OpenBSD: fpu_implode.c,v 1.8 2024/03/29 21:02:11 miod Exp $ */
202b90beaSjason
302b90beaSjason /*
402b90beaSjason * Copyright (c) 1992, 1993
502b90beaSjason * The Regents of the University of California. All rights reserved.
602b90beaSjason *
702b90beaSjason * This software was developed by the Computer Systems Engineering group
802b90beaSjason * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
902b90beaSjason * contributed to Berkeley.
1002b90beaSjason *
1102b90beaSjason * All advertising materials mentioning features or use of this software
1202b90beaSjason * must display the following acknowledgement:
1302b90beaSjason * This product includes software developed by the University of
1402b90beaSjason * California, Lawrence Berkeley Laboratory.
1502b90beaSjason *
1602b90beaSjason * Redistribution and use in source and binary forms, with or without
1702b90beaSjason * modification, are permitted provided that the following conditions
1802b90beaSjason * are met:
1902b90beaSjason * 1. Redistributions of source code must retain the above copyright
2002b90beaSjason * notice, this list of conditions and the following disclaimer.
2102b90beaSjason * 2. Redistributions in binary form must reproduce the above copyright
2202b90beaSjason * notice, this list of conditions and the following disclaimer in the
2302b90beaSjason * documentation and/or other materials provided with the distribution.
2402b90beaSjason * 3. All advertising materials mentioning features or use of this software
2502b90beaSjason * must display the following acknowledgement:
2602b90beaSjason * This product includes software developed by the University of
2702b90beaSjason * California, Berkeley and its contributors.
2802b90beaSjason * 4. Neither the name of the University nor the names of its contributors
2902b90beaSjason * may be used to endorse or promote products derived from this software
3002b90beaSjason * without specific prior written permission.
3102b90beaSjason *
3202b90beaSjason * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3302b90beaSjason * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3402b90beaSjason * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3502b90beaSjason * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3602b90beaSjason * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3702b90beaSjason * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3802b90beaSjason * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3902b90beaSjason * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4002b90beaSjason * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4102b90beaSjason * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4202b90beaSjason * SUCH DAMAGE.
4302b90beaSjason *
4402b90beaSjason * @(#)fpu_implode.c 8.1 (Berkeley) 6/11/93
4502b90beaSjason * $NetBSD: fpu_implode.c,v 1.8 2001/08/26 05:44:46 eeh Exp $
4602b90beaSjason */
4702b90beaSjason
4802b90beaSjason /*
4902b90beaSjason * FPU subroutines: `implode' internal format numbers into the machine's
5002b90beaSjason * `packed binary' format.
5102b90beaSjason */
5202b90beaSjason
5377f9746cSderaadt #include <sys/types.h>
5402b90beaSjason
5502b90beaSjason #include <machine/fsr.h>
5602b90beaSjason #include <machine/ieee.h>
5702b90beaSjason #include <machine/instr.h>
5802b90beaSjason
5902b90beaSjason #include "fpu_arith.h"
6002b90beaSjason #include "fpu_emu.h"
6102b90beaSjason #include "fpu_extern.h"
6202b90beaSjason
636f1dc4c0Sguenther static int fpround(struct fpemu *, struct fpn *);
6402b90beaSjason static int toinf(struct fpemu *, int);
6502b90beaSjason
6602b90beaSjason #define FSR_GET_RD(fsr) (((fsr) >> FSR_RD_SHIFT) & FSR_RD_MASK)
6702b90beaSjason
6802b90beaSjason /*
6902b90beaSjason * Round a number (algorithm from Motorola MC68882 manual, modified for
7002b90beaSjason * our internal format). Set inexact exception if rounding is required.
7102b90beaSjason * Return true iff we rounded up.
7202b90beaSjason *
7302b90beaSjason * After rounding, we discard the guard and round bits by shifting right
7402b90beaSjason * 2 bits (a la fpu_shr(), but we do not bother with fp->fp_sticky).
7502b90beaSjason * This saves effort later.
7602b90beaSjason *
7702b90beaSjason * Note that we may leave the value 2.0 in fp->fp_mant; it is the caller's
7802b90beaSjason * responsibility to fix this if necessary.
7902b90beaSjason */
8002b90beaSjason static int
fpround(struct fpemu * fe,struct fpn * fp)816f1dc4c0Sguenther fpround(struct fpemu *fe, struct fpn *fp)
8202b90beaSjason {
8302b90beaSjason u_int m0, m1, m2, m3;
8402b90beaSjason int gr, s;
8502b90beaSjason
8602b90beaSjason m0 = fp->fp_mant[0];
8702b90beaSjason m1 = fp->fp_mant[1];
8802b90beaSjason m2 = fp->fp_mant[2];
8902b90beaSjason m3 = fp->fp_mant[3];
9002b90beaSjason gr = m3 & 3;
9102b90beaSjason s = fp->fp_sticky;
9202b90beaSjason
9302b90beaSjason /* mant >>= FP_NG */
9402b90beaSjason m3 = (m3 >> FP_NG) | (m2 << (32 - FP_NG));
9502b90beaSjason m2 = (m2 >> FP_NG) | (m1 << (32 - FP_NG));
9602b90beaSjason m1 = (m1 >> FP_NG) | (m0 << (32 - FP_NG));
9702b90beaSjason m0 >>= FP_NG;
9802b90beaSjason
9902b90beaSjason if ((gr | s) == 0) /* result is exact: no rounding needed */
10002b90beaSjason goto rounddown;
10102b90beaSjason
10202b90beaSjason fe->fe_cx |= FSR_NX; /* inexact */
10302b90beaSjason
10402b90beaSjason /* Go to rounddown to round down; break to round up. */
10502b90beaSjason switch (FSR_GET_RD(fe->fe_fsr)) {
10602b90beaSjason case FSR_RD_RN:
10702b90beaSjason default:
10802b90beaSjason /*
10902b90beaSjason * Round only if guard is set (gr & 2). If guard is set,
11002b90beaSjason * but round & sticky both clear, then we want to round
11102b90beaSjason * but have a tie, so round to even, i.e., add 1 iff odd.
11202b90beaSjason */
11302b90beaSjason if ((gr & 2) == 0)
11402b90beaSjason goto rounddown;
11502b90beaSjason if ((gr & 1) || fp->fp_sticky || (m3 & 1))
11602b90beaSjason break;
11702b90beaSjason goto rounddown;
11802b90beaSjason
11902b90beaSjason case FSR_RD_RZ:
12002b90beaSjason /* Round towards zero, i.e., down. */
12102b90beaSjason goto rounddown;
12202b90beaSjason
12302b90beaSjason case FSR_RD_RM:
12402b90beaSjason /* Round towards -Inf: up if negative, down if positive. */
12502b90beaSjason if (fp->fp_sign)
12602b90beaSjason break;
12702b90beaSjason goto rounddown;
12802b90beaSjason
12902b90beaSjason case FSR_RD_RP:
13002b90beaSjason /* Round towards +Inf: up if positive, down otherwise. */
13102b90beaSjason if (!fp->fp_sign)
13202b90beaSjason break;
13302b90beaSjason goto rounddown;
13402b90beaSjason }
13502b90beaSjason
13602b90beaSjason /* Bump low bit of mantissa, with carry. */
13702b90beaSjason FPU_ADDS(m3, m3, 1);
13802b90beaSjason FPU_ADDCS(m2, m2, 0);
13902b90beaSjason FPU_ADDCS(m1, m1, 0);
14002b90beaSjason FPU_ADDC(m0, m0, 0);
14102b90beaSjason fp->fp_mant[0] = m0;
14202b90beaSjason fp->fp_mant[1] = m1;
14302b90beaSjason fp->fp_mant[2] = m2;
14402b90beaSjason fp->fp_mant[3] = m3;
14502b90beaSjason return (1);
14602b90beaSjason
14702b90beaSjason rounddown:
14802b90beaSjason fp->fp_mant[0] = m0;
14902b90beaSjason fp->fp_mant[1] = m1;
15002b90beaSjason fp->fp_mant[2] = m2;
15102b90beaSjason fp->fp_mant[3] = m3;
15202b90beaSjason return (0);
15302b90beaSjason }
15402b90beaSjason
15502b90beaSjason /*
15602b90beaSjason * For overflow: return true if overflow is to go to +/-Inf, according
15702b90beaSjason * to the sign of the overflowing result. If false, overflow is to go
15802b90beaSjason * to the largest magnitude value instead.
15902b90beaSjason */
16002b90beaSjason static int
toinf(struct fpemu * fe,int sign)16102b90beaSjason toinf(struct fpemu *fe, int sign)
16202b90beaSjason {
16302b90beaSjason int inf;
16402b90beaSjason
16502b90beaSjason /* look at rounding direction */
16602b90beaSjason switch (FSR_GET_RD(fe->fe_fsr)) {
16702b90beaSjason default:
16802b90beaSjason case FSR_RD_RN: /* the nearest value is always Inf */
16902b90beaSjason inf = 1;
17002b90beaSjason break;
17102b90beaSjason
17202b90beaSjason case FSR_RD_RZ: /* toward 0 => never towards Inf */
17302b90beaSjason inf = 0;
17402b90beaSjason break;
17502b90beaSjason
17602b90beaSjason case FSR_RD_RP: /* toward +Inf iff positive */
17702b90beaSjason inf = sign == 0;
17802b90beaSjason break;
17902b90beaSjason
18002b90beaSjason case FSR_RD_RM: /* toward -Inf iff negative */
18102b90beaSjason inf = sign;
18202b90beaSjason break;
18302b90beaSjason }
18402b90beaSjason return (inf);
18502b90beaSjason }
18602b90beaSjason
18702b90beaSjason /*
18802b90beaSjason * fpn -> int (int value returned as return value).
18902b90beaSjason *
19002b90beaSjason * N.B.: this conversion always rounds towards zero (this is a peculiarity
19102b90beaSjason * of the SPARC instruction set).
19202b90beaSjason */
19302b90beaSjason u_int
__fpu_ftoi(fe,fp)19402b90beaSjason __fpu_ftoi(fe, fp)
19502b90beaSjason struct fpemu *fe;
19602b90beaSjason struct fpn *fp;
19702b90beaSjason {
19802b90beaSjason u_int i;
19902b90beaSjason int sign, exp;
20002b90beaSjason
20102b90beaSjason sign = fp->fp_sign;
20202b90beaSjason switch (fp->fp_class) {
20302b90beaSjason
20402b90beaSjason case FPC_ZERO:
20502b90beaSjason return (0);
20602b90beaSjason
20702b90beaSjason case FPC_NUM:
20802b90beaSjason /*
20902b90beaSjason * If exp >= 2^32, overflow. Otherwise shift value right
21002b90beaSjason * into last mantissa word (this will not exceed 0xffffffff),
21102b90beaSjason * shifting any guard and round bits out into the sticky
21202b90beaSjason * bit. Then ``round'' towards zero, i.e., just set an
2136f1dc4c0Sguenther * inexact exception if sticky is set (see fpround()).
21402b90beaSjason * If the result is > 0x80000000, or is positive and equals
21502b90beaSjason * 0x80000000, overflow; otherwise the last fraction word
21602b90beaSjason * is the result.
21702b90beaSjason */
21802b90beaSjason if ((exp = fp->fp_exp) >= 32)
21902b90beaSjason break;
22002b90beaSjason /* NB: the following includes exp < 0 cases */
22102b90beaSjason if (__fpu_shr(fp, FP_NMANT - 1 - exp) != 0)
22202b90beaSjason fe->fe_cx |= FSR_NX;
22302b90beaSjason i = fp->fp_mant[3];
22402b90beaSjason if (i >= ((u_int)0x80000000 + sign))
22502b90beaSjason break;
22602b90beaSjason return (sign ? -i : i);
22702b90beaSjason
22802b90beaSjason default: /* Inf, qNaN, sNaN */
22902b90beaSjason break;
23002b90beaSjason }
23102b90beaSjason /* overflow: replace any inexact exception with invalid */
23202b90beaSjason fe->fe_cx = (fe->fe_cx & ~FSR_NX) | FSR_NV;
23302b90beaSjason return (0x7fffffff + sign);
23402b90beaSjason }
23502b90beaSjason
23602b90beaSjason /*
23702b90beaSjason * fpn -> extended int (high bits of int value returned as return value).
23802b90beaSjason *
23902b90beaSjason * N.B.: this conversion always rounds towards zero (this is a peculiarity
24002b90beaSjason * of the SPARC instruction set).
24102b90beaSjason */
24202b90beaSjason u_int
__fpu_ftox(fe,fp,res)24302b90beaSjason __fpu_ftox(fe, fp, res)
24402b90beaSjason struct fpemu *fe;
24502b90beaSjason struct fpn *fp;
24602b90beaSjason u_int *res;
24702b90beaSjason {
24802b90beaSjason u_int64_t i;
24902b90beaSjason int sign, exp;
25002b90beaSjason
25102b90beaSjason sign = fp->fp_sign;
25202b90beaSjason switch (fp->fp_class) {
25302b90beaSjason
25402b90beaSjason case FPC_ZERO:
25502b90beaSjason res[1] = 0;
25602b90beaSjason return (0);
25702b90beaSjason
25802b90beaSjason case FPC_NUM:
25902b90beaSjason /*
26002b90beaSjason * If exp >= 2^64, overflow. Otherwise shift value right
26128a72845Sjason * into last mantissa word (this will not exceed
26228a72845Sjason * 0xffffffffffffffff), shifting any guard and round bits out
26328a72845Sjason * into the sticky bit. Then ``round'' towards zero, i.e.,
264e29147ccSguenther * just set an inexact exception if sticky is set (see
2656f1dc4c0Sguenther * fpround()). If the result is > 0x8000000000000000, or is
266e29147ccSguenther * positive and equals 0x8000000000000000, overflow;
267e29147ccSguenther * otherwise the last fraction word is the result.
26802b90beaSjason */
26902b90beaSjason if ((exp = fp->fp_exp) >= 64)
27002b90beaSjason break;
27102b90beaSjason /* NB: the following includes exp < 0 cases */
27202b90beaSjason if (__fpu_shr(fp, FP_NMANT - 1 - exp) != 0)
27302b90beaSjason fe->fe_cx |= FSR_NX;
27402b90beaSjason i = ((u_int64_t)fp->fp_mant[2]<<32)|fp->fp_mant[3];
27502b90beaSjason if (i >= ((u_int64_t)0x8000000000000000LL + sign))
27602b90beaSjason break;
27702b90beaSjason if (sign)
27828a72845Sjason i = -i;
27902b90beaSjason res[1] = (int)i;
28002b90beaSjason return (i >> 32);
28102b90beaSjason
28202b90beaSjason default: /* Inf, qNaN, sNaN */
28302b90beaSjason break;
28402b90beaSjason }
28502b90beaSjason /* overflow: replace any inexact exception with invalid */
28602b90beaSjason fe->fe_cx = (fe->fe_cx & ~FSR_NX) | FSR_NV;
28702b90beaSjason return (0x7fffffffffffffffLL + sign);
28802b90beaSjason }
28902b90beaSjason
29002b90beaSjason /*
29102b90beaSjason * fpn -> single (32 bit single returned as return value).
29202b90beaSjason * We assume <= 29 bits in a single-precision fraction (1.f part).
29302b90beaSjason */
29402b90beaSjason u_int
__fpu_ftos(fe,fp)29502b90beaSjason __fpu_ftos(fe, fp)
29602b90beaSjason struct fpemu *fe;
29702b90beaSjason struct fpn *fp;
29802b90beaSjason {
29902b90beaSjason u_int sign = fp->fp_sign << 31;
30002b90beaSjason int exp;
30102b90beaSjason
30202b90beaSjason #define SNG_EXP(e) ((e) << SNG_FRACBITS) /* makes e an exponent */
30302b90beaSjason #define SNG_MASK (SNG_EXP(1) - 1) /* mask for fraction */
30402b90beaSjason
30502b90beaSjason /* Take care of non-numbers first. */
30602b90beaSjason if (ISNAN(fp)) {
30702b90beaSjason /*
30802b90beaSjason * Preserve upper bits of NaN, per SPARC V8 appendix N.
30902b90beaSjason * Note that fp->fp_mant[0] has the quiet bit set,
31002b90beaSjason * even if it is classified as a signalling NaN.
31102b90beaSjason */
31202b90beaSjason (void) __fpu_shr(fp, FP_NMANT - 1 - SNG_FRACBITS);
31302b90beaSjason exp = SNG_EXP_INFNAN;
31402b90beaSjason goto done;
31502b90beaSjason }
31602b90beaSjason if (ISINF(fp))
31702b90beaSjason return (sign | SNG_EXP(SNG_EXP_INFNAN));
31802b90beaSjason if (ISZERO(fp))
31902b90beaSjason return (sign);
32002b90beaSjason
32102b90beaSjason /*
32202b90beaSjason * Normals (including subnormals). Drop all the fraction bits
32302b90beaSjason * (including the explicit ``implied'' 1 bit) down into the
32402b90beaSjason * single-precision range. If the number is subnormal, move
32502b90beaSjason * the ``implied'' 1 into the explicit range as well, and shift
32602b90beaSjason * right to introduce leading zeroes. Rounding then acts
32702b90beaSjason * differently for normals and subnormals: the largest subnormal
32802b90beaSjason * may round to the smallest normal (1.0 x 2^minexp), or may
32902b90beaSjason * remain subnormal. In the latter case, signal an underflow
33002b90beaSjason * if the result was inexact or if underflow traps are enabled.
33102b90beaSjason *
33202b90beaSjason * Rounding a normal, on the other hand, always produces another
33302b90beaSjason * normal (although either way the result might be too big for
33402b90beaSjason * single precision, and cause an overflow). If rounding a
33502b90beaSjason * normal produces 2.0 in the fraction, we need not adjust that
33602b90beaSjason * fraction at all, since both 1.0 and 2.0 are zero under the
33702b90beaSjason * fraction mask.
33802b90beaSjason *
33902b90beaSjason * Note that the guard and round bits vanish from the number after
34002b90beaSjason * rounding.
34102b90beaSjason */
34202b90beaSjason if ((exp = fp->fp_exp + SNG_EXP_BIAS) <= 0) { /* subnormal */
34302b90beaSjason /* -NG for g,r; -SNG_FRACBITS-exp for fraction */
34402b90beaSjason (void) __fpu_shr(fp, FP_NMANT - FP_NG - SNG_FRACBITS - exp);
3456f1dc4c0Sguenther if (fpround(fe, fp) && fp->fp_mant[3] == SNG_EXP(1))
34602b90beaSjason return (sign | SNG_EXP(1) | 0);
34702b90beaSjason if ((fe->fe_cx & FSR_NX) ||
34802b90beaSjason (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
34902b90beaSjason fe->fe_cx |= FSR_UF;
35002b90beaSjason return (sign | SNG_EXP(0) | fp->fp_mant[3]);
35102b90beaSjason }
35202b90beaSjason /* -FP_NG for g,r; -1 for implied 1; -SNG_FRACBITS for fraction */
35302b90beaSjason (void) __fpu_shr(fp, FP_NMANT - FP_NG - 1 - SNG_FRACBITS);
35402b90beaSjason #ifdef DIAGNOSTIC
35502b90beaSjason if ((fp->fp_mant[3] & SNG_EXP(1 << FP_NG)) == 0)
35602b90beaSjason __utrap_panic("fpu_ftos");
35702b90beaSjason #endif
3586f1dc4c0Sguenther if (fpround(fe, fp) && fp->fp_mant[3] == SNG_EXP(2))
35902b90beaSjason exp++;
36002b90beaSjason if (exp >= SNG_EXP_INFNAN) {
36102b90beaSjason /* overflow to inf or to max single */
36202b90beaSjason fe->fe_cx |= FSR_OF | FSR_NX;
36302b90beaSjason if (toinf(fe, sign))
36402b90beaSjason return (sign | SNG_EXP(SNG_EXP_INFNAN));
36502b90beaSjason return (sign | SNG_EXP(SNG_EXP_INFNAN - 1) | SNG_MASK);
36602b90beaSjason }
36702b90beaSjason done:
36802b90beaSjason /* phew, made it */
36902b90beaSjason return (sign | SNG_EXP(exp) | (fp->fp_mant[3] & SNG_MASK));
37002b90beaSjason }
37102b90beaSjason
37202b90beaSjason /*
37302b90beaSjason * fpn -> double (32 bit high-order result returned; 32-bit low order result
37402b90beaSjason * left in res[1]). Assumes <= 61 bits in double precision fraction.
37502b90beaSjason *
37602b90beaSjason * This code mimics fpu_ftos; see it for comments.
37702b90beaSjason */
37802b90beaSjason u_int
__fpu_ftod(fe,fp,res)37902b90beaSjason __fpu_ftod(fe, fp, res)
38002b90beaSjason struct fpemu *fe;
38102b90beaSjason struct fpn *fp;
38202b90beaSjason u_int *res;
38302b90beaSjason {
38402b90beaSjason u_int sign = fp->fp_sign << 31;
38502b90beaSjason int exp;
38602b90beaSjason
38702b90beaSjason #define DBL_EXP(e) ((e) << (DBL_FRACBITS & 31))
38802b90beaSjason #define DBL_MASK (DBL_EXP(1) - 1)
38902b90beaSjason
39002b90beaSjason if (ISNAN(fp)) {
39102b90beaSjason (void) __fpu_shr(fp, FP_NMANT - 1 - DBL_FRACBITS);
39202b90beaSjason exp = DBL_EXP_INFNAN;
39302b90beaSjason goto done;
39402b90beaSjason }
39502b90beaSjason if (ISINF(fp)) {
39602b90beaSjason sign |= DBL_EXP(DBL_EXP_INFNAN);
39702b90beaSjason goto zero;
39802b90beaSjason }
39902b90beaSjason if (ISZERO(fp)) {
40002b90beaSjason zero: res[1] = 0;
40102b90beaSjason return (sign);
40202b90beaSjason }
40302b90beaSjason
40402b90beaSjason if ((exp = fp->fp_exp + DBL_EXP_BIAS) <= 0) {
40502b90beaSjason (void) __fpu_shr(fp, FP_NMANT - FP_NG - DBL_FRACBITS - exp);
4066f1dc4c0Sguenther if (fpround(fe, fp) && fp->fp_mant[2] == DBL_EXP(1)) {
40702b90beaSjason res[1] = 0;
40802b90beaSjason return (sign | DBL_EXP(1) | 0);
40902b90beaSjason }
41002b90beaSjason if ((fe->fe_cx & FSR_NX) ||
41102b90beaSjason (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
41202b90beaSjason fe->fe_cx |= FSR_UF;
41302b90beaSjason exp = 0;
41402b90beaSjason goto done;
41502b90beaSjason }
41602b90beaSjason (void) __fpu_shr(fp, FP_NMANT - FP_NG - 1 - DBL_FRACBITS);
4176f1dc4c0Sguenther if (fpround(fe, fp) && fp->fp_mant[2] == DBL_EXP(2))
41802b90beaSjason exp++;
41902b90beaSjason if (exp >= DBL_EXP_INFNAN) {
42002b90beaSjason fe->fe_cx |= FSR_OF | FSR_NX;
42102b90beaSjason if (toinf(fe, sign)) {
42202b90beaSjason res[1] = 0;
42302b90beaSjason return (sign | DBL_EXP(DBL_EXP_INFNAN) | 0);
42402b90beaSjason }
42502b90beaSjason res[1] = ~0;
42602b90beaSjason return (sign | DBL_EXP(DBL_EXP_INFNAN) | DBL_MASK);
42702b90beaSjason }
42802b90beaSjason done:
42902b90beaSjason res[1] = fp->fp_mant[3];
43002b90beaSjason return (sign | DBL_EXP(exp) | (fp->fp_mant[2] & DBL_MASK));
43102b90beaSjason }
43202b90beaSjason
43302b90beaSjason /*
43402b90beaSjason * fpn -> extended (32 bit high-order result returned; low-order fraction
43502b90beaSjason * words left in res[1]..res[3]). Like ftod, which is like ftos ... but
43602b90beaSjason * our internal format *is* extended precision, plus 2 bits for guard/round,
43702b90beaSjason * so we can avoid a small bit of work.
43802b90beaSjason */
43902b90beaSjason u_int
__fpu_ftoq(fe,fp,res)44002b90beaSjason __fpu_ftoq(fe, fp, res)
44102b90beaSjason struct fpemu *fe;
44202b90beaSjason struct fpn *fp;
44302b90beaSjason u_int *res;
44402b90beaSjason {
44502b90beaSjason u_int sign = fp->fp_sign << 31;
44602b90beaSjason int exp;
44702b90beaSjason
44802b90beaSjason #define EXT_EXP(e) ((e) << (EXT_FRACBITS & 31))
44902b90beaSjason #define EXT_MASK (EXT_EXP(1) - 1)
45002b90beaSjason
45102b90beaSjason if (ISNAN(fp)) {
45202b90beaSjason (void) __fpu_shr(fp, 2); /* since we are not rounding */
45302b90beaSjason exp = EXT_EXP_INFNAN;
45402b90beaSjason goto done;
45502b90beaSjason }
45602b90beaSjason if (ISINF(fp)) {
45702b90beaSjason sign |= EXT_EXP(EXT_EXP_INFNAN);
45802b90beaSjason goto zero;
45902b90beaSjason }
46002b90beaSjason if (ISZERO(fp)) {
46102b90beaSjason zero: res[1] = res[2] = res[3] = 0;
46202b90beaSjason return (sign);
46302b90beaSjason }
46402b90beaSjason
46502b90beaSjason if ((exp = fp->fp_exp + EXT_EXP_BIAS) <= 0) {
46602b90beaSjason (void) __fpu_shr(fp, FP_NMANT - FP_NG - EXT_FRACBITS - exp);
4676f1dc4c0Sguenther if (fpround(fe, fp) && fp->fp_mant[0] == EXT_EXP(1)) {
46802b90beaSjason res[1] = res[2] = res[3] = 0;
46902b90beaSjason return (sign | EXT_EXP(1) | 0);
47002b90beaSjason }
47102b90beaSjason if ((fe->fe_cx & FSR_NX) ||
47202b90beaSjason (fe->fe_fsr & (FSR_UF << FSR_TEM_SHIFT)))
47302b90beaSjason fe->fe_cx |= FSR_UF;
47402b90beaSjason exp = 0;
47502b90beaSjason goto done;
47602b90beaSjason }
47702b90beaSjason /* Since internal == extended, no need to shift here. */
4786f1dc4c0Sguenther if (fpround(fe, fp) && fp->fp_mant[0] == EXT_EXP(2))
47902b90beaSjason exp++;
48002b90beaSjason if (exp >= EXT_EXP_INFNAN) {
48102b90beaSjason fe->fe_cx |= FSR_OF | FSR_NX;
48202b90beaSjason if (toinf(fe, sign)) {
48302b90beaSjason res[1] = res[2] = res[3] = 0;
48402b90beaSjason return (sign | EXT_EXP(EXT_EXP_INFNAN) | 0);
48502b90beaSjason }
48602b90beaSjason res[1] = res[2] = res[3] = ~0;
48702b90beaSjason return (sign | EXT_EXP(EXT_EXP_INFNAN) | EXT_MASK);
48802b90beaSjason }
48902b90beaSjason done:
49002b90beaSjason res[1] = fp->fp_mant[1];
49102b90beaSjason res[2] = fp->fp_mant[2];
49202b90beaSjason res[3] = fp->fp_mant[3];
49302b90beaSjason return (sign | EXT_EXP(exp) | (fp->fp_mant[0] & EXT_MASK));
49402b90beaSjason }
49502b90beaSjason
49602b90beaSjason /*
49702b90beaSjason * Implode an fpn, writing the result into the given space.
49802b90beaSjason */
49902b90beaSjason void
__fpu_implode(fe,fp,type,space)50002b90beaSjason __fpu_implode(fe, fp, type, space)
50102b90beaSjason struct fpemu *fe;
50202b90beaSjason struct fpn *fp;
50302b90beaSjason int type;
50402b90beaSjason u_int *space;
50502b90beaSjason {
50602b90beaSjason
50702b90beaSjason switch (type) {
50802b90beaSjason
50902b90beaSjason case FTYPE_LNG:
51002b90beaSjason space[0] = __fpu_ftox(fe, fp, space);
51102b90beaSjason break;
51202b90beaSjason
51302b90beaSjason case FTYPE_INT:
51402b90beaSjason space[0] = __fpu_ftoi(fe, fp);
51502b90beaSjason break;
51602b90beaSjason
51702b90beaSjason case FTYPE_SNG:
51802b90beaSjason space[0] = __fpu_ftos(fe, fp);
51902b90beaSjason break;
52002b90beaSjason
52102b90beaSjason case FTYPE_DBL:
52202b90beaSjason space[0] = __fpu_ftod(fe, fp, space);
52302b90beaSjason break;
52402b90beaSjason
52502b90beaSjason case FTYPE_EXT:
52602b90beaSjason /* funky rounding precision options ?? */
52702b90beaSjason space[0] = __fpu_ftoq(fe, fp, space);
52802b90beaSjason break;
52902b90beaSjason
53002b90beaSjason #ifdef DIAGNOSTIC
53102b90beaSjason default:
53202b90beaSjason __utrap_panic("fpu_implode");
53302b90beaSjason #endif
53402b90beaSjason }
53502b90beaSjason DPRINTF(FPE_REG, ("fpu_implode: %x %x %x %x\n",
53602b90beaSjason space[0], space[1], space[2], space[3]));
53702b90beaSjason }
538