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/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 = {
45 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
49 enum __sse_support __has_sse =
56 #define getfl(x) __asm __volatile("pushfl\n\tpopl %0" : "=mr" (*(x)))
57 #define setfl(x) __asm __volatile("pushl %0\n\tpopfl" : : "g" (x))
58 #define cpuid_dx(x) __asm __volatile("pushl %%ebx\n\tmovl $1, %%eax\n\t" \
59 "cpuid\n\tpopl %%ebx" \
60 : "=d" (*(x)) : : "eax", "ecx")
63 * Test for SSE support on this processor. We need to do this because
64 * we need to use ldmxcsr/stmxcsr to get correct results if any part
65 * of the program was compiled to use SSE floating-point, but we can't
66 * use SSE on older processors.
76 nflag = flag ^ 0x200000;
80 /* Not a 486, so CPUID should work. */
81 cpuid_dx(&dx_features);
82 if (dx_features & 0x2000000) {
83 __has_sse = __SSE_YES;
91 extern inline int feclearexcept(int __excepts);
92 extern inline int fegetexceptflag(fexcept_t *__flagp, int __excepts);
95 fesetexceptflag(const fexcept_t *flagp, int excepts)
101 env.__status &= ~excepts;
102 env.__status |= *flagp & excepts;
108 mxcsr |= *flagp & excepts;
116 feraiseexcept(int excepts)
118 fexcept_t ex = excepts;
120 fesetexceptflag(&ex, excepts);
125 extern inline int fetestexcept(int __excepts);
126 extern inline int fegetround(void);
127 extern inline int fesetround(int __round);
130 fegetenv(fenv_t *envp)
136 * fnstenv masks all exceptions, so we need to restore
137 * the old control word to avoid this side effect.
139 __fldcw(&envp->__control);
142 __set_mxcsr(*envp, mxcsr);
148 feholdexcept(fenv_t *envp)
156 __set_mxcsr(*envp, mxcsr);
157 mxcsr &= ~FE_ALL_EXCEPT;
158 mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
164 extern inline int fesetenv(const fenv_t *__envp);
167 feupdateenv(const fenv_t *envp)
178 feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
183 __feenableexcept(int mask)
185 __uint32_t mxcsr, omask;
188 mask &= FE_ALL_EXCEPT;
194 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
198 mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
205 __fedisableexcept(int mask)
207 __uint32_t mxcsr, omask;
210 mask &= FE_ALL_EXCEPT;
216 omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
220 mxcsr |= mask << _SSE_EMASK_SHIFT;
226 __weak_reference(__feenableexcept, feenableexcept);
227 __weak_reference(__fedisableexcept, fedisableexcept);