1*4b64ca3eSmiod /* $OpenBSD: fpu_explode.c,v 1.8 2024/03/29 21:08:10 miod Exp $ */
2839f47eaSjason /* $NetBSD: fpu_explode.c,v 1.5 2000/08/03 18:32:08 eeh Exp $ */
3839f47eaSjason
4839f47eaSjason /*
5839f47eaSjason * Copyright (c) 1992, 1993
6839f47eaSjason * The Regents of the University of California. All rights reserved.
7839f47eaSjason *
8839f47eaSjason * This software was developed by the Computer Systems Engineering group
9839f47eaSjason * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10839f47eaSjason * contributed to Berkeley.
11839f47eaSjason *
12839f47eaSjason * All advertising materials mentioning features or use of this software
13839f47eaSjason * must display the following acknowledgement:
14839f47eaSjason * This product includes software developed by the University of
15839f47eaSjason * California, Lawrence Berkeley Laboratory.
16839f47eaSjason *
17839f47eaSjason * Redistribution and use in source and binary forms, with or without
18839f47eaSjason * modification, are permitted provided that the following conditions
19839f47eaSjason * are met:
20839f47eaSjason * 1. Redistributions of source code must retain the above copyright
21839f47eaSjason * notice, this list of conditions and the following disclaimer.
22839f47eaSjason * 2. Redistributions in binary form must reproduce the above copyright
23839f47eaSjason * notice, this list of conditions and the following disclaimer in the
24839f47eaSjason * documentation and/or other materials provided with the distribution.
2529295d1cSmillert * 3. Neither the name of the University nor the names of its contributors
26839f47eaSjason * may be used to endorse or promote products derived from this software
27839f47eaSjason * without specific prior written permission.
28839f47eaSjason *
29839f47eaSjason * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30839f47eaSjason * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31839f47eaSjason * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32839f47eaSjason * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33839f47eaSjason * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34839f47eaSjason * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35839f47eaSjason * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36839f47eaSjason * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37839f47eaSjason * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38839f47eaSjason * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39839f47eaSjason * SUCH DAMAGE.
40839f47eaSjason *
41839f47eaSjason * @(#)fpu_explode.c 8.1 (Berkeley) 6/11/93
42839f47eaSjason */
43839f47eaSjason
44839f47eaSjason /*
45839f47eaSjason * FPU subroutines: `explode' the machine's `packed binary' format numbers
46839f47eaSjason * into our internal format.
47839f47eaSjason */
48839f47eaSjason
49839f47eaSjason #include <sys/types.h>
50839f47eaSjason #include <sys/systm.h>
51839f47eaSjason
52*4b64ca3eSmiod #include <machine/fsr.h>
53839f47eaSjason #include <machine/ieee.h>
54839f47eaSjason #include <machine/instr.h>
55839f47eaSjason #include <machine/reg.h>
56839f47eaSjason
57839f47eaSjason #include <sparc64/fpu/fpu_arith.h>
58839f47eaSjason #include <sparc64/fpu/fpu_emu.h>
59839f47eaSjason #include <sparc64/fpu/fpu_extern.h>
60839f47eaSjason
61839f47eaSjason /*
62839f47eaSjason * N.B.: in all of the following, we assume the FP format is
63839f47eaSjason *
64839f47eaSjason * ---------------------------
65839f47eaSjason * | s | exponent | fraction |
66839f47eaSjason * ---------------------------
67839f47eaSjason *
68839f47eaSjason * (which represents -1**s * 1.fraction * 2**exponent), so that the
69839f47eaSjason * sign bit is way at the top (bit 31), the exponent is next, and
70839f47eaSjason * then the remaining bits mark the fraction. A zero exponent means
71839f47eaSjason * zero or denormalized (0.fraction rather than 1.fraction), and the
72839f47eaSjason * maximum possible exponent, 2bias+1, signals inf (fraction==0) or NaN.
73839f47eaSjason *
74839f47eaSjason * Since the sign bit is always the topmost bit---this holds even for
75839f47eaSjason * integers---we set that outside all the *tof functions. Each function
76839f47eaSjason * returns the class code for the new number (but note that we use
77839f47eaSjason * FPC_QNAN for all NaNs; fpu_explode will fix this if appropriate).
78839f47eaSjason */
79839f47eaSjason
80839f47eaSjason /*
81839f47eaSjason * int -> fpn.
82839f47eaSjason */
83839f47eaSjason int
fpu_itof(struct fpn * fp,u_int i)842c7a42e9Smiod fpu_itof(struct fpn *fp, u_int i)
85839f47eaSjason {
86839f47eaSjason
87839f47eaSjason if (i == 0)
88839f47eaSjason return (FPC_ZERO);
89839f47eaSjason /*
90839f47eaSjason * The value FP_1 represents 2^FP_LG, so set the exponent
91839f47eaSjason * there and let normalization fix it up. Convert negative
92839f47eaSjason * numbers to sign-and-magnitude. Note that this relies on
93839f47eaSjason * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
94839f47eaSjason */
95839f47eaSjason fp->fp_exp = FP_LG;
961d6c3468Sjca fp->fp_mant[0] = (fp->fp_sign && (int)i < 0) ? -i : i;
97839f47eaSjason fp->fp_mant[1] = 0;
98839f47eaSjason fp->fp_mant[2] = 0;
99839f47eaSjason fp->fp_mant[3] = 0;
100839f47eaSjason fpu_norm(fp);
101839f47eaSjason return (FPC_NUM);
102839f47eaSjason }
103839f47eaSjason
104839f47eaSjason /*
105839f47eaSjason * 64-bit int -> fpn.
106839f47eaSjason */
107839f47eaSjason int
fpu_xtof(struct fpn * fp,u_int64_t i)1082c7a42e9Smiod fpu_xtof(struct fpn *fp, u_int64_t i)
109839f47eaSjason {
110839f47eaSjason if (i == 0)
111839f47eaSjason return (FPC_ZERO);
1125ebf65afSjason
113839f47eaSjason /*
114839f47eaSjason * The value FP_1 represents 2^FP_LG, so set the exponent
115839f47eaSjason * there and let normalization fix it up. Convert negative
116839f47eaSjason * numbers to sign-and-magnitude. Note that this relies on
117839f47eaSjason * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
118839f47eaSjason */
119839f47eaSjason fp->fp_exp = FP_LG2;
1201d6c3468Sjca i = (fp->fp_sign && (int64_t)i < 0) ? -i : i;
1211d6c3468Sjca fp->fp_mant[0] = (i >> 32) & 0xffffffff;
1221d6c3468Sjca fp->fp_mant[1] = (i >> 0) & 0xffffffff;
123839f47eaSjason fp->fp_mant[2] = 0;
124839f47eaSjason fp->fp_mant[3] = 0;
125839f47eaSjason fpu_norm(fp);
126839f47eaSjason return (FPC_NUM);
127839f47eaSjason }
128839f47eaSjason
129839f47eaSjason #define mask(nbits) ((1L << (nbits)) - 1)
130839f47eaSjason
131839f47eaSjason /*
132839f47eaSjason * All external floating formats convert to internal in the same manner,
133839f47eaSjason * as defined here. Note that only normals get an implied 1.0 inserted.
134839f47eaSjason */
135839f47eaSjason #define FP_TOF(exp, expbias, allfrac, f0, f1, f2, f3) \
136839f47eaSjason if (exp == 0) { \
137839f47eaSjason if (allfrac == 0) \
138839f47eaSjason return (FPC_ZERO); \
139839f47eaSjason fp->fp_exp = 1 - expbias; \
140839f47eaSjason fp->fp_mant[0] = f0; \
141839f47eaSjason fp->fp_mant[1] = f1; \
142839f47eaSjason fp->fp_mant[2] = f2; \
143839f47eaSjason fp->fp_mant[3] = f3; \
144839f47eaSjason fpu_norm(fp); \
145839f47eaSjason return (FPC_NUM); \
146839f47eaSjason } \
147839f47eaSjason if (exp == (2 * expbias + 1)) { \
148839f47eaSjason if (allfrac == 0) \
149839f47eaSjason return (FPC_INF); \
150839f47eaSjason fp->fp_mant[0] = f0; \
151839f47eaSjason fp->fp_mant[1] = f1; \
152839f47eaSjason fp->fp_mant[2] = f2; \
153839f47eaSjason fp->fp_mant[3] = f3; \
154839f47eaSjason return (FPC_QNAN); \
155839f47eaSjason } \
156839f47eaSjason fp->fp_exp = exp - expbias; \
157839f47eaSjason fp->fp_mant[0] = FP_1 | f0; \
158839f47eaSjason fp->fp_mant[1] = f1; \
159839f47eaSjason fp->fp_mant[2] = f2; \
160839f47eaSjason fp->fp_mant[3] = f3; \
161839f47eaSjason return (FPC_NUM)
162839f47eaSjason
163839f47eaSjason /*
164839f47eaSjason * 32-bit single precision -> fpn.
165839f47eaSjason * We assume a single occupies at most (64-FP_LG) bits in the internal
166839f47eaSjason * format: i.e., needs at most fp_mant[0] and fp_mant[1].
167839f47eaSjason */
168839f47eaSjason int
fpu_stof(struct fpn * fp,u_int i)1692c7a42e9Smiod fpu_stof(struct fpn *fp, u_int i)
170839f47eaSjason {
1712c7a42e9Smiod int exp;
1722c7a42e9Smiod u_int frac, f0, f1;
173839f47eaSjason #define SNG_SHIFT (SNG_FRACBITS - FP_LG)
174839f47eaSjason
175839f47eaSjason exp = (i >> (32 - 1 - SNG_EXPBITS)) & mask(SNG_EXPBITS);
176839f47eaSjason frac = i & mask(SNG_FRACBITS);
177839f47eaSjason f0 = frac >> SNG_SHIFT;
178839f47eaSjason f1 = frac << (32 - SNG_SHIFT);
179839f47eaSjason FP_TOF(exp, SNG_EXP_BIAS, frac, f0, f1, 0, 0);
180839f47eaSjason }
181839f47eaSjason
182839f47eaSjason /*
183839f47eaSjason * 64-bit double -> fpn.
184839f47eaSjason * We assume this uses at most (96-FP_LG) bits.
185839f47eaSjason */
186839f47eaSjason int
fpu_dtof(struct fpn * fp,u_int i,u_int j)1872c7a42e9Smiod fpu_dtof(struct fpn *fp, u_int i, u_int j)
188839f47eaSjason {
1892c7a42e9Smiod int exp;
1902c7a42e9Smiod u_int frac, f0, f1, f2;
191839f47eaSjason #define DBL_SHIFT (DBL_FRACBITS - 32 - FP_LG)
192839f47eaSjason
193839f47eaSjason exp = (i >> (32 - 1 - DBL_EXPBITS)) & mask(DBL_EXPBITS);
194839f47eaSjason frac = i & mask(DBL_FRACBITS - 32);
195839f47eaSjason f0 = frac >> DBL_SHIFT;
196839f47eaSjason f1 = (frac << (32 - DBL_SHIFT)) | (j >> DBL_SHIFT);
197839f47eaSjason f2 = j << (32 - DBL_SHIFT);
198839f47eaSjason frac |= j;
199839f47eaSjason FP_TOF(exp, DBL_EXP_BIAS, frac, f0, f1, f2, 0);
200839f47eaSjason }
201839f47eaSjason
202839f47eaSjason /*
203839f47eaSjason * 128-bit extended -> fpn.
204839f47eaSjason */
205839f47eaSjason int
fpu_qtof(struct fpn * fp,u_int i,u_int j,u_int k,u_int l)2062c7a42e9Smiod fpu_qtof(struct fpn *fp, u_int i, u_int j, u_int k, u_int l)
207839f47eaSjason {
2082c7a42e9Smiod int exp;
2092c7a42e9Smiod u_int frac, f0, f1, f2, f3;
210839f47eaSjason #define EXT_SHIFT (-(EXT_FRACBITS - 3 * 32 - FP_LG)) /* left shift! */
211839f47eaSjason
212839f47eaSjason /*
213839f47eaSjason * Note that ext and fpn `line up', hence no shifting needed.
214839f47eaSjason */
215839f47eaSjason exp = (i >> (32 - 1 - EXT_EXPBITS)) & mask(EXT_EXPBITS);
216839f47eaSjason frac = i & mask(EXT_FRACBITS - 3 * 32);
217839f47eaSjason f0 = (frac << EXT_SHIFT) | (j >> (32 - EXT_SHIFT));
218839f47eaSjason f1 = (j << EXT_SHIFT) | (k >> (32 - EXT_SHIFT));
219839f47eaSjason f2 = (k << EXT_SHIFT) | (l >> (32 - EXT_SHIFT));
220839f47eaSjason f3 = l << EXT_SHIFT;
221839f47eaSjason frac |= j | k | l;
222839f47eaSjason FP_TOF(exp, EXT_EXP_BIAS, frac, f0, f1, f2, f3);
223839f47eaSjason }
224839f47eaSjason
225839f47eaSjason /*
226839f47eaSjason * Explode the contents of a register / regpair / regquad.
227839f47eaSjason * If the input is a signalling NaN, an NV (invalid) exception
228839f47eaSjason * will be set. (Note that nothing but NV can occur until ALU
229839f47eaSjason * operations are performed.)
230839f47eaSjason */
231839f47eaSjason void
fpu_explode(struct fpemu * fe,struct fpn * fp,int type,int reg)2322c7a42e9Smiod fpu_explode(struct fpemu *fe, struct fpn *fp, int type, int reg)
233839f47eaSjason {
2342c7a42e9Smiod u_int s, *space;
235839f47eaSjason u_int64_t l, *xspace;
236839f47eaSjason
237839f47eaSjason xspace = (u_int64_t *)&fe->fe_fpstate->fs_regs[reg & ~1];
238839f47eaSjason l = xspace[0];
239839f47eaSjason space = &fe->fe_fpstate->fs_regs[reg];
240839f47eaSjason s = space[0];
2411d6c3468Sjca fp->fp_sign = (type == FTYPE_LNG) ? l >> 63 : s >> 31;
242839f47eaSjason fp->fp_sticky = 0;
2435ebf65afSjason DPRINTF(FPE_INSN, ("fpu_explode: "));
244839f47eaSjason switch (type) {
245839f47eaSjason case FTYPE_LNG:
2465ebf65afSjason DPRINTF(FPE_INSN, ("LNG: %llx", l));
247839f47eaSjason s = fpu_xtof(fp, l);
248839f47eaSjason break;
249839f47eaSjason
250839f47eaSjason case FTYPE_INT:
2515ebf65afSjason DPRINTF(FPE_INSN, ("INT: %x", s));
252839f47eaSjason s = fpu_itof(fp, s);
253839f47eaSjason break;
254839f47eaSjason
255839f47eaSjason case FTYPE_SNG:
2565ebf65afSjason DPRINTF(FPE_INSN, ("SNG: %x", s));
257839f47eaSjason s = fpu_stof(fp, s);
258839f47eaSjason break;
259839f47eaSjason
260839f47eaSjason case FTYPE_DBL:
2615ebf65afSjason DPRINTF(FPE_INSN, ("DBL: %x %x", s, space[1]));
262839f47eaSjason s = fpu_dtof(fp, s, space[1]);
263839f47eaSjason break;
264839f47eaSjason
265839f47eaSjason case FTYPE_EXT:
2665ebf65afSjason DPRINTF(FPE_INSN, ("EXT: %x %x %x %x", s, space[1],
2675ebf65afSjason space[2], space[3]));
268839f47eaSjason s = fpu_qtof(fp, s, space[1], space[2], space[3]);
269839f47eaSjason break;
270839f47eaSjason
271839f47eaSjason default:
272839f47eaSjason panic("fpu_explode");
273839f47eaSjason }
2745ebf65afSjason DPRINTF(FPE_INSN, ("\n"));
275839f47eaSjason
276839f47eaSjason if (s == FPC_QNAN && (fp->fp_mant[0] & FP_QUIETBIT) == 0) {
277839f47eaSjason /*
278839f47eaSjason * Input is a signalling NaN. All operations that return
279839f47eaSjason * an input NaN operand put it through a ``NaN conversion'',
280839f47eaSjason * which basically just means ``turn on the quiet bit''.
281839f47eaSjason * We do this here so that all NaNs internally look quiet
282839f47eaSjason * (we can tell signalling ones by their class).
283839f47eaSjason */
284839f47eaSjason fp->fp_mant[0] |= FP_QUIETBIT;
285839f47eaSjason fe->fe_cx = FSR_NV; /* assert invalid operand */
286839f47eaSjason s = FPC_SNAN;
287839f47eaSjason }
288839f47eaSjason fp->fp_class = s;
289839f47eaSjason DPRINTF(FPE_REG, ("fpu_explode: %%%c%d => ", (type == FTYPE_LNG) ? 'x' :
290839f47eaSjason ((type == FTYPE_INT) ? 'i' :
291839f47eaSjason ((type == FTYPE_SNG) ? 's' :
292839f47eaSjason ((type == FTYPE_DBL) ? 'd' :
293839f47eaSjason ((type == FTYPE_EXT) ? 'q' : '?')))),
294839f47eaSjason reg));
295839f47eaSjason DUMPFPN(FPE_REG, fp);
296839f47eaSjason DPRINTF(FPE_REG, ("\n"));
297839f47eaSjason }
298