1*2c7a42e9Smiod /* $OpenBSD: fpu_emu.h,v 1.7 2024/03/29 21:07:11 miod Exp $ */ 2839f47eaSjason /* $NetBSD: fpu_emu.h,v 1.4 2000/08/03 18:32:07 eeh Exp $ */ 3839f47eaSjason 4839f47eaSjason /* 5839f47eaSjason * Copyright (c) 1992, 1993 6839f47eaSjason * The Regents of the University of California. All rights reserved. 7839f47eaSjason * 8839f47eaSjason * This software was developed by the Computer Systems Engineering group 9839f47eaSjason * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and 10839f47eaSjason * contributed to Berkeley. 11839f47eaSjason * 12839f47eaSjason * All advertising materials mentioning features or use of this software 13839f47eaSjason * must display the following acknowledgement: 14839f47eaSjason * This product includes software developed by the University of 15839f47eaSjason * California, Lawrence Berkeley Laboratory. 16839f47eaSjason * 17839f47eaSjason * Redistribution and use in source and binary forms, with or without 18839f47eaSjason * modification, are permitted provided that the following conditions 19839f47eaSjason * are met: 20839f47eaSjason * 1. Redistributions of source code must retain the above copyright 21839f47eaSjason * notice, this list of conditions and the following disclaimer. 22839f47eaSjason * 2. Redistributions in binary form must reproduce the above copyright 23839f47eaSjason * notice, this list of conditions and the following disclaimer in the 24839f47eaSjason * documentation and/or other materials provided with the distribution. 2529295d1cSmillert * 3. Neither the name of the University nor the names of its contributors 26839f47eaSjason * may be used to endorse or promote products derived from this software 27839f47eaSjason * without specific prior written permission. 28839f47eaSjason * 29839f47eaSjason * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30839f47eaSjason * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31839f47eaSjason * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32839f47eaSjason * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33839f47eaSjason * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34839f47eaSjason * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35839f47eaSjason * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36839f47eaSjason * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37839f47eaSjason * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38839f47eaSjason * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39839f47eaSjason * SUCH DAMAGE. 40839f47eaSjason * 41839f47eaSjason * @(#)fpu_emu.h 8.1 (Berkeley) 6/11/93 42839f47eaSjason */ 43839f47eaSjason 44839f47eaSjason /* 45839f47eaSjason * Floating point emulator (tailored for SPARC, but structurally 46839f47eaSjason * machine-independent). 47839f47eaSjason * 48839f47eaSjason * Floating point numbers are carried around internally in an `expanded' 49839f47eaSjason * or `unpacked' form consisting of: 50839f47eaSjason * - sign 51839f47eaSjason * - unbiased exponent 52839f47eaSjason * - mantissa (`1.' + 112-bit fraction + guard + round) 53839f47eaSjason * - sticky bit 54839f47eaSjason * Any implied `1' bit is inserted, giving a 113-bit mantissa that is 55839f47eaSjason * always nonzero. Additional low-order `guard' and `round' bits are 56839f47eaSjason * scrunched in, making the entire mantissa 115 bits long. This is divided 57839f47eaSjason * into four 32-bit words, with `spare' bits left over in the upper part 58839f47eaSjason * of the top word (the high bits of fp_mant[0]). An internal `exploded' 59839f47eaSjason * number is thus kept within the half-open interval [1.0,2.0) (but see 60839f47eaSjason * the `number classes' below). This holds even for denormalized numbers: 61839f47eaSjason * when we explode an external denorm, we normalize it, introducing low-order 62839f47eaSjason * zero bits, so that the rest of the code always sees normalized values. 63839f47eaSjason * 64839f47eaSjason * Note that a number of our algorithms use the `spare' bits at the top. 65839f47eaSjason * The most demanding algorithm---the one for sqrt---depends on two such 66839f47eaSjason * bits, so that it can represent values up to (but not including) 8.0, 67839f47eaSjason * and then it needs a carry on top of that, so that we need three `spares'. 68839f47eaSjason * 69839f47eaSjason * The sticky-word is 32 bits so that we can use `OR' operators to goosh 70839f47eaSjason * whole words from the mantissa into it. 71839f47eaSjason * 72839f47eaSjason * All operations are done in this internal extended precision. According 73839f47eaSjason * to Hennesey & Patterson, Appendix A, rounding can be repeated---that is, 74839f47eaSjason * it is OK to do a+b in extended precision and then round the result to 75839f47eaSjason * single precision---provided single, double, and extended precisions are 76839f47eaSjason * `far enough apart' (they always are), but we will try to avoid any such 77839f47eaSjason * extra work where possible. 78839f47eaSjason */ 79839f47eaSjason struct fpn { 80839f47eaSjason int fp_class; /* see below */ 81839f47eaSjason int fp_sign; /* 0 => positive, 1 => negative */ 82839f47eaSjason int fp_exp; /* exponent (unbiased) */ 83839f47eaSjason int fp_sticky; /* nonzero bits lost at right end */ 84839f47eaSjason u_int fp_mant[4]; /* 115-bit mantissa */ 85839f47eaSjason }; 86839f47eaSjason 87839f47eaSjason #define FP_NMANT 115 /* total bits in mantissa (incl g,r) */ 88839f47eaSjason #define FP_NG 2 /* number of low-order guard bits */ 89839f47eaSjason #define FP_LG ((FP_NMANT - 1) & 31) /* log2(1.0) for fp_mant[0] */ 90839f47eaSjason #define FP_LG2 ((FP_NMANT - 1) & 63) /* log2(1.0) for fp_mant[0] and fp_mant[1] */ 91839f47eaSjason #define FP_QUIETBIT (1 << (FP_LG - 1)) /* Quiet bit in NaNs (0.5) */ 92839f47eaSjason #define FP_1 (1 << FP_LG) /* 1.0 in fp_mant[0] */ 93839f47eaSjason #define FP_2 (1 << (FP_LG + 1)) /* 2.0 in fp_mant[0] */ 94839f47eaSjason 95839f47eaSjason /* 96839f47eaSjason * Number classes. Since zero, Inf, and NaN cannot be represented using 97839f47eaSjason * the above layout, we distinguish these from other numbers via a class. 98839f47eaSjason * In addition, to make computation easier and to follow Appendix N of 99839f47eaSjason * the SPARC Version 8 standard, we give each kind of NaN a separate class. 100839f47eaSjason */ 101839f47eaSjason #define FPC_SNAN -2 /* signalling NaN (sign irrelevant) */ 102839f47eaSjason #define FPC_QNAN -1 /* quiet NaN (sign irrelevant) */ 103839f47eaSjason #define FPC_ZERO 0 /* zero (sign matters) */ 104839f47eaSjason #define FPC_NUM 1 /* number (sign matters) */ 105839f47eaSjason #define FPC_INF 2 /* infinity (sign matters) */ 106839f47eaSjason 107839f47eaSjason #define ISNAN(fp) ((fp)->fp_class < 0) 108839f47eaSjason #define ISZERO(fp) ((fp)->fp_class == 0) 109839f47eaSjason #define ISINF(fp) ((fp)->fp_class == FPC_INF) 110839f47eaSjason 111839f47eaSjason /* 112839f47eaSjason * ORDER(x,y) `sorts' a pair of `fpn *'s so that the right operand (y) points 113839f47eaSjason * to the `more significant' operand for our purposes. Appendix N says that 114839f47eaSjason * the result of a computation involving two numbers are: 115839f47eaSjason * 116839f47eaSjason * If both are SNaN: operand 2, converted to Quiet 117839f47eaSjason * If only one is SNaN: the SNaN operand, converted to Quiet 118839f47eaSjason * If both are QNaN: operand 2 119839f47eaSjason * If only one is QNaN: the QNaN operand 120839f47eaSjason * 121839f47eaSjason * In addition, in operations with an Inf operand, the result is usually 122839f47eaSjason * Inf. The class numbers are carefully arranged so that if 123839f47eaSjason * (unsigned)class(op1) > (unsigned)class(op2) 124839f47eaSjason * then op1 is the one we want; otherwise op2 is the one we want. 125839f47eaSjason */ 126839f47eaSjason #define ORDER(x, y) { \ 127839f47eaSjason if ((u_int)(x)->fp_class > (u_int)(y)->fp_class) \ 128839f47eaSjason SWAP(x, y); \ 129839f47eaSjason } 130839f47eaSjason #define SWAP(x, y) { \ 131*2c7a42e9Smiod struct fpn *swap; \ 132839f47eaSjason swap = (x), (x) = (y), (y) = swap; \ 133839f47eaSjason } 134839f47eaSjason 135839f47eaSjason /* 136839f47eaSjason * Emulator state. 137839f47eaSjason */ 138839f47eaSjason struct fpemu { 1391c00236eSmiod struct fpstate *fe_fpstate; /* registers, etc */ 140839f47eaSjason int fe_fsr; /* fsr copy (modified during op) */ 141839f47eaSjason int fe_cx; /* exceptions */ 142839f47eaSjason struct fpn fe_f1; /* operand 1 */ 143839f47eaSjason struct fpn fe_f2; /* operand 2, if required */ 144839f47eaSjason struct fpn fe_f3; /* available storage for result */ 145839f47eaSjason }; 146839f47eaSjason 147839f47eaSjason /* 148839f47eaSjason * Arithmetic functions. 149839f47eaSjason * Each of these may modify its inputs (f1,f2) and/or the temporary. 150839f47eaSjason * Each returns a pointer to the result and/or sets exceptions. 151839f47eaSjason */ 152839f47eaSjason struct fpn *fpu_add(struct fpemu *); 153839f47eaSjason #define fpu_sub(fe) ((fe)->fe_f2.fp_sign ^= 1, fpu_add(fe)) 154839f47eaSjason struct fpn *fpu_mul(struct fpemu *); 155839f47eaSjason struct fpn *fpu_div(struct fpemu *); 156839f47eaSjason struct fpn *fpu_sqrt(struct fpemu *); 157839f47eaSjason 158839f47eaSjason /* 159839f47eaSjason * Other functions. 160839f47eaSjason */ 161839f47eaSjason 162839f47eaSjason /* Perform a compare instruction (with or without unordered exception). */ 163839f47eaSjason void fpu_compare(struct fpemu *, int); 164839f47eaSjason 165839f47eaSjason /* Build a new Quiet NaN (sign=0, frac=all 1's). */ 166839f47eaSjason struct fpn *fpu_newnan(struct fpemu *); 167839f47eaSjason 168839f47eaSjason /* 169839f47eaSjason * Shift a number right some number of bits, taking care of round/sticky. 170839f47eaSjason * Note that the result is probably not a well-formed number (it will lack 171839f47eaSjason * the normal 1-bit mant[0]&FP_1). 172839f47eaSjason */ 173839f47eaSjason int fpu_shr(struct fpn *, int); 174839f47eaSjason 175839f47eaSjason void fpu_explode(struct fpemu *, struct fpn *, int, int); 176839f47eaSjason void fpu_implode(struct fpemu *, struct fpn *, int, u_int *); 177839f47eaSjason 178839f47eaSjason #ifdef DEBUG 179839f47eaSjason #define FPE_INSN 0x1 180839f47eaSjason #define FPE_REG 0x2 181e16d5dfcSjason #define FPE_STATE 0x4 182839f47eaSjason extern int fpe_debug; 183839f47eaSjason void fpu_dumpfpn(struct fpn *); 1841c00236eSmiod void fpu_dumpstate(struct fpstate *); 185839f47eaSjason #define DPRINTF(x, y) if (fpe_debug & (x)) printf y 186839f47eaSjason #define DUMPFPN(x, f) if (fpe_debug & (x)) fpu_dumpfpn((f)) 187db7753cfSjason #define DUMPSTATE(x, s) if (fpe_debug & (x)) fpu_dumpstate((s)) 188839f47eaSjason #else 189839f47eaSjason #define DPRINTF(x, y) 190839f47eaSjason #define DUMPFPN(x, f) 191db7753cfSjason #define DUMPSTATE(x, s) 192839f47eaSjason #endif 193