]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/i386/fpu/fenv_private.h
2 #define FENV_PRIVATE_H 1
5 #include <fpu_control.h>
8 # define math_opt_barrier(x) \
10 if (sizeof (x) <= sizeof (double)) \
11 __asm ("" : "=x" (__x) : "0" (x)); \
13 __asm ("" : "=t" (__x) : "0" (x)); \
15 # define math_force_eval(x) \
17 if (sizeof (x) <= sizeof (double)) \
18 __asm __volatile ("" : : "x" (x)); \
20 __asm __volatile ("" : : "f" (x)); \
23 # define math_opt_barrier(x) \
24 ({ __typeof (x) __x; \
25 __asm ("" : "=t" (__x) : "0" (x)); \
27 # define math_force_eval(x) \
29 __typeof (x) __x = (x); \
30 if (sizeof (x) <= sizeof (double)) \
31 __asm __volatile ("" : : "m" (__x)); \
33 __asm __volatile ("" : : "f" (__x)); \
37 /* This file is used by both the 32- and 64-bit ports. The 64-bit port
38 has a field in the fenv_t for the mxcsr; the 32-bit port does not.
39 Instead, we (ab)use the only 32-bit field extant in the struct. */
41 # define __mxcsr __eip
45 /* All of these functions are private to libm, and are all used in pairs
46 to save+change the fp state and restore the original state. Thus we
47 need not care for both the 387 and the sse unit, only the one we're
50 #if defined __AVX__ || defined SSE2AVX
51 # define STMXCSR "vstmxcsr"
52 # define LDMXCSR "vldmxcsr"
54 # define STMXCSR "stmxcsr"
55 # define LDMXCSR "ldmxcsr"
58 static __always_inline
void
59 libc_feholdexcept_sse (fenv_t
*e
)
62 asm (STMXCSR
" %0" : "=m" (*&mxcsr
));
64 mxcsr
= (mxcsr
| 0x1f80) & ~0x3f;
65 asm volatile (LDMXCSR
" %0" : : "m" (*&mxcsr
));
68 static __always_inline
void
69 libc_feholdexcept_387 (fenv_t
*e
)
71 /* Recall that fnstenv has a side-effect of masking exceptions.
72 Clobber all of the fp registers so that the TOS field is 0. */
73 asm volatile ("fnstenv %0; fnclex"
75 : : "st", "st(1)", "st(2)", "st(3)",
76 "st(4)", "st(5)", "st(6)", "st(7)");
79 static __always_inline
void
80 libc_fesetround_sse (int r
)
83 asm (STMXCSR
" %0" : "=m" (*&mxcsr
));
84 mxcsr
= (mxcsr
& ~0x6000) | (r
<< 3);
85 asm volatile (LDMXCSR
" %0" : : "m" (*&mxcsr
));
88 static __always_inline
void
89 libc_fesetround_387 (int r
)
93 cw
= (cw
& ~0xc00) | r
;
97 static __always_inline
void
98 libc_feholdexcept_setround_sse (fenv_t
*e
, int r
)
101 asm (STMXCSR
" %0" : "=m" (*&mxcsr
));
103 mxcsr
= ((mxcsr
| 0x1f80) & ~0x603f) | (r
<< 3);
104 asm volatile (LDMXCSR
" %0" : : "m" (*&mxcsr
));
107 /* Set both rounding mode and precision. A convenience function for use
108 by libc_feholdexcept_setround and libc_feholdexcept_setround_53bit. */
109 static __always_inline
void
110 libc_feholdexcept_setround_387_prec (fenv_t
*e
, int r
)
112 libc_feholdexcept_387 (e
);
114 fpu_control_t cw
= e
->__control_word
;
115 cw
&= ~(_FPU_RC_ZERO
| _FPU_EXTENDED
);
120 static __always_inline
void
121 libc_feholdexcept_setround_387 (fenv_t
*e
, int r
)
123 libc_feholdexcept_setround_387_prec (e
, r
| _FPU_EXTENDED
);
126 static __always_inline
void
127 libc_feholdexcept_setround_387_53bit (fenv_t
*e
, int r
)
129 libc_feholdexcept_setround_387_prec (e
, r
| _FPU_DOUBLE
);
132 static __always_inline
int
133 libc_fetestexcept_sse (int e
)
136 asm volatile (STMXCSR
" %0" : "=m" (*&mxcsr
));
137 return mxcsr
& e
& FE_ALL_EXCEPT
;
140 static __always_inline
int
141 libc_fetestexcept_387 (int ex
)
144 asm volatile ("fnstsw %0" : "=a" (temp
));
145 return temp
& ex
& FE_ALL_EXCEPT
;
148 static __always_inline
void
149 libc_fesetenv_sse (fenv_t
*e
)
151 asm volatile (LDMXCSR
" %0" : : "m" (e
->__mxcsr
));
154 static __always_inline
void
155 libc_fesetenv_387 (fenv_t
*e
)
157 /* Clobber all fp registers so that the TOS value we saved earlier is
158 compatible with the current state of the compiler. */
159 asm volatile ("fldenv %0"
161 : "st", "st(1)", "st(2)", "st(3)",
162 "st(4)", "st(5)", "st(6)", "st(7)");
165 static __always_inline
int
166 libc_feupdateenv_test_sse (fenv_t
*e
, int ex
)
168 unsigned int mxcsr
, old_mxcsr
, cur_ex
;
169 asm volatile (STMXCSR
" %0" : "=m" (*&mxcsr
));
170 cur_ex
= mxcsr
& FE_ALL_EXCEPT
;
172 /* Merge current exceptions with the old environment. */
173 old_mxcsr
= e
->__mxcsr
;
174 mxcsr
= old_mxcsr
| cur_ex
;
175 asm volatile (LDMXCSR
" %0" : : "m" (*&mxcsr
));
177 /* Raise SIGFPE for any new exceptions since the hold. Expect that
178 the normal environment has all exceptions masked. */
179 if (__builtin_expect ((old_mxcsr
>> 7) & cur_ex
, 0))
180 __feraiseexcept (cur_ex
);
182 /* Test for exceptions raised since the hold. */
186 static __always_inline
int
187 libc_feupdateenv_test_387 (fenv_t
*e
, int ex
)
191 /* Save current exceptions. */
192 asm volatile ("fnstsw %0" : "=a" (cur_ex
));
193 cur_ex
&= FE_ALL_EXCEPT
;
195 /* Reload original environment. */
196 libc_fesetenv_387 (e
);
198 /* Merge current exceptions. */
199 __feraiseexcept (cur_ex
);
201 /* Test for exceptions raised since the hold. */
205 static __always_inline
void
206 libc_feupdateenv_sse (fenv_t
*e
)
208 libc_feupdateenv_test_sse (e
, 0);
211 static __always_inline
void
212 libc_feupdateenv_387 (fenv_t
*e
)
214 libc_feupdateenv_test_387 (e
, 0);
217 static __always_inline
void
218 libc_feholdsetround_sse (fenv_t
*e
, int r
)
221 asm (STMXCSR
" %0" : "=m" (*&mxcsr
));
223 mxcsr
= (mxcsr
& ~0x6000) | (r
<< 3);
224 asm volatile (LDMXCSR
" %0" : : "m" (*&mxcsr
));
227 static __always_inline
void
228 libc_feholdsetround_387_prec (fenv_t
*e
, int r
)
233 e
->__control_word
= cw
;
234 cw
&= ~(_FPU_RC_ZERO
| _FPU_EXTENDED
);
239 static __always_inline
void
240 libc_feholdsetround_387 (fenv_t
*e
, int r
)
242 libc_feholdsetround_387_prec (e
, r
| _FPU_EXTENDED
);
245 static __always_inline
void
246 libc_feholdsetround_387_53bit (fenv_t
*e
, int r
)
248 libc_feholdsetround_387_prec (e
, r
| _FPU_DOUBLE
);
251 static __always_inline
void
252 libc_feresetround_sse (fenv_t
*e
)
255 asm (STMXCSR
" %0" : "=m" (*&mxcsr
));
256 mxcsr
= (mxcsr
& ~0x6000) | (e
->__mxcsr
& 0x6000);
257 asm volatile (LDMXCSR
" %0" : : "m" (*&mxcsr
));
260 static __always_inline
void
261 libc_feresetround_387 (fenv_t
*e
)
263 _FPU_SETCW (e
->__control_word
);
267 # define libc_feholdexceptf libc_feholdexcept_sse
268 # define libc_fesetroundf libc_fesetround_sse
269 # define libc_feholdexcept_setroundf libc_feholdexcept_setround_sse
270 # define libc_fetestexceptf libc_fetestexcept_sse
271 # define libc_fesetenvf libc_fesetenv_sse
272 # define libc_feupdateenv_testf libc_feupdateenv_test_sse
273 # define libc_feupdateenvf libc_feupdateenv_sse
274 # define libc_feholdsetroundf libc_feholdsetround_sse
275 # define libc_feresetroundf libc_feresetround_sse
277 # define libc_feholdexceptf libc_feholdexcept_387
278 # define libc_fesetroundf libc_fesetround_387
279 # define libc_feholdexcept_setroundf libc_feholdexcept_setround_387
280 # define libc_fetestexceptf libc_fetestexcept_387
281 # define libc_fesetenvf libc_fesetenv_387
282 # define libc_feupdateenv_testf libc_feupdateenv_test_387
283 # define libc_feupdateenvf libc_feupdateenv_387
284 # define libc_feholdsetroundf libc_feholdsetround_387
285 # define libc_feresetroundf libc_feresetround_387
286 #endif /* __SSE_MATH__ */
289 # define libc_feholdexcept libc_feholdexcept_sse
290 # define libc_fesetround libc_fesetround_sse
291 # define libc_feholdexcept_setround libc_feholdexcept_setround_sse
292 # define libc_fetestexcept libc_fetestexcept_sse
293 # define libc_fesetenv libc_fesetenv_sse
294 # define libc_feupdateenv_test libc_feupdateenv_test_sse
295 # define libc_feupdateenv libc_feupdateenv_sse
296 # define libc_feholdsetround libc_feholdsetround_sse
297 # define libc_feresetround libc_feresetround_sse
299 # define libc_feholdexcept libc_feholdexcept_387
300 # define libc_fesetround libc_fesetround_387
301 # define libc_feholdexcept_setround libc_feholdexcept_setround_387
302 # define libc_fetestexcept libc_fetestexcept_387
303 # define libc_fesetenv libc_fesetenv_387
304 # define libc_feupdateenv_test libc_feupdateenv_test_387
305 # define libc_feupdateenv libc_feupdateenv_387
306 # define libc_feholdsetround libc_feholdsetround_387
307 # define libc_feresetround libc_feresetround_387
308 #endif /* __SSE2_MATH__ */
310 #define libc_feholdexceptl libc_feholdexcept_387
311 #define libc_fesetroundl libc_fesetround_387
312 #define libc_feholdexcept_setroundl libc_feholdexcept_setround_387
313 #define libc_fetestexceptl libc_fetestexcept_387
314 #define libc_fesetenvl libc_fesetenv_387
315 #define libc_feupdateenv_testl libc_feupdateenv_test_387
316 #define libc_feupdateenvl libc_feupdateenv_387
317 #define libc_feholdsetroundl libc_feholdsetround_387
318 #define libc_feresetroundl libc_feresetround_387
320 #ifndef __SSE2_MATH__
321 # define libc_feholdexcept_setround_53bit libc_feholdexcept_setround_387_53bit
322 # define libc_feholdsetround_53bit libc_feholdsetround_387_53bit
327 #endif /* FENV_PRIVATE_H */