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