fpu_emu.h revision 1.12 1 1.12 rin /* $NetBSD: fpu_emu.h,v 1.12 2022/09/07 02:41:39 rin Exp $ */
2 1.1 simonb
3 1.1 simonb /*
4 1.1 simonb * Copyright (c) 1992, 1993
5 1.1 simonb * The Regents of the University of California. All rights reserved.
6 1.1 simonb *
7 1.1 simonb * This software was developed by the Computer Systems Engineering group
8 1.1 simonb * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9 1.1 simonb * contributed to Berkeley.
10 1.1 simonb *
11 1.1 simonb * All advertising materials mentioning features or use of this software
12 1.1 simonb * must display the following acknowledgement:
13 1.1 simonb * This product includes software developed by the University of
14 1.1 simonb * California, Lawrence Berkeley Laboratory.
15 1.1 simonb *
16 1.1 simonb * Redistribution and use in source and binary forms, with or without
17 1.1 simonb * modification, are permitted provided that the following conditions
18 1.1 simonb * are met:
19 1.1 simonb * 1. Redistributions of source code must retain the above copyright
20 1.1 simonb * notice, this list of conditions and the following disclaimer.
21 1.1 simonb * 2. Redistributions in binary form must reproduce the above copyright
22 1.1 simonb * notice, this list of conditions and the following disclaimer in the
23 1.1 simonb * documentation and/or other materials provided with the distribution.
24 1.2 agc * 3. Neither the name of the University nor the names of its contributors
25 1.1 simonb * may be used to endorse or promote products derived from this software
26 1.1 simonb * without specific prior written permission.
27 1.1 simonb *
28 1.1 simonb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 simonb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 simonb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 simonb * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 simonb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 simonb * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 simonb * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 simonb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 simonb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 simonb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 simonb * SUCH DAMAGE.
39 1.1 simonb *
40 1.1 simonb * @(#)fpu_emu.h 8.1 (Berkeley) 6/11/93
41 1.1 simonb */
42 1.1 simonb
43 1.1 simonb /*
44 1.1 simonb * Floating point emulator (tailored for SPARC, but structurally
45 1.1 simonb * machine-independent).
46 1.1 simonb *
47 1.1 simonb * Floating point numbers are carried around internally in an `expanded'
48 1.1 simonb * or `unpacked' form consisting of:
49 1.1 simonb * - sign
50 1.1 simonb * - unbiased exponent
51 1.1 simonb * - mantissa (`1.' + 112-bit fraction + guard + round)
52 1.1 simonb * - sticky bit
53 1.1 simonb * Any implied `1' bit is inserted, giving a 113-bit mantissa that is
54 1.1 simonb * always nonzero. Additional low-order `guard' and `round' bits are
55 1.1 simonb * scrunched in, making the entire mantissa 115 bits long. This is divided
56 1.1 simonb * into four 32-bit words, with `spare' bits left over in the upper part
57 1.1 simonb * of the top word (the high bits of fp_mant[0]). An internal `exploded'
58 1.1 simonb * number is thus kept within the half-open interval [1.0,2.0) (but see
59 1.1 simonb * the `number classes' below). This holds even for denormalized numbers:
60 1.1 simonb * when we explode an external denorm, we normalize it, introducing low-order
61 1.1 simonb * zero bits, so that the rest of the code always sees normalized values.
62 1.1 simonb *
63 1.1 simonb * Note that a number of our algorithms use the `spare' bits at the top.
64 1.1 simonb * The most demanding algorithm---the one for sqrt---depends on two such
65 1.1 simonb * bits, so that it can represent values up to (but not including) 8.0,
66 1.1 simonb * and then it needs a carry on top of that, so that we need three `spares'.
67 1.1 simonb *
68 1.1 simonb * The sticky-word is 32 bits so that we can use `OR' operators to goosh
69 1.1 simonb * whole words from the mantissa into it.
70 1.1 simonb *
71 1.1 simonb * All operations are done in this internal extended precision. According
72 1.1 simonb * to Hennesey & Patterson, Appendix A, rounding can be repeated---that is,
73 1.1 simonb * it is OK to do a+b in extended precision and then round the result to
74 1.1 simonb * single precision---provided single, double, and extended precisions are
75 1.1 simonb * `far enough apart' (they always are), but we will try to avoid any such
76 1.1 simonb * extra work where possible.
77 1.1 simonb */
78 1.1 simonb struct fpn {
79 1.1 simonb int fp_class; /* see below */
80 1.1 simonb int fp_sign; /* 0 => positive, 1 => negative */
81 1.1 simonb int fp_exp; /* exponent (unbiased) */
82 1.1 simonb int fp_sticky; /* nonzero bits lost at right end */
83 1.1 simonb u_int fp_mant[4]; /* 115-bit mantissa */
84 1.1 simonb };
85 1.1 simonb
86 1.1 simonb #define FP_NMANT 115 /* total bits in mantissa (incl g,r) */
87 1.1 simonb #define FP_NG 2 /* number of low-order guard bits */
88 1.1 simonb #define FP_LG ((FP_NMANT - 1) & 31) /* log2(1.0) for fp_mant[0] */
89 1.1 simonb #define FP_LG2 ((FP_NMANT - 1) & 63) /* log2(1.0) for fp_mant[0] and fp_mant[1] */
90 1.1 simonb #define FP_QUIETBIT (1 << (FP_LG - 1)) /* Quiet bit in NaNs (0.5) */
91 1.1 simonb #define FP_1 (1 << FP_LG) /* 1.0 in fp_mant[0] */
92 1.1 simonb #define FP_2 (1 << (FP_LG + 1)) /* 2.0 in fp_mant[0] */
93 1.1 simonb
94 1.1 simonb /*
95 1.1 simonb * Number classes. Since zero, Inf, and NaN cannot be represented using
96 1.1 simonb * the above layout, we distinguish these from other numbers via a class.
97 1.1 simonb * In addition, to make computation easier and to follow Appendix N of
98 1.1 simonb * the SPARC Version 8 standard, we give each kind of NaN a separate class.
99 1.1 simonb */
100 1.1 simonb #define FPC_SNAN -2 /* signalling NaN (sign irrelevant) */
101 1.1 simonb #define FPC_QNAN -1 /* quiet NaN (sign irrelevant) */
102 1.1 simonb #define FPC_ZERO 0 /* zero (sign matters) */
103 1.1 simonb #define FPC_NUM 1 /* number (sign matters) */
104 1.1 simonb #define FPC_INF 2 /* infinity (sign matters) */
105 1.1 simonb
106 1.1 simonb #define ISSNAN(fp) ((fp)->fp_class == FPC_SNAN)
107 1.1 simonb #define ISQNAN(fp) ((fp)->fp_class == FPC_QNAN)
108 1.1 simonb #define ISNAN(fp) ((fp)->fp_class < 0)
109 1.1 simonb #define ISZERO(fp) ((fp)->fp_class == 0)
110 1.1 simonb #define ISINF(fp) ((fp)->fp_class == FPC_INF)
111 1.1 simonb
112 1.1 simonb /*
113 1.1 simonb * ORDER(x,y) `sorts' a pair of `fpn *'s so that the right operand (y) points
114 1.1 simonb * to the `more significant' operand for our purposes. Appendix N says that
115 1.1 simonb * the result of a computation involving two numbers are:
116 1.1 simonb *
117 1.1 simonb * If both are SNaN: operand 2, converted to Quiet
118 1.1 simonb * If only one is SNaN: the SNaN operand, converted to Quiet
119 1.1 simonb * If both are QNaN: operand 2
120 1.1 simonb * If only one is QNaN: the QNaN operand
121 1.1 simonb *
122 1.1 simonb * In addition, in operations with an Inf operand, the result is usually
123 1.1 simonb * Inf. The class numbers are carefully arranged so that if
124 1.1 simonb * (unsigned)class(op1) > (unsigned)class(op2)
125 1.1 simonb * then op1 is the one we want; otherwise op2 is the one we want.
126 1.1 simonb */
127 1.1 simonb #define ORDER(x, y) { \
128 1.1 simonb if ((u_int)(x)->fp_class > (u_int)(y)->fp_class) \
129 1.1 simonb SWAP(x, y); \
130 1.1 simonb }
131 1.1 simonb #define SWAP(x, y) { \
132 1.1 simonb struct fpn *swap; \
133 1.1 simonb swap = (x), (x) = (y), (y) = swap; \
134 1.1 simonb }
135 1.1 simonb
136 1.1 simonb /*
137 1.5 rin * FPU data types.
138 1.5 rin */
139 1.7 rin #define FTYPE_INT 0x00 /* data = 32-bit signed integer */
140 1.7 rin #define FTYPE_LNG 0x01 /* data = 64-bit signed long integer */
141 1.7 rin #define FTYPE_SNG 0x02 /* data = 32-bit float */
142 1.7 rin #define FTYPE_DBL 0x04 /* data = 64-bit double */
143 1.7 rin #define FTYPE_RD_RZ 0x08
144 1.9 rin #define FTYPE_FPSCR 0x10
145 1.9 rin #define FTYPE_FLAG_MASK (FTYPE_RD_RZ | FTYPE_FPSCR)
146 1.5 rin
147 1.5 rin /*
148 1.1 simonb * Emulator state.
149 1.1 simonb */
150 1.1 simonb struct fpemu {
151 1.1 simonb struct fpreg *fe_fpstate; /* registers, etc */
152 1.1 simonb int fe_fpscr; /* fpscr copy (modified during op) */
153 1.1 simonb int fe_cx; /* keep track of exceptions */
154 1.1 simonb struct fpn fe_f1; /* operand 1 */
155 1.1 simonb struct fpn fe_f2; /* operand 2, if required */
156 1.1 simonb struct fpn fe_f3; /* available storage for result */
157 1.4 matt vaddr_t fe_addr; /* last address accessed */
158 1.1 simonb };
159 1.1 simonb
160 1.1 simonb /*
161 1.1 simonb * Arithmetic functions.
162 1.1 simonb * Each of these may modify its inputs (f1,f2) and/or the temporary.
163 1.1 simonb * Each returns a pointer to the result and/or sets exceptions.
164 1.1 simonb */
165 1.1 simonb struct fpn *fpu_add(struct fpemu *);
166 1.1 simonb struct fpn *fpu_mul(struct fpemu *);
167 1.1 simonb struct fpn *fpu_div(struct fpemu *);
168 1.1 simonb struct fpn *fpu_sqrt(struct fpemu *);
169 1.1 simonb
170 1.12 rin static inline struct fpn *
171 1.12 rin fpu_sub(struct fpemu *fe)
172 1.12 rin {
173 1.12 rin struct fpn *fp = &fe->fe_f2;
174 1.12 rin
175 1.12 rin if (!ISNAN(fp))
176 1.12 rin fp->fp_sign ^= 1;
177 1.12 rin return fpu_add(fe);
178 1.12 rin }
179 1.12 rin
180 1.1 simonb /*
181 1.1 simonb * Other functions.
182 1.1 simonb */
183 1.1 simonb
184 1.1 simonb /* Perform a compare instruction (with or without unordered exception). */
185 1.1 simonb void fpu_compare(struct fpemu *, int);
186 1.1 simonb
187 1.1 simonb /* Build a new Quiet NaN (sign=0, frac=all 1's). */
188 1.1 simonb struct fpn *fpu_newnan(struct fpemu *);
189 1.1 simonb
190 1.1 simonb /*
191 1.1 simonb * Shift a number right some number of bits, taking care of round/sticky.
192 1.1 simonb * Note that the result is probably not a well-formed number (it will lack
193 1.1 simonb * the normal 1-bit mant[0]&FP_1).
194 1.1 simonb */
195 1.1 simonb int fpu_shr(struct fpn *, int);
196 1.1 simonb
197 1.10 rin void fpu_norm(struct fpn *);
198 1.10 rin
199 1.8 rin void fpu_explode(struct fpemu *, struct fpn *, int, uint64_t);
200 1.8 rin void fpu_implode(struct fpemu *, struct fpn *, int, uint64_t *);
201 1.1 simonb
202 1.1 simonb #ifdef DEBUG
203 1.1 simonb #define FPE_EX 0x1
204 1.1 simonb #define FPE_INSN 0x2
205 1.1 simonb #define FPE_OP 0x4
206 1.1 simonb #define FPE_REG 0x8
207 1.1 simonb extern int fpe_debug;
208 1.1 simonb void fpu_dumpfpn(struct fpn *);
209 1.1 simonb #define DPRINTF(x, y) if (fpe_debug & (x)) printf y
210 1.1 simonb #define DUMPFPN(x, f) if (fpe_debug & (x)) fpu_dumpfpn((f))
211 1.1 simonb #else
212 1.1 simonb #define DPRINTF(x, y)
213 1.1 simonb #define DUMPFPN(x, f)
214 1.1 simonb #endif
215