xref: /relibc/openlibm/i387/bsd_ieeefp.h (revision 28fb939b0de8e3c8a7fd6064a7277410b84b292f)
1 /*-
2  * Copyright (c) 2003 Peter Wemm.
3  * Copyright (c) 1990 Andrew Moore, Talke Studio
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * 	from: @(#) ieeefp.h 	1.0 (Berkeley) 9/23/93
35  * $FreeBSD$
36  */
37 
38 #ifndef _MACHINE_IEEEFP_H_
39 #define _MACHINE_IEEEFP_H_
40 
41 /*
42  * Deprecated historical FPU control interface
43  *
44  * IEEE floating point type, constant and function definitions.
45  * XXX: FP*FLD and FP*OFF are undocumented pollution.
46  */
47 
48 /* VBS
49 
50 #ifndef _SYS_CDEFS_H_
51 #error this file needs sys/cdefs.h as a prerequisite
52 #endif
53 
54 */
55 
56 /*
57  * Rounding modes.
58  */
59 typedef enum {
60 	FP_RN=0,	/* round to nearest */
61 	FP_RM,		/* round down towards minus infinity */
62 	FP_RP,		/* round up towards plus infinity */
63 	FP_RZ		/* truncate */
64 } fp_rnd_t;
65 
66 /*
67  * Precision (i.e., rounding precision) modes.
68  */
69 typedef enum {
70 	FP_PS=0,	/* 24 bit (single-precision) */
71 	FP_PRS,		/* reserved */
72 	FP_PD,		/* 53 bit (double-precision) */
73 	FP_PE		/* 64 bit (extended-precision) */
74 } fp_prec_t;
75 
76 #define fp_except_t	int
77 
78 /*
79  * Exception bit masks.
80  */
81 #define FP_X_INV	0x01	/* invalid operation */
82 #define FP_X_DNML	0x02	/* denormal */
83 #define FP_X_DZ		0x04	/* zero divide */
84 #define FP_X_OFL	0x08	/* overflow */
85 #define FP_X_UFL	0x10	/* underflow */
86 #define FP_X_IMP	0x20	/* (im)precision */
87 #define FP_X_STK	0x40	/* stack fault */
88 
89 /*
90  * FPU control word bit-field masks.
91  */
92 #define FP_MSKS_FLD	0x3f	/* exception masks field */
93 #define FP_PRC_FLD	0x300	/* precision control field */
94 #define	FP_RND_FLD	0xc00	/* rounding control field */
95 
96 /*
97  * FPU status word bit-field masks.
98  */
99 #define FP_STKY_FLD	0x3f	/* sticky flags field */
100 
101 /*
102  * FPU control word bit-field offsets (shift counts).
103  */
104 #define FP_MSKS_OFF	0	/* exception masks offset */
105 #define FP_PRC_OFF	8	/* precision control offset */
106 #define	FP_RND_OFF	10	/* rounding control offset */
107 
108 /*
109  * FPU status word bit-field offsets (shift counts).
110  */
111 #define FP_STKY_OFF	0	/* sticky flags offset */
112 
113 //VBS
114 //#ifdef __GNUCLIKE_ASM
115 
116 #define	__fldcw(addr)	__asm __volatile("fldcw %0" : : "m" (*(addr)))
117 #define	__fldenv(addr)	__asm __volatile("fldenv %0" : : "m" (*(addr)))
118 #define	__fnclex()	__asm __volatile("fnclex")
119 #define	__fnstcw(addr)	__asm __volatile("fnstcw %0" : "=m" (*(addr)))
120 #define	__fnstenv(addr)	__asm __volatile("fnstenv %0" : "=m" (*(addr)))
121 #define	__fnstsw(addr)	__asm __volatile("fnstsw %0" : "=m" (*(addr)))
122 
123 /*
124  * Load the control word.  Be careful not to trap if there is a currently
125  * unmasked exception (ones that will become freshly unmasked are not a
126  * problem).  This case must be handled by a save/restore of the
127  * environment or even of the full x87 state.  Accessing the environment
128  * is very inefficient, so only do it when necessary.
129  */
130 static __inline void
131 __fnldcw(unsigned short _cw, unsigned short _newcw)
132 {
133 	struct {
134 		unsigned _cw;
135 		unsigned _other[6];
136 	} _env;
137 	unsigned short _sw;
138 
139 	if ((_cw & FP_MSKS_FLD) != FP_MSKS_FLD) {
140 		__fnstsw(&_sw);
141 		if (((_sw & ~_cw) & FP_STKY_FLD) != 0) {
142 			__fnstenv(&_env);
143 			_env._cw = _newcw;
144 			__fldenv(&_env);
145 			return;
146 		}
147 	}
148 	__fldcw(&_newcw);
149 }
150 
151 static __inline fp_rnd_t
152 fpgetround(void)
153 {
154 	unsigned short _cw;
155 
156 	__fnstcw(&_cw);
157 	return ((fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF));
158 }
159 
160 static __inline fp_rnd_t
161 fpsetround(fp_rnd_t _m)
162 {
163 	fp_rnd_t _p;
164 	unsigned short _cw, _newcw;
165 
166 	__fnstcw(&_cw);
167 	_p = (fp_rnd_t)((_cw & FP_RND_FLD) >> FP_RND_OFF);
168 	_newcw = _cw & ~FP_RND_FLD;
169 	_newcw |= (_m << FP_RND_OFF) & FP_RND_FLD;
170 	__fnldcw(_cw, _newcw);
171 	return (_p);
172 }
173 
174 //static __inline fp_prec_t
175 OLM_DLLEXPORT fp_prec_t
176 fpgetprec(void)
177 {
178 	unsigned short _cw;
179 
180 	__fnstcw(&_cw);
181 	return ((fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF));
182 }
183 
184 //static __inline fp_prec_t
185 OLM_DLLEXPORT fp_prec_t
186 fpsetprec(fp_prec_t _m)
187 {
188 	fp_prec_t _p;
189 	unsigned short _cw, _newcw;
190 
191 	__fnstcw(&_cw);
192 	_p = (fp_prec_t)((_cw & FP_PRC_FLD) >> FP_PRC_OFF);
193 	_newcw = _cw & ~FP_PRC_FLD;
194 	_newcw |= (_m << FP_PRC_OFF) & FP_PRC_FLD;
195 	__fnldcw(_cw, _newcw);
196 	return (_p);
197 }
198 
199 /*
200  * Get or set the exception mask.
201  * Note that the x87 mask bits are inverted by the API -- a mask bit of 1
202  * means disable for x87 and SSE, but for fp*mask() it means enable.
203  */
204 
205 static __inline fp_except_t
206 fpgetmask(void)
207 {
208 	unsigned short _cw;
209 
210 	__fnstcw(&_cw);
211 	return ((~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF);
212 }
213 
214 static __inline fp_except_t
215 fpsetmask(fp_except_t _m)
216 {
217 	fp_except_t _p;
218 	unsigned short _cw, _newcw;
219 
220 	__fnstcw(&_cw);
221 	_p = (~_cw & FP_MSKS_FLD) >> FP_MSKS_OFF;
222 	_newcw = _cw & ~FP_MSKS_FLD;
223 	_newcw |= (~_m << FP_MSKS_OFF) & FP_MSKS_FLD;
224 	__fnldcw(_cw, _newcw);
225 	return (_p);
226 }
227 
228 static __inline fp_except_t
229 fpgetsticky(void)
230 {
231 	unsigned _ex;
232 	unsigned short _sw;
233 
234 	__fnstsw(&_sw);
235 	_ex = (_sw & FP_STKY_FLD) >> FP_STKY_OFF;
236 	return ((fp_except_t)_ex);
237 }
238 
239 static __inline fp_except_t
240 fpresetsticky(fp_except_t _m)
241 {
242 	struct {
243 		unsigned _cw;
244 		unsigned _sw;
245 		unsigned _other[5];
246 	} _env;
247 	fp_except_t _p;
248 
249 	_m &= FP_STKY_FLD >> FP_STKY_OFF;
250 	_p = fpgetsticky();
251 	if ((_p & ~_m) == _p)
252 		return (_p);
253 	if ((_p & ~_m) == 0) {
254 		__fnclex();
255 		return (_p);
256 	}
257 	__fnstenv(&_env);
258 	_env._sw &= ~_m;
259 	__fldenv(&_env);
260 	return (_p);
261 }
262 
263 //#endif /* __GNUCLIKE_ASM */
264 
265 #endif /* !_MACHINE_IEEEFP_H_ */
266