2 * SPDX-License-Identifier: BSD-2-Clause
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
32 #include <sys/cdefs.h>
33 #include <sys/_types.h>
37 #define __fenv_static static
40 typedef __uint16_t fexcept_t;
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)
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)
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
65 #define _SSE_ROUND_SHIFT 3
66 #define _SSE_EMASK_SHIFT 7
70 * To preserve binary compatibility with FreeBSD 5.3, we pack the
71 * mxcsr into some reserved fields, rather than changing sizeof(fenv_t).
75 __uint16_t __mxcsr_hi;
77 __uint16_t __mxcsr_lo;
95 /* Default floating-point environment */
96 extern const fenv_t __fe_dfl_env;
97 #define FE_DFL_ENV (&__fe_dfl_env)
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")
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);
110 __fenv_static inline int
113 __uint16_t __control;
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.
121 __fnstcw(&__control);
122 return (__control & _ROUND_MASK);
127 int feenableexcept(int __mask);
128 int fedisableexcept(int __mask);
130 /* We currently provide no external definition of fegetexcept(). */
134 __uint16_t __control;
137 * We assume that the masks for the x87 and the SSE unit are
140 __fnstcw(&__control);
141 return (~__control & FE_ALL_EXCEPT);
144 #endif /* __BSD_VISIBLE */
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);
153 #define __HAS_SSE() 1
155 #define __HAS_SSE() (__has_sse == __SSE_YES || \
156 (__has_sse == __SSE_UNK && __test_sse()))
159 #define __get_mxcsr(env) (((env).__mxcsr_hi << 16) | \
161 #define __set_mxcsr(env, x) do { \
162 (env).__mxcsr_hi = (__uint32_t)(x) >> 16; \
163 (env).__mxcsr_lo = (__uint16_t)(x); \
166 __fenv_static inline int
167 feclearexcept(int __excepts)
172 if (__excepts == FE_ALL_EXCEPT) {
176 __env.__status &= ~__excepts;
181 __mxcsr &= ~__excepts;
187 __fenv_static inline int
188 fegetexceptflag(fexcept_t *__flagp, int __excepts)
198 *__flagp = (__mxcsr | __status) & __excepts;
202 __fenv_static inline int
203 fetestexcept(int __excepts)
213 return ((__status | __mxcsr) & __excepts);
216 __fenv_static inline int
217 fesetround(int __round)
220 __uint16_t __control;
222 if (__round & ~_ROUND_MASK)
225 __fnstcw(&__control);
226 __control &= ~_ROUND_MASK;
227 __control |= __round;
232 __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
233 __mxcsr |= __round << _SSE_ROUND_SHIFT;
240 __fenv_static inline int
241 fesetenv(const fenv_t *__envp)
243 fenv_t __env = *__envp;
246 __mxcsr = __get_mxcsr(__env);
247 __set_mxcsr(__env, 0xffffffff);
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.
262 #else /* __amd64__ */
264 __fenv_static inline int
265 feclearexcept(int __excepts)
269 if (__excepts == FE_ALL_EXCEPT) {
272 __fnstenv(&__env.__x87);
273 __env.__x87.__status &= ~__excepts;
274 __fldenv(&__env.__x87);
276 __stmxcsr(&__env.__mxcsr);
277 __env.__mxcsr &= ~__excepts;
278 __ldmxcsr(&__env.__mxcsr);
282 __fenv_static inline int
283 fegetexceptflag(fexcept_t *__flagp, int __excepts)
290 *__flagp = (__mxcsr | __status) & __excepts;
294 __fenv_static inline int
295 fetestexcept(int __excepts)
302 return ((__status | __mxcsr) & __excepts);
305 __fenv_static inline int
306 fesetround(int __round)
309 __uint16_t __control;
311 if (__round & ~_ROUND_MASK)
314 __fnstcw(&__control);
315 __control &= ~_ROUND_MASK;
316 __control |= __round;
320 __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
321 __mxcsr |= __round << _SSE_ROUND_SHIFT;
327 __fenv_static inline int
328 fesetenv(const fenv_t *__envp)
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.
339 __fldenvx(__envp->__x87);
340 __ldmxcsr(&__envp->__mxcsr);
344 #endif /* __i386__ */
348 #endif /* !_FENV_H_ */