fpu_emulate.h revision 1.9.8.2 1 1.9.8.2 toshii /* $NetBSD: fpu_emulate.h,v 1.9.8.2 2001/07/05 08:38:26 toshii Exp $ */
2 1.9.8.2 toshii
3 1.9.8.2 toshii /*
4 1.9.8.2 toshii * Copyright (c) 1995 Gordon Ross
5 1.9.8.2 toshii * Copyright (c) 1995 Ken Nakata
6 1.9.8.2 toshii * All rights reserved.
7 1.9.8.2 toshii *
8 1.9.8.2 toshii * Redistribution and use in source and binary forms, with or without
9 1.9.8.2 toshii * modification, are permitted provided that the following conditions
10 1.9.8.2 toshii * are met:
11 1.9.8.2 toshii * 1. Redistributions of source code must retain the above copyright
12 1.9.8.2 toshii * notice, this list of conditions and the following disclaimer.
13 1.9.8.2 toshii * 2. Redistributions in binary form must reproduce the above copyright
14 1.9.8.2 toshii * notice, this list of conditions and the following disclaimer in the
15 1.9.8.2 toshii * documentation and/or other materials provided with the distribution.
16 1.9.8.2 toshii * 3. The name of the author may not be used to endorse or promote products
17 1.9.8.2 toshii * derived from this software without specific prior written permission.
18 1.9.8.2 toshii * 4. All advertising materials mentioning features or use of this software
19 1.9.8.2 toshii * must display the following acknowledgement:
20 1.9.8.2 toshii * This product includes software developed by Gordon Ross
21 1.9.8.2 toshii *
22 1.9.8.2 toshii * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 1.9.8.2 toshii * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 1.9.8.2 toshii * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 1.9.8.2 toshii * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.9.8.2 toshii * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.9.8.2 toshii * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 1.9.8.2 toshii * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 1.9.8.2 toshii * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 1.9.8.2 toshii * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 1.9.8.2 toshii * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 1.9.8.2 toshii */
33 1.9.8.2 toshii
34 1.9.8.2 toshii #ifndef _FPU_EMULATE_H_
35 1.9.8.2 toshii #define _FPU_EMULATE_H_
36 1.9.8.2 toshii
37 1.9.8.2 toshii #include <sys/types.h>
38 1.9.8.2 toshii
39 1.9.8.2 toshii /*
40 1.9.8.2 toshii * Floating point emulator (tailored for SPARC/modified for m68k, but
41 1.9.8.2 toshii * structurally machine-independent).
42 1.9.8.2 toshii *
43 1.9.8.2 toshii * Floating point numbers are carried around internally in an `expanded'
44 1.9.8.2 toshii * or `unpacked' form consisting of:
45 1.9.8.2 toshii * - sign
46 1.9.8.2 toshii * - unbiased exponent
47 1.9.8.2 toshii * - mantissa (`1.' + 80-bit fraction + guard + round)
48 1.9.8.2 toshii * - sticky bit
49 1.9.8.2 toshii * Any implied `1' bit is inserted, giving a 81-bit mantissa that is
50 1.9.8.2 toshii * always nonzero. Additional low-order `guard' and `round' bits are
51 1.9.8.2 toshii * scrunched in, making the entire mantissa 83 bits long. This is divided
52 1.9.8.2 toshii * into three 32-bit words, with `spare' bits left over in the upper part
53 1.9.8.2 toshii * of the top word (the high bits of fp_mant[0]). An internal `exploded'
54 1.9.8.2 toshii * number is thus kept within the half-open interval [1.0,2.0) (but see
55 1.9.8.2 toshii * the `number classes' below). This holds even for denormalized numbers:
56 1.9.8.2 toshii * when we explode an external denorm, we normalize it, introducing low-order
57 1.9.8.2 toshii * zero bits, so that the rest of the code always sees normalized values.
58 1.9.8.2 toshii *
59 1.9.8.2 toshii * Note that a number of our algorithms use the `spare' bits at the top.
60 1.9.8.2 toshii * The most demanding algorithm---the one for sqrt---depends on two such
61 1.9.8.2 toshii * bits, so that it can represent values up to (but not including) 8.0,
62 1.9.8.2 toshii * and then it needs a carry on top of that, so that we need three `spares'.
63 1.9.8.2 toshii *
64 1.9.8.2 toshii * The sticky-word is 32 bits so that we can use `OR' operators to goosh
65 1.9.8.2 toshii * whole words from the mantissa into it.
66 1.9.8.2 toshii *
67 1.9.8.2 toshii * All operations are done in this internal extended precision. According
68 1.9.8.2 toshii * to Hennesey & Patterson, Appendix A, rounding can be repeated---that is,
69 1.9.8.2 toshii * it is OK to do a+b in extended precision and then round the result to
70 1.9.8.2 toshii * single precision---provided single, double, and extended precisions are
71 1.9.8.2 toshii * `far enough apart' (they always are), but we will try to avoid any such
72 1.9.8.2 toshii * extra work where possible.
73 1.9.8.2 toshii */
74 1.9.8.2 toshii struct fpn {
75 1.9.8.2 toshii int fp_class; /* see below */
76 1.9.8.2 toshii int fp_sign; /* 0 => positive, 1 => negative */
77 1.9.8.2 toshii int fp_exp; /* exponent (unbiased) */
78 1.9.8.2 toshii int fp_sticky; /* nonzero bits lost at right end */
79 1.9.8.2 toshii u_int fp_mant[3]; /* 83-bit mantissa */
80 1.9.8.2 toshii };
81 1.9.8.2 toshii
82 1.9.8.2 toshii #define FP_NMANT 83 /* total bits in mantissa (incl g,r) */
83 1.9.8.2 toshii #define FP_NG 2 /* number of low-order guard bits */
84 1.9.8.2 toshii #define FP_LG ((FP_NMANT - 1) & 31) /* log2(1.0) for fp_mant[0] */
85 1.9.8.2 toshii #define FP_QUIETBIT (1 << (FP_LG - 1)) /* Quiet bit in NaNs (0.5) */
86 1.9.8.2 toshii #define FP_1 (1 << FP_LG) /* 1.0 in fp_mant[0] */
87 1.9.8.2 toshii #define FP_2 (1 << (FP_LG + 1)) /* 2.0 in fp_mant[0] */
88 1.9.8.2 toshii
89 1.9.8.2 toshii #define CPYFPN(dst, src) \
90 1.9.8.2 toshii if ((dst) != (src)) { \
91 1.9.8.2 toshii (dst)->fp_class = (src)->fp_class; \
92 1.9.8.2 toshii (dst)->fp_sign = (src)->fp_sign; \
93 1.9.8.2 toshii (dst)->fp_exp = (src)->fp_exp; \
94 1.9.8.2 toshii (dst)->fp_sticky = (src)->fp_sticky; \
95 1.9.8.2 toshii (dst)->fp_mant[0] = (src)->fp_mant[0]; \
96 1.9.8.2 toshii (dst)->fp_mant[1] = (src)->fp_mant[1]; \
97 1.9.8.2 toshii (dst)->fp_mant[2] = (src)->fp_mant[2]; \
98 1.9.8.2 toshii }
99 1.9.8.2 toshii
100 1.9.8.2 toshii /*
101 1.9.8.2 toshii * Number classes. Since zero, Inf, and NaN cannot be represented using
102 1.9.8.2 toshii * the above layout, we distinguish these from other numbers via a class.
103 1.9.8.2 toshii */
104 1.9.8.2 toshii #define FPC_SNAN -2 /* signalling NaN (sign irrelevant) */
105 1.9.8.2 toshii #define FPC_QNAN -1 /* quiet NaN (sign irrelevant) */
106 1.9.8.2 toshii #define FPC_ZERO 0 /* zero (sign matters) */
107 1.9.8.2 toshii #define FPC_NUM 1 /* number (sign matters) */
108 1.9.8.2 toshii #define FPC_INF 2 /* infinity (sign matters) */
109 1.9.8.2 toshii
110 1.9.8.2 toshii #define ISNAN(fp) ((fp)->fp_class < 0)
111 1.9.8.2 toshii #define ISZERO(fp) ((fp)->fp_class == 0)
112 1.9.8.2 toshii #define ISINF(fp) ((fp)->fp_class == FPC_INF)
113 1.9.8.2 toshii
114 1.9.8.2 toshii /*
115 1.9.8.2 toshii * ORDER(x,y) `sorts' a pair of `fpn *'s so that the right operand (y) points
116 1.9.8.2 toshii * to the `more significant' operand for our purposes. Appendix N says that
117 1.9.8.2 toshii * the result of a computation involving two numbers are:
118 1.9.8.2 toshii *
119 1.9.8.2 toshii * If both are SNaN: operand 2, converted to Quiet
120 1.9.8.2 toshii * If only one is SNaN: the SNaN operand, converted to Quiet
121 1.9.8.2 toshii * If both are QNaN: operand 2
122 1.9.8.2 toshii * If only one is QNaN: the QNaN operand
123 1.9.8.2 toshii *
124 1.9.8.2 toshii * In addition, in operations with an Inf operand, the result is usually
125 1.9.8.2 toshii * Inf. The class numbers are carefully arranged so that if
126 1.9.8.2 toshii * (unsigned)class(op1) > (unsigned)class(op2)
127 1.9.8.2 toshii * then op1 is the one we want; otherwise op2 is the one we want.
128 1.9.8.2 toshii */
129 1.9.8.2 toshii #define ORDER(x, y) { \
130 1.9.8.2 toshii if ((u_int)(x)->fp_class > (u_int)(y)->fp_class) \
131 1.9.8.2 toshii SWAP(x, y); \
132 1.9.8.2 toshii }
133 1.9.8.2 toshii #define SWAP(x, y) { \
134 1.9.8.2 toshii register struct fpn *swap; \
135 1.9.8.2 toshii swap = (x), (x) = (y), (y) = swap; \
136 1.9.8.2 toshii }
137 1.9.8.2 toshii
138 1.9.8.2 toshii /*
139 1.9.8.2 toshii * Emulator state.
140 1.9.8.2 toshii */
141 1.9.8.2 toshii struct fpemu {
142 1.9.8.2 toshii struct frame *fe_frame; /* integer regs, etc */
143 1.9.8.2 toshii struct fpframe *fe_fpframe; /* FP registers, etc */
144 1.9.8.2 toshii u_int fe_fpsr; /* fpsr copy (modified during op) */
145 1.9.8.2 toshii u_int fe_fpcr; /* fpcr copy */
146 1.9.8.2 toshii struct fpn fe_f1; /* operand 1 */
147 1.9.8.2 toshii struct fpn fe_f2; /* operand 2, if required */
148 1.9.8.2 toshii struct fpn fe_f3; /* available storage for result */
149 1.9.8.2 toshii };
150 1.9.8.2 toshii
151 1.9.8.2 toshii /*****************************************************************************
152 1.9.8.2 toshii * End of definitions derived from Sparc FPE
153 1.9.8.2 toshii *****************************************************************************/
154 1.9.8.2 toshii
155 1.9.8.2 toshii /*
156 1.9.8.2 toshii * Internal info about a decoded effective address.
157 1.9.8.2 toshii */
158 1.9.8.2 toshii struct insn_ea {
159 1.9.8.2 toshii int ea_regnum;
160 1.9.8.2 toshii int ea_ext[3]; /* extension words if any */
161 1.9.8.2 toshii int ea_flags; /* flags == 0 means mode 2: An@ */
162 1.9.8.2 toshii #define EA_DIRECT 0x001 /* mode [01]: Dn or An */
163 1.9.8.2 toshii #define EA_PREDECR 0x002 /* mode 4: An@- */
164 1.9.8.2 toshii #define EA_POSTINCR 0x004 /* mode 3: An@+ */
165 1.9.8.2 toshii #define EA_OFFSET 0x008 /* mode 5 or (7,2): APC@(d16) */
166 1.9.8.2 toshii #define EA_INDEXED 0x010 /* mode 6 or (7,3): APC@(Xn:*:*,d8) etc */
167 1.9.8.2 toshii #define EA_ABS 0x020 /* mode (7,[01]): abs */
168 1.9.8.2 toshii #define EA_PC_REL 0x040 /* mode (7,[23]): PC@(d16) etc */
169 1.9.8.2 toshii #define EA_IMMED 0x080 /* mode (7,4): #immed */
170 1.9.8.2 toshii #define EA_MEM_INDIR 0x100 /* mode 6 or (7,3): APC@(Xn:*:*,*)@(*) etc */
171 1.9.8.2 toshii #define EA_BASE_SUPPRSS 0x200 /* mode 6 or (7,3): base register suppressed */
172 1.9.8.2 toshii #define EA_FRAME_EA 0x400 /* MC68LC040 only: precalculated EA from
173 1.9.8.2 toshii format 4 stack frame */
174 1.9.8.2 toshii int ea_moffs; /* offset used for fmoveMulti */
175 1.9.8.2 toshii };
176 1.9.8.2 toshii
177 1.9.8.2 toshii #define ea_offset ea_ext[0] /* mode 5: offset word */
178 1.9.8.2 toshii #define ea_absaddr ea_ext[0] /* mode (7,[01]): absolute address */
179 1.9.8.2 toshii #define ea_immed ea_ext /* mode (7,4): immediate value */
180 1.9.8.2 toshii #define ea_basedisp ea_ext[0] /* mode 6: base displacement */
181 1.9.8.2 toshii #define ea_outerdisp ea_ext[1] /* mode 6: outer displacement */
182 1.9.8.2 toshii #define ea_idxreg ea_ext[2] /* mode 6: index register number */
183 1.9.8.2 toshii #define ea_fea ea_ext[0] /* MC68LC040 only: frame EA */
184 1.9.8.2 toshii
185 1.9.8.2 toshii struct instruction {
186 1.9.8.2 toshii u_int is_pc; /* insn's address */
187 1.9.8.2 toshii u_int is_nextpc; /* next PC */
188 1.9.8.2 toshii int is_advance; /* length of instruction */
189 1.9.8.2 toshii int is_datasize; /* size of memory operand */
190 1.9.8.2 toshii int is_opcode; /* opcode word */
191 1.9.8.2 toshii int is_word1; /* second word */
192 1.9.8.2 toshii struct insn_ea is_ea; /* decoded effective address mode */
193 1.9.8.2 toshii };
194 1.9.8.2 toshii
195 1.9.8.2 toshii /*
196 1.9.8.2 toshii * FP data types
197 1.9.8.2 toshii */
198 1.9.8.2 toshii #define FTYPE_LNG 0 /* Long Word Integer */
199 1.9.8.2 toshii #define FTYPE_SNG 1 /* Single Prec */
200 1.9.8.2 toshii #define FTYPE_EXT 2 /* Extended Prec */
201 1.9.8.2 toshii #define FTYPE_BCD 3 /* Packed BCD */
202 1.9.8.2 toshii #define FTYPE_WRD 4 /* Word Integer */
203 1.9.8.2 toshii #define FTYPE_DBL 5 /* Double Prec */
204 1.9.8.2 toshii #define FTYPE_BYT 6 /* Byte Integer */
205 1.9.8.2 toshii
206 1.9.8.2 toshii /*
207 1.9.8.2 toshii * MC68881/68882 FPcr bit definitions (should these go to <m68k/reg.h>
208 1.9.8.2 toshii * or <m68k/fpu.h> or something?)
209 1.9.8.2 toshii */
210 1.9.8.2 toshii
211 1.9.8.2 toshii /* fpsr */
212 1.9.8.2 toshii #define FPSR_CCB 0xff000000
213 1.9.8.2 toshii # define FPSR_NEG 0x08000000
214 1.9.8.2 toshii # define FPSR_ZERO 0x04000000
215 1.9.8.2 toshii # define FPSR_INF 0x02000000
216 1.9.8.2 toshii # define FPSR_NAN 0x01000000
217 1.9.8.2 toshii #define FPSR_QTT 0x00ff0000
218 1.9.8.2 toshii # define FPSR_QSG 0x00800000
219 1.9.8.2 toshii # define FPSR_QUO 0x007f0000
220 1.9.8.2 toshii #define FPSR_EXCP 0x0000ff00
221 1.9.8.2 toshii # define FPSR_BSUN 0x00008000
222 1.9.8.2 toshii # define FPSR_SNAN 0x00004000
223 1.9.8.2 toshii # define FPSR_OPERR 0x00002000
224 1.9.8.2 toshii # define FPSR_OVFL 0x00001000
225 1.9.8.2 toshii # define FPSR_UNFL 0x00000800
226 1.9.8.2 toshii # define FPSR_DZ 0x00000400
227 1.9.8.2 toshii # define FPSR_INEX2 0x00000200
228 1.9.8.2 toshii # define FPSR_INEX1 0x00000100
229 1.9.8.2 toshii #define FPSR_AEX 0x000000ff
230 1.9.8.2 toshii # define FPSR_AIOP 0x00000080
231 1.9.8.2 toshii # define FPSR_AOVFL 0x00000040
232 1.9.8.2 toshii # define FPSR_AUNFL 0x00000020
233 1.9.8.2 toshii # define FPSR_ADZ 0x00000010
234 1.9.8.2 toshii # define FPSR_AINEX 0x00000008
235 1.9.8.2 toshii
236 1.9.8.2 toshii /* fpcr */
237 1.9.8.2 toshii #define FPCR_EXCP FPSR_EXCP
238 1.9.8.2 toshii # define FPCR_BSUN FPSR_BSUN
239 1.9.8.2 toshii # define FPCR_SNAN FPSR_SNAN
240 1.9.8.2 toshii # define FPCR_OPERR FPSR_OPERR
241 1.9.8.2 toshii # define FPCR_OVFL FPSR_OVFL
242 1.9.8.2 toshii # define FPCR_UNFL FPSR_UNFL
243 1.9.8.2 toshii # define FPCR_DZ FPSR_DZ
244 1.9.8.2 toshii # define FPCR_INEX2 FPSR_INEX2
245 1.9.8.2 toshii # define FPCR_INEX1 FPSR_INEX1
246 1.9.8.2 toshii #define FPCR_MODE 0x000000ff
247 1.9.8.2 toshii # define FPCR_PREC 0x000000c0
248 1.9.8.2 toshii # define FPCR_EXTD 0x00000000
249 1.9.8.2 toshii # define FPCR_SNGL 0x00000040
250 1.9.8.2 toshii # define FPCR_DBL 0x00000080
251 1.9.8.2 toshii # define FPCR_ROUND 0x00000030
252 1.9.8.2 toshii # define FPCR_NEAR 0x00000000
253 1.9.8.2 toshii # define FPCR_ZERO 0x00000010
254 1.9.8.2 toshii # define FPCR_MINF 0x00000020
255 1.9.8.2 toshii # define FPCR_PINF 0x00000030
256 1.9.8.2 toshii
257 1.9.8.2 toshii /*
258 1.9.8.2 toshii * Other functions.
259 1.9.8.2 toshii */
260 1.9.8.2 toshii
261 1.9.8.2 toshii /* Build a new Quiet NaN (sign=0, frac=all 1's). */
262 1.9.8.2 toshii struct fpn *fpu_newnan __P((struct fpemu *fe));
263 1.9.8.2 toshii
264 1.9.8.2 toshii /*
265 1.9.8.2 toshii * Shift a number right some number of bits, taking care of round/sticky.
266 1.9.8.2 toshii * Note that the result is probably not a well-formed number (it will lack
267 1.9.8.2 toshii * the normal 1-bit mant[0]&FP_1).
268 1.9.8.2 toshii */
269 1.9.8.2 toshii int fpu_shr __P((struct fpn * fp, int shr));
270 1.9.8.2 toshii /*
271 1.9.8.2 toshii * Round a number according to the round mode in FPCR
272 1.9.8.2 toshii */
273 1.9.8.2 toshii int fpu_round __P((register struct fpemu *fe, register struct fpn *fp));
274 1.9.8.2 toshii
275 1.9.8.2 toshii /* type conversion */
276 1.9.8.2 toshii void fpu_explode __P((struct fpemu *fe, struct fpn *fp, int t, u_int *src));
277 1.9.8.2 toshii void fpu_implode __P((struct fpemu *fe, struct fpn *fp, int t, u_int *dst));
278 1.9.8.2 toshii
279 1.9.8.2 toshii /*
280 1.9.8.2 toshii * non-static emulation functions
281 1.9.8.2 toshii */
282 1.9.8.2 toshii /* type 0 */
283 1.9.8.2 toshii int fpu_emul_fmovecr __P((struct fpemu *fe, struct instruction *insn));
284 1.9.8.2 toshii int fpu_emul_fstore __P((struct fpemu *fe, struct instruction *insn));
285 1.9.8.2 toshii int fpu_emul_fscale __P((struct fpemu *fe, struct instruction *insn));
286 1.9.8.2 toshii
287 1.9.8.2 toshii /*
288 1.9.8.2 toshii * include function declarations of those which are called by fpu_emul_arith()
289 1.9.8.2 toshii */
290 1.9.8.2 toshii #include "fpu_arith_proto.h"
291 1.9.8.2 toshii
292 1.9.8.2 toshii int fpu_emulate __P((struct frame *frame, struct fpframe *fpf));
293 1.9.8.2 toshii
294 1.9.8.2 toshii /*
295 1.9.8.2 toshii * "helper" functions
296 1.9.8.2 toshii */
297 1.9.8.2 toshii /* return values from constant rom */
298 1.9.8.2 toshii struct fpn *fpu_const __P((struct fpn *fp, u_int offset));
299 1.9.8.2 toshii /* update exceptions and FPSR */
300 1.9.8.2 toshii int fpu_upd_excp __P((struct fpemu *fe));
301 1.9.8.2 toshii u_int fpu_upd_fpsr __P((struct fpemu *fe, struct fpn *fp));
302 1.9.8.2 toshii
303 1.9.8.2 toshii /* address mode decoder, and load/store */
304 1.9.8.2 toshii int fpu_decode_ea __P((struct frame *frame, struct instruction *insn,
305 1.9.8.2 toshii struct insn_ea *ea, int modreg));
306 1.9.8.2 toshii int fpu_load_ea __P((struct frame *frame, struct instruction *insn,
307 1.9.8.2 toshii struct insn_ea *ea, char *dst));
308 1.9.8.2 toshii int fpu_store_ea __P((struct frame *frame, struct instruction *insn,
309 1.9.8.2 toshii struct insn_ea *ea, char *src));
310 1.9.8.2 toshii
311 1.9.8.2 toshii /* fpu_subr.c */
312 1.9.8.2 toshii void fpu_norm __P((register struct fpn *fp));
313 1.9.8.2 toshii
314 1.9.8.2 toshii #if !defined(FPE_DEBUG)
315 1.9.8.2 toshii # define FPE_DEBUG 0
316 1.9.8.2 toshii #endif
317 1.9.8.2 toshii
318 1.9.8.2 toshii #endif /* _FPU_EMULATE_H_ */
319