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
26 * $FreeBSD: head/lib/msun/amd64/fenv.c 226594 2011-10-21 06:25:31Z das $
29 #include <sys/types.h>
30 #include <machine/npx.h>
35 #ifdef __GNUC_GNU_INLINE__
36 #error "This file must be compiled with C99 'inline' semantics"
39 const fenv_t __fe_dfl_env = {
40 { 0xffff0000 | __INITIAL_FPUCW__,
43 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
49 extern inline int feclearexcept(int __excepts);
50 extern inline int fegetexceptflag(fexcept_t *__flagp, int __excepts);
53 fesetexceptflag(const fexcept_t *flagp, int excepts)
57 __fnstenv(&env.__x87);
58 env.__x87.__status &= ~excepts;
59 env.__x87.__status |= *flagp & excepts;
62 __stmxcsr(&env.__mxcsr);
63 env.__mxcsr &= ~excepts;
64 env.__mxcsr |= *flagp & excepts;
65 __ldmxcsr(env.__mxcsr);
71 feraiseexcept(int excepts)
73 fexcept_t ex = excepts;
75 fesetexceptflag(&ex, excepts);
80 extern inline int fetestexcept(int __excepts);
81 extern inline int fegetround(void);
82 extern inline int fesetround(int __round);
85 fegetenv(fenv_t *envp)
88 __fnstenv(&envp->__x87);
89 __stmxcsr(&envp->__mxcsr);
91 * fnstenv masks all exceptions, so we need to restore the
92 * control word to avoid this side effect.
94 __fldcw(envp->__x87.__control);
99 feholdexcept(fenv_t *envp)
104 __fnstenv(&envp->__x87);
106 envp->__mxcsr = mxcsr;
107 mxcsr &= ~FE_ALL_EXCEPT;
108 mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
113 extern inline int fesetenv(const fenv_t *__envp);
116 feupdateenv(const fenv_t *envp)
124 feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
129 __feenableexcept(int mask)
131 __uint32_t mxcsr, omask;
134 mask &= FE_ALL_EXCEPT;
137 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
140 mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
146 __fedisableexcept(int mask)
148 __uint32_t mxcsr, omask;
151 mask &= FE_ALL_EXCEPT;
154 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
157 mxcsr |= mask << _SSE_EMASK_SHIFT;
162 __weak_reference(__feenableexcept, feenableexcept);
163 __weak_reference(__fedisableexcept, fedisableexcept);