1 /* $NetBSD: fenv.c,v 1.6 2013/11/11 00:31:51 joerg Exp $ */
4 * Copyright (c) 2004-2005 David Schultz <das@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 $");
32 #include <sys/param.h>
33 #include <sys/sysctl.h>
39 /* Load x87 Control Word */
40 #define __fldcw(__cw) __asm__ __volatile__ \
41 ("fldcw %0" : : "m" (__cw))
43 /* No-Wait Store Control Word */
44 #define __fnstcw(__cw) __asm__ __volatile__ \
45 ("fnstcw %0" : "=m" (*(__cw)))
47 /* No-Wait Store Status Word */
48 #define __fnstsw(__sw) __asm__ __volatile__ \
49 ("fnstsw %0" : "=am" (*(__sw)))
51 /* No-Wait Clear Exception Flags */
52 #define __fnclex() __asm__ __volatile__ \
55 /* Load x87 Environment */
56 #define __fldenv(__env) __asm__ __volatile__ \
57 ("fldenv %0" : : "m" (__env))
59 /* No-Wait Store x87 environment */
60 #define __fnstenv(__env) __asm__ __volatile__ \
61 ("fnstenv %0" : "=m" (*(__env)))
63 /* Check for and handle pending unmasked x87 pending FPU exceptions */
64 #define __fwait(__env) __asm__ __volatile__ \
67 /* Load the MXCSR register */
68 #define __ldmxcsr(__mxcsr) __asm__ __volatile__ \
69 ("ldmxcsr %0" : : "m" (__mxcsr))
71 /* Store the MXCSR register state */
72 #define __stmxcsr(__mxcsr) __asm__ __volatile__ \
73 ("stmxcsr %0" : "=m" (*(__mxcsr)))
76 * The following constant represents the default floating-point environment
77 * (that is, the one installed at program startup) and has type pointer to
78 * const-qualified fenv_t.
80 * It can be used as an argument to the functions within the <fenv.h> header
81 * that manage the floating-point environment, namely fesetenv() and
84 * x87 fpu registers are 16bit wide. The upper bits, 31-16, are marked as
85 * RESERVED. We provide a partial floating-point environment, where we
86 * define only the lower bits. The reserved bits are extracted and set by the
87 * consumers of FE_DFL_ENV, during runtime.
89 fenv_t __fe_dfl_env
= {
91 __NetBSD_NPXCW__
, /* Control word register */
93 0x0000, /* Status word register */
95 0x0000ffff, /* Tag word register */
102 __INITIAL_MXCSR__
/* MXCSR register */
106 * Test for SSE support on this processor.
108 * We need to use ldmxcsr/stmxcsr to get correct results if any part
109 * of the program was compiled to use SSE floating-point, but we can't
110 * use SSE on older processors.
112 * In order to do so, we need to query the processor capabilities via the CPUID
113 * instruction. We can make it even simpler though, by querying the machdep.sse
116 static int __HAS_SSE
= 0;
118 static void __init_libm(void) __attribute__ ((constructor
, used
));
120 static void __init_libm(void)
122 #if !defined(__minix)
123 size_t oldlen
= sizeof(__HAS_SSE
);
127 rv
= sysctlbyname("machdep.sse", &__HAS_SSE
, &oldlen
, NULL
, 0);
133 #endif /* !defined(__minix) */
136 __fe_dfl_env
.x87
.control
= control
;
140 * The feclearexcept() function clears the supported floating-point exceptions
141 * represented by `excepts'.
144 feclearexcept(int excepts
)
150 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
152 ex
= excepts
& FE_ALL_EXCEPT
;
154 /* It's ~3x faster to call fnclex, than store/load fp env */
155 if (ex
== FE_ALL_EXCEPT
) {
159 env
.x87
.status
&= ~ex
;
174 * The fegetexceptflag() function stores an implementation-defined
175 * representation of the states of the floating-point status flags indicated by
176 * the argument excepts in the object pointed to by the argument flagp.
179 fegetexceptflag(fexcept_t
*flagp
, int excepts
)
185 _DIAGASSERT(flagp
!= NULL
);
186 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
188 ex
= excepts
& FE_ALL_EXCEPT
;
196 *flagp
= (mxcsr
| status
) & ex
;
203 * The feraiseexcept() function raises the supported floating-point exceptions
204 * represented by the argument `excepts'.
206 * The standard explicitly allows us to execute an instruction that has the
207 * exception as a side effect, but we choose to manipulate the status register
210 * The validation of input is being deferred to fesetexceptflag().
213 feraiseexcept(int excepts
)
217 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
219 ex
= excepts
& FE_ALL_EXCEPT
;
220 fesetexceptflag(&ex
, excepts
);
228 * This function sets the floating-point status flags indicated by the argument
229 * `excepts' to the states stored in the object pointed to by `flagp'. It does
230 * NOT raise any floating-point exceptions, but only sets the state of the flags.
233 fesetexceptflag(const fexcept_t
*flagp
, int excepts
)
239 _DIAGASSERT(flagp
!= NULL
);
240 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
242 ex
= excepts
& FE_ALL_EXCEPT
;
245 env
.x87
.status
&= ~ex
;
246 env
.x87
.status
|= *flagp
& ex
;
252 mxcsr
|= *flagp
& ex
;
261 * The fetestexcept() function determines which of a specified subset of the
262 * floating-point exception flags are currently set. The `excepts' argument
263 * specifies the floating-point status flags to be queried.
266 fetestexcept(int excepts
)
272 _DIAGASSERT((excepts
& ~FE_ALL_EXCEPT
) == 0);
274 ex
= excepts
& FE_ALL_EXCEPT
;
282 return ((status
| mxcsr
) & ex
);
291 * We assume that the x87 and the SSE unit agree on the
292 * rounding mode. Reading the control word on the x87 turns
293 * out to be about 5 times faster than reading it on the SSE
294 * unit on an Opteron 244.
298 return (control
& __X87_ROUND_MASK
);
302 * The fesetround() function shall establish the rounding direction represented
303 * by its argument round. If the argument is not equal to the value of a
304 * rounding direction macro, the rounding direction is not changed.
307 fesetround(int round
)
312 if (round
& ~__X87_ROUND_MASK
) {
318 control
&= ~__X87_ROUND_MASK
;
324 mxcsr
&= ~(__X87_ROUND_MASK
<< __SSE_ROUND_SHIFT
);
325 mxcsr
|= round
<< __SSE_ROUND_SHIFT
;
334 * The fegetenv() function attempts to store the current floating-point
335 * environment in the object pointed to by envp.
338 fegetenv(fenv_t
*envp
)
342 _DIAGASSERT(flagp
!= NULL
);
345 * fnstenv masks all exceptions, so we need to restore the old control
346 * word to avoid this side effect.
349 __fldcw(envp
->x87
.control
);
360 * The feholdexcept() function saves the current floating-point environment in
361 * the object pointed to by envp, clears the floating-point status flags, and
362 * then installs a non-stop (continue on floating-point exceptions) mode, if
363 * available, for all floating-point exceptions.
366 feholdexcept(fenv_t
*envp
)
370 _DIAGASSERT(envp
!= NULL
);
377 mxcsr
&= ~FE_ALL_EXCEPT
;
378 mxcsr
|= FE_ALL_EXCEPT
<< __SSE_EMASK_SHIFT
;
387 * The fesetenv() function attempts to establish the floating-point environment
388 * represented by the object pointed to by envp. The argument `envp' points
389 * to an object set by a call to fegetenv() or feholdexcept(), or equal a
390 * floating-point environment macro. The fesetenv() function does not raise
391 * floating-point exceptions, but only installs the state of the floating-point
392 * status flags represented through its argument.
395 fesetenv(const fenv_t
*envp
)
399 _DIAGASSERT(envp
!= NULL
);
401 /* Store the x87 floating-point environment */
402 memset(&env
, 0, sizeof(env
));
405 __fe_dfl_env
.x87
.unused1
= env
.x87
.unused1
;
406 __fe_dfl_env
.x87
.unused2
= env
.x87
.unused2
;
407 __fe_dfl_env
.x87
.unused3
= env
.x87
.unused3
;
408 memcpy(__fe_dfl_env
.x87
.others
,
410 sizeof(__fe_dfl_env
.x87
.others
) / sizeof(uint32_t));
414 __ldmxcsr(envp
->mxcsr
);
421 * The feupdateenv() function saves the currently raised floating-point
422 * exceptions in its automatic storage, installs the floating-point environment
423 * represented by the object pointed to by `envp', and then raises the saved
424 * floating-point exceptions. The argument `envp' shall point to an object set
425 * by a call to feholdexcept() or fegetenv(), or equal a floating-point
429 feupdateenv(const fenv_t
*envp
)
435 _DIAGASSERT(envp
!= NULL
);
437 /* Store the x87 floating-point environment */
438 memset(&env
, 0, sizeof(env
));
441 __fe_dfl_env
.x87
.unused1
= env
.x87
.unused1
;
442 __fe_dfl_env
.x87
.unused2
= env
.x87
.unused2
;
443 __fe_dfl_env
.x87
.unused3
= env
.x87
.unused3
;
444 memcpy(__fe_dfl_env
.x87
.others
,
446 sizeof(__fe_dfl_env
.x87
.others
) / sizeof(uint32_t));
454 feraiseexcept((mxcsr
| status
) & FE_ALL_EXCEPT
);
461 * The following functions are extentions to the standard
464 feenableexcept(int mask
)
466 uint32_t mxcsr
, omask
;
469 mask
&= FE_ALL_EXCEPT
;
476 omask
= (control
| mxcsr
>> __SSE_EMASK_SHIFT
) & FE_ALL_EXCEPT
;
480 mxcsr
&= ~(mask
<< __SSE_EMASK_SHIFT
);
484 return (FE_ALL_EXCEPT
& ~omask
);
488 fedisableexcept(int mask
)
490 uint32_t mxcsr
, omask
;
493 mask
&= FE_ALL_EXCEPT
;
500 omask
= (control
| mxcsr
>> __SSE_EMASK_SHIFT
) & FE_ALL_EXCEPT
;
504 mxcsr
|= mask
<< __SSE_EMASK_SHIFT
;
508 return (FE_ALL_EXCEPT
& ~omask
);
517 * We assume that the masks for the x87 and the SSE unit are
522 return (~control
& FE_ALL_EXCEPT
);