xref: /freebsd/lib/msun/x86/fenv.h (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef	_FENV_H_
30 #define	_FENV_H_
31 
32 #include <sys/cdefs.h>
33 #include <sys/_types.h>
34 #include <ieeefp.h>
35 
36 #ifndef	__fenv_static
37 #define	__fenv_static	static
38 #endif
39 
40 typedef	__uint16_t	fexcept_t;
41 
42 /* Exception flags */
43 #define	FE_INVALID	0x01
44 #define	FE_DENORMAL	0x02
45 #define	FE_DIVBYZERO	0x04
46 #define	FE_OVERFLOW	0x08
47 #define	FE_UNDERFLOW	0x10
48 #define	FE_INEXACT	0x20
49 #define	FE_ALL_EXCEPT	(FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
50 			 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
51 
52 /* Rounding modes */
53 #define	FE_TONEAREST	0x0000
54 #define	FE_DOWNWARD	0x0400
55 #define	FE_UPWARD	0x0800
56 #define	FE_TOWARDZERO	0x0c00
57 #define	_ROUND_MASK	(FE_TONEAREST | FE_DOWNWARD | \
58 			 FE_UPWARD | FE_TOWARDZERO)
59 
60 /*
61  * As compared to the x87 control word, the SSE unit's control word
62  * has the rounding control bits offset by 3 and the exception mask
63  * bits offset by 7.
64  */
65 #define	_SSE_ROUND_SHIFT	3
66 #define	_SSE_EMASK_SHIFT	7
67 
68 #ifdef __i386__
69 /*
70  * To preserve binary compatibility with FreeBSD 5.3, we pack the
71  * mxcsr into some reserved fields, rather than changing sizeof(fenv_t).
72  */
73 typedef struct {
74 	__uint16_t	__control;
75 	__uint16_t      __mxcsr_hi;
76 	__uint16_t	__status;
77 	__uint16_t      __mxcsr_lo;
78 	__uint32_t	__tag;
79 	char		__other[16];
80 } fenv_t;
81 #else /* __amd64__ */
82 typedef struct {
83 	struct {
84 		__uint32_t	__control;
85 		__uint32_t	__status;
86 		__uint32_t	__tag;
87 		char		__other[16];
88 	} __x87;
89 	__uint32_t		__mxcsr;
90 } fenv_t;
91 #endif /* __i386__ */
92 
93 __BEGIN_DECLS
94 
95 /* Default floating-point environment */
96 extern const fenv_t	__fe_dfl_env;
97 #define	FE_DFL_ENV	(&__fe_dfl_env)
98 
99 #define	__fldenvx(__env)	__asm __volatile("fldenv %0" : : "m" (__env)  \
100 				: "st", "st(1)", "st(2)", "st(3)", "st(4)",   \
101 				"st(5)", "st(6)", "st(7)")
102 #define	__fwait()		__asm __volatile("fwait")
103 
104 int fegetenv(fenv_t *__envp);
105 int feholdexcept(fenv_t *__envp);
106 int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
107 int feraiseexcept(int __excepts);
108 int feupdateenv(const fenv_t *__envp);
109 
110 __fenv_static inline int
fegetround(void)111 fegetround(void)
112 {
113 	__uint16_t __control;
114 
115 	/*
116 	 * We assume that the x87 and the SSE unit agree on the
117 	 * rounding mode.  Reading the control word on the x87 turns
118 	 * out to be about 5 times faster than reading it on the SSE
119 	 * unit on an Opteron 244.
120 	 */
121 	__fnstcw(&__control);
122 	return (__control & _ROUND_MASK);
123 }
124 
125 #if __BSD_VISIBLE
126 
127 int feenableexcept(int __mask);
128 int fedisableexcept(int __mask);
129 
130 /* We currently provide no external definition of fegetexcept(). */
131 static inline int
fegetexcept(void)132 fegetexcept(void)
133 {
134 	__uint16_t __control;
135 
136 	/*
137 	 * We assume that the masks for the x87 and the SSE unit are
138 	 * the same.
139 	 */
140 	__fnstcw(&__control);
141 	return (~__control & FE_ALL_EXCEPT);
142 }
143 
144 #endif /* __BSD_VISIBLE */
145 
146 #ifdef __i386__
147 
148 /* After testing for SSE support once, we cache the result in __has_sse. */
149 enum __sse_support { __SSE_YES, __SSE_NO, __SSE_UNK };
150 extern enum __sse_support __has_sse;
151 int __test_sse(void);
152 #ifdef __SSE__
153 #define	__HAS_SSE()	1
154 #else
155 #define	__HAS_SSE()	(__has_sse == __SSE_YES ||			\
156 			 (__has_sse == __SSE_UNK && __test_sse()))
157 #endif
158 
159 #define	__get_mxcsr(env)	(((env).__mxcsr_hi << 16) |	\
160 				 ((env).__mxcsr_lo))
161 #define	__set_mxcsr(env, x)	do {				\
162 	(env).__mxcsr_hi = (__uint32_t)(x) >> 16;		\
163 	(env).__mxcsr_lo = (__uint16_t)(x);			\
164 } while (0)
165 
166 __fenv_static inline int
feclearexcept(int __excepts)167 feclearexcept(int __excepts)
168 {
169 	fenv_t __env;
170 	__uint32_t __mxcsr;
171 
172 	if (__excepts == FE_ALL_EXCEPT) {
173 		__fnclex();
174 	} else {
175 		__fnstenv(&__env);
176 		__env.__status &= ~__excepts;
177 		__fldenv(&__env);
178 	}
179 	if (__HAS_SSE()) {
180 		__stmxcsr(&__mxcsr);
181 		__mxcsr &= ~__excepts;
182 		__ldmxcsr(&__mxcsr);
183 	}
184 	return (0);
185 }
186 
187 __fenv_static inline int
fegetexceptflag(fexcept_t * __flagp,int __excepts)188 fegetexceptflag(fexcept_t *__flagp, int __excepts)
189 {
190 	__uint32_t __mxcsr;
191 	__uint16_t __status;
192 
193 	__fnstsw(&__status);
194 	if (__HAS_SSE())
195 		__stmxcsr(&__mxcsr);
196 	else
197 		__mxcsr = 0;
198 	*__flagp = (__mxcsr | __status) & __excepts;
199 	return (0);
200 }
201 
202 __fenv_static inline int
fetestexcept(int __excepts)203 fetestexcept(int __excepts)
204 {
205 	__uint32_t __mxcsr;
206 	__uint16_t __status;
207 
208 	__fnstsw(&__status);
209 	if (__HAS_SSE())
210 		__stmxcsr(&__mxcsr);
211 	else
212 		__mxcsr = 0;
213 	return ((__status | __mxcsr) & __excepts);
214 }
215 
216 __fenv_static inline int
fesetround(int __round)217 fesetround(int __round)
218 {
219 	__uint32_t __mxcsr;
220 	__uint16_t __control;
221 
222 	if (__round & ~_ROUND_MASK)
223 		return (-1);
224 
225 	__fnstcw(&__control);
226 	__control &= ~_ROUND_MASK;
227 	__control |= __round;
228 	__fldcw(&__control);
229 
230 	if (__HAS_SSE()) {
231 		__stmxcsr(&__mxcsr);
232 		__mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
233 		__mxcsr |= __round << _SSE_ROUND_SHIFT;
234 		__ldmxcsr(&__mxcsr);
235 	}
236 
237 	return (0);
238 }
239 
240 __fenv_static inline int
fesetenv(const fenv_t * __envp)241 fesetenv(const fenv_t *__envp)
242 {
243 	fenv_t __env = *__envp;
244 	__uint32_t __mxcsr;
245 
246 	__mxcsr = __get_mxcsr(__env);
247 	__set_mxcsr(__env, 0xffffffff);
248 	/*
249 	 * XXX Using fldenvx() instead of fldenv() tells the compiler that this
250 	 * instruction clobbers the i387 register stack.  This happens because
251 	 * we restore the tag word from the saved environment.  Normally, this
252 	 * would happen anyway and we wouldn't care, because the ABI allows
253 	 * function calls to clobber the i387 regs.  However, fesetenv() is
254 	 * inlined, so we need to be more careful.
255 	 */
256 	__fldenvx(__env);
257 	if (__HAS_SSE())
258 		__ldmxcsr(&__mxcsr);
259 	return (0);
260 }
261 
262 #else /* __amd64__ */
263 
264 __fenv_static inline int
feclearexcept(int __excepts)265 feclearexcept(int __excepts)
266 {
267 	fenv_t __env;
268 
269 	if (__excepts == FE_ALL_EXCEPT) {
270 		__fnclex();
271 	} else {
272 		__fnstenv(&__env.__x87);
273 		__env.__x87.__status &= ~__excepts;
274 		__fldenv(&__env.__x87);
275 	}
276 	__stmxcsr(&__env.__mxcsr);
277 	__env.__mxcsr &= ~__excepts;
278 	__ldmxcsr(&__env.__mxcsr);
279 	return (0);
280 }
281 
282 __fenv_static inline int
fegetexceptflag(fexcept_t * __flagp,int __excepts)283 fegetexceptflag(fexcept_t *__flagp, int __excepts)
284 {
285 	__uint32_t __mxcsr;
286 	__uint16_t __status;
287 
288 	__stmxcsr(&__mxcsr);
289 	__fnstsw(&__status);
290 	*__flagp = (__mxcsr | __status) & __excepts;
291 	return (0);
292 }
293 
294 __fenv_static inline int
fetestexcept(int __excepts)295 fetestexcept(int __excepts)
296 {
297 	__uint32_t __mxcsr;
298 	__uint16_t __status;
299 
300 	__stmxcsr(&__mxcsr);
301 	__fnstsw(&__status);
302 	return ((__status | __mxcsr) & __excepts);
303 }
304 
305 __fenv_static inline int
fesetround(int __round)306 fesetround(int __round)
307 {
308 	__uint32_t __mxcsr;
309 	__uint16_t __control;
310 
311 	if (__round & ~_ROUND_MASK)
312 		return (-1);
313 
314 	__fnstcw(&__control);
315 	__control &= ~_ROUND_MASK;
316 	__control |= __round;
317 	__fldcw(&__control);
318 
319 	__stmxcsr(&__mxcsr);
320 	__mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
321 	__mxcsr |= __round << _SSE_ROUND_SHIFT;
322 	__ldmxcsr(&__mxcsr);
323 
324 	return (0);
325 }
326 
327 __fenv_static inline int
fesetenv(const fenv_t * __envp)328 fesetenv(const fenv_t *__envp)
329 {
330 
331 	/*
332 	 * XXX Using fldenvx() instead of fldenv() tells the compiler that this
333 	 * instruction clobbers the i387 register stack.  This happens because
334 	 * we restore the tag word from the saved environment.  Normally, this
335 	 * would happen anyway and we wouldn't care, because the ABI allows
336 	 * function calls to clobber the i387 regs.  However, fesetenv() is
337 	 * inlined, so we need to be more careful.
338 	 */
339 	__fldenvx(__envp->__x87);
340 	__ldmxcsr(&__envp->__mxcsr);
341 	return (0);
342 }
343 
344 #endif /* __i386__ */
345 
346 __END_DECLS
347 
348 #endif	/* !_FENV_H_ */
349