2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/cdefs.h>
35 #include <sys/_types.h>
39 #define __fenv_static static
42 typedef __uint16_t fexcept_t;
45 #define FE_INVALID 0x01
46 #define FE_DENORMAL 0x02
47 #define FE_DIVBYZERO 0x04
48 #define FE_OVERFLOW 0x08
49 #define FE_UNDERFLOW 0x10
50 #define FE_INEXACT 0x20
51 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
52 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
55 #define FE_TONEAREST 0x0000
56 #define FE_DOWNWARD 0x0400
57 #define FE_UPWARD 0x0800
58 #define FE_TOWARDZERO 0x0c00
59 #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
60 FE_UPWARD | FE_TOWARDZERO)
63 * As compared to the x87 control word, the SSE unit's control word
64 * has the rounding control bits offset by 3 and the exception mask
67 #define _SSE_ROUND_SHIFT 3
68 #define _SSE_EMASK_SHIFT 7
72 * To preserve binary compatibility with FreeBSD 5.3, we pack the
73 * mxcsr into some reserved fields, rather than changing sizeof(fenv_t).
77 __uint16_t __mxcsr_hi;
79 __uint16_t __mxcsr_lo;
97 /* Default floating-point environment */
98 extern const fenv_t __fe_dfl_env;
99 #define FE_DFL_ENV (&__fe_dfl_env)
101 #define __fldenvx(__env) __asm __volatile("fldenv %0" : : "m" (__env) \
102 : "st", "st(1)", "st(2)", "st(3)", "st(4)", \
103 "st(5)", "st(6)", "st(7)")
104 #define __fwait() __asm __volatile("fwait")
106 int fegetenv(fenv_t *__envp);
107 int feholdexcept(fenv_t *__envp);
108 int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
109 int feraiseexcept(int __excepts);
110 int feupdateenv(const fenv_t *__envp);
112 __fenv_static inline int
115 __uint16_t __control;
118 * We assume that the x87 and the SSE unit agree on the
119 * rounding mode. Reading the control word on the x87 turns
120 * out to be about 5 times faster than reading it on the SSE
121 * unit on an Opteron 244.
123 __fnstcw(&__control);
124 return (__control & _ROUND_MASK);
129 int feenableexcept(int __mask);
130 int fedisableexcept(int __mask);
132 /* We currently provide no external definition of fegetexcept(). */
136 __uint16_t __control;
139 * We assume that the masks for the x87 and the SSE unit are
142 __fnstcw(&__control);
143 return (~__control & FE_ALL_EXCEPT);
146 #endif /* __BSD_VISIBLE */
150 /* After testing for SSE support once, we cache the result in __has_sse. */
151 enum __sse_support { __SSE_YES, __SSE_NO, __SSE_UNK };
152 extern enum __sse_support __has_sse;
153 int __test_sse(void);
155 #define __HAS_SSE() 1
157 #define __HAS_SSE() (__has_sse == __SSE_YES || \
158 (__has_sse == __SSE_UNK && __test_sse()))
161 #define __get_mxcsr(env) (((env).__mxcsr_hi << 16) | \
163 #define __set_mxcsr(env, x) do { \
164 (env).__mxcsr_hi = (__uint32_t)(x) >> 16; \
165 (env).__mxcsr_lo = (__uint16_t)(x); \
168 __fenv_static inline int
169 feclearexcept(int __excepts)
174 if (__excepts == FE_ALL_EXCEPT) {
178 __env.__status &= ~__excepts;
183 __mxcsr &= ~__excepts;
189 __fenv_static inline int
190 fegetexceptflag(fexcept_t *__flagp, int __excepts)
200 *__flagp = (__mxcsr | __status) & __excepts;
204 __fenv_static inline int
205 fetestexcept(int __excepts)
215 return ((__status | __mxcsr) & __excepts);
218 __fenv_static inline int
219 fesetround(int __round)
222 __uint16_t __control;
224 if (__round & ~_ROUND_MASK)
227 __fnstcw(&__control);
228 __control &= ~_ROUND_MASK;
229 __control |= __round;
234 __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
235 __mxcsr |= __round << _SSE_ROUND_SHIFT;
242 __fenv_static inline int
243 fesetenv(const fenv_t *__envp)
245 fenv_t __env = *__envp;
248 __mxcsr = __get_mxcsr(__env);
249 __set_mxcsr(__env, 0xffffffff);
251 * XXX Using fldenvx() instead of fldenv() tells the compiler that this
252 * instruction clobbers the i387 register stack. This happens because
253 * we restore the tag word from the saved environment. Normally, this
254 * would happen anyway and we wouldn't care, because the ABI allows
255 * function calls to clobber the i387 regs. However, fesetenv() is
256 * inlined, so we need to be more careful.
264 #else /* __amd64__ */
266 __fenv_static inline int
267 feclearexcept(int __excepts)
271 if (__excepts == FE_ALL_EXCEPT) {
274 __fnstenv(&__env.__x87);
275 __env.__x87.__status &= ~__excepts;
276 __fldenv(&__env.__x87);
278 __stmxcsr(&__env.__mxcsr);
279 __env.__mxcsr &= ~__excepts;
280 __ldmxcsr(&__env.__mxcsr);
284 __fenv_static inline int
285 fegetexceptflag(fexcept_t *__flagp, int __excepts)
292 *__flagp = (__mxcsr | __status) & __excepts;
296 __fenv_static inline int
297 fetestexcept(int __excepts)
304 return ((__status | __mxcsr) & __excepts);
307 __fenv_static inline int
308 fesetround(int __round)
311 __uint16_t __control;
313 if (__round & ~_ROUND_MASK)
316 __fnstcw(&__control);
317 __control &= ~_ROUND_MASK;
318 __control |= __round;
322 __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
323 __mxcsr |= __round << _SSE_ROUND_SHIFT;
329 __fenv_static inline int
330 fesetenv(const fenv_t *__envp)
334 * XXX Using fldenvx() instead of fldenv() tells the compiler that this
335 * instruction clobbers the i387 register stack. This happens because
336 * we restore the tag word from the saved environment. Normally, this
337 * would happen anyway and we wouldn't care, because the ABI allows
338 * function calls to clobber the i387 regs. However, fesetenv() is
339 * inlined, so we need to be more careful.
341 __fldenvx(__envp->__x87);
342 __ldmxcsr(&__envp->__mxcsr);
346 #endif /* __i386__ */
350 #endif /* !_FENV_H_ */