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

source code of glibc/sysdeps/x86_64/fpu/fesetenv.c