ieeefp.h revision 109520
1279377Simp/*-
2279377Simp * Copyright (c) 1990 Andrew Moore, Talke Studio
3279377Simp * All rights reserved.
4279377Simp *
5279377Simp * Redistribution and use in source and binary forms, with or without
6279377Simp * modification, are permitted provided that the following conditions
7279377Simp * are met:
8279377Simp * 1. Redistributions of source code must retain the above copyright
9279377Simp *    notice, this list of conditions and the following disclaimer.
10279377Simp * 2. Redistributions in binary form must reproduce the above copyright
11279377Simp *    notice, this list of conditions and the following disclaimer in the
12279377Simp *    documentation and/or other materials provided with the distribution.
13279377Simp * 3. All advertising materials mentioning features or use of this software
14279377Simp *    must display the following acknowledgement:
15279377Simp *	This product includes software developed by the University of
16279377Simp *	California, Berkeley and its contributors.
17279377Simp * 4. Neither the name of the University nor the names of its contributors
18279377Simp *    may be used to endorse or promote products derived from this software
19279377Simp *    without specific prior written permission.
20279377Simp *
21279377Simp * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22279377Simp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23279377Simp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24279377Simp * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25279377Simp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26279377Simp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27279377Simp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28279377Simp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29279377Simp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30279377Simp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31279377Simp * SUCH DAMAGE.
32279377Simp *
33279377Simp * 	from: @(#) ieeefp.h 	1.0 (Berkeley) 9/23/93
34279377Simp * $FreeBSD: head/sys/i386/include/ieeefp.h 109520 2003-01-19 06:01:33Z marcel $
35279377Simp */
36279377Simp
37279377Simp/*
38279377Simp *	IEEE floating point type and constant definitions.
39279377Simp */
40279377Simp
41279377Simp#ifndef _MACHINE_IEEEFP_H_
42279377Simp#define _MACHINE_IEEEFP_H_
43279377Simp
44279377Simp/*
45279377Simp * FP rounding modes
46279377Simp */
47279377Simptypedef enum {
48279377Simp	FP_RN=0,	/* round to nearest */
49279377Simp	FP_RM,		/* round down to minus infinity */
50279377Simp	FP_RP,		/* round up to plus infinity */
51279377Simp	FP_RZ		/* truncate */
52279377Simp} fp_rnd_t;
53279377Simp
54279377Simp/*
55279377Simp * FP precision modes
56279377Simp */
57279377Simptypedef enum {
58279377Simp	FP_PS=0,	/* 24 bit (single-precision) */
59279377Simp	FP_PRS,		/* reserved */
60279377Simp	FP_PD,		/* 53 bit (double-precision) */
61279377Simp	FP_PE		/* 64 bit (extended-precision) */
62279377Simp} fp_prec_t;
63279377Simp
64279377Simp#define fp_except_t	int
65279377Simp
66279377Simp/*
67279377Simp * FP exception masks
68279377Simp */
69279377Simp#define FP_X_INV	0x01	/* invalid operation */
70279377Simp#define FP_X_DNML	0x02	/* denormal */
71279377Simp#define FP_X_DZ		0x04	/* zero divide */
72279377Simp#define FP_X_OFL	0x08	/* overflow */
73279377Simp#define FP_X_UFL	0x10	/* underflow */
74279377Simp#define FP_X_IMP	0x20	/* (im)precision */
75279377Simp#define FP_X_STK	0x40	/* stack fault */
76279377Simp
77279377Simp/*
78279377Simp * FP registers
79279377Simp */
80279377Simp#define FP_MSKS_REG	0	/* exception masks */
81284090Sian#define FP_PRC_REG	0	/* precision */
82284090Sian#define FP_RND_REG	0	/* direction */
83284090Sian#define FP_STKY_REG	1	/* sticky flags */
84284090Sian
85/*
86 * FP register bit field masks
87 */
88#define FP_MSKS_FLD	0x3f	/* exception masks field */
89#define FP_PRC_FLD	0x300	/* precision control field */
90#define FP_RND_FLD	0xc00	/* round control field */
91#define FP_STKY_FLD	0x3f	/* sticky flags field */
92
93/*
94 * FP register bit field offsets
95 */
96#define FP_MSKS_OFF	0	/* exception masks offset */
97#define FP_PRC_OFF	8	/* precision control offset */
98#define FP_RND_OFF	10	/* round control offset */
99#define FP_STKY_OFF	0	/* sticky flags offset */
100
101#ifdef __GNUC__
102
103#define	__fldenv(addr)	__asm __volatile("fldenv %0" : : "m" (*(addr)))
104#define	__fnstenv(addr)	__asm __volatile("fnstenv %0" : "=m" (*(addr)))
105#define	__fnstcw(addr)	__asm __volatile("fnstcw %0" : "=m" (*(addr)))
106#define	__fnstsw(addr)	__asm __volatile("fnstsw %0" : "=m" (*(addr)))
107
108/*
109 * return the contents of a FP register
110 */
111static __inline__ int
112__fpgetreg(int _reg)
113{
114	unsigned short _mem;
115
116	/*-
117	 * This is more efficient than it looks.  The switch gets optimized
118	 * away if _reg is constant.
119	 *
120	 * The default case only supports _reg == 0.  We could handle more
121	 * registers (e.g., tags) using fnstenv, but the interface doesn't
122	 * support more.
123	 */
124	switch(_reg) {
125	default:
126		__fnstcw(&_mem);
127		break;
128	case FP_STKY_REG:
129		__fnstsw(&_mem);
130		break;
131	}
132	return _mem;
133}
134
135/*
136 * set a FP mode; return previous mode
137 */
138static __inline__ int
139__fpsetreg(int _m, int _reg, int _fld, int _off)
140{
141	unsigned _env[7];
142	unsigned _p;
143
144	/*
145	 * _reg == 0 could be handled better using fnstcw/fldcw.
146	 */
147	__fnstenv(_env);
148	_p =  (_env[_reg] & _fld) >> _off;
149	_env[_reg] = (_env[_reg] & ~_fld) | (_m << _off & _fld);
150	__fldenv(_env);
151	return _p;
152}
153
154#endif /* __GNUC__ */
155
156/*
157 * SysV/386 FP control interface
158 */
159#define	fpgetround()	((fp_rnd_t)					\
160	((__fpgetreg(FP_RND_REG) & FP_RND_FLD) >> FP_RND_OFF))
161#define	fpsetround(m)	((fp_rnd_t)					\
162	__fpsetreg((m), FP_RND_REG, FP_RND_FLD, FP_RND_OFF))
163#define	fpgetprec()	((fp_prec_t)					\
164	((__fpgetreg(FP_PRC_REG) & FP_PRC_FLD) >> FP_PRC_OFF))
165#define	fpsetprec(m)	((fp_prec_t)					\
166	__fpsetreg((m), FP_PRC_REG, FP_PRC_FLD, FP_PRC_OFF))
167#define	fpgetmask()	((fp_except_t)					\
168	((~__fpgetreg(FP_MSKS_REG) & FP_MSKS_FLD) >> FP_MSKS_OFF))
169#define	fpsetmask(m)	((fp_except_t)					\
170	(~__fpsetreg(~(m), FP_MSKS_REG, FP_MSKS_FLD, FP_MSKS_OFF)) &	\
171	    (FP_MSKS_FLD >> FP_MSKS_OFF))
172#define	fpgetsticky()	((fp_except_t)					\
173	((__fpgetreg(FP_STKY_REG) & FP_STKY_FLD) >> FP_STKY_OFF))
174#define	fpresetsticky(m) ((fp_except_t)					\
175	__fpsetreg(0, FP_STKY_REG, (m), FP_STKY_OFF))
176#define	fpsetsticky(m)	fpresetsticky(m)
177
178/* Suppress prototypes in the MI header. */
179#define	_IEEEFP_INLINED_	1
180
181#endif /* !_MACHINE_IEEEFP_H_ */
182