xref: /freebsd/lib/msun/x86/fenv.h (revision dece5b05f577fc4c6b89af7ad48dfbdf7cc4945c)
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 /*
94  * Control modes: x87 control word plus MXCSR bits other than the
95  * exception flags.  fesetmode() restores those bits without changing
96  * the raised exception flags.
97  */
98 typedef struct {
99 	__uint16_t	__control;
100 	__uint16_t	__reserved;
101 	__uint32_t	__mxcsr;
102 } femode_t;
103 
104 __BEGIN_DECLS
105 
106 /* Default floating-point environment */
107 extern const fenv_t	__fe_dfl_env;
108 #define	FE_DFL_ENV	(&__fe_dfl_env)
109 
110 /* Default floating-point control modes */
111 extern const femode_t	__fe_dfl_mode;
112 #define	FE_DFL_MODE	(&__fe_dfl_mode)
113 
114 #define	__fldenvx(__env)	__asm __volatile("fldenv %0" : : "m" (__env)  \
115 				: "st", "st(1)", "st(2)", "st(3)", "st(4)",   \
116 				"st(5)", "st(6)", "st(7)")
117 #define	__fwait()		__asm __volatile("fwait")
118 
119 int fegetenv(fenv_t *__envp);
120 int feholdexcept(fenv_t *__envp);
121 int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
122 int feraiseexcept(int __excepts);
123 int feupdateenv(const fenv_t *__envp);
124 
125 __fenv_static inline int
126 __fegetround_int(void)
127 {
128 	__uint16_t __control;
129 
130 	/*
131 	 * We assume that the x87 and the SSE unit agree on the
132 	 * rounding mode.  Reading the control word on the x87 turns
133 	 * out to be about 5 times faster than reading it on the SSE
134 	 * unit on an Opteron 244.
135 	 */
136 	__fnstcw(&__control);
137 	return (__control & _ROUND_MASK);
138 }
139 
140 #if __BSD_VISIBLE
141 
142 int feenableexcept(int __mask);
143 int fedisableexcept(int __mask);
144 
145 /* We currently provide no external definition of fegetexcept(). */
146 static inline int
147 fegetexcept(void)
148 {
149 	__uint16_t __control;
150 
151 	/*
152 	 * We assume that the masks for the x87 and the SSE unit are
153 	 * the same.
154 	 */
155 	__fnstcw(&__control);
156 	return (~__control & FE_ALL_EXCEPT);
157 }
158 
159 #endif /* __BSD_VISIBLE */
160 
161 int feclearexcept(int);
162 int fegetexceptflag(fexcept_t *, int);
163 int fetestexcept(int);
164 int fesetround(int);
165 int fegetround(void);
166 int fegetmode(femode_t *);
167 int fesetmode(const femode_t *);
168 int fesetenv(const fenv_t *);
169 
170 /*
171  * C permits a standard library function to also be exposed as a function-like
172  * macro (C23 7.1.4), and msun uses that here to inline the fast path.  C++
173  * forbids it: <cfenv> imports these names into namespace std (using
174  * ::feclearexcept; etc.), so std::feclearexcept() and friends must denote the
175  * actual functions.  Expose the inlining macros to C only; C++ uses the real
176  * extern functions (defined in the matching lib/msun/<arch>/fenv.c).
177  */
178 #ifndef __cplusplus
179 #define	feclearexcept(a)	__feclearexcept_int(a)
180 #define	fegetexceptflag(e, a)	__fegetexceptflag_int(e, a)
181 #define	fetestexcept(a)		__fetestexcept_int(a)
182 #define	fesetround(a)		__fesetround_int(a)
183 #define	fegetround()		__fegetround_int()
184 #define	fegetmode(m)		__fegetmode_int(m)
185 #define	fesetmode(m)		__fesetmode_int(m)
186 #define	fesetenv(a)		__fesetenv_int(a)
187 #endif /* !__cplusplus */
188 
189 #ifdef __i386__
190 
191 /* After testing for SSE support once, we cache the result in __has_sse. */
192 enum __sse_support { __SSE_YES, __SSE_NO, __SSE_UNK };
193 extern enum __sse_support __has_sse;
194 int __test_sse(void);
195 #ifdef __SSE__
196 #define	__HAS_SSE()	1
197 #else
198 #define	__HAS_SSE()	(__has_sse == __SSE_YES ||			\
199 			 (__has_sse == __SSE_UNK && __test_sse()))
200 #endif
201 
202 #define	__get_mxcsr(env)	(((env).__mxcsr_hi << 16) |	\
203 				 ((env).__mxcsr_lo))
204 #define	__set_mxcsr(env, x)	do {				\
205 	(env).__mxcsr_hi = (__uint32_t)(x) >> 16;		\
206 	(env).__mxcsr_lo = (__uint16_t)(x);			\
207 } while (0)
208 
209 __fenv_static inline int
210 __feclearexcept_int(int __excepts)
211 {
212 	fenv_t __env;
213 	__uint32_t __mxcsr;
214 
215 	if (__excepts == FE_ALL_EXCEPT) {
216 		__fnclex();
217 	} else {
218 		__fnstenv(&__env);
219 		__env.__status &= ~__excepts;
220 		__fldenv(&__env);
221 	}
222 	if (__HAS_SSE()) {
223 		__stmxcsr(&__mxcsr);
224 		__mxcsr &= ~__excepts;
225 		__ldmxcsr(&__mxcsr);
226 	}
227 	return (0);
228 }
229 
230 __fenv_static inline int
231 __fegetexceptflag_int(fexcept_t *__flagp, int __excepts)
232 {
233 	__uint32_t __mxcsr;
234 	__uint16_t __status;
235 
236 	__fnstsw(&__status);
237 	if (__HAS_SSE())
238 		__stmxcsr(&__mxcsr);
239 	else
240 		__mxcsr = 0;
241 	*__flagp = (__mxcsr | __status) & __excepts;
242 	return (0);
243 }
244 
245 __fenv_static inline int
246 __fetestexcept_int(int __excepts)
247 {
248 	__uint32_t __mxcsr;
249 	__uint16_t __status;
250 
251 	__fnstsw(&__status);
252 	if (__HAS_SSE())
253 		__stmxcsr(&__mxcsr);
254 	else
255 		__mxcsr = 0;
256 	return ((__status | __mxcsr) & __excepts);
257 }
258 
259 __fenv_static inline int
260 __fesetround_int(int __round)
261 {
262 	__uint32_t __mxcsr;
263 	__uint16_t __control;
264 
265 	if (__round & ~_ROUND_MASK)
266 		return (-1);
267 
268 	__fnstcw(&__control);
269 	__control &= ~_ROUND_MASK;
270 	__control |= __round;
271 	__fldcw(&__control);
272 
273 	if (__HAS_SSE()) {
274 		__stmxcsr(&__mxcsr);
275 		__mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
276 		__mxcsr |= __round << _SSE_ROUND_SHIFT;
277 		__ldmxcsr(&__mxcsr);
278 	}
279 
280 	return (0);
281 }
282 
283 __fenv_static inline int
284 __fegetmode_int(femode_t *__modep)
285 {
286 
287 	__fnstcw(&__modep->__control);
288 	__modep->__reserved = 0;
289 	if (__HAS_SSE()) {
290 		__stmxcsr(&__modep->__mxcsr);
291 		__modep->__mxcsr &= ~FE_ALL_EXCEPT;
292 	} else
293 		__modep->__mxcsr = 0;
294 
295 	return (0);
296 }
297 
298 __fenv_static inline int
299 __fesetmode_int(const femode_t *__modep)
300 {
301 	__uint32_t __mxcsr;
302 
303 	__fldcw(&__modep->__control);
304 
305 	if (__HAS_SSE()) {
306 		__stmxcsr(&__mxcsr);
307 		__mxcsr &= FE_ALL_EXCEPT;
308 		__mxcsr |= __modep->__mxcsr & ~FE_ALL_EXCEPT;
309 		__ldmxcsr(&__mxcsr);
310 	}
311 
312 	return (0);
313 }
314 
315 __fenv_static inline int
316 __fesetenv_int(const fenv_t *__envp)
317 {
318 	fenv_t __env = *__envp;
319 	__uint32_t __mxcsr;
320 
321 	__mxcsr = __get_mxcsr(__env);
322 	__set_mxcsr(__env, 0xffffffff);
323 	/*
324 	 * XXX Using fldenvx() instead of fldenv() tells the compiler that this
325 	 * instruction clobbers the i387 register stack.  This happens because
326 	 * we restore the tag word from the saved environment.  Normally, this
327 	 * would happen anyway and we wouldn't care, because the ABI allows
328 	 * function calls to clobber the i387 regs.  However, fesetenv() is
329 	 * inlined, so we need to be more careful.
330 	 */
331 	__fldenvx(__env);
332 	if (__HAS_SSE())
333 		__ldmxcsr(&__mxcsr);
334 	return (0);
335 }
336 
337 #else /* __amd64__ */
338 
339 __fenv_static inline int
340 __feclearexcept_int(int __excepts)
341 {
342 	fenv_t __env;
343 
344 	if (__excepts == FE_ALL_EXCEPT) {
345 		__fnclex();
346 	} else {
347 		__fnstenv(&__env.__x87);
348 		__env.__x87.__status &= ~__excepts;
349 		__fldenv(&__env.__x87);
350 	}
351 	__stmxcsr(&__env.__mxcsr);
352 	__env.__mxcsr &= ~__excepts;
353 	__ldmxcsr(&__env.__mxcsr);
354 	return (0);
355 }
356 
357 __fenv_static inline int
358 __fegetexceptflag_int(fexcept_t *__flagp, int __excepts)
359 {
360 	__uint32_t __mxcsr;
361 	__uint16_t __status;
362 
363 	__stmxcsr(&__mxcsr);
364 	__fnstsw(&__status);
365 	*__flagp = (__mxcsr | __status) & __excepts;
366 	return (0);
367 }
368 
369 __fenv_static inline int
370 __fetestexcept_int(int __excepts)
371 {
372 	__uint32_t __mxcsr;
373 	__uint16_t __status;
374 
375 	__stmxcsr(&__mxcsr);
376 	__fnstsw(&__status);
377 	return ((__status | __mxcsr) & __excepts);
378 }
379 
380 __fenv_static inline int
381 __fesetround_int(int __round)
382 {
383 	__uint32_t __mxcsr;
384 	__uint16_t __control;
385 
386 	if (__round & ~_ROUND_MASK)
387 		return (-1);
388 
389 	__fnstcw(&__control);
390 	__control &= ~_ROUND_MASK;
391 	__control |= __round;
392 	__fldcw(&__control);
393 
394 	__stmxcsr(&__mxcsr);
395 	__mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
396 	__mxcsr |= __round << _SSE_ROUND_SHIFT;
397 	__ldmxcsr(&__mxcsr);
398 
399 	return (0);
400 }
401 
402 __fenv_static inline int
403 __fegetmode_int(femode_t *__modep)
404 {
405 
406 	__fnstcw(&__modep->__control);
407 	__modep->__reserved = 0;
408 	__stmxcsr(&__modep->__mxcsr);
409 	__modep->__mxcsr &= ~FE_ALL_EXCEPT;
410 
411 	return (0);
412 }
413 
414 __fenv_static inline int
415 __fesetmode_int(const femode_t *__modep)
416 {
417 	__uint32_t __mxcsr;
418 
419 	__fldcw(&__modep->__control);
420 
421 	__stmxcsr(&__mxcsr);
422 	__mxcsr &= FE_ALL_EXCEPT;
423 	__mxcsr |= __modep->__mxcsr & ~FE_ALL_EXCEPT;
424 	__ldmxcsr(&__mxcsr);
425 
426 	return (0);
427 }
428 
429 __fenv_static inline int
430 __fesetenv_int(const fenv_t *__envp)
431 {
432 
433 	/*
434 	 * XXX Using fldenvx() instead of fldenv() tells the compiler that this
435 	 * instruction clobbers the i387 register stack.  This happens because
436 	 * we restore the tag word from the saved environment.  Normally, this
437 	 * would happen anyway and we wouldn't care, because the ABI allows
438 	 * function calls to clobber the i387 regs.  However, fesetenv() is
439 	 * inlined, so we need to be more careful.
440 	 */
441 	__fldenvx(__envp->__x87);
442 	__ldmxcsr(&__envp->__mxcsr);
443 	return (0);
444 }
445 
446 #endif /* __i386__ */
447 
448 __END_DECLS
449 
450 #endif	/* !_FENV_H_ */
451