1 /* $NetBSD: fenv.c,v 1.6 2013/11/11 00:31:51 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.6 2013/11/11 00:31:51 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 /* Check for and handle pending unmasked x87 pending FPU exceptions */
62 #define __fwait(__env) __asm__ __volatile__ \
65 /* Load the MXCSR register */
66 #define __ldmxcsr(__mxcsr) __asm__ __volatile__ \
67 ("ldmxcsr %0" : : "m" (__mxcsr))
69 /* Store the MXCSR register state */
70 #define __stmxcsr(__mxcsr) __asm__ __volatile__ \
71 ("stmxcsr %0" : "=m" (*(__mxcsr)))
74 * The following constant represents the default floating-point environment
75 * (that is, the one installed at program startup) and has type pointer to
76 * const-qualified fenv_t.
78 * It can be used as an argument to the functions within the <fenv.h> header
79 * that manage the floating-point environment, namely fesetenv() and
82 * x87 fpu registers are 16bit wide. The upper bits, 31-16, are marked as
83 * RESERVED. We provide a partial floating-point environment, where we
84 * define only the lower bits. The reserved bits are extracted and set by
85 * the consumers of FE_DFL_ENV, during runtime.
87 fenv_t __fe_dfl_env
= {
89 __NetBSD_NPXCW__
, /* Control word register */
90 0x00000000, /* Status word register */
91 0x0000ffff, /* Tag word register */
99 __INITIAL_MXCSR__
/* MXCSR register */
101 #define FE_DFL_ENV ((const fenv_t *) &__fe_dfl_env)
103 static void __init_libm(void) __attribute__ ((constructor
, used
));
105 static void __init_libm(void)
110 __fe_dfl_env
.x87
.control
= control
;
115 * The feclearexcept() function clears the supported floating-point exceptions
116 * represented by `excepts'.
119 feclearexcept(int excepts
)
124 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
126 ex
= excepts
& FE_ALL_EXCEPT
;
128 /* Store the current x87 floating-point environment */
131 /* Clear the requested floating-point exceptions */
132 fenv
.x87
.status
&= ~ex
;
134 /* Load the x87 floating-point environent */
137 /* Same for SSE environment */
138 __stmxcsr(&fenv
.mxcsr
);
140 __ldmxcsr(fenv
.mxcsr
);
147 * The fegetexceptflag() function stores an implementation-defined
148 * representation of the states of the floating-point status flags indicated by
149 * the argument excepts in the object pointed to by the argument flagp.
152 fegetexceptflag(fexcept_t
*flagp
, int excepts
)
158 _DIAGASSERT(flagp
!= NULL
);
159 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
161 ex
= excepts
& FE_ALL_EXCEPT
;
163 /* Store the current x87 status register */
164 __fnstsw(&x87_status
);
166 /* Store the MXCSR register */
169 /* Store the results in flagp */
170 *flagp
= (x87_status
| mxcsr
) & ex
;
177 * The feraiseexcept() function raises the supported floating-point exceptions
178 * represented by the argument `excepts'.
180 * The standard explicitly allows us to execute an instruction that has the
181 * exception as a side effect, but we choose to manipulate the status register
184 * The validation of input is being deferred to fesetexceptflag().
187 feraiseexcept(int excepts
)
191 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
193 ex
= excepts
& FE_ALL_EXCEPT
;
194 fesetexceptflag((unsigned int *)&ex
, ex
);
202 * This function sets the floating-point status flags indicated by the argument
203 * `excepts' to the states stored in the object pointed to by `flagp'. It does
204 * NOT raise any floating-point exceptions, but only sets the state of the flags.
207 fesetexceptflag(const fexcept_t
*flagp
, int excepts
)
212 _DIAGASSERT(flagp
!= NULL
);
213 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
215 ex
= excepts
& FE_ALL_EXCEPT
;
217 /* Store the current x87 floating-point environment */
220 /* Set the requested status flags */
221 fenv
.x87
.status
|= *flagp
& ex
;
223 /* Load the x87 floating-point environent */
226 /* Same for SSE environment */
227 __stmxcsr(&fenv
.mxcsr
);
228 fenv
.mxcsr
|= *flagp
& ex
;
229 __ldmxcsr(fenv
.mxcsr
);
236 * The fetestexcept() function determines which of a specified subset of the
237 * floating-point exception flags are currently set. The `excepts' argument
238 * specifies the floating-point status flags to be queried.
241 fetestexcept(int excepts
)
248 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
250 ex
= excepts
& FE_ALL_EXCEPT
;
252 /* Store the current x87 floating-point environment */
253 memset(&fenv
, 0, sizeof(fenv
));
258 /* Store the MXCSR register state */
259 __stmxcsr(&fenv
.mxcsr
);
262 return ((fenv
.x87
.status
| fenv
.mxcsr
) & ex
);
266 * The fegetround() function gets the current rounding direction.
275 * We check both the x87 floating-point unit _and_ the SSE unit.
276 * Normally, those two must agree with respect to each other. If they
277 * don't, it's not our fault and the result is non-determinable, in
278 * which case POSIX says that a negative value should be returned.
283 if ((control
& _X87_ROUNDING_MASK
)
284 != ((mxcsr
& _SSE_ROUNDING_MASK
) >> 3)) {
288 return (control
& _X87_ROUNDING_MASK
);
292 * The fesetround() function establishes the rounding direction represented by
293 * its argument `round'. If the argument is not equal to the value of a rounding
294 * direction macro, the rounding direction is not changed.
297 fesetround(int round
)
302 /* Check whether requested rounding direction is supported */
303 if (round
& (~_X87_ROUNDING_MASK
))
306 /* Store the current x87 control word register */
310 * Set the rounding direction
311 * Rounding Control is bits 10-11, so shift appropriately
313 control
&= ~_X87_ROUNDING_MASK
;
316 /* Load the x87 control word register */
320 * Same for the SSE environment
321 * Rounding Control is bits 13-14, so shift appropriately
324 mxcsr
&= ~_SSE_ROUNDING_MASK
;
325 mxcsr
|= (round
<< _SSE_ROUND_SHIFT
);
333 * The fegetenv() function attempts to store the current floating-point
334 * environment in the object pointed to by envp.
337 fegetenv(fenv_t
*envp
)
339 _DIAGASSERT(envp
!= NULL
);
341 /* Store the current x87 floating-point environment */
344 /* Store the MXCSR register state */
345 __stmxcsr(&envp
->mxcsr
);
348 * When an FNSTENV instruction is executed, all pending exceptions are
349 * essentially lost (either the x87 FPU status register is cleared or all
350 * exceptions are masked).
352 * 8.6 X87 FPU EXCEPTION SYNCHRONIZATION -
353 * Intel(R) 64 and IA-32 Architectures Softare Developer's Manual - Vol 1
356 __fldcw(envp
->x87
.control
);
363 * The feholdexcept() function saves the current floating-point environment
364 * in the object pointed to by envp, clears the floating-point status flags, and
365 * then installs a non-stop (continue on floating-point exceptions) mode, if
366 * available, for all floating-point exceptions.
369 feholdexcept(fenv_t
*envp
)
373 _DIAGASSERT(envp
!= NULL
);
375 /* Store the current x87 floating-point environment */
378 /* Clear all exception flags in FPU */
381 /* Store the MXCSR register state */
382 __stmxcsr(&envp
->mxcsr
);
384 /* Clear exception flags in MXCSR XXX */
386 mxcsr
&= ~FE_ALL_EXCEPT
;
388 /* Mask all exceptions */
389 mxcsr
|= FE_ALL_EXCEPT
<< _SSE_EMASK_SHIFT
;
398 * The fesetenv() function attempts to establish the floating-point environment
399 * represented by the object pointed to by envp. The argument `envp' points
400 * to an object set by a call to fegetenv() or feholdexcept(), or equal a
401 * floating-point environment macro. The fesetenv() function does not raise
402 * floating-point exceptions, but only installs the state of the floating-point
403 * status flags represented through its argument.
406 fesetenv(const fenv_t
*envp
)
410 _DIAGASSERT(envp
!= NULL
);
412 /* Store the x87 floating-point environment */
413 memset(&fenv
, 0, sizeof fenv
);
416 __fe_dfl_env
.x87
.control
= (fenv
.x87
.control
& 0xffff0000)
417 | (__fe_dfl_env
.x87
.control
& 0x0000ffff);
418 __fe_dfl_env
.x87
.status
= (fenv
.x87
.status
& 0xffff0000)
419 | (__fe_dfl_env
.x87
.status
& 0x0000ffff);
420 __fe_dfl_env
.x87
.tag
= (fenv
.x87
.tag
& 0xffff0000)
421 | (__fe_dfl_env
.x87
.tag
& 0x0000ffff);
422 __fe_dfl_env
.x87
.others
[3] = (fenv
.x87
.others
[3] & 0xffff0000)
423 | (__fe_dfl_env
.x87
.others
[3] & 0x0000ffff);
426 /* Store the MXCSR register */
427 __ldmxcsr(envp
->mxcsr
);
434 * The feupdateenv() function saves the currently raised floating-point
435 * exceptions in its automatic storage, installs the floating-point environment
436 * represented by the object pointed to by `envp', and then raises the saved
437 * floating-point exceptions. The argument `envp' shall point to an object set
438 * by a call to feholdexcept() or fegetenv(), or equal a floating-point
442 feupdateenv(const fenv_t
*envp
)
448 _DIAGASSERT(envp
!= NULL
);
450 /* Store the x87 floating-point environment */
451 memset(&fenv
, 0, sizeof(fenv
));
454 __fe_dfl_env
.x87
.control
= (fenv
.x87
.control
& 0xffff0000)
455 | (__fe_dfl_env
.x87
.control
& 0x0000ffff);
456 __fe_dfl_env
.x87
.status
= (fenv
.x87
.status
& 0xffff0000)
457 | (__fe_dfl_env
.x87
.status
& 0x0000ffff);
458 __fe_dfl_env
.x87
.tag
= (fenv
.x87
.tag
& 0xffff0000)
459 | (__fe_dfl_env
.x87
.tag
& 0x0000ffff);
460 __fe_dfl_env
.x87
.others
[3] = (fenv
.x87
.others
[3] & 0xffff0000)
461 | (__fe_dfl_env
.x87
.others
[3] & 0x0000ffff);
463 /* Store the x87 status register */
466 /* Store the MXCSR register */
469 /* Install new floating-point environment */
472 /* Raise any previously accumulated exceptions */
473 feraiseexcept((sw
| mxcsr
) & FE_ALL_EXCEPT
);
480 * The following functions are extentions to the standard
483 feenableexcept(int mask
)
485 uint32_t mxcsr
, omask
;
488 _DIAGASSERT((mask
& ~FE_ALL_EXCEPT
) == 0);
489 mask
&= FE_ALL_EXCEPT
;
494 omask
= (control
| mxcsr
>> _SSE_EMASK_SHIFT
) & FE_ALL_EXCEPT
;
498 mxcsr
&= ~(mask
<< _SSE_EMASK_SHIFT
);
501 return (FE_ALL_EXCEPT
& ~omask
);
506 fedisableexcept(int mask
)
508 uint32_t mxcsr
, omask
;
511 _DIAGASSERT((mask
& ~FE_ALL_EXCEPT
) == 0);
516 omask
= (control
| mxcsr
>> _SSE_EMASK_SHIFT
) & FE_ALL_EXCEPT
;
520 mxcsr
|= mask
<< _SSE_EMASK_SHIFT
;
523 return (FE_ALL_EXCEPT
& ~omask
);
532 * We assume that the masks for the x87 and the SSE unit are
537 return (~control
& FE_ALL_EXCEPT
);