fpu_explode.c revision 1.10 1 /* $NetBSD: fpu_explode.c,v 1.10 2003/07/15 00:04:59 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1992, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This software was developed by the Computer Systems Engineering group
8 * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 * contributed to Berkeley.
10 *
11 * All advertising materials mentioning features or use of this software
12 * must display the following acknowledgement:
13 * This product includes software developed by the University of
14 * California, Lawrence Berkeley Laboratory.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 * notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 * notice, this list of conditions and the following disclaimer in the
23 * documentation and/or other materials provided with the distribution.
24 * 3. All advertising materials mentioning features or use of this software
25 * must display the following acknowledgement:
26 * This product includes software developed by the University of
27 * California, Berkeley and its contributors.
28 * 4. Neither the name of the University nor the names of its contributors
29 * may be used to endorse or promote products derived from this software
30 * without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
33 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
40 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
41 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * SUCH DAMAGE.
43 *
44 * @(#)fpu_explode.c 8.1 (Berkeley) 6/11/93
45 */
46
47 /*
48 * FPU subroutines: `explode' the machine's `packed binary' format numbers
49 * into our internal format.
50 */
51
52 #include <sys/cdefs.h>
53 __KERNEL_RCSID(0, "$NetBSD: fpu_explode.c,v 1.10 2003/07/15 00:04:59 lukem Exp $");
54
55 #if defined(_KERNEL_OPT)
56 #include "opt_sparc_arch.h"
57 #endif
58
59 #include <sys/types.h>
60 #include <sys/systm.h>
61
62 #include <machine/ieee.h>
63 #include <machine/instr.h>
64 #include <machine/reg.h>
65
66 #include <sparc/fpu/fpu_arith.h>
67 #include <sparc/fpu/fpu_emu.h>
68 #include <sparc/fpu/fpu_extern.h>
69
70 /*
71 * N.B.: in all of the following, we assume the FP format is
72 *
73 * ---------------------------
74 * | s | exponent | fraction |
75 * ---------------------------
76 *
77 * (which represents -1**s * 1.fraction * 2**exponent), so that the
78 * sign bit is way at the top (bit 31), the exponent is next, and
79 * then the remaining bits mark the fraction. A zero exponent means
80 * zero or denormalized (0.fraction rather than 1.fraction), and the
81 * maximum possible exponent, 2bias+1, signals inf (fraction==0) or NaN.
82 *
83 * Since the sign bit is always the topmost bit---this holds even for
84 * integers---we set that outside all the *tof functions. Each function
85 * returns the class code for the new number (but note that we use
86 * FPC_QNAN for all NaNs; fpu_explode will fix this if appropriate).
87 */
88
89 /*
90 * int -> fpn.
91 */
92 int
93 fpu_itof(fp, i)
94 register struct fpn *fp;
95 register u_int i;
96 {
97
98 if (i == 0)
99 return (FPC_ZERO);
100 /*
101 * The value FP_1 represents 2^FP_LG, so set the exponent
102 * there and let normalization fix it up. Convert negative
103 * numbers to sign-and-magnitude. Note that this relies on
104 * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
105 */
106 fp->fp_exp = FP_LG;
107 fp->fp_mant[0] = (int)i < 0 ? -i : i;
108 fp->fp_mant[1] = 0;
109 fp->fp_mant[2] = 0;
110 fp->fp_mant[3] = 0;
111 fpu_norm(fp);
112 return (FPC_NUM);
113 }
114
115 #ifdef SUN4U
116 /*
117 * 64-bit int -> fpn.
118 */
119 int
120 fpu_xtof(fp, i)
121 register struct fpn *fp;
122 register u_int64_t i;
123 {
124
125 if (i == 0)
126 return (FPC_ZERO);
127 /*
128 * The value FP_1 represents 2^FP_LG, so set the exponent
129 * there and let normalization fix it up. Convert negative
130 * numbers to sign-and-magnitude. Note that this relies on
131 * fpu_norm()'s handling of `supernormals'; see fpu_subr.c.
132 */
133 fp->fp_exp = FP_LG2;
134 *((int64_t*)fp->fp_mant) = (int64_t)i < 0 ? -i : i;
135 fp->fp_mant[2] = 0;
136 fp->fp_mant[3] = 0;
137 fpu_norm(fp);
138 return (FPC_NUM);
139 }
140 #endif /* SUN4U */
141
142 #define mask(nbits) ((1L << (nbits)) - 1)
143
144 /*
145 * All external floating formats convert to internal in the same manner,
146 * as defined here. Note that only normals get an implied 1.0 inserted.
147 */
148 #define FP_TOF(exp, expbias, allfrac, f0, f1, f2, f3) \
149 if (exp == 0) { \
150 if (allfrac == 0) \
151 return (FPC_ZERO); \
152 fp->fp_exp = 1 - expbias; \
153 fp->fp_mant[0] = f0; \
154 fp->fp_mant[1] = f1; \
155 fp->fp_mant[2] = f2; \
156 fp->fp_mant[3] = f3; \
157 fpu_norm(fp); \
158 return (FPC_NUM); \
159 } \
160 if (exp == (2 * expbias + 1)) { \
161 if (allfrac == 0) \
162 return (FPC_INF); \
163 fp->fp_mant[0] = f0; \
164 fp->fp_mant[1] = f1; \
165 fp->fp_mant[2] = f2; \
166 fp->fp_mant[3] = f3; \
167 return (FPC_QNAN); \
168 } \
169 fp->fp_exp = exp - expbias; \
170 fp->fp_mant[0] = FP_1 | f0; \
171 fp->fp_mant[1] = f1; \
172 fp->fp_mant[2] = f2; \
173 fp->fp_mant[3] = f3; \
174 return (FPC_NUM)
175
176 /*
177 * 32-bit single precision -> fpn.
178 * We assume a single occupies at most (64-FP_LG) bits in the internal
179 * format: i.e., needs at most fp_mant[0] and fp_mant[1].
180 */
181 int
182 fpu_stof(fp, i)
183 register struct fpn *fp;
184 register u_int i;
185 {
186 register int exp;
187 register u_int frac, f0, f1;
188 #define SNG_SHIFT (SNG_FRACBITS - FP_LG)
189
190 exp = (i >> (32 - 1 - SNG_EXPBITS)) & mask(SNG_EXPBITS);
191 frac = i & mask(SNG_FRACBITS);
192 f0 = frac >> SNG_SHIFT;
193 f1 = frac << (32 - SNG_SHIFT);
194 FP_TOF(exp, SNG_EXP_BIAS, frac, f0, f1, 0, 0);
195 }
196
197 /*
198 * 64-bit double -> fpn.
199 * We assume this uses at most (96-FP_LG) bits.
200 */
201 int
202 fpu_dtof(fp, i, j)
203 register struct fpn *fp;
204 register u_int i, j;
205 {
206 register int exp;
207 register u_int frac, f0, f1, f2;
208 #define DBL_SHIFT (DBL_FRACBITS - 32 - FP_LG)
209
210 exp = (i >> (32 - 1 - DBL_EXPBITS)) & mask(DBL_EXPBITS);
211 frac = i & mask(DBL_FRACBITS - 32);
212 f0 = frac >> DBL_SHIFT;
213 f1 = (frac << (32 - DBL_SHIFT)) | (j >> DBL_SHIFT);
214 f2 = j << (32 - DBL_SHIFT);
215 frac |= j;
216 FP_TOF(exp, DBL_EXP_BIAS, frac, f0, f1, f2, 0);
217 }
218
219 /*
220 * 128-bit extended -> fpn.
221 */
222 int
223 fpu_qtof(fp, i, j, k, l)
224 register struct fpn *fp;
225 register u_int i, j, k, l;
226 {
227 register int exp;
228 register u_int frac, f0, f1, f2, f3;
229 #define EXT_SHIFT (-(EXT_FRACBITS - 3 * 32 - FP_LG)) /* left shift! */
230
231 /*
232 * Note that ext and fpn `line up', hence no shifting needed.
233 */
234 exp = (i >> (32 - 1 - EXT_EXPBITS)) & mask(EXT_EXPBITS);
235 frac = i & mask(EXT_FRACBITS - 3 * 32);
236 f0 = (frac << EXT_SHIFT) | (j >> (32 - EXT_SHIFT));
237 f1 = (j << EXT_SHIFT) | (k >> (32 - EXT_SHIFT));
238 f2 = (k << EXT_SHIFT) | (l >> (32 - EXT_SHIFT));
239 f3 = l << EXT_SHIFT;
240 frac |= j | k | l;
241 FP_TOF(exp, EXT_EXP_BIAS, frac, f0, f1, f2, f3);
242 }
243
244 /*
245 * Explode the contents of a register / regpair / regquad.
246 * If the input is a signalling NaN, an NV (invalid) exception
247 * will be set. (Note that nothing but NV can occur until ALU
248 * operations are performed.)
249 */
250 void
251 fpu_explode(fe, fp, type, reg)
252 register struct fpemu *fe;
253 register struct fpn *fp;
254 int type, reg;
255 {
256 register u_int s, *space;
257 #ifdef SUN4U
258 u_int64_t l, *xspace;
259
260 xspace = (u_int64_t *)&fe->fe_fpstate->fs_regs[reg & ~1];
261 l = xspace[0];
262 #endif /* SUN4U */
263 space = &fe->fe_fpstate->fs_regs[reg];
264 s = space[0];
265 fp->fp_sign = s >> 31;
266 fp->fp_sticky = 0;
267 switch (type) {
268 #ifdef SUN4U
269 case FTYPE_LNG:
270 s = fpu_xtof(fp, l);
271 break;
272 #endif /* SUN4U */
273
274 case FTYPE_INT:
275 s = fpu_itof(fp, s);
276 break;
277
278 case FTYPE_SNG:
279 s = fpu_stof(fp, s);
280 break;
281
282 case FTYPE_DBL:
283 s = fpu_dtof(fp, s, space[1]);
284 break;
285
286 case FTYPE_EXT:
287 s = fpu_qtof(fp, s, space[1], space[2], space[3]);
288 break;
289
290 default:
291 panic("fpu_explode");
292 }
293
294 if (s == FPC_QNAN && (fp->fp_mant[0] & FP_QUIETBIT) == 0) {
295 /*
296 * Input is a signalling NaN. All operations that return
297 * an input NaN operand put it through a ``NaN conversion'',
298 * which basically just means ``turn on the quiet bit''.
299 * We do this here so that all NaNs internally look quiet
300 * (we can tell signalling ones by their class).
301 */
302 fp->fp_mant[0] |= FP_QUIETBIT;
303 fe->fe_cx = FSR_NV; /* assert invalid operand */
304 s = FPC_SNAN;
305 }
306 fp->fp_class = s;
307 DPRINTF(FPE_REG, ("fpu_explode: %%%c%d => ", (type == FTYPE_LNG) ? 'x' :
308 ((type == FTYPE_INT) ? 'i' :
309 ((type == FTYPE_SNG) ? 's' :
310 ((type == FTYPE_DBL) ? 'd' :
311 ((type == FTYPE_EXT) ? 'q' : '?')))),
312 reg));
313 #ifdef DEBUG
314 if (fpe_debug & FPE_REG) {
315 if (type == FTYPE_INT) printf("%d ", s);
316 #ifdef SUN4U
317 #ifdef _LP64
318 if (type == FTYPE_LNG) printf("%ld ", l);
319 #else
320 if (type == FTYPE_LNG) printf("%lld ", l);
321 #endif
322 #endif /* SUN4U */
323 }
324 #endif /* DEBUG */
325 DUMPFPN(FPE_REG, fp);
326 DPRINTF(FPE_REG, ("\n"));
327 }
328