Branch data Line data Source code
1 : : /*-
2 : : * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
3 : : * All rights reserved.
4 : : *
5 : : * Redistribution and use in source and binary forms, with or without
6 : : * modification, are permitted provided that the following conditions
7 : : * are met:
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.
13 : : *
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
24 : : * SUCH DAMAGE.
25 : : *
26 : : * $FreeBSD: src/lib/msun/amd64/fenv.h,v 1.8 2011/10/10 15:43:09 das Exp $
27 : : */
28 : :
29 : : #ifndef _FENV_H_
30 : : #define _FENV_H_
31 : :
32 : : #include <openlibm_defs.h>
33 : : #include "cdefs-compat.h"
34 : : #include "types-compat.h"
35 : :
36 : : #ifndef __fenv_static
37 : : #define __fenv_static static
38 : : #endif
39 : :
40 : : typedef struct {
41 : : struct {
42 : : uint32_t __control;
43 : : uint32_t __status;
44 : : uint32_t __tag;
45 : : char __other[16];
46 : : } __x87;
47 : : uint32_t __mxcsr;
48 : : } fenv_t;
49 : :
50 : : typedef uint16_t fexcept_t;
51 : :
52 : : /* Exception flags */
53 : : #define FE_INVALID 0x01
54 : : #define FE_DENORMAL 0x02
55 : : #define FE_DIVBYZERO 0x04
56 : : #define FE_OVERFLOW 0x08
57 : : #define FE_UNDERFLOW 0x10
58 : : #define FE_INEXACT 0x20
59 : : #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
60 : : FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
61 : :
62 : : /* Rounding modes */
63 : : #define FE_TONEAREST 0x0000
64 : : #define FE_DOWNWARD 0x0400
65 : : #define FE_UPWARD 0x0800
66 : : #define FE_TOWARDZERO 0x0c00
67 : : #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
68 : : FE_UPWARD | FE_TOWARDZERO)
69 : :
70 : : /*
71 : : * As compared to the x87 control word, the SSE unit's control word
72 : : * has the rounding control bits offset by 3 and the exception mask
73 : : * bits offset by 7.
74 : : */
75 : : #define _SSE_ROUND_SHIFT 3
76 : : #define _SSE_EMASK_SHIFT 7
77 : :
78 : : __BEGIN_DECLS
79 : :
80 : : /* Default floating-point environment */
81 : : extern const fenv_t __fe_dfl_env;
82 : : #define FE_DFL_ENV (&__fe_dfl_env)
83 : :
84 : : #define __fldcw(__cw) __asm __volatile("fldcw %0" : : "m" (__cw))
85 : : #define __fldenv(__env) __asm __volatile("fldenv %0" : : "m" (__env))
86 : : #define __fldenvx(__env) __asm __volatile("fldenv %0" : : "m" (__env) \
87 : : : "st", "st(1)", "st(2)", "st(3)", "st(4)", \
88 : : "st(5)", "st(6)", "st(7)")
89 : : #define __fnclex() __asm __volatile("fnclex")
90 : : #define __fnstenv(__env) __asm __volatile("fnstenv %0" : "=m" (*(__env)))
91 : : #define __fnstcw(__cw) __asm __volatile("fnstcw %0" : "=m" (*(__cw)))
92 : : #define __fnstsw(__sw) __asm __volatile("fnstsw %0" : "=am" (*(__sw)))
93 : : #define __fwait() __asm __volatile("fwait")
94 : : #define __ldmxcsr(__csr) __asm __volatile("ldmxcsr %0" : : "m" (__csr))
95 : : #define __stmxcsr(__csr) __asm __volatile("stmxcsr %0" : "=m" (*(__csr)))
96 : :
97 : : __fenv_static __attribute__((always_inline)) inline int
98 : : feclearexcept(int __excepts)
99 : : {
100 : : fenv_t __env;
101 : :
102 : : if (__excepts == FE_ALL_EXCEPT) {
103 : : __fnclex();
104 : : } else {
105 : : __fnstenv(&__env.__x87);
106 : : __env.__x87.__status &= ~__excepts;
107 : : __fldenv(__env.__x87);
108 : : }
109 : : __stmxcsr(&__env.__mxcsr);
110 : : __env.__mxcsr &= ~__excepts;
111 : : __ldmxcsr(__env.__mxcsr);
112 : : return (0);
113 : : }
114 : :
115 : : __fenv_static inline int
116 : : fegetexceptflag(fexcept_t *__flagp, int __excepts)
117 : : {
118 : : uint32_t __mxcsr;
119 : : uint16_t __status;
120 : :
121 : : __stmxcsr(&__mxcsr);
122 : : __fnstsw(&__status);
123 : : *__flagp = (__mxcsr | __status) & __excepts;
124 : : return (0);
125 : : }
126 : :
127 : : OLM_DLLEXPORT int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
128 : : OLM_DLLEXPORT int feraiseexcept(int __excepts);
129 : :
130 : : __fenv_static __attribute__((always_inline)) inline int
131 : : fetestexcept(int __excepts)
132 : : {
133 : : uint32_t __mxcsr;
134 : : uint16_t __status;
135 : :
136 : : __stmxcsr(&__mxcsr);
137 : : __fnstsw(&__status);
138 : : return ((__status | __mxcsr) & __excepts);
139 : : }
140 : :
141 : : __fenv_static inline int
142 : 1 : fegetround(void)
143 : : {
144 : : uint16_t __control;
145 : :
146 : : /*
147 : : * We assume that the x87 and the SSE unit agree on the
148 : : * rounding mode. Reading the control word on the x87 turns
149 : : * out to be about 5 times faster than reading it on the SSE
150 : : * unit on an Opteron 244.
151 : : */
152 : 1 : __fnstcw(&__control);
153 : 1 : return (__control & _ROUND_MASK);
154 : : }
155 : :
156 : : __fenv_static inline int
157 : 1 : fesetround(int __round)
158 : : {
159 : : uint32_t __mxcsr;
160 : : uint16_t __control;
161 : :
162 [ - + ]: 1 : if (__round & ~_ROUND_MASK)
163 : 0 : return (-1);
164 : :
165 : 1 : __fnstcw(&__control);
166 : 1 : __control &= ~_ROUND_MASK;
167 : 1 : __control |= __round;
168 : 1 : __fldcw(__control);
169 : :
170 : 1 : __stmxcsr(&__mxcsr);
171 : 1 : __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
172 : 1 : __mxcsr |= __round << _SSE_ROUND_SHIFT;
173 : 1 : __ldmxcsr(__mxcsr);
174 : :
175 : 1 : return (0);
176 : : }
177 : :
178 : : OLM_DLLEXPORT int fegetenv(fenv_t *__envp);
179 : : OLM_DLLEXPORT int feholdexcept(fenv_t *__envp);
180 : :
181 : : __fenv_static inline int
182 : 18 : fesetenv(const fenv_t *__envp)
183 : : {
184 : :
185 : : /*
186 : : * XXX Using fldenvx() instead of fldenv() tells the compiler that this
187 : : * instruction clobbers the i387 register stack. This happens because
188 : : * we restore the tag word from the saved environment. Normally, this
189 : : * would happen anyway and we wouldn't care, because the ABI allows
190 : : * function calls to clobber the i387 regs. However, fesetenv() is
191 : : * inlined, so we need to be more careful.
192 : : */
193 : 18 : __fldenvx(__envp->__x87);
194 : 18 : __ldmxcsr(__envp->__mxcsr);
195 : 18 : return (0);
196 : : }
197 : :
198 : : OLM_DLLEXPORT int feupdateenv(const fenv_t *__envp);
199 : :
200 : : #if __BSD_VISIBLE
201 : :
202 : : OLM_DLLEXPORT int feenableexcept(int __mask);
203 : : OLM_DLLEXPORT int fedisableexcept(int __mask);
204 : :
205 : : /* We currently provide no external definition of fegetexcept(). */
206 : : static inline int
207 : : fegetexcept(void)
208 : : {
209 : : uint16_t __control;
210 : :
211 : : /*
212 : : * We assume that the masks for the x87 and the SSE unit are
213 : : * the same.
214 : : */
215 : : __fnstcw(&__control);
216 : : return (~__control & FE_ALL_EXCEPT);
217 : : }
218 : :
219 : : #endif /* __BSD_VISIBLE */
220 : :
221 : : __END_DECLS
222 : :
223 : : #endif /* !_FENV_H_ */
|