155113Storek /* 2*63319Sbostic * Copyright (c) 1992, 1993 3*63319Sbostic * The Regents of the University of California. All rights reserved. 455113Storek * 555113Storek * This software was developed by the Computer Systems Engineering group 655113Storek * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 755113Storek * contributed to Berkeley. 855113Storek * 955500Sbostic * All advertising materials mentioning features or use of this software 1055500Sbostic * must display the following acknowledgement: 1155500Sbostic * This product includes software developed by the University of 1259195Storek * California, Lawrence Berkeley Laboratory. 1355500Sbostic * 1455113Storek * %sccs.include.redist.c% 1555113Storek * 16*63319Sbostic * @(#)fpu_emu.h 8.1 (Berkeley) 06/11/93 1755113Storek * 1859195Storek * from: $Header: fpu_emu.h,v 1.3 92/11/26 01:30:54 torek Exp $ 1955113Storek */ 2055113Storek 2155113Storek /* 2255113Storek * Floating point emulator (tailored for SPARC, but structurally 2355113Storek * machine-independent). 2455113Storek * 2555113Storek * Floating point numbers are carried around internally in an `expanded' 2655113Storek * or `unpacked' form consisting of: 2755113Storek * - sign 2855113Storek * - unbiased exponent 2955113Storek * - mantissa (`1.' + 112-bit fraction + guard + round) 3055113Storek * - sticky bit 3155113Storek * Any implied `1' bit is inserted, giving a 113-bit mantissa that is 3255113Storek * always nonzero. Additional low-order `guard' and `round' bits are 3355113Storek * scrunched in, making the entire mantissa 115 bits long. This is divided 3455113Storek * into four 32-bit words, with `spare' bits left over in the upper part 3555113Storek * of the top word (the high bits of fp_mant[0]). An internal `exploded' 3655113Storek * number is thus kept within the half-open interval [1.0,2.0) (but see 3755113Storek * the `number classes' below). This holds even for denormalized numbers: 3855113Storek * when we explode an external denorm, we normalize it, introducing low-order 3955113Storek * zero bits, so that the rest of the code always sees normalized values. 4055113Storek * 4155113Storek * Note that a number of our algorithms use the `spare' bits at the top. 4255113Storek * The most demanding algorithm---the one for sqrt---depends on two such 4355113Storek * bits, so that it can represent values up to (but not including) 8.0, 4455113Storek * and then it needs a carry on top of that, so that we need three `spares'. 4555113Storek * 4655113Storek * The sticky-word is 32 bits so that we can use `OR' operators to goosh 4755113Storek * whole words from the mantissa into it. 4855113Storek * 4955113Storek * All operations are done in this internal extended precision. According 5055113Storek * to Hennesey & Patterson, Appendix A, rounding can be repeated---that is, 5155113Storek * it is OK to do a+b in extended precision and then round the result to 5255113Storek * single precision---provided single, double, and extended precisions are 5355113Storek * `far enough apart' (they always are), but we will try to avoid any such 5455113Storek * extra work where possible. 5555113Storek */ 5655113Storek struct fpn { 5755113Storek int fp_class; /* see below */ 5855113Storek int fp_sign; /* 0 => positive, 1 => negative */ 5955113Storek int fp_exp; /* exponent (unbiased) */ 6055113Storek int fp_sticky; /* nonzero bits lost at right end */ 6155113Storek u_int fp_mant[4]; /* 115-bit mantissa */ 6255113Storek }; 6355113Storek 6455113Storek #define FP_NMANT 115 /* total bits in mantissa (incl g,r) */ 6555113Storek #define FP_NG 2 /* number of low-order guard bits */ 6655113Storek #define FP_LG ((FP_NMANT - 1) & 31) /* log2(1.0) for fp_mant[0] */ 6755113Storek #define FP_QUIETBIT (1 << (FP_LG - 1)) /* Quiet bit in NaNs (0.5) */ 6855113Storek #define FP_1 (1 << FP_LG) /* 1.0 in fp_mant[0] */ 6955113Storek #define FP_2 (1 << (FP_LG + 1)) /* 2.0 in fp_mant[0] */ 7055113Storek 7155113Storek /* 7255113Storek * Number classes. Since zero, Inf, and NaN cannot be represented using 7355113Storek * the above layout, we distinguish these from other numbers via a class. 7455113Storek * In addition, to make computation easier and to follow Appendix N of 7555113Storek * the SPARC Version 8 standard, we give each kind of NaN a separate class. 7655113Storek */ 7755113Storek #define FPC_SNAN -2 /* signalling NaN (sign irrelevant) */ 7855113Storek #define FPC_QNAN -1 /* quiet NaN (sign irrelevant) */ 7955113Storek #define FPC_ZERO 0 /* zero (sign matters) */ 8055113Storek #define FPC_NUM 1 /* number (sign matters) */ 8155113Storek #define FPC_INF 2 /* infinity (sign matters) */ 8255113Storek 8355113Storek #define ISNAN(fp) ((fp)->fp_class < 0) 8455113Storek #define ISZERO(fp) ((fp)->fp_class == 0) 8555113Storek #define ISINF(fp) ((fp)->fp_class == FPC_INF) 8655113Storek 8755113Storek /* 8855113Storek * ORDER(x,y) `sorts' a pair of `fpn *'s so that the right operand (y) points 8955113Storek * to the `more significant' operand for our purposes. Appendix N says that 9055113Storek * the result of a computation involving two numbers are: 9155113Storek * 9255113Storek * If both are SNaN: operand 2, converted to Quiet 9355113Storek * If only one is SNaN: the SNaN operand, converted to Quiet 9455113Storek * If both are QNaN: operand 2 9555113Storek * If only one is QNaN: the QNaN operand 9655113Storek * 9755113Storek * In addition, in operations with an Inf operand, the result is usually 9855113Storek * Inf. The class numbers are carefully arranged so that if 9955113Storek * (unsigned)class(op1) > (unsigned)class(op2) 10055113Storek * then op1 is the one we want; otherwise op2 is the one we want. 10155113Storek */ 10255113Storek #define ORDER(x, y) { \ 10355113Storek if ((u_int)(x)->fp_class > (u_int)(y)->fp_class) \ 10455113Storek SWAP(x, y); \ 10555113Storek } 10655113Storek #define SWAP(x, y) { \ 10755113Storek register struct fpn *swap; \ 10855113Storek swap = (x), (x) = (y), (y) = swap; \ 10955113Storek } 11055113Storek 11155113Storek /* 11255113Storek * Emulator state. 11355113Storek */ 11455113Storek struct fpemu { 11555113Storek struct fpstate *fe_fpstate; /* registers, etc */ 11655113Storek int fe_fsr; /* fsr copy (modified during op) */ 11755113Storek int fe_cx; /* exceptions */ 11855113Storek struct fpn fe_f1; /* operand 1 */ 11955113Storek struct fpn fe_f2; /* operand 2, if required */ 12055113Storek struct fpn fe_f3; /* available storage for result */ 12155113Storek }; 12255113Storek 12355113Storek /* 12455113Storek * Arithmetic functions. 12555113Storek * Each of these may modify its inputs (f1,f2) and/or the temporary. 12655113Storek * Each returns a pointer to the result and/or sets exceptions. 12755113Storek */ 12855113Storek struct fpn *fpu_add(struct fpemu *); 12955113Storek #define fpu_sub(fe) ((fe)->fe_f2.fp_sign ^= 1, fpu_add(fe)) 13055113Storek struct fpn *fpu_mul(struct fpemu *); 13155113Storek struct fpn *fpu_div(struct fpemu *); 13255113Storek struct fpn *fpu_sqrt(struct fpemu *); 13355113Storek 13455113Storek /* 13555113Storek * Other functions. 13655113Storek */ 13755113Storek 13855113Storek /* Perform a compare instruction (with or without unordered exception). */ 13955113Storek void fpu_compare(struct fpemu *, int); 14055113Storek 14155113Storek /* Build a new Quiet NaN (sign=0, frac=all 1's). */ 14255113Storek struct fpn *fpu_newnan(struct fpemu *); 14355113Storek 14455113Storek /* 14555113Storek * Shift a number right some number of bits, taking care of round/sticky. 14655113Storek * Note that the result is probably not a well-formed number (it will lack 14755113Storek * the normal 1-bit mant[0]&FP_1). 14855113Storek */ 14955113Storek int fpu_shr(struct fpn *, int); 15055113Storek 15155113Storek /* Conversion to and from internal format -- note asymmetry. */ 15255113Storek int fpu_itofpn(struct fpn *, u_int); 15355113Storek int fpu_stofpn(struct fpn *, u_int); 15455113Storek int fpu_dtofpn(struct fpn *, u_int, u_int); 15555113Storek int fpu_xtofpn(struct fpn *, u_int, u_int, u_int, u_int); 15655113Storek 15755113Storek u_int fpu_fpntoi(struct fpemu *, struct fpn *); 15855113Storek u_int fpu_fpntos(struct fpemu *, struct fpn *); 15955113Storek u_int fpu_fpntod(struct fpemu *, struct fpn *); 16055113Storek u_int fpu_fpntox(struct fpemu *, struct fpn *); 16155113Storek 16255113Storek void fpu_explode(struct fpemu *, struct fpn *, int, int); 16355113Storek void fpu_implode(struct fpemu *, struct fpn *, int, u_int *); 164