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
29 #include <sys/cdefs.h>
30 #include <sys/types.h>
31 #include <machine/npx.h>
36 #ifdef __GNUC_GNU_INLINE__
37 #error "This file must be compiled with C99 'inline' semantics"
40 const fenv_t __fe_dfl_env = {
46 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
50 enum __sse_support __has_sse =
57 #define getfl(x) __asm __volatile("pushfl\n\tpopl %0" : "=mr" (*(x)))
58 #define setfl(x) __asm __volatile("pushl %0\n\tpopfl" : : "g" (x))
59 #define cpuid_dx(x) __asm __volatile("pushl %%ebx\n\tmovl $1, %%eax\n\t" \
60 "cpuid\n\tpopl %%ebx" \
61 : "=d" (*(x)) : : "eax", "ecx")
64 * Test for SSE support on this processor. We need to do this because
65 * we need to use ldmxcsr/stmxcsr to get correct results if any part
66 * of the program was compiled to use SSE floating-point, but we can't
67 * use SSE on older processors.
77 nflag = flag ^ 0x200000;
81 /* Not a 486, so CPUID should work. */
82 cpuid_dx(&dx_features);
83 if (dx_features & 0x2000000) {
84 __has_sse = __SSE_YES;
92 extern inline int feclearexcept(int __excepts);
93 extern inline int fegetexceptflag(fexcept_t *__flagp, int __excepts);
96 fesetexceptflag(const fexcept_t *flagp, int excepts)
102 env.__status &= ~excepts;
103 env.__status |= *flagp & excepts;
109 mxcsr |= *flagp & excepts;
117 feraiseexcept(int excepts)
119 fexcept_t ex = excepts;
121 fesetexceptflag(&ex, excepts);
126 extern inline int fetestexcept(int __excepts);
127 extern inline int fegetround(void);
128 extern inline int fesetround(int __round);
131 fegetenv(fenv_t *envp)
137 * fnstenv masks all exceptions, so we need to restore
138 * the old control word to avoid this side effect.
140 __fldcw(&envp->__control);
143 __set_mxcsr(*envp, mxcsr);
149 feholdexcept(fenv_t *envp)
157 __set_mxcsr(*envp, mxcsr);
158 mxcsr &= ~FE_ALL_EXCEPT;
159 mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
165 extern inline int fesetenv(const fenv_t *__envp);
168 feupdateenv(const fenv_t *envp)
179 feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
184 __feenableexcept(int mask)
186 __uint32_t mxcsr, omask;
189 mask &= FE_ALL_EXCEPT;
195 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
199 mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
206 __fedisableexcept(int mask)
208 __uint32_t mxcsr, omask;
211 mask &= FE_ALL_EXCEPT;
217 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
221 mxcsr |= mask << _SSE_EMASK_SHIFT;
227 __weak_reference(__feenableexcept, feenableexcept);
228 __weak_reference(__fedisableexcept, fedisableexcept);