]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/i386/fpu/fesetenv.c
3b261000f3cf0f2a6ca2a9abcd6b94b42944b0d4
[thirdparty/glibc.git] / sysdeps / i386 / fpu / fesetenv.c
1 /* Install given floating-point environment.
2 Copyright (C) 1997-2019 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with the GNU C Library; if not, see
18 <http://www.gnu.org/licenses/>. */
19
20 #include <fenv.h>
21 #include <fpu_control.h>
22 #include <assert.h>
23 #include <unistd.h>
24 #include <ldsodefs.h>
25 #include <dl-procinfo.h>
26
27
28 /* All exceptions, including the x86-specific "denormal operand"
29 exception. */
30 #define FE_ALL_EXCEPT_X86 (FE_ALL_EXCEPT | __FE_DENORM)
31
32
33 int
34 __fesetenv (const fenv_t *envp)
35 {
36 fenv_t temp;
37
38 /* The memory block used by fstenv/fldenv has a size of 28 bytes. */
39 assert (sizeof (fenv_t) == 28);
40
41 /* Install the environment specified by ENVP. But there are a few
42 values which we do not want to come from the saved environment.
43 Therefore, we get the current environment and replace the values
44 we want to use from the environment specified by the parameter. */
45 __asm__ ("fnstenv %0" : "=m" (*&temp));
46
47 if (envp == FE_DFL_ENV)
48 {
49 temp.__control_word |= FE_ALL_EXCEPT_X86;
50 temp.__control_word &= ~FE_TOWARDZERO;
51 temp.__control_word |= _FPU_EXTENDED;
52 temp.__status_word &= ~FE_ALL_EXCEPT_X86;
53 }
54 else if (envp == FE_NOMASK_ENV)
55 {
56 temp.__control_word &= ~(FE_ALL_EXCEPT | FE_TOWARDZERO);
57 /* Keep the "denormal operand" exception masked. */
58 temp.__control_word |= __FE_DENORM;
59 temp.__control_word |= _FPU_EXTENDED;
60 temp.__status_word &= ~FE_ALL_EXCEPT_X86;
61 }
62 else
63 {
64 temp.__control_word &= ~(FE_ALL_EXCEPT_X86
65 | FE_TOWARDZERO
66 | _FPU_EXTENDED);
67 temp.__control_word |= (envp->__control_word
68 & (FE_ALL_EXCEPT_X86
69 | FE_TOWARDZERO
70 | _FPU_EXTENDED));
71 temp.__status_word &= ~FE_ALL_EXCEPT_X86;
72 temp.__status_word |= envp->__status_word & FE_ALL_EXCEPT_X86;
73 }
74 temp.__eip = 0;
75 temp.__cs_selector = 0;
76 temp.__opcode = 0;
77 temp.__data_offset = 0;
78 temp.__data_selector = 0;
79
80 __asm__ ("fldenv %0" : : "m" (temp));
81
82 if (HAS_CPU_FEATURE (SSE))
83 {
84 unsigned int mxcsr;
85 __asm__ ("stmxcsr %0" : "=m" (mxcsr));
86
87 if (envp == FE_DFL_ENV)
88 {
89 /* Clear SSE exceptions. */
90 mxcsr &= ~FE_ALL_EXCEPT_X86;
91 /* Set mask for SSE MXCSR. */
92 mxcsr |= (FE_ALL_EXCEPT_X86 << 7);
93 /* Set rounding to FE_TONEAREST. */
94 mxcsr &= ~0x6000;
95 mxcsr |= (FE_TONEAREST << 3);
96 /* Clear the FZ and DAZ bits. */
97 mxcsr &= ~0x8040;
98 }
99 else if (envp == FE_NOMASK_ENV)
100 {
101 /* Clear SSE exceptions. */
102 mxcsr &= ~FE_ALL_EXCEPT_X86;
103 /* Do not mask exceptions. */
104 mxcsr &= ~(FE_ALL_EXCEPT << 7);
105 /* Keep the "denormal operand" exception masked. */
106 mxcsr |= (__FE_DENORM << 7);
107 /* Set rounding to FE_TONEAREST. */
108 mxcsr &= ~0x6000;
109 mxcsr |= (FE_TONEAREST << 3);
110 /* Clear the FZ and DAZ bits. */
111 mxcsr &= ~0x8040;
112 }
113 else
114 mxcsr = envp->__eip;
115
116 __asm__ ("ldmxcsr %0" : : "m" (mxcsr));
117 }
118
119 /* Success. */
120 return 0;
121 }
122
123 #include <shlib-compat.h>
124 #if SHLIB_COMPAT (libm, GLIBC_2_1, GLIBC_2_2)
125 strong_alias (__fesetenv, __old_fesetenv)
126 compat_symbol (libm, __old_fesetenv, fesetenv, GLIBC_2_1);
127 #endif
128
129 libm_hidden_def (__fesetenv)
130 libm_hidden_ver (__fesetenv, fesetenv)
131 versioned_symbol (libm, __fesetenv, fesetenv, GLIBC_2_2);