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