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