1 /* $NetBSD: fenv.c,v 1.1 2010/07/31 21:47:53 joerg Exp $ */
4 * Copyright (c) 2004-2005 David Schultz <das (at) 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/cdefs.h>
30 __RCSID("$NetBSD: fenv.c,v 1.1 2010/07/31 21:47:53 joerg Exp $");
37 /* Load x87 Control Word */
38 #define __fldcw(__cw) __asm__ __volatile__ \
39 ("fldcw %0" : : "m" (__cw))
41 /* No-Wait Store Control Word */
42 #define __fnstcw(__cw) __asm__ __volatile__ \
43 ("fnstcw %0" : "=m" (*(__cw)))
45 /* No-Wait Store Status Word */
46 #define __fnstsw(__sw) __asm__ __volatile__ \
47 ("fnstsw %0" : "=am" (*(__sw)))
49 /* No-Wait Clear Exception Flags */
50 #define __fnclex() __asm__ __volatile__ \
53 /* Load x87 Environment */
54 #define __fldenv(__env) __asm__ __volatile__ \
55 ("fldenv %0" : : "m" (__env))
57 /* No-Wait Store x87 environment */
58 #define __fnstenv(__env) __asm__ __volatile__ \
59 ("fnstenv %0" : "=m" (*(__env)))
61 /* Load the MXCSR register */
62 #define __ldmxcsr(__mxcsr) __asm__ __volatile__ \
63 ("ldmxcsr %0" : : "m" (__mxcsr))
65 /* Store the MXCSR register state */
66 #define __stmxcsr(__mxcsr) __asm__ __volatile__ \
67 ("stmxcsr %0" : "=m" (*(__mxcsr)))
70 * The following constant represents the default floating-point environment
71 * (that is, the one installed at program startup) and has type pointer to
72 * const-qualified fenv_t.
74 * It can be used as an argument to the functions within the <fenv.h> header
75 * that manage the floating-point environment, namely fesetenv() and
78 * x87 fpu registers are 16bit wide. The upper bits, 31-16, are marked as
79 * RESERVED. We provide a partial floating-point environment, where we
80 * define only the lower bits. The reserved bits are extracted and set by
81 * the consumers of FE_DFL_ENV, during runtime.
83 fenv_t __fe_dfl_env
= {
85 __NetBSD_NPXCW__
, /* Control word register */
86 0x00000000, /* Status word register */
87 0x0000ffff, /* Tag word register */
95 __INITIAL_MXCSR__
/* MXCSR register */
97 #define FE_DFL_ENV ((const fenv_t *) &__fe_dfl_env)
101 * The feclearexcept() function clears the supported floating-point exceptions
102 * represented by `excepts'.
105 feclearexcept(int excepts
)
110 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
112 ex
= excepts
& FE_ALL_EXCEPT
;
114 /* Store the current x87 floating-point environment */
117 /* Clear the requested floating-point exceptions */
118 fenv
.x87
.status
&= ~ex
;
120 /* Load the x87 floating-point environent */
123 /* Same for SSE environment */
124 __stmxcsr(&fenv
.mxcsr
);
126 __ldmxcsr(fenv
.mxcsr
);
133 * The fegetexceptflag() function stores an implementation-defined
134 * representation of the states of the floating-point status flags indicated by
135 * the argument excepts in the object pointed to by the argument flagp.
138 fegetexceptflag(fexcept_t
*flagp
, int excepts
)
144 _DIAGASSERT(flagp
!= NULL
);
145 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
147 ex
= excepts
& FE_ALL_EXCEPT
;
149 /* Store the current x87 status register */
150 __fnstsw(&x87_status
);
152 /* Store the MXCSR register */
155 /* Store the results in flagp */
156 *flagp
= (x87_status
| mxcsr
) & ex
;
163 * The feraiseexcept() function raises the supported floating-point exceptions
164 * represented by the argument `excepts'.
166 * The standard explicitly allows us to execute an instruction that has the
167 * exception as a side effect, but we choose to manipulate the status register
170 * The validation of input is being deferred to fesetexceptflag().
173 feraiseexcept(int excepts
)
177 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
179 ex
= excepts
& FE_ALL_EXCEPT
;
180 fesetexceptflag((unsigned int *)&excepts
, excepts
);
187 * This function sets the floating-point status flags indicated by the argument
188 * `excepts' to the states stored in the object pointed to by `flagp'. It does
189 * NOT raise any floating-point exceptions, but only sets the state of the flags.
192 fesetexceptflag(const fexcept_t
*flagp
, int excepts
)
197 _DIAGASSERT(flagp
!= NULL
);
198 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
200 ex
= excepts
& FE_ALL_EXCEPT
;
202 /* Store the current x87 floating-point environment */
205 /* Set the requested status flags */
206 fenv
.x87
.status
|= *flagp
& ex
;
208 /* Load the x87 floating-point environent */
211 /* Same for SSE environment */
212 __stmxcsr(&fenv
.mxcsr
);
213 fenv
.mxcsr
|= *flagp
& ex
;
214 __ldmxcsr(fenv
.mxcsr
);
221 * The fetestexcept() function determines which of a specified subset of the
222 * floating-point exception flags are currently set. The `excepts' argument
223 * specifies the floating-point status flags to be queried.
226 fetestexcept(int excepts
)
233 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
235 ex
= excepts
& FE_ALL_EXCEPT
;
237 /* Store the current x87 floating-point environment */
238 memset(&fenv
, 0, sizeof(fenv
));
243 /* Store the MXCSR register state */
244 __stmxcsr(&fenv
.mxcsr
);
247 return ((fenv
.x87
.status
| fenv
.mxcsr
) & ex
);
251 * The fegetround() function gets the current rounding direction.
260 * We check both the x87 floating-point unit _and_ the SSE unit.
261 * Normally, those two must agree with respect to each other. If they
262 * don't, it's not our fault and the result is non-determinable, in
263 * which case POSIX says that a negative value should be returned.
268 if ((control
& _X87_ROUNDING_MASK
)
269 != ((mxcsr
& _SSE_ROUNDING_MASK
) >> 3)) {
273 return (control
& _X87_ROUNDING_MASK
);
277 * The fesetround() function establishes the rounding direction represented by
278 * its argument `round'. If the argument is not equal to the value of a rounding
279 * direction macro, the rounding direction is not changed.
282 fesetround(int round
)
287 /* Check whether requested rounding direction is supported */
288 if (round
& (~_X87_ROUNDING_MASK
))
291 /* Store the current x87 control word register */
295 * Set the rounding direction
296 * Rounding Control is bits 10-11, so shift appropriately
298 control
&= ~_X87_ROUNDING_MASK
;
301 /* Load the x87 control word register */
305 * Same for the SSE environment
306 * Rounding Control is bits 13-14, so shift appropriately
309 mxcsr
&= ~_SSE_ROUNDING_MASK
;
310 mxcsr
|= (round
<< _SSE_ROUND_SHIFT
);
318 * The fegetenv() function attempts to store the current floating-point
319 * environment in the object pointed to by envp.
322 fegetenv(fenv_t
*envp
)
324 _DIAGASSERT(envp
!= NULL
);
326 /* Store the current x87 floating-point environment */
329 /* Store the MXCSR register state */
330 __stmxcsr(&envp
->mxcsr
);
333 * When an FNSTENV instruction is executed, all pending exceptions are
334 * essentially lost (either the x87 FPU status register is cleared or all
335 * exceptions are masked).
337 * 8.6 X87 FPU EXCEPTION SYNCHRONIZATION -
338 * Intel(R) 64 and IA-32 Architectures Softare Developer's Manual - Vol 1
341 __fldcw(envp
->x87
.control
);
348 * The feholdexcept() function saves the current floating-point environment
349 * in the object pointed to by envp, clears the floating-point status flags, and
350 * then installs a non-stop (continue on floating-point exceptions) mode, if
351 * available, for all floating-point exceptions.
354 feholdexcept(fenv_t
*envp
)
358 _DIAGASSERT(envp
!= NULL
);
360 /* Store the current x87 floating-point environment */
363 /* Clear all exception flags in FPU */
366 /* Store the MXCSR register state */
367 __stmxcsr(&envp
->mxcsr
);
369 /* Clear exception flags in MXCSR XXX */
371 mxcsr
&= ~FE_ALL_EXCEPT
;
373 /* Mask all exceptions */
374 mxcsr
|= FE_ALL_EXCEPT
<< _SSE_EMASK_SHIFT
;
383 * The fesetenv() function attempts to establish the floating-point environment
384 * represented by the object pointed to by envp. The argument `envp' points
385 * to an object set by a call to fegetenv() or feholdexcept(), or equal a
386 * floating-point environment macro. The fesetenv() function does not raise
387 * floating-point exceptions, but only installs the state of the floating-point
388 * status flags represented through its argument.
391 fesetenv(const fenv_t
*envp
)
395 _DIAGASSERT(envp
!= NULL
);
397 /* Store the x87 floating-point environment */
398 memset(&fenv
, 0, sizeof fenv
);
401 __fe_dfl_env
.x87
.control
= (fenv
.x87
.control
& 0xffff0000)
402 | (__fe_dfl_env
.x87
.control
& 0x0000ffff);
403 __fe_dfl_env
.x87
.status
= (fenv
.x87
.status
& 0xffff0000)
404 | (__fe_dfl_env
.x87
.status
& 0x0000ffff);
405 __fe_dfl_env
.x87
.tag
= (fenv
.x87
.tag
& 0xffff0000)
406 | (__fe_dfl_env
.x87
.tag
& 0x0000ffff);
407 __fe_dfl_env
.x87
.others
[3] = (fenv
.x87
.others
[3] & 0xffff0000)
408 | (__fe_dfl_env
.x87
.others
[3] & 0x0000ffff);
411 /* Store the MXCSR register */
412 __ldmxcsr(envp
->mxcsr
);
419 * The feupdateenv() function saves the currently raised floating-point
420 * exceptions in its automatic storage, installs the floating-point environment
421 * represented by the object pointed to by `envp', and then raises the saved
422 * floating-point exceptions. The argument `envp' shall point to an object set
423 * by a call to feholdexcept() or fegetenv(), or equal a floating-point
427 feupdateenv(const fenv_t
*envp
)
433 _DIAGASSERT(envp
!= NULL
);
435 /* Store the x87 floating-point environment */
436 memset(&fenv
, 0, sizeof(fenv
));
439 __fe_dfl_env
.x87
.control
= (fenv
.x87
.control
& 0xffff0000)
440 | (__fe_dfl_env
.x87
.control
& 0x0000ffff);
441 __fe_dfl_env
.x87
.status
= (fenv
.x87
.status
& 0xffff0000)
442 | (__fe_dfl_env
.x87
.status
& 0x0000ffff);
443 __fe_dfl_env
.x87
.tag
= (fenv
.x87
.tag
& 0xffff0000)
444 | (__fe_dfl_env
.x87
.tag
& 0x0000ffff);
445 __fe_dfl_env
.x87
.others
[3] = (fenv
.x87
.others
[3] & 0xffff0000)
446 | (__fe_dfl_env
.x87
.others
[3] & 0x0000ffff);
448 /* Store the x87 status register */
451 /* Store the MXCSR register */
454 /* Install new floating-point environment */
457 /* Raise any previously accumulated exceptions */
458 feraiseexcept((sw
| mxcsr
) & FE_ALL_EXCEPT
);
465 * The following functions are extentions to the standard
468 feenableexcept(int mask
)
470 uint32_t mxcsr
, omask
;
473 _DIAGASSERT((mask
& ~FE_ALL_EXCEPT
) == 0);
474 mask
&= FE_ALL_EXCEPT
;
479 omask
= (control
| mxcsr
>> _SSE_EMASK_SHIFT
) & FE_ALL_EXCEPT
;
483 mxcsr
&= ~(mask
<< _SSE_EMASK_SHIFT
);
491 fedisableexcept(int mask
)
493 uint32_t mxcsr
, omask
;
496 _DIAGASSERT((mask
& ~FE_ALL_EXCEPT
) == 0);
501 omask
= (control
| mxcsr
>> _SSE_EMASK_SHIFT
) & FE_ALL_EXCEPT
;
505 mxcsr
|= mask
<< _SSE_EMASK_SHIFT
;
517 * We assume that the masks for the x87 and the SSE unit are
522 return (control
& FE_ALL_EXCEPT
);