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
33 #include <sys/cdefs.h>
34 #include <sys/types.h>
46 typedef uint16_t fexcept_t
;
49 #define FE_INVALID 0x01
50 #define FE_DENORMAL 0x02
51 #define FE_DIVBYZERO 0x04
52 #define FE_OVERFLOW 0x08
53 #define FE_UNDERFLOW 0x10
54 #define FE_INEXACT 0x20
55 #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
56 FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
59 #define FE_TONEAREST 0x0000
60 #define FE_DOWNWARD 0x0400
61 #define FE_UPWARD 0x0800
62 #define FE_TOWARDZERO 0x0c00
63 #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
64 FE_UPWARD | FE_TOWARDZERO)
67 * As compared to the x87 control word, the SSE unit's control word
68 * has the rounding control bits offset by 3 and the exception mask
71 #define _SSE_ROUND_SHIFT 3
72 #define _SSE_EMASK_SHIFT 7
76 /* Default floating-point environment */
77 extern const fenv_t __fe_dfl_env
;
78 #define FE_DFL_ENV (&__fe_dfl_env)
80 #define __fldcw(__cw) __asm __volatile("fldcw %0" : : "m" (__cw))
81 #define __fldenv(__env) __asm __volatile("fldenv %0" : : "m" (__env))
82 #define __fldenvx(__env) __asm __volatile("fldenv %0" : : "m" (__env) \
83 : "st", "st(1)", "st(2)", "st(3)", "st(4)", \
84 "st(5)", "st(6)", "st(7)")
85 #define __fnclex() __asm __volatile("fnclex")
86 #define __fnstenv(__env) __asm __volatile("fnstenv %0" : "=m" (*(__env)))
87 #define __fnstcw(__cw) __asm __volatile("fnstcw %0" : "=m" (*(__cw)))
88 #define __fnstsw(__sw) __asm __volatile("fnstsw %0" : "=am" (*(__sw)))
89 #define __fwait() __asm __volatile("fwait")
90 #define __ldmxcsr(__csr) __asm __volatile("ldmxcsr %0" : : "m" (__csr))
91 #define __stmxcsr(__csr) __asm __volatile("stmxcsr %0" : "=m" (*(__csr)))
94 feclearexcept(int __excepts
)
98 if (__excepts
== FE_ALL_EXCEPT
) {
101 __fnstenv(&__env
.__x87
);
102 __env
.__x87
.__status
&= ~__excepts
;
103 __fldenv(__env
.__x87
);
105 __stmxcsr(&__env
.__mxcsr
);
106 __env
.__mxcsr
&= ~__excepts
;
107 __ldmxcsr(__env
.__mxcsr
);
112 fegetexceptflag(fexcept_t
*__flagp
, int __excepts
)
119 *__flagp
= (__mxcsr
| __status
) & __excepts
;
123 int fesetexceptflag(const fexcept_t
*__flagp
, int __excepts
);
124 int feraiseexcept(int __excepts
);
127 fetestexcept(int __excepts
)
134 return ((__status
| __mxcsr
) & __excepts
);
143 * We assume that the x87 and the SSE unit agree on the
144 * rounding mode. Reading the control word on the x87 turns
145 * out to be about 5 times faster than reading it on the SSE
146 * unit on an Opteron 244.
148 __fnstcw(&__control
);
149 return (__control
& _ROUND_MASK
);
153 fesetround(int __round
)
158 if (__round
& ~_ROUND_MASK
)
161 __fnstcw(&__control
);
162 __control
&= ~_ROUND_MASK
;
163 __control
|= __round
;
167 __mxcsr
&= ~(_ROUND_MASK
<< _SSE_ROUND_SHIFT
);
168 __mxcsr
|= __round
<< _SSE_ROUND_SHIFT
;
174 int fegetenv(fenv_t
*__envp
);
175 int feholdexcept(fenv_t
*__envp
);
178 fesetenv(const fenv_t
*__envp
)
182 * XXX Using fldenvx() instead of fldenv() tells the compiler that this
183 * instruction clobbers the i387 register stack. This happens because
184 * we restore the tag word from the saved environment. Normally, this
185 * would happen anyway and we wouldn't care, because the ABI allows
186 * function calls to clobber the i387 regs. However, fesetenv() is
187 * inlined, so we need to be more careful.
189 __fldenvx(__envp
->__x87
);
190 __ldmxcsr(__envp
->__mxcsr
);
194 int feupdateenv(const fenv_t
*__envp
);
198 int feenableexcept(int __mask
);
199 int fedisableexcept(int __mask
);
207 * We assume that the masks for the x87 and the SSE unit are
210 __fnstcw(&__control
);
211 return (~__control
& FE_ALL_EXCEPT
);
214 #endif /* __BSD_VISIBLE */
218 #endif /* !_FENV_H_ */