1*6244ddccSmiod /* $OpenBSD: fpu_explode.c,v 1.12 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_explode.c 8.1 (Berkeley) 6/11/93
4502b90beaSjason * $NetBSD: fpu_explode.c,v 1.5 2000/08/03 18:32:08 eeh Exp $
4602b90beaSjason */
4702b90beaSjason
4802b90beaSjason /*
4902b90beaSjason * FPU subroutines: `explode' the machine's `packed binary' format numbers
5002b90beaSjason * into our internal 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 #include "fpu_reg.h"
6302b90beaSjason
6402b90beaSjason /*
6502b90beaSjason * N.B.: in all of the following, we assume the FP format is
6602b90beaSjason *
6702b90beaSjason * ---------------------------
6802b90beaSjason * | s | exponent | fraction |
6902b90beaSjason * ---------------------------
7002b90beaSjason *
7102b90beaSjason * (which represents -1**s * 1.fraction * 2**exponent), so that the
7202b90beaSjason * sign bit is way at the top (bit 31), the exponent is next, and
7302b90beaSjason * then the remaining bits mark the fraction. A zero exponent means
7402b90beaSjason * zero or denormalized (0.fraction rather than 1.fraction), and the
7502b90beaSjason * maximum possible exponent, 2bias+1, signals inf (fraction==0) or NaN.
7602b90beaSjason *
7702b90beaSjason * Since the sign bit is always the topmost bit---this holds even for
7802b90beaSjason * integers---we set that outside all the *tof functions. Each function
7902b90beaSjason * returns the class code for the new number (but note that we use
8002b90beaSjason * FPC_QNAN for all NaNs; fpu_explode will fix this if appropriate).
8102b90beaSjason */
8202b90beaSjason
8302b90beaSjason /*
8402b90beaSjason * int -> fpn.
8502b90beaSjason */
8602b90beaSjason int
__fpu_itof(fp,i)8702b90beaSjason __fpu_itof(fp, i)
8802b90beaSjason struct fpn *fp;
8902b90beaSjason u_int i;
9002b90beaSjason {
9102b90beaSjason
9202b90beaSjason if (i == 0)
9302b90beaSjason return (FPC_ZERO);
9402b90beaSjason /*
9502b90beaSjason * The value FP_1 represents 2^FP_LG, so set the exponent
9602b90beaSjason * there and let normalization fix it up. Convert negative
9702b90beaSjason * numbers to sign-and-magnitude. Note that this relies on
9802b90beaSjason * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
9902b90beaSjason */
10002b90beaSjason fp->fp_exp = FP_LG;
101dcaa94faSjca fp->fp_mant[0] = (fp->fp_sign && (int)i < 0) ? -i : i;
10202b90beaSjason fp->fp_mant[1] = 0;
10302b90beaSjason fp->fp_mant[2] = 0;
10402b90beaSjason fp->fp_mant[3] = 0;
10502b90beaSjason __fpu_norm(fp);
10602b90beaSjason return (FPC_NUM);
10702b90beaSjason }
10802b90beaSjason
10902b90beaSjason /*
11090c97895Sotto * uint -> fpn.
11190c97895Sotto */
11290c97895Sotto int
__fpu_uitof(fp,i)11390c97895Sotto __fpu_uitof(fp, i)
11490c97895Sotto struct fpn *fp;
11590c97895Sotto u_int i;
11690c97895Sotto {
11790c97895Sotto
11890c97895Sotto if (i == 0)
11990c97895Sotto return (FPC_ZERO);
12090c97895Sotto /*
12190c97895Sotto * The value FP_1 represents 2^FP_LG, so set the exponent
12290c97895Sotto * there and let normalization fix it up.
12390c97895Sotto * Note that this relies on fpu_norm()'s handling of
12490c97895Sotto * `supernormals'; see fpu_subr.c.
12590c97895Sotto */
12690c97895Sotto fp->fp_exp = FP_LG;
12790c97895Sotto fp->fp_mant[0] = i;
12890c97895Sotto fp->fp_mant[1] = 0;
12990c97895Sotto fp->fp_mant[2] = 0;
13090c97895Sotto fp->fp_mant[3] = 0;
13190c97895Sotto __fpu_norm(fp);
13290c97895Sotto return (FPC_NUM);
13390c97895Sotto }
13490c97895Sotto
13590c97895Sotto /*
13602b90beaSjason * 64-bit int -> fpn.
13702b90beaSjason */
13802b90beaSjason int
__fpu_xtof(fp,i)13902b90beaSjason __fpu_xtof(fp, i)
14002b90beaSjason struct fpn *fp;
14102b90beaSjason u_int64_t i;
14202b90beaSjason {
14302b90beaSjason
14402b90beaSjason if (i == 0)
14502b90beaSjason return (FPC_ZERO);
14602b90beaSjason /*
14702b90beaSjason * The value FP_1 represents 2^FP_LG, so set the exponent
14802b90beaSjason * there and let normalization fix it up. Convert negative
14902b90beaSjason * numbers to sign-and-magnitude. Note that this relies on
15002b90beaSjason * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
15102b90beaSjason */
15202b90beaSjason fp->fp_exp = FP_LG2;
153dcaa94faSjca i = (fp->fp_sign && (int64_t)i < 0) ? -i : i;
15478c5984cSjason fp->fp_mant[0] = (i >> 32) & 0xffffffff;
15578c5984cSjason fp->fp_mant[1] = (i >> 0) & 0xffffffff;
15602b90beaSjason fp->fp_mant[2] = 0;
15702b90beaSjason fp->fp_mant[3] = 0;
15802b90beaSjason __fpu_norm(fp);
15902b90beaSjason return (FPC_NUM);
16002b90beaSjason }
16102b90beaSjason
16290c97895Sotto /*
16390c97895Sotto * 64-bit uint -> fpn.
16490c97895Sotto */
16590c97895Sotto int
__fpu_uxtof(fp,i)16690c97895Sotto __fpu_uxtof(fp, i)
16790c97895Sotto struct fpn *fp;
16890c97895Sotto u_int64_t i;
16990c97895Sotto {
17090c97895Sotto
17190c97895Sotto if (i == 0)
17290c97895Sotto return (FPC_ZERO);
17390c97895Sotto /*
17490c97895Sotto * The value FP_1 represents 2^FP_LG, so set the exponent
17590c97895Sotto * there and let normalization fix it up.
17690c97895Sotto * Note that this relies on fpu_norm()'s handling of
17790c97895Sotto * `supernormals'; see fpu_subr.c.
17890c97895Sotto */
17990c97895Sotto fp->fp_exp = FP_LG2;
18090c97895Sotto fp->fp_mant[0] = (i >> 32) & 0xffffffff;
18190c97895Sotto fp->fp_mant[1] = (i >> 0) & 0xffffffff;
18290c97895Sotto fp->fp_mant[2] = 0;
18390c97895Sotto fp->fp_mant[3] = 0;
18490c97895Sotto __fpu_norm(fp);
18590c97895Sotto return (FPC_NUM);
18690c97895Sotto }
18790c97895Sotto
18802b90beaSjason #define mask(nbits) ((1L << (nbits)) - 1)
18902b90beaSjason
19002b90beaSjason /*
19102b90beaSjason * All external floating formats convert to internal in the same manner,
19202b90beaSjason * as defined here. Note that only normals get an implied 1.0 inserted.
19302b90beaSjason */
19402b90beaSjason #define FP_TOF(exp, expbias, allfrac, f0, f1, f2, f3) \
19502b90beaSjason if (exp == 0) { \
19602b90beaSjason if (allfrac == 0) \
19702b90beaSjason return (FPC_ZERO); \
19802b90beaSjason fp->fp_exp = 1 - expbias; \
19902b90beaSjason fp->fp_mant[0] = f0; \
20002b90beaSjason fp->fp_mant[1] = f1; \
20102b90beaSjason fp->fp_mant[2] = f2; \
20202b90beaSjason fp->fp_mant[3] = f3; \
20302b90beaSjason __fpu_norm(fp); \
20402b90beaSjason return (FPC_NUM); \
20502b90beaSjason } \
20602b90beaSjason if (exp == (2 * expbias + 1)) { \
20702b90beaSjason if (allfrac == 0) \
20802b90beaSjason return (FPC_INF); \
20902b90beaSjason fp->fp_mant[0] = f0; \
21002b90beaSjason fp->fp_mant[1] = f1; \
21102b90beaSjason fp->fp_mant[2] = f2; \
21202b90beaSjason fp->fp_mant[3] = f3; \
21302b90beaSjason return (FPC_QNAN); \
21402b90beaSjason } \
21502b90beaSjason fp->fp_exp = exp - expbias; \
21602b90beaSjason fp->fp_mant[0] = FP_1 | f0; \
21702b90beaSjason fp->fp_mant[1] = f1; \
21802b90beaSjason fp->fp_mant[2] = f2; \
21902b90beaSjason fp->fp_mant[3] = f3; \
22002b90beaSjason return (FPC_NUM)
22102b90beaSjason
22202b90beaSjason /*
22302b90beaSjason * 32-bit single precision -> fpn.
22402b90beaSjason * We assume a single occupies at most (64-FP_LG) bits in the internal
22502b90beaSjason * format: i.e., needs at most fp_mant[0] and fp_mant[1].
22602b90beaSjason */
22702b90beaSjason int
__fpu_stof(fp,i)22802b90beaSjason __fpu_stof(fp, i)
22902b90beaSjason struct fpn *fp;
23002b90beaSjason u_int i;
23102b90beaSjason {
23202b90beaSjason int exp;
23302b90beaSjason u_int frac, f0, f1;
23402b90beaSjason #define SNG_SHIFT (SNG_FRACBITS - FP_LG)
23502b90beaSjason
23602b90beaSjason exp = (i >> (32 - 1 - SNG_EXPBITS)) & mask(SNG_EXPBITS);
23702b90beaSjason frac = i & mask(SNG_FRACBITS);
23802b90beaSjason f0 = frac >> SNG_SHIFT;
23902b90beaSjason f1 = frac << (32 - SNG_SHIFT);
24002b90beaSjason FP_TOF(exp, SNG_EXP_BIAS, frac, f0, f1, 0, 0);
24102b90beaSjason }
24202b90beaSjason
24302b90beaSjason /*
24402b90beaSjason * 64-bit double -> fpn.
24502b90beaSjason * We assume this uses at most (96-FP_LG) bits.
24602b90beaSjason */
24702b90beaSjason int
__fpu_dtof(fp,i,j)24802b90beaSjason __fpu_dtof(fp, i, j)
24902b90beaSjason struct fpn *fp;
25002b90beaSjason u_int i, j;
25102b90beaSjason {
25202b90beaSjason int exp;
25302b90beaSjason u_int frac, f0, f1, f2;
25402b90beaSjason #define DBL_SHIFT (DBL_FRACBITS - 32 - FP_LG)
25502b90beaSjason
25602b90beaSjason exp = (i >> (32 - 1 - DBL_EXPBITS)) & mask(DBL_EXPBITS);
25702b90beaSjason frac = i & mask(DBL_FRACBITS - 32);
25802b90beaSjason f0 = frac >> DBL_SHIFT;
25902b90beaSjason f1 = (frac << (32 - DBL_SHIFT)) | (j >> DBL_SHIFT);
26002b90beaSjason f2 = j << (32 - DBL_SHIFT);
26102b90beaSjason frac |= j;
26202b90beaSjason FP_TOF(exp, DBL_EXP_BIAS, frac, f0, f1, f2, 0);
26302b90beaSjason }
26402b90beaSjason
26502b90beaSjason /*
26602b90beaSjason * 128-bit extended -> fpn.
26702b90beaSjason */
26802b90beaSjason int
__fpu_qtof(fp,i,j,k,l)26902b90beaSjason __fpu_qtof(fp, i, j, k, l)
27002b90beaSjason struct fpn *fp;
27102b90beaSjason u_int i, j, k, l;
27202b90beaSjason {
27302b90beaSjason int exp;
27402b90beaSjason u_int frac, f0, f1, f2, f3;
27502b90beaSjason #define EXT_SHIFT (-(EXT_FRACBITS - 3 * 32 - FP_LG)) /* left shift! */
27602b90beaSjason
27702b90beaSjason /*
27802b90beaSjason * Note that ext and fpn `line up', hence no shifting needed.
27902b90beaSjason */
28002b90beaSjason exp = (i >> (32 - 1 - EXT_EXPBITS)) & mask(EXT_EXPBITS);
28102b90beaSjason frac = i & mask(EXT_FRACBITS - 3 * 32);
28202b90beaSjason f0 = (frac << EXT_SHIFT) | (j >> (32 - EXT_SHIFT));
28302b90beaSjason f1 = (j << EXT_SHIFT) | (k >> (32 - EXT_SHIFT));
28402b90beaSjason f2 = (k << EXT_SHIFT) | (l >> (32 - EXT_SHIFT));
28502b90beaSjason f3 = l << EXT_SHIFT;
28602b90beaSjason frac |= j | k | l;
28702b90beaSjason FP_TOF(exp, EXT_EXP_BIAS, frac, f0, f1, f2, f3);
28802b90beaSjason }
28902b90beaSjason
29019e206fbSguenther #if 0 /* __fpu_explode is unused */
29102b90beaSjason /*
29202b90beaSjason * Explode the contents of a / regpair / regquad.
29302b90beaSjason * If the input is a signalling NaN, an NV (invalid) exception
29402b90beaSjason * will be set. (Note that nothing but NV can occur until ALU
29502b90beaSjason * operations are performed.)
29602b90beaSjason */
29702b90beaSjason void
29802b90beaSjason __fpu_explode(fe, fp, type, reg)
29902b90beaSjason struct fpemu *fe;
30002b90beaSjason struct fpn *fp;
30102b90beaSjason int type, reg;
30202b90beaSjason {
30302b90beaSjason u_int32_t s = 0/* XXX gcc */, *sp;
30402b90beaSjason u_int64_t l[2];
30502b90beaSjason
30602b90beaSjason if (type == FTYPE_LNG || type == FTYPE_DBL || type == FTYPE_EXT) {
30702b90beaSjason l[0] = __fpu_getreg64(reg & ~1);
30802b90beaSjason sp = (u_int32_t *)l;
30902b90beaSjason fp->fp_sign = sp[0] >> 31;
31002b90beaSjason fp->fp_sticky = 0;
31102b90beaSjason switch (type) {
31202b90beaSjason case FTYPE_LNG:
31302b90beaSjason s = __fpu_xtof(fp, l[0]);
31402b90beaSjason break;
31502b90beaSjason case FTYPE_DBL:
31602b90beaSjason s = __fpu_dtof(fp, sp[0], sp[1]);
31702b90beaSjason break;
31802b90beaSjason case FTYPE_EXT:
31902b90beaSjason l[1] = __fpu_getreg64((reg & ~1) + 2);
32002b90beaSjason s = __fpu_qtof(fp, sp[0], sp[1], sp[2], sp[3]);
32102b90beaSjason break;
32202b90beaSjason default:
32302b90beaSjason #ifdef DIAGNOSTIC
32402b90beaSjason __utrap_panic("fpu_explode");
32502b90beaSjason #endif
3265ecfcfc9Sderaadt break;
32702b90beaSjason }
32802b90beaSjason } else {
32902b90beaSjason #ifdef DIAGNOSTIC
33002b90beaSjason if (type != FTYPE_SNG)
33102b90beaSjason __utrap_panic("fpu_explode");
33202b90beaSjason #endif
33302b90beaSjason s = __fpu_getreg32(reg);
33402b90beaSjason fp->fp_sign = s >> 31;
33502b90beaSjason fp->fp_sticky = 0;
33602b90beaSjason s = __fpu_stof(fp, s);
33702b90beaSjason }
33802b90beaSjason
33902b90beaSjason if (s == FPC_QNAN && (fp->fp_mant[0] & FP_QUIETBIT) == 0) {
34002b90beaSjason /*
34102b90beaSjason * Input is a signalling NaN. All operations that return
34202b90beaSjason * an input NaN operand put it through a ``NaN conversion'',
34302b90beaSjason * which basically just means ``turn on the quiet bit''.
34402b90beaSjason * We do this here so that all NaNs internally look quiet
34502b90beaSjason * (we can tell signalling ones by their class).
34602b90beaSjason */
34702b90beaSjason fp->fp_mant[0] |= FP_QUIETBIT;
34802b90beaSjason fe->fe_cx = FSR_NV; /* assert invalid operand */
34902b90beaSjason s = FPC_SNAN;
35002b90beaSjason }
35102b90beaSjason fp->fp_class = s;
35202b90beaSjason DPRINTF(FPE_REG, ("fpu_explode: %%%c%d => ", (type == FTYPE_LNG) ? 'x' :
35302b90beaSjason ((type == FTYPE_INT) ? 'i' :
35402b90beaSjason ((type == FTYPE_SNG) ? 's' :
35502b90beaSjason ((type == FTYPE_DBL) ? 'd' :
35602b90beaSjason ((type == FTYPE_EXT) ? 'q' : '?')))),
35702b90beaSjason reg));
35802b90beaSjason DUMPFPN(FPE_REG, fp);
35902b90beaSjason DPRINTF(FPE_REG, ("\n"));
36002b90beaSjason }
36119e206fbSguenther #endif
362