1*f3f59a5dSrin /* $NetBSD: fpu_explode.c,v 1.14 2022/09/07 06:51:58 rin Exp $ */
218b2f7e6Ssimonb
318b2f7e6Ssimonb /*
418b2f7e6Ssimonb * Copyright (c) 1992, 1993
518b2f7e6Ssimonb * The Regents of the University of California. All rights reserved.
618b2f7e6Ssimonb *
718b2f7e6Ssimonb * This software was developed by the Computer Systems Engineering group
818b2f7e6Ssimonb * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
918b2f7e6Ssimonb * contributed to Berkeley.
1018b2f7e6Ssimonb *
1118b2f7e6Ssimonb * All advertising materials mentioning features or use of this software
1218b2f7e6Ssimonb * must display the following acknowledgement:
1318b2f7e6Ssimonb * This product includes software developed by the University of
1418b2f7e6Ssimonb * California, Lawrence Berkeley Laboratory.
1518b2f7e6Ssimonb *
1618b2f7e6Ssimonb * Redistribution and use in source and binary forms, with or without
1718b2f7e6Ssimonb * modification, are permitted provided that the following conditions
1818b2f7e6Ssimonb * are met:
1918b2f7e6Ssimonb * 1. Redistributions of source code must retain the above copyright
2018b2f7e6Ssimonb * notice, this list of conditions and the following disclaimer.
2118b2f7e6Ssimonb * 2. Redistributions in binary form must reproduce the above copyright
2218b2f7e6Ssimonb * notice, this list of conditions and the following disclaimer in the
2318b2f7e6Ssimonb * documentation and/or other materials provided with the distribution.
24aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
2518b2f7e6Ssimonb * may be used to endorse or promote products derived from this software
2618b2f7e6Ssimonb * without specific prior written permission.
2718b2f7e6Ssimonb *
2818b2f7e6Ssimonb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2918b2f7e6Ssimonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3018b2f7e6Ssimonb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3118b2f7e6Ssimonb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3218b2f7e6Ssimonb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3318b2f7e6Ssimonb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3418b2f7e6Ssimonb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3518b2f7e6Ssimonb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3618b2f7e6Ssimonb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3718b2f7e6Ssimonb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3818b2f7e6Ssimonb * SUCH DAMAGE.
3918b2f7e6Ssimonb *
4018b2f7e6Ssimonb * @(#)fpu_explode.c 8.1 (Berkeley) 6/11/93
4118b2f7e6Ssimonb */
4218b2f7e6Ssimonb
4318b2f7e6Ssimonb /*
4418b2f7e6Ssimonb * FPU subroutines: `explode' the machine's `packed binary' format numbers
4518b2f7e6Ssimonb * into our internal format.
4618b2f7e6Ssimonb */
4718b2f7e6Ssimonb
48ed517291Slukem #include <sys/cdefs.h>
49*f3f59a5dSrin __KERNEL_RCSID(0, "$NetBSD: fpu_explode.c,v 1.14 2022/09/07 06:51:58 rin Exp $");
50ed517291Slukem
5118b2f7e6Ssimonb #include <sys/types.h>
5218b2f7e6Ssimonb #include <sys/systm.h>
5318b2f7e6Ssimonb
5418b2f7e6Ssimonb #include <powerpc/instr.h>
5518b2f7e6Ssimonb #include <machine/fpu.h>
5606f65540Srin #include <machine/ieee.h>
5706f65540Srin #include <machine/reg.h>
5818b2f7e6Ssimonb
5918b2f7e6Ssimonb #include <powerpc/fpu/fpu_arith.h>
6018b2f7e6Ssimonb #include <powerpc/fpu/fpu_emu.h>
6118b2f7e6Ssimonb #include <powerpc/fpu/fpu_extern.h>
6218b2f7e6Ssimonb
63f3fb7613Srin static int fpu_itof(struct fpn *, u_int);
64f3fb7613Srin static int fpu_xtof(struct fpn *, uint64_t);
65f3fb7613Srin static int fpu_stof(struct fpn *, u_int);
66f3fb7613Srin static int fpu_dtof(struct fpn *, u_int, u_int);
67f3fb7613Srin
6818b2f7e6Ssimonb /*
6918b2f7e6Ssimonb * N.B.: in all of the following, we assume the FP format is
7018b2f7e6Ssimonb *
7118b2f7e6Ssimonb * ---------------------------
7218b2f7e6Ssimonb * | s | exponent | fraction |
7318b2f7e6Ssimonb * ---------------------------
7418b2f7e6Ssimonb *
7518b2f7e6Ssimonb * (which represents -1**s * 1.fraction * 2**exponent), so that the
7618b2f7e6Ssimonb * sign bit is way at the top (bit 31), the exponent is next, and
7718b2f7e6Ssimonb * then the remaining bits mark the fraction. A zero exponent means
7818b2f7e6Ssimonb * zero or denormalized (0.fraction rather than 1.fraction), and the
7918b2f7e6Ssimonb * maximum possible exponent, 2bias+1, signals inf (fraction==0) or NaN.
8018b2f7e6Ssimonb *
8118b2f7e6Ssimonb * Since the sign bit is always the topmost bit---this holds even for
8218b2f7e6Ssimonb * integers---we set that outside all the *tof functions. Each function
8318b2f7e6Ssimonb * returns the class code for the new number (but note that we use
8418b2f7e6Ssimonb * FPC_QNAN for all NaNs; fpu_explode will fix this if appropriate).
8518b2f7e6Ssimonb */
8618b2f7e6Ssimonb
8718b2f7e6Ssimonb /*
8818b2f7e6Ssimonb * int -> fpn.
8918b2f7e6Ssimonb */
90f3fb7613Srin static int
fpu_itof(struct fpn * fp,u_int lo)91beee0953Srin fpu_itof(struct fpn *fp, u_int lo)
9218b2f7e6Ssimonb {
9318b2f7e6Ssimonb
94beee0953Srin if (lo == 0)
9518b2f7e6Ssimonb return (FPC_ZERO);
9618b2f7e6Ssimonb /*
9718b2f7e6Ssimonb * The value FP_1 represents 2^FP_LG, so set the exponent
9818b2f7e6Ssimonb * there and let normalization fix it up. Convert negative
9918b2f7e6Ssimonb * numbers to sign-and-magnitude. Note that this relies on
10018b2f7e6Ssimonb * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
10118b2f7e6Ssimonb */
10218b2f7e6Ssimonb fp->fp_exp = FP_LG;
103beee0953Srin fp->fp_mant[0] = (int)lo < 0 ? -lo : lo;
10418b2f7e6Ssimonb fp->fp_mant[1] = 0;
10518b2f7e6Ssimonb fp->fp_mant[2] = 0;
10618b2f7e6Ssimonb fp->fp_mant[3] = 0;
10718b2f7e6Ssimonb fpu_norm(fp);
10818b2f7e6Ssimonb return (FPC_NUM);
10918b2f7e6Ssimonb }
11018b2f7e6Ssimonb
11118b2f7e6Ssimonb /*
11218b2f7e6Ssimonb * 64-bit int -> fpn.
11318b2f7e6Ssimonb */
114f3fb7613Srin static int
fpu_xtof(struct fpn * fp,uint64_t i)1157a813560Srin fpu_xtof(struct fpn *fp, uint64_t i)
11618b2f7e6Ssimonb {
11718b2f7e6Ssimonb
11818b2f7e6Ssimonb if (i == 0)
11918b2f7e6Ssimonb return (FPC_ZERO);
12018b2f7e6Ssimonb /*
12118b2f7e6Ssimonb * The value FP_1 represents 2^FP_LG, so set the exponent
12218b2f7e6Ssimonb * there and let normalization fix it up. Convert negative
12318b2f7e6Ssimonb * numbers to sign-and-magnitude. Note that this relies on
12418b2f7e6Ssimonb * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
12518b2f7e6Ssimonb */
12618b2f7e6Ssimonb fp->fp_exp = FP_LG2;
12718b2f7e6Ssimonb *((int64_t*)fp->fp_mant) = (int64_t)i < 0 ? -i : i;
12818b2f7e6Ssimonb fp->fp_mant[2] = 0;
12918b2f7e6Ssimonb fp->fp_mant[3] = 0;
13018b2f7e6Ssimonb fpu_norm(fp);
13118b2f7e6Ssimonb return (FPC_NUM);
13218b2f7e6Ssimonb }
13318b2f7e6Ssimonb
13418b2f7e6Ssimonb #define mask(nbits) ((1L << (nbits)) - 1)
13518b2f7e6Ssimonb
13618b2f7e6Ssimonb /*
13718b2f7e6Ssimonb * All external floating formats convert to internal in the same manner,
13818b2f7e6Ssimonb * as defined here. Note that only normals get an implied 1.0 inserted.
13918b2f7e6Ssimonb */
14018b2f7e6Ssimonb #define FP_TOF(exp, expbias, allfrac, f0, f1, f2, f3) \
14118b2f7e6Ssimonb if (exp == 0) { \
14218b2f7e6Ssimonb if (allfrac == 0) \
14318b2f7e6Ssimonb return (FPC_ZERO); \
14418b2f7e6Ssimonb fp->fp_exp = 1 - expbias; \
14518b2f7e6Ssimonb fp->fp_mant[0] = f0; \
14618b2f7e6Ssimonb fp->fp_mant[1] = f1; \
14718b2f7e6Ssimonb fp->fp_mant[2] = f2; \
14818b2f7e6Ssimonb fp->fp_mant[3] = f3; \
14918b2f7e6Ssimonb fpu_norm(fp); \
15018b2f7e6Ssimonb return (FPC_NUM); \
15118b2f7e6Ssimonb } \
15218b2f7e6Ssimonb if (exp == (2 * expbias + 1)) { \
15318b2f7e6Ssimonb if (allfrac == 0) \
15418b2f7e6Ssimonb return (FPC_INF); \
15518b2f7e6Ssimonb fp->fp_mant[0] = f0; \
15618b2f7e6Ssimonb fp->fp_mant[1] = f1; \
15718b2f7e6Ssimonb fp->fp_mant[2] = f2; \
15818b2f7e6Ssimonb fp->fp_mant[3] = f3; \
15918b2f7e6Ssimonb return (FPC_QNAN); \
16018b2f7e6Ssimonb } \
16118b2f7e6Ssimonb fp->fp_exp = exp - expbias; \
16218b2f7e6Ssimonb fp->fp_mant[0] = FP_1 | f0; \
16318b2f7e6Ssimonb fp->fp_mant[1] = f1; \
16418b2f7e6Ssimonb fp->fp_mant[2] = f2; \
16518b2f7e6Ssimonb fp->fp_mant[3] = f3; \
16618b2f7e6Ssimonb return (FPC_NUM)
16718b2f7e6Ssimonb
16818b2f7e6Ssimonb /*
16918b2f7e6Ssimonb * 32-bit single precision -> fpn.
17018b2f7e6Ssimonb * We assume a single occupies at most (64-FP_LG) bits in the internal
17118b2f7e6Ssimonb * format: i.e., needs at most fp_mant[0] and fp_mant[1].
17218b2f7e6Ssimonb */
173f3fb7613Srin static int
fpu_stof(struct fpn * fp,u_int hi)174beee0953Srin fpu_stof(struct fpn *fp, u_int hi)
17518b2f7e6Ssimonb {
17618b2f7e6Ssimonb int exp;
17718b2f7e6Ssimonb u_int frac, f0, f1;
17818b2f7e6Ssimonb #define SNG_SHIFT (SNG_FRACBITS - FP_LG)
17918b2f7e6Ssimonb
180beee0953Srin exp = (hi >> (32 - 1 - SNG_EXPBITS)) & mask(SNG_EXPBITS);
181beee0953Srin frac = hi & mask(SNG_FRACBITS);
18218b2f7e6Ssimonb f0 = frac >> SNG_SHIFT;
18318b2f7e6Ssimonb f1 = frac << (32 - SNG_SHIFT);
18418b2f7e6Ssimonb FP_TOF(exp, SNG_EXP_BIAS, frac, f0, f1, 0, 0);
18518b2f7e6Ssimonb }
18618b2f7e6Ssimonb
18718b2f7e6Ssimonb /*
18818b2f7e6Ssimonb * 64-bit double -> fpn.
18918b2f7e6Ssimonb * We assume this uses at most (96-FP_LG) bits.
19018b2f7e6Ssimonb */
191f3fb7613Srin static int
fpu_dtof(struct fpn * fp,u_int hi,u_int lo)192beee0953Srin fpu_dtof(struct fpn *fp, u_int hi, u_int lo)
19318b2f7e6Ssimonb {
19418b2f7e6Ssimonb int exp;
19518b2f7e6Ssimonb u_int frac, f0, f1, f2;
19618b2f7e6Ssimonb #define DBL_SHIFT (DBL_FRACBITS - 32 - FP_LG)
19718b2f7e6Ssimonb
198beee0953Srin exp = (hi >> (32 - 1 - DBL_EXPBITS)) & mask(DBL_EXPBITS);
199beee0953Srin frac = hi & mask(DBL_FRACBITS - 32);
20018b2f7e6Ssimonb f0 = frac >> DBL_SHIFT;
201beee0953Srin f1 = (frac << (32 - DBL_SHIFT)) | (lo >> DBL_SHIFT);
202beee0953Srin f2 = lo << (32 - DBL_SHIFT);
203beee0953Srin frac |= lo;
20418b2f7e6Ssimonb FP_TOF(exp, DBL_EXP_BIAS, frac, f0, f1, f2, 0);
20518b2f7e6Ssimonb }
20618b2f7e6Ssimonb
20718b2f7e6Ssimonb /*
20818b2f7e6Ssimonb * Explode the contents of a register / regpair / regquad.
20918b2f7e6Ssimonb * If the input is a signalling NaN, an NV (invalid) exception
21018b2f7e6Ssimonb * will be set. (Note that nothing but NV can occur until ALU
21118b2f7e6Ssimonb * operations are performed.)
21218b2f7e6Ssimonb */
21318b2f7e6Ssimonb void
fpu_explode(struct fpemu * fe,struct fpn * fp,int type,uint64_t i)214e41cdfd4Srin fpu_explode(struct fpemu *fe, struct fpn *fp, int type, uint64_t i)
21518b2f7e6Ssimonb {
216e41cdfd4Srin u_int hi, lo;
217e41cdfd4Srin int class;
21818b2f7e6Ssimonb
219e41cdfd4Srin hi = (u_int)(i >> 32);
220e41cdfd4Srin lo = (u_int)i;
221e41cdfd4Srin fp->fp_sign = hi >> 31;
22218b2f7e6Ssimonb fp->fp_sticky = 0;
22318b2f7e6Ssimonb switch (type) {
22418b2f7e6Ssimonb
22518b2f7e6Ssimonb case FTYPE_LNG:
226e41cdfd4Srin class = fpu_xtof(fp, i);
22718b2f7e6Ssimonb break;
22818b2f7e6Ssimonb
22918b2f7e6Ssimonb case FTYPE_INT:
230*f3f59a5dSrin fp->fp_sign = lo >> 31;
231e41cdfd4Srin class = fpu_itof(fp, lo);
23218b2f7e6Ssimonb break;
23318b2f7e6Ssimonb
23418b2f7e6Ssimonb case FTYPE_SNG:
235e41cdfd4Srin class = fpu_stof(fp, hi);
23618b2f7e6Ssimonb break;
23718b2f7e6Ssimonb
23818b2f7e6Ssimonb case FTYPE_DBL:
239e41cdfd4Srin class = fpu_dtof(fp, hi, lo);
24018b2f7e6Ssimonb break;
24118b2f7e6Ssimonb
2420b5b7fffSrin default:
243499ef1c1Ssimonb panic("fpu_explode: invalid type %d", type);
24418b2f7e6Ssimonb }
24518b2f7e6Ssimonb
246e41cdfd4Srin if (class == FPC_QNAN && (fp->fp_mant[0] & FP_QUIETBIT) == 0) {
24718b2f7e6Ssimonb /*
24818b2f7e6Ssimonb * Input is a signalling NaN. All operations that return
24918b2f7e6Ssimonb * an input NaN operand put it through a ``NaN conversion'',
25018b2f7e6Ssimonb * which basically just means ``turn on the quiet bit''.
25118b2f7e6Ssimonb * We do this here so that all NaNs internally look quiet
25218b2f7e6Ssimonb * (we can tell signalling ones by their class).
25318b2f7e6Ssimonb */
25418b2f7e6Ssimonb fp->fp_mant[0] |= FP_QUIETBIT;
25518b2f7e6Ssimonb fe->fe_cx = FPSCR_VXSNAN; /* assert invalid operand */
256e41cdfd4Srin class = FPC_SNAN;
25718b2f7e6Ssimonb }
258e41cdfd4Srin fp->fp_class = class;
25918b2f7e6Ssimonb DUMPFPN(FPE_REG, fp);
26018b2f7e6Ssimonb }
261