/* $NetBSD: fpu_emu.h,v 1.3 2005/12/11 12:18:42 christos Exp $ */12/*-3* SPDX-License-Identifier: BSD-3-Clause4*5* Copyright (c) 1992, 19936* The Regents of the University of California. All rights reserved.7*8* This software was developed by the Computer Systems Engineering group9* at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and10* contributed to Berkeley.11*12* All advertising materials mentioning features or use of this software13* must display the following acknowledgement:14* This product includes software developed by the University of15* California, Lawrence Berkeley Laboratory.16*17* Redistribution and use in source and binary forms, with or without18* modification, are permitted provided that the following conditions19* are met:20* 1. Redistributions of source code must retain the above copyright21* notice, this list of conditions and the following disclaimer.22* 2. Redistributions in binary form must reproduce the above copyright23* notice, this list of conditions and the following disclaimer in the24* documentation and/or other materials provided with the distribution.25* 3. Neither the name of the University nor the names of its contributors26* may be used to endorse or promote products derived from this software27* without specific prior written permission.28*29* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND30* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE31* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE32* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE33* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL34* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS35* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)36* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT37* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY38* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF39* SUCH DAMAGE.40*/4142/*43* Floating point emulator (tailored for SPARC, but structurally44* machine-independent).45*46* Floating point numbers are carried around internally in an `expanded'47* or `unpacked' form consisting of:48* - sign49* - unbiased exponent50* - mantissa (`1.' + 112-bit fraction + guard + round)51* - sticky bit52* Any implied `1' bit is inserted, giving a 113-bit mantissa that is53* always nonzero. Additional low-order `guard' and `round' bits are54* scrunched in, making the entire mantissa 115 bits long. This is divided55* into four 32-bit words, with `spare' bits left over in the upper part56* of the top word (the high bits of fp_mant[0]). An internal `exploded'57* number is thus kept within the half-open interval [1.0,2.0) (but see58* the `number classes' below). This holds even for denormalized numbers:59* when we explode an external denorm, we normalize it, introducing low-order60* zero bits, so that the rest of the code always sees normalized values.61*62* Note that a number of our algorithms use the `spare' bits at the top.63* The most demanding algorithm---the one for sqrt---depends on two such64* bits, so that it can represent values up to (but not including) 8.0,65* and then it needs a carry on top of that, so that we need three `spares'.66*67* The sticky-word is 32 bits so that we can use `OR' operators to goosh68* whole words from the mantissa into it.69*70* All operations are done in this internal extended precision. According71* to Hennesey & Patterson, Appendix A, rounding can be repeated---that is,72* it is OK to do a+b in extended precision and then round the result to73* single precision---provided single, double, and extended precisions are74* `far enough apart' (they always are), but we will try to avoid any such75* extra work where possible.76*/77struct fpn {78int fp_class; /* see below */79int fp_sign; /* 0 => positive, 1 => negative */80int fp_exp; /* exponent (unbiased) */81int fp_sticky; /* nonzero bits lost at right end */82u_int fp_mant[4]; /* 115-bit mantissa */83};8485#define FP_NMANT 115 /* total bits in mantissa (incl g,r) */86#define FP_NG 2 /* number of low-order guard bits */87#define FP_LG ((FP_NMANT - 1) & 31) /* log2(1.0) for fp_mant[0] */88#define FP_LG2 ((FP_NMANT - 1) & 63) /* log2(1.0) for fp_mant[0] and fp_mant[1] */89#define FP_QUIETBIT (1 << (FP_LG - 1)) /* Quiet bit in NaNs (0.5) */90#define FP_1 (1 << FP_LG) /* 1.0 in fp_mant[0] */91#define FP_2 (1 << (FP_LG + 1)) /* 2.0 in fp_mant[0] */9293/*94* Number classes. Since zero, Inf, and NaN cannot be represented using95* the above layout, we distinguish these from other numbers via a class.96* In addition, to make computation easier and to follow Appendix N of97* the SPARC Version 8 standard, we give each kind of NaN a separate class.98*/99#define FPC_SNAN -2 /* signalling NaN (sign irrelevant) */100#define FPC_QNAN -1 /* quiet NaN (sign irrelevant) */101#define FPC_ZERO 0 /* zero (sign matters) */102#define FPC_NUM 1 /* number (sign matters) */103#define FPC_INF 2 /* infinity (sign matters) */104105#define ISSNAN(fp) ((fp)->fp_class == FPC_SNAN)106#define ISQNAN(fp) ((fp)->fp_class == FPC_QNAN)107#define ISNAN(fp) ((fp)->fp_class < 0)108#define ISZERO(fp) ((fp)->fp_class == 0)109#define ISINF(fp) ((fp)->fp_class == FPC_INF)110111/*112* ORDER(x,y) `sorts' a pair of `fpn *'s so that the right operand (y) points113* to the `more significant' operand for our purposes. Appendix N says that114* the result of a computation involving two numbers are:115*116* If both are SNaN: operand 2, converted to Quiet117* If only one is SNaN: the SNaN operand, converted to Quiet118* If both are QNaN: operand 2119* If only one is QNaN: the QNaN operand120*121* In addition, in operations with an Inf operand, the result is usually122* Inf. The class numbers are carefully arranged so that if123* (unsigned)class(op1) > (unsigned)class(op2)124* then op1 is the one we want; otherwise op2 is the one we want.125*/126#define ORDER(x, y) { \127if ((u_int)(x)->fp_class > (u_int)(y)->fp_class) \128SWAP(x, y); \129}130#define SWAP(x, y) { \131struct fpn *swap; \132swap = (x), (x) = (y), (y) = swap; \133}134135/*136* Emulator state.137*/138struct fpemu {139struct fpu *fe_fpstate; /* registers, etc */140int fe_fpscr; /* fpscr copy (modified during op) */141int fe_cx; /* keep track of exceptions */142struct fpn fe_f1; /* operand 1 */143struct fpn fe_f2; /* operand 2, if required */144struct fpn fe_f3; /* available storage for result */145};146147/*148* Arithmetic functions.149* Each of these may modify its inputs (f1,f2) and/or the temporary.150* Each returns a pointer to the result and/or sets exceptions.151*/152struct fpn *fpu_add(struct fpemu *);153#define fpu_sub(fe) ((fe)->fe_f2.fp_sign ^= 1, fpu_add(fe))154struct fpn *fpu_mul(struct fpemu *);155struct fpn *fpu_div(struct fpemu *);156struct fpn *fpu_sqrt(struct fpemu *);157158/*159* Other functions.160*/161162/* Perform a compare instruction (with or without unordered exception). */163void fpu_compare(struct fpemu *, int);164165/* Build a new Quiet NaN (sign=0, frac=all 1's). */166struct fpn *fpu_newnan(struct fpemu *);167168void fpu_norm(struct fpn *);169170/*171* Shift a number right some number of bits, taking care of round/sticky.172* Note that the result is probably not a well-formed number (it will lack173* the normal 1-bit mant[0]&FP_1).174*/175int fpu_shr(struct fpn *, int);176177void fpu_explode(struct fpemu *, struct fpn *, int, int);178void fpu_implode(struct fpemu *, struct fpn *, int, u_int *);179180#ifdef DEBUG181#define FPE_EX 0x1182#define FPE_INSN 0x2183#define FPE_OP 0x4184#define FPE_REG 0x8185extern int fpe_debug;186void fpu_dumpfpn(struct fpn *);187#define DPRINTF(x, y) if (fpe_debug & (x)) printf y188#define DUMPFPN(x, f) if (fpe_debug & (x)) fpu_dumpfpn((f))189#else190#define DPRINTF(x, y)191#define DUMPFPN(x, f)192#endif193194195