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