fenv.c revision 1.6 1 1.6 joerg /* $NetBSD: fenv.c,v 1.6 2013/11/11 00:31:51 joerg Exp $ */
2 1.1 joerg
3 1.1 joerg /*-
4 1.1 joerg * Copyright (c) 2004-2005 David Schultz <das (at) FreeBSD.ORG>
5 1.1 joerg * All rights reserved.
6 1.1 joerg *
7 1.1 joerg * Redistribution and use in source and binary forms, with or without
8 1.1 joerg * modification, are permitted provided that the following conditions
9 1.1 joerg * are met:
10 1.1 joerg * 1. Redistributions of source code must retain the above copyright
11 1.1 joerg * notice, this list of conditions and the following disclaimer.
12 1.1 joerg * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 joerg * notice, this list of conditions and the following disclaimer in the
14 1.1 joerg * documentation and/or other materials provided with the distribution.
15 1.1 joerg *
16 1.1 joerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 joerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 joerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 joerg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 joerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 joerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 joerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 joerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 joerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 joerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 joerg * SUCH DAMAGE.
27 1.1 joerg */
28 1.1 joerg
29 1.1 joerg #include <sys/cdefs.h>
30 1.6 joerg __RCSID("$NetBSD: fenv.c,v 1.6 2013/11/11 00:31:51 joerg Exp $");
31 1.1 joerg
32 1.1 joerg #include <assert.h>
33 1.1 joerg #include <fenv.h>
34 1.1 joerg #include <stddef.h>
35 1.1 joerg #include <string.h>
36 1.1 joerg
37 1.1 joerg /* Load x87 Control Word */
38 1.1 joerg #define __fldcw(__cw) __asm__ __volatile__ \
39 1.1 joerg ("fldcw %0" : : "m" (__cw))
40 1.1 joerg
41 1.1 joerg /* No-Wait Store Control Word */
42 1.1 joerg #define __fnstcw(__cw) __asm__ __volatile__ \
43 1.1 joerg ("fnstcw %0" : "=m" (*(__cw)))
44 1.1 joerg
45 1.1 joerg /* No-Wait Store Status Word */
46 1.1 joerg #define __fnstsw(__sw) __asm__ __volatile__ \
47 1.1 joerg ("fnstsw %0" : "=am" (*(__sw)))
48 1.1 joerg
49 1.1 joerg /* No-Wait Clear Exception Flags */
50 1.1 joerg #define __fnclex() __asm__ __volatile__ \
51 1.1 joerg ("fnclex")
52 1.1 joerg
53 1.1 joerg /* Load x87 Environment */
54 1.1 joerg #define __fldenv(__env) __asm__ __volatile__ \
55 1.1 joerg ("fldenv %0" : : "m" (__env))
56 1.1 joerg
57 1.1 joerg /* No-Wait Store x87 environment */
58 1.1 joerg #define __fnstenv(__env) __asm__ __volatile__ \
59 1.1 joerg ("fnstenv %0" : "=m" (*(__env)))
60 1.1 joerg
61 1.4 riastrad /* Check for and handle pending unmasked x87 pending FPU exceptions */
62 1.4 riastrad #define __fwait(__env) __asm__ __volatile__ \
63 1.4 riastrad ("fwait")
64 1.4 riastrad
65 1.1 joerg /* Load the MXCSR register */
66 1.1 joerg #define __ldmxcsr(__mxcsr) __asm__ __volatile__ \
67 1.1 joerg ("ldmxcsr %0" : : "m" (__mxcsr))
68 1.1 joerg
69 1.1 joerg /* Store the MXCSR register state */
70 1.1 joerg #define __stmxcsr(__mxcsr) __asm__ __volatile__ \
71 1.1 joerg ("stmxcsr %0" : "=m" (*(__mxcsr)))
72 1.1 joerg
73 1.1 joerg /*
74 1.1 joerg * The following constant represents the default floating-point environment
75 1.1 joerg * (that is, the one installed at program startup) and has type pointer to
76 1.1 joerg * const-qualified fenv_t.
77 1.1 joerg *
78 1.1 joerg * It can be used as an argument to the functions within the <fenv.h> header
79 1.1 joerg * that manage the floating-point environment, namely fesetenv() and
80 1.1 joerg * feupdateenv().
81 1.1 joerg *
82 1.1 joerg * x87 fpu registers are 16bit wide. The upper bits, 31-16, are marked as
83 1.1 joerg * RESERVED. We provide a partial floating-point environment, where we
84 1.1 joerg * define only the lower bits. The reserved bits are extracted and set by
85 1.1 joerg * the consumers of FE_DFL_ENV, during runtime.
86 1.1 joerg */
87 1.1 joerg fenv_t __fe_dfl_env = {
88 1.1 joerg {
89 1.1 joerg __NetBSD_NPXCW__, /* Control word register */
90 1.1 joerg 0x00000000, /* Status word register */
91 1.1 joerg 0x0000ffff, /* Tag word register */
92 1.1 joerg {
93 1.1 joerg 0x00000000,
94 1.1 joerg 0x00000000,
95 1.1 joerg 0x00000000,
96 1.1 joerg 0x00000000,
97 1.1 joerg },
98 1.1 joerg },
99 1.1 joerg __INITIAL_MXCSR__ /* MXCSR register */
100 1.1 joerg };
101 1.1 joerg #define FE_DFL_ENV ((const fenv_t *) &__fe_dfl_env)
102 1.1 joerg
103 1.6 joerg static void __init_libm(void) __attribute__ ((constructor, used));
104 1.6 joerg
105 1.6 joerg static void __init_libm(void)
106 1.6 joerg {
107 1.6 joerg uint16_t control;
108 1.6 joerg
109 1.6 joerg __fnstcw(&control);
110 1.6 joerg __fe_dfl_env.x87.control = control;
111 1.6 joerg }
112 1.6 joerg
113 1.1 joerg
114 1.1 joerg /*
115 1.1 joerg * The feclearexcept() function clears the supported floating-point exceptions
116 1.1 joerg * represented by `excepts'.
117 1.1 joerg */
118 1.1 joerg int
119 1.1 joerg feclearexcept(int excepts)
120 1.1 joerg {
121 1.1 joerg fenv_t fenv;
122 1.1 joerg int ex;
123 1.1 joerg
124 1.1 joerg _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
125 1.1 joerg
126 1.1 joerg ex = excepts & FE_ALL_EXCEPT;
127 1.1 joerg
128 1.1 joerg /* Store the current x87 floating-point environment */
129 1.1 joerg __fnstenv(&fenv);
130 1.1 joerg
131 1.1 joerg /* Clear the requested floating-point exceptions */
132 1.1 joerg fenv.x87.status &= ~ex;
133 1.1 joerg
134 1.1 joerg /* Load the x87 floating-point environent */
135 1.1 joerg __fldenv(fenv);
136 1.1 joerg
137 1.1 joerg /* Same for SSE environment */
138 1.1 joerg __stmxcsr(&fenv.mxcsr);
139 1.1 joerg fenv.mxcsr &= ~ex;
140 1.1 joerg __ldmxcsr(fenv.mxcsr);
141 1.1 joerg
142 1.1 joerg /* Success */
143 1.1 joerg return (0);
144 1.1 joerg }
145 1.1 joerg
146 1.1 joerg /*
147 1.1 joerg * The fegetexceptflag() function stores an implementation-defined
148 1.1 joerg * representation of the states of the floating-point status flags indicated by
149 1.1 joerg * the argument excepts in the object pointed to by the argument flagp.
150 1.1 joerg */
151 1.1 joerg int
152 1.1 joerg fegetexceptflag(fexcept_t *flagp, int excepts)
153 1.1 joerg {
154 1.1 joerg uint32_t mxcsr;
155 1.1 joerg uint16_t x87_status;
156 1.1 joerg int ex;
157 1.1 joerg
158 1.1 joerg _DIAGASSERT(flagp != NULL);
159 1.1 joerg _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
160 1.1 joerg
161 1.1 joerg ex = excepts & FE_ALL_EXCEPT;
162 1.1 joerg
163 1.1 joerg /* Store the current x87 status register */
164 1.1 joerg __fnstsw(&x87_status);
165 1.1 joerg
166 1.1 joerg /* Store the MXCSR register */
167 1.1 joerg __stmxcsr(&mxcsr);
168 1.1 joerg
169 1.1 joerg /* Store the results in flagp */
170 1.1 joerg *flagp = (x87_status | mxcsr) & ex;
171 1.1 joerg
172 1.1 joerg /* Success */
173 1.1 joerg return (0);
174 1.1 joerg }
175 1.1 joerg
176 1.1 joerg /*
177 1.1 joerg * The feraiseexcept() function raises the supported floating-point exceptions
178 1.1 joerg * represented by the argument `excepts'.
179 1.1 joerg *
180 1.1 joerg * The standard explicitly allows us to execute an instruction that has the
181 1.1 joerg * exception as a side effect, but we choose to manipulate the status register
182 1.1 joerg * directly.
183 1.1 joerg *
184 1.1 joerg * The validation of input is being deferred to fesetexceptflag().
185 1.1 joerg */
186 1.1 joerg int
187 1.1 joerg feraiseexcept(int excepts)
188 1.1 joerg {
189 1.1 joerg int ex;
190 1.1 joerg
191 1.1 joerg _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
192 1.1 joerg
193 1.1 joerg ex = excepts & FE_ALL_EXCEPT;
194 1.5 christos fesetexceptflag((unsigned int *)&ex, ex);
195 1.4 riastrad __fwait();
196 1.1 joerg
197 1.1 joerg /* Success */
198 1.1 joerg return (0);
199 1.1 joerg }
200 1.1 joerg
201 1.1 joerg /*
202 1.1 joerg * This function sets the floating-point status flags indicated by the argument
203 1.1 joerg * `excepts' to the states stored in the object pointed to by `flagp'. It does
204 1.1 joerg * NOT raise any floating-point exceptions, but only sets the state of the flags.
205 1.1 joerg */
206 1.1 joerg int
207 1.1 joerg fesetexceptflag(const fexcept_t *flagp, int excepts)
208 1.1 joerg {
209 1.1 joerg fenv_t fenv;
210 1.1 joerg int ex;
211 1.1 joerg
212 1.1 joerg _DIAGASSERT(flagp != NULL);
213 1.1 joerg _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
214 1.1 joerg
215 1.1 joerg ex = excepts & FE_ALL_EXCEPT;
216 1.1 joerg
217 1.1 joerg /* Store the current x87 floating-point environment */
218 1.1 joerg __fnstenv(&fenv);
219 1.1 joerg
220 1.1 joerg /* Set the requested status flags */
221 1.1 joerg fenv.x87.status |= *flagp & ex;
222 1.1 joerg
223 1.1 joerg /* Load the x87 floating-point environent */
224 1.1 joerg __fldenv(fenv);
225 1.1 joerg
226 1.1 joerg /* Same for SSE environment */
227 1.1 joerg __stmxcsr(&fenv.mxcsr);
228 1.1 joerg fenv.mxcsr |= *flagp & ex;
229 1.1 joerg __ldmxcsr(fenv.mxcsr);
230 1.1 joerg
231 1.1 joerg /* Success */
232 1.1 joerg return (0);
233 1.1 joerg }
234 1.1 joerg
235 1.1 joerg /*
236 1.1 joerg * The fetestexcept() function determines which of a specified subset of the
237 1.1 joerg * floating-point exception flags are currently set. The `excepts' argument
238 1.1 joerg * specifies the floating-point status flags to be queried.
239 1.1 joerg */
240 1.1 joerg int
241 1.1 joerg fetestexcept(int excepts)
242 1.1 joerg {
243 1.1 joerg fenv_t fenv;
244 1.1 joerg uint32_t mxcsr;
245 1.1 joerg uint16_t status;
246 1.1 joerg int ex;
247 1.1 joerg
248 1.1 joerg _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
249 1.1 joerg
250 1.1 joerg ex = excepts & FE_ALL_EXCEPT;
251 1.1 joerg
252 1.1 joerg /* Store the current x87 floating-point environment */
253 1.1 joerg memset(&fenv, 0, sizeof(fenv));
254 1.1 joerg
255 1.1 joerg __fnstenv(&fenv);
256 1.1 joerg __fnstsw(&status);
257 1.1 joerg
258 1.1 joerg /* Store the MXCSR register state */
259 1.1 joerg __stmxcsr(&fenv.mxcsr);
260 1.1 joerg __stmxcsr(&mxcsr);
261 1.1 joerg
262 1.1 joerg return ((fenv.x87.status | fenv.mxcsr) & ex);
263 1.1 joerg }
264 1.1 joerg
265 1.1 joerg /*
266 1.1 joerg * The fegetround() function gets the current rounding direction.
267 1.1 joerg */
268 1.1 joerg int
269 1.1 joerg fegetround(void)
270 1.1 joerg {
271 1.1 joerg uint32_t mxcsr;
272 1.1 joerg uint16_t control;
273 1.1 joerg
274 1.1 joerg /*
275 1.1 joerg * We check both the x87 floating-point unit _and_ the SSE unit.
276 1.1 joerg * Normally, those two must agree with respect to each other. If they
277 1.1 joerg * don't, it's not our fault and the result is non-determinable, in
278 1.1 joerg * which case POSIX says that a negative value should be returned.
279 1.1 joerg */
280 1.1 joerg __fnstcw(&control);
281 1.1 joerg __stmxcsr(&mxcsr);
282 1.1 joerg
283 1.1 joerg if ((control & _X87_ROUNDING_MASK)
284 1.1 joerg != ((mxcsr & _SSE_ROUNDING_MASK) >> 3)) {
285 1.1 joerg return (-1);
286 1.1 joerg }
287 1.1 joerg
288 1.1 joerg return (control & _X87_ROUNDING_MASK);
289 1.1 joerg }
290 1.1 joerg
291 1.1 joerg /*
292 1.1 joerg * The fesetround() function establishes the rounding direction represented by
293 1.1 joerg * its argument `round'. If the argument is not equal to the value of a rounding
294 1.1 joerg * direction macro, the rounding direction is not changed.
295 1.1 joerg */
296 1.1 joerg int
297 1.1 joerg fesetround(int round)
298 1.1 joerg {
299 1.1 joerg uint32_t mxcsr;
300 1.1 joerg uint16_t control;
301 1.1 joerg
302 1.1 joerg /* Check whether requested rounding direction is supported */
303 1.1 joerg if (round & (~_X87_ROUNDING_MASK))
304 1.1 joerg return (-1);
305 1.1 joerg
306 1.1 joerg /* Store the current x87 control word register */
307 1.1 joerg __fnstcw(&control);
308 1.1 joerg
309 1.1 joerg /*
310 1.1 joerg * Set the rounding direction
311 1.1 joerg * Rounding Control is bits 10-11, so shift appropriately
312 1.1 joerg */
313 1.1 joerg control &= ~_X87_ROUNDING_MASK;
314 1.1 joerg control |= round;
315 1.1 joerg
316 1.1 joerg /* Load the x87 control word register */
317 1.1 joerg __fldcw(control);
318 1.1 joerg
319 1.1 joerg /*
320 1.1 joerg * Same for the SSE environment
321 1.1 joerg * Rounding Control is bits 13-14, so shift appropriately
322 1.1 joerg */
323 1.1 joerg __stmxcsr(&mxcsr);
324 1.1 joerg mxcsr &= ~_SSE_ROUNDING_MASK;
325 1.1 joerg mxcsr |= (round << _SSE_ROUND_SHIFT);
326 1.1 joerg __ldmxcsr(mxcsr);
327 1.1 joerg
328 1.1 joerg /* Success */
329 1.1 joerg return (0);
330 1.1 joerg }
331 1.1 joerg
332 1.1 joerg /*
333 1.1 joerg * The fegetenv() function attempts to store the current floating-point
334 1.1 joerg * environment in the object pointed to by envp.
335 1.1 joerg */
336 1.1 joerg int
337 1.1 joerg fegetenv(fenv_t *envp)
338 1.1 joerg {
339 1.1 joerg _DIAGASSERT(envp != NULL);
340 1.1 joerg
341 1.1 joerg /* Store the current x87 floating-point environment */
342 1.1 joerg __fnstenv(envp);
343 1.1 joerg
344 1.1 joerg /* Store the MXCSR register state */
345 1.1 joerg __stmxcsr(&envp->mxcsr);
346 1.1 joerg
347 1.1 joerg /*
348 1.1 joerg * When an FNSTENV instruction is executed, all pending exceptions are
349 1.1 joerg * essentially lost (either the x87 FPU status register is cleared or all
350 1.1 joerg * exceptions are masked).
351 1.1 joerg *
352 1.1 joerg * 8.6 X87 FPU EXCEPTION SYNCHRONIZATION -
353 1.1 joerg * Intel(R) 64 and IA-32 Architectures Softare Developer's Manual - Vol 1
354 1.1 joerg *
355 1.1 joerg */
356 1.1 joerg __fldcw(envp->x87.control);
357 1.1 joerg
358 1.1 joerg /* Success */
359 1.1 joerg return (0);
360 1.1 joerg }
361 1.1 joerg
362 1.1 joerg /*
363 1.1 joerg * The feholdexcept() function saves the current floating-point environment
364 1.1 joerg * in the object pointed to by envp, clears the floating-point status flags, and
365 1.1 joerg * then installs a non-stop (continue on floating-point exceptions) mode, if
366 1.1 joerg * available, for all floating-point exceptions.
367 1.1 joerg */
368 1.1 joerg int
369 1.1 joerg feholdexcept(fenv_t *envp)
370 1.1 joerg {
371 1.1 joerg uint32_t mxcsr;
372 1.1 joerg
373 1.1 joerg _DIAGASSERT(envp != NULL);
374 1.1 joerg
375 1.1 joerg /* Store the current x87 floating-point environment */
376 1.1 joerg __fnstenv(envp);
377 1.1 joerg
378 1.1 joerg /* Clear all exception flags in FPU */
379 1.1 joerg __fnclex();
380 1.1 joerg
381 1.1 joerg /* Store the MXCSR register state */
382 1.1 joerg __stmxcsr(&envp->mxcsr);
383 1.1 joerg
384 1.1 joerg /* Clear exception flags in MXCSR XXX */
385 1.1 joerg mxcsr = envp->mxcsr;
386 1.1 joerg mxcsr &= ~FE_ALL_EXCEPT;
387 1.1 joerg
388 1.1 joerg /* Mask all exceptions */
389 1.1 joerg mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
390 1.1 joerg
391 1.1 joerg __ldmxcsr(mxcsr);
392 1.1 joerg
393 1.1 joerg /* Success */
394 1.1 joerg return (0);
395 1.1 joerg }
396 1.1 joerg
397 1.1 joerg /*
398 1.1 joerg * The fesetenv() function attempts to establish the floating-point environment
399 1.1 joerg * represented by the object pointed to by envp. The argument `envp' points
400 1.1 joerg * to an object set by a call to fegetenv() or feholdexcept(), or equal a
401 1.1 joerg * floating-point environment macro. The fesetenv() function does not raise
402 1.1 joerg * floating-point exceptions, but only installs the state of the floating-point
403 1.1 joerg * status flags represented through its argument.
404 1.1 joerg */
405 1.1 joerg int
406 1.1 joerg fesetenv(const fenv_t *envp)
407 1.1 joerg {
408 1.1 joerg fenv_t fenv;
409 1.1 joerg
410 1.1 joerg _DIAGASSERT(envp != NULL);
411 1.1 joerg
412 1.1 joerg /* Store the x87 floating-point environment */
413 1.1 joerg memset(&fenv, 0, sizeof fenv);
414 1.1 joerg __fnstenv(&fenv);
415 1.1 joerg
416 1.1 joerg __fe_dfl_env.x87.control = (fenv.x87.control & 0xffff0000)
417 1.1 joerg | (__fe_dfl_env.x87.control & 0x0000ffff);
418 1.1 joerg __fe_dfl_env.x87.status = (fenv.x87.status & 0xffff0000)
419 1.1 joerg | (__fe_dfl_env.x87.status & 0x0000ffff);
420 1.1 joerg __fe_dfl_env.x87.tag = (fenv.x87.tag & 0xffff0000)
421 1.1 joerg | (__fe_dfl_env.x87.tag & 0x0000ffff);
422 1.1 joerg __fe_dfl_env.x87.others[3] = (fenv.x87.others[3] & 0xffff0000)
423 1.1 joerg | (__fe_dfl_env.x87.others[3] & 0x0000ffff);
424 1.1 joerg __fldenv(*envp);
425 1.1 joerg
426 1.1 joerg /* Store the MXCSR register */
427 1.1 joerg __ldmxcsr(envp->mxcsr);
428 1.1 joerg
429 1.1 joerg /* Success */
430 1.1 joerg return (0);
431 1.1 joerg }
432 1.1 joerg
433 1.1 joerg /*
434 1.1 joerg * The feupdateenv() function saves the currently raised floating-point
435 1.1 joerg * exceptions in its automatic storage, installs the floating-point environment
436 1.1 joerg * represented by the object pointed to by `envp', and then raises the saved
437 1.1 joerg * floating-point exceptions. The argument `envp' shall point to an object set
438 1.1 joerg * by a call to feholdexcept() or fegetenv(), or equal a floating-point
439 1.1 joerg * environment macro.
440 1.1 joerg */
441 1.1 joerg int
442 1.1 joerg feupdateenv(const fenv_t *envp)
443 1.1 joerg {
444 1.1 joerg fenv_t fenv;
445 1.1 joerg uint32_t mxcsr;
446 1.1 joerg uint16_t sw;
447 1.1 joerg
448 1.1 joerg _DIAGASSERT(envp != NULL);
449 1.1 joerg
450 1.1 joerg /* Store the x87 floating-point environment */
451 1.1 joerg memset(&fenv, 0, sizeof(fenv));
452 1.1 joerg __fnstenv(&fenv);
453 1.1 joerg
454 1.1 joerg __fe_dfl_env.x87.control = (fenv.x87.control & 0xffff0000)
455 1.1 joerg | (__fe_dfl_env.x87.control & 0x0000ffff);
456 1.1 joerg __fe_dfl_env.x87.status = (fenv.x87.status & 0xffff0000)
457 1.1 joerg | (__fe_dfl_env.x87.status & 0x0000ffff);
458 1.1 joerg __fe_dfl_env.x87.tag = (fenv.x87.tag & 0xffff0000)
459 1.1 joerg | (__fe_dfl_env.x87.tag & 0x0000ffff);
460 1.1 joerg __fe_dfl_env.x87.others[3] = (fenv.x87.others[3] & 0xffff0000)
461 1.1 joerg | (__fe_dfl_env.x87.others[3] & 0x0000ffff);
462 1.1 joerg
463 1.1 joerg /* Store the x87 status register */
464 1.1 joerg __fnstsw(&sw);
465 1.1 joerg
466 1.1 joerg /* Store the MXCSR register */
467 1.1 joerg __stmxcsr(&mxcsr);
468 1.1 joerg
469 1.1 joerg /* Install new floating-point environment */
470 1.1 joerg fesetenv(envp);
471 1.1 joerg
472 1.1 joerg /* Raise any previously accumulated exceptions */
473 1.1 joerg feraiseexcept((sw | mxcsr) & FE_ALL_EXCEPT);
474 1.1 joerg
475 1.1 joerg /* Success */
476 1.1 joerg return (0);
477 1.1 joerg }
478 1.1 joerg
479 1.1 joerg /*
480 1.1 joerg * The following functions are extentions to the standard
481 1.1 joerg */
482 1.1 joerg int
483 1.1 joerg feenableexcept(int mask)
484 1.1 joerg {
485 1.1 joerg uint32_t mxcsr, omask;
486 1.1 joerg uint16_t control;
487 1.1 joerg
488 1.1 joerg _DIAGASSERT((mask & ~FE_ALL_EXCEPT) == 0);
489 1.1 joerg mask &= FE_ALL_EXCEPT;
490 1.1 joerg
491 1.1 joerg __fnstcw(&control);
492 1.1 joerg __stmxcsr(&mxcsr);
493 1.1 joerg
494 1.1 joerg omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
495 1.1 joerg control &= ~mask;
496 1.1 joerg __fldcw(control);
497 1.1 joerg
498 1.1 joerg mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
499 1.1 joerg __ldmxcsr(mxcsr);
500 1.1 joerg
501 1.2 riastrad return (FE_ALL_EXCEPT & ~omask);
502 1.1 joerg
503 1.1 joerg }
504 1.1 joerg
505 1.1 joerg int
506 1.1 joerg fedisableexcept(int mask)
507 1.1 joerg {
508 1.1 joerg uint32_t mxcsr, omask;
509 1.1 joerg uint16_t control;
510 1.1 joerg
511 1.1 joerg _DIAGASSERT((mask & ~FE_ALL_EXCEPT) == 0);
512 1.1 joerg
513 1.1 joerg __fnstcw(&control);
514 1.1 joerg __stmxcsr(&mxcsr);
515 1.1 joerg
516 1.1 joerg omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
517 1.1 joerg control |= mask;
518 1.1 joerg __fldcw(control);
519 1.1 joerg
520 1.1 joerg mxcsr |= mask << _SSE_EMASK_SHIFT;
521 1.1 joerg __ldmxcsr(mxcsr);
522 1.1 joerg
523 1.2 riastrad return (FE_ALL_EXCEPT & ~omask);
524 1.1 joerg }
525 1.1 joerg
526 1.1 joerg int
527 1.1 joerg fegetexcept(void)
528 1.1 joerg {
529 1.1 joerg uint16_t control;
530 1.1 joerg
531 1.1 joerg /*
532 1.1 joerg * We assume that the masks for the x87 and the SSE unit are
533 1.1 joerg * the same.
534 1.1 joerg */
535 1.1 joerg __fnstcw(&control);
536 1.1 joerg
537 1.3 riastrad return (~control & FE_ALL_EXCEPT);
538 1.1 joerg }
539 1.1 joerg
540