2 * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #include <machine/fpu.h>
36 #ifdef __GNUC_GNU_INLINE__
37 #error "This file must be compiled with C99 'inline' semantics"
40 const fenv_t __fe_dfl_env = {
41 { 0xffff0000 | __INITIAL_FPUCW__,
44 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
45 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
50 extern inline int feclearexcept(int __excepts);
51 extern inline int fegetexceptflag(fexcept_t *__flagp, int __excepts);
54 fesetexceptflag(const fexcept_t *flagp, int excepts)
58 __fnstenv(&env.__x87);
59 env.__x87.__status &= ~excepts;
60 env.__x87.__status |= *flagp & excepts;
63 __stmxcsr(&env.__mxcsr);
64 env.__mxcsr &= ~excepts;
65 env.__mxcsr |= *flagp & excepts;
66 __ldmxcsr(env.__mxcsr);
72 feraiseexcept(int excepts)
74 fexcept_t ex = excepts;
76 fesetexceptflag(&ex, excepts);
81 extern inline int fetestexcept(int __excepts);
82 extern inline int fegetround(void);
83 extern inline int fesetround(int __round);
86 fegetenv(fenv_t *envp)
89 __fnstenv(&envp->__x87);
90 __stmxcsr(&envp->__mxcsr);
92 * fnstenv masks all exceptions, so we need to restore the
93 * control word to avoid this side effect.
95 __fldcw(envp->__x87.__control);
100 feholdexcept(fenv_t *envp)
105 __fnstenv(&envp->__x87);
107 envp->__mxcsr = mxcsr;
108 mxcsr &= ~FE_ALL_EXCEPT;
109 mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
114 extern inline int fesetenv(const fenv_t *__envp);
117 feupdateenv(const fenv_t *envp)
125 feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
130 __feenableexcept(int mask)
132 __uint32_t mxcsr, omask;
135 mask &= FE_ALL_EXCEPT;
138 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
141 mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
147 __fedisableexcept(int mask)
149 __uint32_t mxcsr, omask;
152 mask &= FE_ALL_EXCEPT;
155 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
158 mxcsr |= mask << _SSE_EMASK_SHIFT;
163 __weak_reference(__feenableexcept, feenableexcept);
164 __weak_reference(__fedisableexcept, fedisableexcept);