1 /**
2 * This file has no copyright assigned and is placed in the Public Domain.
3 * This file is part of the mingw-w64 runtime package.
4 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6 #include <_mingw.h>
7 #include <fenv.h>
8 #include <float.h>
9
10 #if !(defined(_ARM_) || defined(__arm__) || defined(_ARM64_) || defined(__aarch64__))
11 extern int __mingw_has_sse (void);
12 #endif /* !(defined(_ARM_) || defined(__arm__) || defined(_ARM64_) || defined(__aarch64__)) */
13
14 /* 7.6.4.3
15 The fesetenv function establishes the floating-point environment
16 represented by the object pointed to by envp. The argument envp
17 points to an object set by a call to fegetenv or feholdexcept, or
18 equal the macro FE_DFL_ENV or an implementation-defined environment
19 macro. Note that fesetenv merely installs the state of the exception
20 flags represented through its argument, and does not raise these
21 exceptions.
22 */
23
24 extern void (* __MINGW_IMP_SYMBOL(_fpreset))(void);
25 extern void _fpreset(void);
26
fesetenv(const fenv_t * envp)27 int fesetenv (const fenv_t * envp)
28 {
29 #if defined(_ARM_) || defined(__arm__)
30 if (envp == FE_DFL_ENV)
31 /* Use the choice made at app startup */
32 _fpreset();
33 else
34 __asm__ volatile ("fmxr FPSCR, %0" : : "r" (*envp));
35 #elif defined(_ARM64_) || defined(__aarch64__)
36 if (envp == FE_DFL_ENV) {
37 /* Use the choice made at app startup */
38 _fpreset();
39 } else {
40 unsigned __int64 fpcr = envp->__cw;
41 __asm__ volatile ("msr fpcr, %0" : : "r" (fpcr));
42 }
43 #else
44 if (envp == FE_PC64_ENV)
45 /*
46 * fninit initializes the control register to 0x37f,
47 * the status register to zero and the tag word to 0FFFFh.
48 * The other registers are unaffected.
49 */
50 __asm__ __volatile__ ("fninit");
51
52 else if (envp == FE_PC53_ENV)
53 /*
54 * MS _fpreset() does same *except* it sets control word
55 * to 0x27f (53-bit precision).
56 * We force calling _fpreset in msvcrt.dll
57 */
58
59 (* __MINGW_IMP_SYMBOL(_fpreset))();
60
61 else if (envp == FE_DFL_ENV)
62 /* Use the choice made at app startup */
63 _fpreset();
64
65 else
66 {
67 fenv_t env = *envp;
68 int has_sse = __mingw_has_sse ();
69 int _mxcsr;
70 /*_mxcsr = ((int)envp->__unused0 << 16) | (int)envp->__unused1; *//* mxcsr low and high */
71 if (has_sse)
72 __asm__ ("stmxcsr %0" : "=m" (*&_mxcsr));
73 env.__unused0 = 0xffff;
74 env.__unused1 = 0xffff;
75 __asm__ volatile ("fldenv %0" : : "m" (env)
76 : "st", "st(1)", "st(2)", "st(3)", "st(4)",
77 "st(5)", "st(6)", "st(7)");
78 if (has_sse)
79 __asm__ volatile ("ldmxcsr %0" : : "m" (*&_mxcsr));
80 }
81
82 #endif /* defined(_ARM_) || defined(__arm__) || defined(_ARM64_) || defined(__aarch64__) */
83 return 0;
84 }
85