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: src/lib/msun/amd64/fenv.c,v 1.5 2010/02/03 20:23:47 kib Exp $
29 #include <sys/types.h>
30 #include <machine/npx.h>
33 const fenv_t __fe_dfl_env = {
34 { 0xffff0000 | __INITIAL_FPUCW__,
37 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
38 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
44 fesetexceptflag(const fexcept_t *flagp, int excepts)
48 __fnstenv(&env.__x87);
49 env.__x87.__status &= ~excepts;
50 env.__x87.__status |= *flagp & excepts;
53 __stmxcsr(&env.__mxcsr);
54 env.__mxcsr &= ~excepts;
55 env.__mxcsr |= *flagp & excepts;
56 __ldmxcsr(env.__mxcsr);
62 feraiseexcept(int excepts)
64 fexcept_t ex = excepts;
66 fesetexceptflag(&ex, excepts);
72 fegetenv(fenv_t *envp)
75 __fnstenv(&envp->__x87);
76 __stmxcsr(&envp->__mxcsr);
78 * fnstenv masks all exceptions, so we need to restore the
79 * control word to avoid this side effect.
81 __fldcw(envp->__x87.__control);
86 feholdexcept(fenv_t *envp)
91 __fnstenv(&envp->__x87);
93 envp->__mxcsr = mxcsr;
94 mxcsr &= ~FE_ALL_EXCEPT;
95 mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
101 feupdateenv(const fenv_t *envp)
109 feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
114 __feenableexcept(int mask)
116 __uint32_t mxcsr, omask;
119 mask &= FE_ALL_EXCEPT;
122 omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
125 mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
131 __fedisableexcept(int mask)
133 __uint32_t mxcsr, omask;
136 mask &= FE_ALL_EXCEPT;
139 omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
142 mxcsr |= mask << _SSE_EMASK_SHIFT;
147 __weak_reference(__feenableexcept, feenableexcept);
148 __weak_reference(__fedisableexcept, fedisableexcept);