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
31 #include <sys/cdefs.h>
32 #include <sys/types.h>
33 #include <machine/fpu.h>
38 #ifdef __GNUC_GNU_INLINE__
39 #error "This file must be compiled with C99 'inline' semantics"
42 const fenv_t __fe_dfl_env = {
43 { 0xffff0000 | __INITIAL_FPUCW__,
46 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
52 extern inline int feclearexcept(int __excepts);
53 extern inline int fegetexceptflag(fexcept_t *__flagp, int __excepts);
56 fesetexceptflag(const fexcept_t *flagp, int excepts)
60 __fnstenv(&env.__x87);
61 env.__x87.__status &= ~excepts;
62 env.__x87.__status |= *flagp & excepts;
65 __stmxcsr(&env.__mxcsr);
66 env.__mxcsr &= ~excepts;
67 env.__mxcsr |= *flagp & excepts;
68 __ldmxcsr(&env.__mxcsr);
74 feraiseexcept(int excepts)
76 fexcept_t ex = excepts;
78 fesetexceptflag(&ex, excepts);
83 extern inline int fetestexcept(int __excepts);
84 extern inline int fegetround(void);
85 extern inline int fesetround(int __round);
88 fegetenv(fenv_t *envp)
91 __fnstenv(&envp->__x87);
92 __stmxcsr(&envp->__mxcsr);
94 * fnstenv masks all exceptions, so we need to restore the
95 * control word to avoid this side effect.
97 __fldcw(&envp->__x87.__control);
102 feholdexcept(fenv_t *envp)
107 __fnstenv(&envp->__x87);
109 envp->__mxcsr = mxcsr;
110 mxcsr &= ~FE_ALL_EXCEPT;
111 mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
116 extern inline int fesetenv(const fenv_t *__envp);
119 feupdateenv(const fenv_t *envp)
127 feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
132 __feenableexcept(int mask)
134 __uint32_t mxcsr, omask;
137 mask &= FE_ALL_EXCEPT;
140 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
143 mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
149 __fedisableexcept(int mask)
151 __uint32_t mxcsr, omask;
154 mask &= FE_ALL_EXCEPT;
157 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
160 mxcsr |= mask << _SSE_EMASK_SHIFT;
165 __weak_reference(__feenableexcept, feenableexcept);
166 __weak_reference(__fedisableexcept, fedisableexcept);