OSDN Git Service

More rationalization of CRT_INLINE function implementations.
[mingw/mingw-org-wsl.git] / mingwrt / mingwex / fesetenv.c
1 #include <fenv.h>
2 #include <float.h>
3 #include "cpu_features.h"
4
5 /* 7.6.4.3
6    The fesetenv function establishes the floating-point environment
7    represented by the object pointed to by envp. The argument envp
8    points to an object set by a call to fegetenv or feholdexcept, or
9    equal the macro FE_DFL_ENV or an implementation-defined environment
10    macro. Note that fesetenv merely installs the state of the exception
11    flags represented through its argument, and does not raise these
12    exceptions.
13  */
14
15 extern void (*_imp___fpreset)( void ) ;
16
17 int fesetenv (const fenv_t * envp)
18 {
19   /* Default mxcsr status is to mask all exceptions.  All other bits
20      are zero.  */
21
22   unsigned int _csr = FE_ALL_EXCEPT << __MXCSR_EXCEPT_MASK_SHIFT /*= 0x1f80 */;
23
24   if (envp == FE_PC64_ENV)
25    /*
26     *  fninit initializes the control register to 0x37f,
27     *  the status register to zero and the tag word to 0FFFFh.
28     *  The other registers are unaffected.
29     */
30     __asm__ ("fninit");
31
32   else if (envp == FE_PC53_ENV)
33    /*
34     * MS _fpreset() does same *except* it sets control word
35     * to 0x27f (53-bit precison).
36     * We force calling _fpreset in msvcrt.dll
37     */
38
39    (*_imp___fpreset)();
40
41   else if (envp == FE_DFL_ENV)
42     /* Use the choice made at app startup */
43     _fpreset();
44
45   else
46     {
47       __asm__ ("fldenv %0;" : : "m" (*envp));
48        /* Setting the reserved high order bits of MXCSR causes a segfault */
49        _csr = envp ->__mxcsr & 0xffff;
50     }
51
52   /* Set MXCSR */
53    if (__HAS_SSE)
54      __asm__ volatile ("ldmxcsr %0" : : "m" (_csr));
55
56   return 0;
57 }