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