]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/i386/fpu/fesetenv.c
Fix i386/x86_64 fesetenv SSE exception clearing (bug 19181).
[thirdparty/glibc.git] / sysdeps / i386 / fpu / fesetenv.c
1 /* Install given floating-point environment.
2 Copyright (C) 1997-2015 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 <assert.h>
22 #include <unistd.h>
23 #include <ldsodefs.h>
24 #include <dl-procinfo.h>
25
26
27 int
28 __fesetenv (const fenv_t *envp)
29 {
30 fenv_t temp;
31
32 /* The memory block used by fstenv/fldenv has a size of 28 bytes. */
33 assert (sizeof (fenv_t) == 28);
34
35 /* Install the environment specified by ENVP. But there are a few
36 values which we do not want to come from the saved environment.
37 Therefore, we get the current environment and replace the values
38 we want to use from the environment specified by the parameter. */
39 __asm__ ("fnstenv %0" : "=m" (*&temp));
40
41 if (envp == FE_DFL_ENV)
42 {
43 temp.__control_word |= FE_ALL_EXCEPT;
44 temp.__control_word &= ~FE_TOWARDZERO;
45 temp.__status_word &= ~FE_ALL_EXCEPT;
46 }
47 else if (envp == FE_NOMASK_ENV)
48 {
49 temp.__control_word &= ~(FE_ALL_EXCEPT | FE_TOWARDZERO);
50 temp.__status_word &= ~FE_ALL_EXCEPT;
51 }
52 else
53 {
54 temp.__control_word &= ~(FE_ALL_EXCEPT | FE_TOWARDZERO);
55 temp.__control_word |= (envp->__control_word
56 & (FE_ALL_EXCEPT | FE_TOWARDZERO));
57 temp.__status_word &= ~FE_ALL_EXCEPT;
58 temp.__status_word |= envp->__status_word & FE_ALL_EXCEPT;
59 }
60 temp.__eip = 0;
61 temp.__cs_selector = 0;
62 temp.__opcode = 0;
63 temp.__data_offset = 0;
64 temp.__data_selector = 0;
65
66 __asm__ ("fldenv %0" : : "m" (temp));
67
68 if ((GLRO(dl_hwcap) & HWCAP_I386_XMM) != 0)
69 {
70 unsigned int mxcsr;
71 __asm__ ("stmxcsr %0" : "=m" (mxcsr));
72
73 if (envp == FE_DFL_ENV)
74 {
75 /* Clear SSE exceptions. */
76 mxcsr &= ~FE_ALL_EXCEPT;
77 /* Set mask for SSE MXCSR. */
78 mxcsr |= (FE_ALL_EXCEPT << 7);
79 /* Set rounding to FE_TONEAREST. */
80 mxcsr &= ~0x6000;
81 mxcsr |= (FE_TONEAREST << 3);
82 }
83 else if (envp == FE_NOMASK_ENV)
84 {
85 /* Clear SSE exceptions. */
86 mxcsr &= ~FE_ALL_EXCEPT;
87 /* Do not mask exceptions. */
88 mxcsr &= ~(FE_ALL_EXCEPT << 7);
89 /* Set rounding to FE_TONEAREST. */
90 mxcsr &= ~0x6000;
91 mxcsr |= (FE_TONEAREST << 3);
92 }
93 else
94 mxcsr = envp->__eip;
95
96 __asm__ ("ldmxcsr %0" : : "m" (mxcsr));
97 }
98
99 /* Success. */
100 return 0;
101 }
102
103 #include <shlib-compat.h>
104 #if SHLIB_COMPAT (libm, GLIBC_2_1, GLIBC_2_2)
105 strong_alias (__fesetenv, __old_fesetenv)
106 compat_symbol (libm, __old_fesetenv, fesetenv, GLIBC_2_1);
107 #endif
108
109 libm_hidden_def (__fesetenv)
110 libm_hidden_ver (__fesetenv, fesetenv)
111 versioned_symbol (libm, __fesetenv, fesetenv, GLIBC_2_2);