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/npx.h>
38 #ifdef __GNUC_GNU_INLINE__
39 #error "This file must be compiled with C99 'inline' semantics"
42 const fenv_t __fe_dfl_env = {
48 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
49 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
52 enum __sse_support __has_sse =
59 #define getfl(x) __asm __volatile("pushfl\n\tpopl %0" : "=mr" (*(x)))
60 #define setfl(x) __asm __volatile("pushl %0\n\tpopfl" : : "g" (x))
61 #define cpuid_dx(x) __asm __volatile("pushl %%ebx\n\tmovl $1, %%eax\n\t" \
62 "cpuid\n\tpopl %%ebx" \
63 : "=d" (*(x)) : : "eax", "ecx")
66 * Test for SSE support on this processor. We need to do this because
67 * we need to use ldmxcsr/stmxcsr to get correct results if any part
68 * of the program was compiled to use SSE floating-point, but we can't
69 * use SSE on older processors.
79 nflag = flag ^ 0x200000;
83 /* Not a 486, so CPUID should work. */
84 cpuid_dx(&dx_features);
85 if (dx_features & 0x2000000) {
86 __has_sse = __SSE_YES;
94 extern inline int feclearexcept(int __excepts);
95 extern inline int fegetexceptflag(fexcept_t *__flagp, int __excepts);
98 fesetexceptflag(const fexcept_t *flagp, int excepts)
104 env.__status &= ~excepts;
105 env.__status |= *flagp & excepts;
111 mxcsr |= *flagp & excepts;
119 feraiseexcept(int excepts)
121 fexcept_t ex = excepts;
123 fesetexceptflag(&ex, excepts);
128 extern inline int fetestexcept(int __excepts);
129 extern inline int fegetround(void);
130 extern inline int fesetround(int __round);
133 fegetenv(fenv_t *envp)
139 * fnstenv masks all exceptions, so we need to restore
140 * the old control word to avoid this side effect.
142 __fldcw(envp->__control);
145 __set_mxcsr(*envp, mxcsr);
151 feholdexcept(fenv_t *envp)
159 __set_mxcsr(*envp, mxcsr);
160 mxcsr &= ~FE_ALL_EXCEPT;
161 mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
167 extern inline int fesetenv(const fenv_t *__envp);
170 feupdateenv(const fenv_t *envp)
181 feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
186 __feenableexcept(int mask)
188 __uint32_t mxcsr, omask;
191 mask &= FE_ALL_EXCEPT;
197 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
201 mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
208 __fedisableexcept(int mask)
210 __uint32_t mxcsr, omask;
213 mask &= FE_ALL_EXCEPT;
219 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
223 mxcsr |= mask << _SSE_EMASK_SHIFT;
229 __weak_reference(__feenableexcept, feenableexcept);
230 __weak_reference(__fedisableexcept, fedisableexcept);