Remove building with NOCRYPTO option
[minix3.git] / lib / libm / arch / x86_64 / fenv.c
blobb5309321f109174f2ed6201fe414097291a7e7a1
1 /* $NetBSD: fenv.c,v 1.6 2013/11/11 00:31:51 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.6 2013/11/11 00:31:51 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 /* Check for and handle pending unmasked x87 pending FPU exceptions */
62 #define __fwait(__env) __asm__ __volatile__ \
63 ("fwait")
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
80 * feupdateenv().
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 */
93 0x00000000,
94 0x00000000,
95 0x00000000,
96 0x00000000,
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)
107 uint16_t control;
109 __fnstcw(&control);
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)
121 fenv_t fenv;
122 int ex;
124 _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
126 ex = excepts & FE_ALL_EXCEPT;
128 /* Store the current x87 floating-point environment */
129 __fnstenv(&fenv);
131 /* Clear the requested floating-point exceptions */
132 fenv.x87.status &= ~ex;
134 /* Load the x87 floating-point environent */
135 __fldenv(fenv);
137 /* Same for SSE environment */
138 __stmxcsr(&fenv.mxcsr);
139 fenv.mxcsr &= ~ex;
140 __ldmxcsr(fenv.mxcsr);
142 /* Success */
143 return (0);
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)
154 uint32_t mxcsr;
155 uint16_t x87_status;
156 int ex;
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 */
167 __stmxcsr(&mxcsr);
169 /* Store the results in flagp */
170 *flagp = (x87_status | mxcsr) & ex;
172 /* Success */
173 return (0);
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
182 * directly.
184 * The validation of input is being deferred to fesetexceptflag().
187 feraiseexcept(int excepts)
189 int ex;
191 _DIAGASSERT((excepts & ~FE_ALL_EXCEPT) == 0);
193 ex = excepts & FE_ALL_EXCEPT;
194 fesetexceptflag((unsigned int *)&ex, ex);
195 __fwait();
197 /* Success */
198 return (0);
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)
209 fenv_t fenv;
210 int ex;
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 */
218 __fnstenv(&fenv);
220 /* Set the requested status flags */
221 fenv.x87.status |= *flagp & ex;
223 /* Load the x87 floating-point environent */
224 __fldenv(fenv);
226 /* Same for SSE environment */
227 __stmxcsr(&fenv.mxcsr);
228 fenv.mxcsr |= *flagp & ex;
229 __ldmxcsr(fenv.mxcsr);
231 /* Success */
232 return (0);
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)
243 fenv_t fenv;
244 uint32_t mxcsr;
245 uint16_t status;
246 int ex;
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));
255 __fnstenv(&fenv);
256 __fnstsw(&status);
258 /* Store the MXCSR register state */
259 __stmxcsr(&fenv.mxcsr);
260 __stmxcsr(&mxcsr);
262 return ((fenv.x87.status | fenv.mxcsr) & ex);
266 * The fegetround() function gets the current rounding direction.
269 fegetround(void)
271 uint32_t mxcsr;
272 uint16_t control;
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.
280 __fnstcw(&control);
281 __stmxcsr(&mxcsr);
283 if ((control & _X87_ROUNDING_MASK)
284 != ((mxcsr & _SSE_ROUNDING_MASK) >> 3)) {
285 return (-1);
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)
299 uint32_t mxcsr;
300 uint16_t control;
302 /* Check whether requested rounding direction is supported */
303 if (round & (~_X87_ROUNDING_MASK))
304 return (-1);
306 /* Store the current x87 control word register */
307 __fnstcw(&control);
310 * Set the rounding direction
311 * Rounding Control is bits 10-11, so shift appropriately
313 control &= ~_X87_ROUNDING_MASK;
314 control |= round;
316 /* Load the x87 control word register */
317 __fldcw(control);
320 * Same for the SSE environment
321 * Rounding Control is bits 13-14, so shift appropriately
323 __stmxcsr(&mxcsr);
324 mxcsr &= ~_SSE_ROUNDING_MASK;
325 mxcsr |= (round << _SSE_ROUND_SHIFT);
326 __ldmxcsr(mxcsr);
328 /* Success */
329 return (0);
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 */
342 __fnstenv(envp);
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);
358 /* Success */
359 return (0);
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)
371 uint32_t mxcsr;
373 _DIAGASSERT(envp != NULL);
375 /* Store the current x87 floating-point environment */
376 __fnstenv(envp);
378 /* Clear all exception flags in FPU */
379 __fnclex();
381 /* Store the MXCSR register state */
382 __stmxcsr(&envp->mxcsr);
384 /* Clear exception flags in MXCSR XXX */
385 mxcsr = envp->mxcsr;
386 mxcsr &= ~FE_ALL_EXCEPT;
388 /* Mask all exceptions */
389 mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
391 __ldmxcsr(mxcsr);
393 /* Success */
394 return (0);
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)
408 fenv_t fenv;
410 _DIAGASSERT(envp != NULL);
412 /* Store the x87 floating-point environment */
413 memset(&fenv, 0, sizeof fenv);
414 __fnstenv(&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);
424 __fldenv(*envp);
426 /* Store the MXCSR register */
427 __ldmxcsr(envp->mxcsr);
429 /* Success */
430 return (0);
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
439 * environment macro.
442 feupdateenv(const fenv_t *envp)
444 fenv_t fenv;
445 uint32_t mxcsr;
446 uint16_t sw;
448 _DIAGASSERT(envp != NULL);
450 /* Store the x87 floating-point environment */
451 memset(&fenv, 0, sizeof(fenv));
452 __fnstenv(&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 */
464 __fnstsw(&sw);
466 /* Store the MXCSR register */
467 __stmxcsr(&mxcsr);
469 /* Install new floating-point environment */
470 fesetenv(envp);
472 /* Raise any previously accumulated exceptions */
473 feraiseexcept((sw | mxcsr) & FE_ALL_EXCEPT);
475 /* Success */
476 return (0);
480 * The following functions are extentions to the standard
483 feenableexcept(int mask)
485 uint32_t mxcsr, omask;
486 uint16_t control;
488 _DIAGASSERT((mask & ~FE_ALL_EXCEPT) == 0);
489 mask &= FE_ALL_EXCEPT;
491 __fnstcw(&control);
492 __stmxcsr(&mxcsr);
494 omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
495 control &= ~mask;
496 __fldcw(control);
498 mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
499 __ldmxcsr(mxcsr);
501 return (FE_ALL_EXCEPT & ~omask);
506 fedisableexcept(int mask)
508 uint32_t mxcsr, omask;
509 uint16_t control;
511 _DIAGASSERT((mask & ~FE_ALL_EXCEPT) == 0);
513 __fnstcw(&control);
514 __stmxcsr(&mxcsr);
516 omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
517 control |= mask;
518 __fldcw(control);
520 mxcsr |= mask << _SSE_EMASK_SHIFT;
521 __ldmxcsr(mxcsr);
523 return (FE_ALL_EXCEPT & ~omask);
527 fegetexcept(void)
529 uint16_t control;
532 * We assume that the masks for the x87 and the SSE unit are
533 * the same.
535 __fnstcw(&control);
537 return (~control & FE_ALL_EXCEPT);