1 /* $NetBSD: fpu_emu.h,v 1.5 2001/12/04 00:05:04 darrenr 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_emu.h 8.1 (Berkeley) 6/11/93 45 */ 46 47 #if defined(_KERNEL_OPT) 48 #include "opt_sparc_arch.h" 49 #endif 50 51 /* 52 * Floating point emulator (tailored for SPARC, but structurally 53 * machine-independent). 54 * 55 * Floating point numbers are carried around internally in an `expanded' 56 * or `unpacked' form consisting of: 57 * - sign 58 * - unbiased exponent 59 * - mantissa (`1.' + 112-bit fraction + guard + round) 60 * - sticky bit 61 * Any implied `1' bit is inserted, giving a 113-bit mantissa that is 62 * always nonzero. Additional low-order `guard' and `round' bits are 63 * scrunched in, making the entire mantissa 115 bits long. This is divided 64 * into four 32-bit words, with `spare' bits left over in the upper part 65 * of the top word (the high bits of fp_mant[0]). An internal `exploded' 66 * number is thus kept within the half-open interval [1.0,2.0) (but see 67 * the `number classes' below). This holds even for denormalized numbers: 68 * when we explode an external denorm, we normalize it, introducing low-order 69 * zero bits, so that the rest of the code always sees normalized values. 70 * 71 * Note that a number of our algorithms use the `spare' bits at the top. 72 * The most demanding algorithm---the one for sqrt---depends on two such 73 * bits, so that it can represent values up to (but not including) 8.0, 74 * and then it needs a carry on top of that, so that we need three `spares'. 75 * 76 * The sticky-word is 32 bits so that we can use `OR' operators to goosh 77 * whole words from the mantissa into it. 78 * 79 * All operations are done in this internal extended precision. According 80 * to Hennesey & Patterson, Appendix A, rounding can be repeated---that is, 81 * it is OK to do a+b in extended precision and then round the result to 82 * single precision---provided single, double, and extended precisions are 83 * `far enough apart' (they always are), but we will try to avoid any such 84 * extra work where possible. 85 */ 86 struct fpn { 87 int fp_class; /* see below */ 88 int fp_sign; /* 0 => positive, 1 => negative */ 89 int fp_exp; /* exponent (unbiased) */ 90 int fp_sticky; /* nonzero bits lost at right end */ 91 u_int fp_mant[4]; /* 115-bit mantissa */ 92 }; 93 94 #define FP_NMANT 115 /* total bits in mantissa (incl g,r) */ 95 #define FP_NG 2 /* number of low-order guard bits */ 96 #define FP_LG ((FP_NMANT - 1) & 31) /* log2(1.0) for fp_mant[0] */ 97 #define FP_LG2 ((FP_NMANT - 1) & 63) /* log2(1.0) for fp_mant[0] and fp_mant[1] */ 98 #define FP_QUIETBIT (1 << (FP_LG - 1)) /* Quiet bit in NaNs (0.5) */ 99 #define FP_1 (1 << FP_LG) /* 1.0 in fp_mant[0] */ 100 #define FP_2 (1 << (FP_LG + 1)) /* 2.0 in fp_mant[0] */ 101 102 /* 103 * Number classes. Since zero, Inf, and NaN cannot be represented using 104 * the above layout, we distinguish these from other numbers via a class. 105 * In addition, to make computation easier and to follow Appendix N of 106 * the SPARC Version 8 standard, we give each kind of NaN a separate class. 107 */ 108 #define FPC_SNAN -2 /* signalling NaN (sign irrelevant) */ 109 #define FPC_QNAN -1 /* quiet NaN (sign irrelevant) */ 110 #define FPC_ZERO 0 /* zero (sign matters) */ 111 #define FPC_NUM 1 /* number (sign matters) */ 112 #define FPC_INF 2 /* infinity (sign matters) */ 113 114 #define ISNAN(fp) ((fp)->fp_class < 0) 115 #define ISZERO(fp) ((fp)->fp_class == 0) 116 #define ISINF(fp) ((fp)->fp_class == FPC_INF) 117 118 /* 119 * ORDER(x,y) `sorts' a pair of `fpn *'s so that the right operand (y) points 120 * to the `more significant' operand for our purposes. Appendix N says that 121 * the result of a computation involving two numbers are: 122 * 123 * If both are SNaN: operand 2, converted to Quiet 124 * If only one is SNaN: the SNaN operand, converted to Quiet 125 * If both are QNaN: operand 2 126 * If only one is QNaN: the QNaN operand 127 * 128 * In addition, in operations with an Inf operand, the result is usually 129 * Inf. The class numbers are carefully arranged so that if 130 * (unsigned)class(op1) > (unsigned)class(op2) 131 * then op1 is the one we want; otherwise op2 is the one we want. 132 */ 133 #define ORDER(x, y) { \ 134 if ((u_int)(x)->fp_class > (u_int)(y)->fp_class) \ 135 SWAP(x, y); \ 136 } 137 #define SWAP(x, y) { \ 138 register struct fpn *swap; \ 139 swap = (x), (x) = (y), (y) = swap; \ 140 } 141 142 /* 143 * Emulator state. 144 */ 145 struct fpemu { 146 #ifndef SUN4U 147 struct fpstate *fe_fpstate; /* registers, etc */ 148 #else /* SUN4U */ 149 struct fpstate64 *fe_fpstate; /* registers, etc */ 150 #endif /* SUN4U */ 151 int fe_fsr; /* fsr copy (modified during op) */ 152 int fe_cx; /* exceptions */ 153 struct fpn fe_f1; /* operand 1 */ 154 struct fpn fe_f2; /* operand 2, if required */ 155 struct fpn fe_f3; /* available storage for result */ 156 }; 157 158 /* 159 * Arithmetic functions. 160 * Each of these may modify its inputs (f1,f2) and/or the temporary. 161 * Each returns a pointer to the result and/or sets exceptions. 162 */ 163 struct fpn *fpu_add(struct fpemu *); 164 #define fpu_sub(fe) ((fe)->fe_f2.fp_sign ^= 1, fpu_add(fe)) 165 struct fpn *fpu_mul(struct fpemu *); 166 struct fpn *fpu_div(struct fpemu *); 167 struct fpn *fpu_sqrt(struct fpemu *); 168 169 /* 170 * Other functions. 171 */ 172 173 /* Perform a compare instruction (with or without unordered exception). */ 174 void fpu_compare(struct fpemu *, int); 175 176 /* Build a new Quiet NaN (sign=0, frac=all 1's). */ 177 struct fpn *fpu_newnan(struct fpemu *); 178 179 /* 180 * Shift a number right some number of bits, taking care of round/sticky. 181 * Note that the result is probably not a well-formed number (it will lack 182 * the normal 1-bit mant[0]&FP_1). 183 */ 184 int fpu_shr(struct fpn *, int); 185 186 void fpu_explode(struct fpemu *, struct fpn *, int, int); 187 void fpu_implode(struct fpemu *, struct fpn *, int, u_int *); 188 189 #ifdef DEBUG 190 #define FPE_INSN 0x1 191 #define FPE_REG 0x2 192 extern int fpe_debug; 193 void fpu_dumpfpn(struct fpn *); 194 #define DPRINTF(x, y) if (fpe_debug & (x)) printf y 195 #define DUMPFPN(x, f) if (fpe_debug & (x)) fpu_dumpfpn((f)) 196 #else 197 #define DPRINTF(x, y) 198 #define DUMPFPN(x, f) 199 #endif 200