VM: full munmap
[minix.git] / lib / libm / arch / x86_64 / fenv.c
blob9f27fcedc4f7de27eb2a1374a8fb891051a65561
1 /* $NetBSD: fenv.c,v 1.1 2010/07/31 21:47:53 joerg Exp $ */
3 /*-
4 * Copyright (c) 2004-2005 David Schultz <das (at) FreeBSD.ORG>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: fenv.c,v 1.1 2010/07/31 21:47:53 joerg Exp $");
32 #include <assert.h>
33 #include <fenv.h>
34 #include <stddef.h>
35 #include <string.h>
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__ \
51 ("fnclex")
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
76 * feupdateenv().
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 */
89 0x00000000,
90 0x00000000,
91 0x00000000,
92 0x00000000,
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)
107 fenv_t fenv;
108 int ex;
110 _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
112 ex = excepts & FE_ALL_EXCEPT;
114 /* Store the current x87 floating-point environment */
115 __fnstenv(&fenv);
117 /* Clear the requested floating-point exceptions */
118 fenv.x87.status &= ~ex;
120 /* Load the x87 floating-point environent */
121 __fldenv(fenv);
123 /* Same for SSE environment */
124 __stmxcsr(&fenv.mxcsr);
125 fenv.mxcsr &= ~ex;
126 __ldmxcsr(fenv.mxcsr);
128 /* Success */
129 return (0);
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)
140 uint32_t mxcsr;
141 uint16_t x87_status;
142 int ex;
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 */
153 __stmxcsr(&mxcsr);
155 /* Store the results in flagp */
156 *flagp = (x87_status | mxcsr) & ex;
158 /* Success */
159 return (0);
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
168 * directly.
170 * The validation of input is being deferred to fesetexceptflag().
173 feraiseexcept(int excepts)
175 int ex;
177 _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
179 ex = excepts & FE_ALL_EXCEPT;
180 fesetexceptflag((unsigned int *)&excepts, excepts);
182 /* Success */
183 return (0);
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)
194 fenv_t fenv;
195 int ex;
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 */
203 __fnstenv(&fenv);
205 /* Set the requested status flags */
206 fenv.x87.status |= *flagp & ex;
208 /* Load the x87 floating-point environent */
209 __fldenv(fenv);
211 /* Same for SSE environment */
212 __stmxcsr(&fenv.mxcsr);
213 fenv.mxcsr |= *flagp & ex;
214 __ldmxcsr(fenv.mxcsr);
216 /* Success */
217 return (0);
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)
228 fenv_t fenv;
229 uint32_t mxcsr;
230 uint16_t status;
231 int ex;
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));
240 __fnstenv(&fenv);
241 __fnstsw(&status);
243 /* Store the MXCSR register state */
244 __stmxcsr(&fenv.mxcsr);
245 __stmxcsr(&mxcsr);
247 return ((fenv.x87.status | fenv.mxcsr) & ex);
251 * The fegetround() function gets the current rounding direction.
254 fegetround(void)
256 uint32_t mxcsr;
257 uint16_t control;
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.
265 __fnstcw(&control);
266 __stmxcsr(&mxcsr);
268 if ((control & _X87_ROUNDING_MASK)
269 != ((mxcsr & _SSE_ROUNDING_MASK) >> 3)) {
270 return (-1);
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)
284 uint32_t mxcsr;
285 uint16_t control;
287 /* Check whether requested rounding direction is supported */
288 if (round & (~_X87_ROUNDING_MASK))
289 return (-1);
291 /* Store the current x87 control word register */
292 __fnstcw(&control);
295 * Set the rounding direction
296 * Rounding Control is bits 10-11, so shift appropriately
298 control &= ~_X87_ROUNDING_MASK;
299 control |= round;
301 /* Load the x87 control word register */
302 __fldcw(control);
305 * Same for the SSE environment
306 * Rounding Control is bits 13-14, so shift appropriately
308 __stmxcsr(&mxcsr);
309 mxcsr &= ~_SSE_ROUNDING_MASK;
310 mxcsr |= (round << _SSE_ROUND_SHIFT);
311 __ldmxcsr(mxcsr);
313 /* Success */
314 return (0);
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 */
327 __fnstenv(envp);
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);
343 /* Success */
344 return (0);
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)
356 uint32_t mxcsr;
358 _DIAGASSERT(envp != NULL);
360 /* Store the current x87 floating-point environment */
361 __fnstenv(envp);
363 /* Clear all exception flags in FPU */
364 __fnclex();
366 /* Store the MXCSR register state */
367 __stmxcsr(&envp->mxcsr);
369 /* Clear exception flags in MXCSR XXX */
370 mxcsr = envp->mxcsr;
371 mxcsr &= ~FE_ALL_EXCEPT;
373 /* Mask all exceptions */
374 mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
376 __ldmxcsr(mxcsr);
378 /* Success */
379 return (0);
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)
393 fenv_t fenv;
395 _DIAGASSERT(envp != NULL);
397 /* Store the x87 floating-point environment */
398 memset(&fenv, 0, sizeof fenv);
399 __fnstenv(&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);
409 __fldenv(*envp);
411 /* Store the MXCSR register */
412 __ldmxcsr(envp->mxcsr);
414 /* Success */
415 return (0);
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
424 * environment macro.
427 feupdateenv(const fenv_t *envp)
429 fenv_t fenv;
430 uint32_t mxcsr;
431 uint16_t sw;
433 _DIAGASSERT(envp != NULL);
435 /* Store the x87 floating-point environment */
436 memset(&fenv, 0, sizeof(fenv));
437 __fnstenv(&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 */
449 __fnstsw(&sw);
451 /* Store the MXCSR register */
452 __stmxcsr(&mxcsr);
454 /* Install new floating-point environment */
455 fesetenv(envp);
457 /* Raise any previously accumulated exceptions */
458 feraiseexcept((sw | mxcsr) & FE_ALL_EXCEPT);
460 /* Success */
461 return (0);
465 * The following functions are extentions to the standard
468 feenableexcept(int mask)
470 uint32_t mxcsr, omask;
471 uint16_t control;
473 _DIAGASSERT((mask & ~FE_ALL_EXCEPT) == 0);
474 mask &= FE_ALL_EXCEPT;
476 __fnstcw(&control);
477 __stmxcsr(&mxcsr);
479 omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
480 control &= ~mask;
481 __fldcw(control);
483 mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
484 __ldmxcsr(mxcsr);
486 return (~omask);
491 fedisableexcept(int mask)
493 uint32_t mxcsr, omask;
494 uint16_t control;
496 _DIAGASSERT((mask & ~FE_ALL_EXCEPT) == 0);
498 __fnstcw(&control);
499 __stmxcsr(&mxcsr);
501 omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
502 control |= mask;
503 __fldcw(control);
505 mxcsr |= mask << _SSE_EMASK_SHIFT;
506 __ldmxcsr(mxcsr);
508 return (~omask);
512 fegetexcept(void)
514 uint16_t control;
517 * We assume that the masks for the x87 and the SSE unit are
518 * the same.
520 __fnstcw(&control);
522 return (control & FE_ALL_EXCEPT);