2.9
[glibc/nacl-glibc.git] / sysdeps / x86_64 / fpu / fesetenv.c
blob8a3b7a074453f4cd8482f6f15b6de572e0f018e3
1 /* Install given floating-point environment.
2 Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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.
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.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <fenv.h>
21 #include <assert.h>
24 int
25 fesetenv (const fenv_t *envp)
27 fenv_t temp;
29 /* Install the environment specified by ENVP. But there are a few
30 values which we do not want to come from the saved environment.
31 Therefore, we get the current environment and replace the values
32 we want to use from the environment specified by the parameter. */
33 __asm__ ("fnstenv %0\n"
34 "stmxcsr %1" : "=m" (*&temp), "=m" (*&temp.__mxcsr));
36 if (envp == FE_DFL_ENV)
38 temp.__control_word |= FE_ALL_EXCEPT;
39 temp.__control_word &= ~FE_TOWARDZERO;
40 temp.__status_word &= ~FE_ALL_EXCEPT;
41 temp.__eip = 0;
42 temp.__cs_selector = 0;
43 temp.__opcode = 0;
44 temp.__data_offset = 0;
45 temp.__data_selector = 0;
46 /* Set mask for SSE MXCSR. */
47 temp.__mxcsr |= (FE_ALL_EXCEPT << 7);
48 /* Set rounding to FE_TONEAREST. */
49 temp.__mxcsr &= ~ 0x6000;
50 temp.__mxcsr |= (FE_TONEAREST << 3);
52 else if (envp == FE_NOMASK_ENV)
54 temp.__control_word &= ~(FE_ALL_EXCEPT | FE_TOWARDZERO);
55 temp.__status_word &= ~FE_ALL_EXCEPT;
56 temp.__eip = 0;
57 temp.__cs_selector = 0;
58 temp.__opcode = 0;
59 temp.__data_offset = 0;
60 temp.__data_selector = 0;
61 /* Set mask for SSE MXCSR. */
62 /* Set rounding to FE_TONEAREST. */
63 temp.__mxcsr &= ~ 0x6000;
64 temp.__mxcsr |= (FE_TONEAREST << 3);
65 /* Do not mask exceptions. */
66 temp.__mxcsr &= ~(FE_ALL_EXCEPT << 7);
68 else
70 temp.__control_word &= ~(FE_ALL_EXCEPT | FE_TOWARDZERO);
71 temp.__control_word |= (envp->__control_word
72 & (FE_ALL_EXCEPT | FE_TOWARDZERO));
73 temp.__status_word &= ~FE_ALL_EXCEPT;
74 temp.__status_word |= envp->__status_word & FE_ALL_EXCEPT;
75 temp.__eip = envp->__eip;
76 temp.__cs_selector = envp->__cs_selector;
77 temp.__opcode = envp->__opcode;
78 temp.__data_offset = envp->__data_offset;
79 temp.__data_selector = envp->__data_selector;
80 temp.__mxcsr = envp->__mxcsr;
83 __asm__ ("fldenv %0\n"
84 "ldmxcsr %1" : : "m" (temp), "m" (temp.__mxcsr));
86 /* Success. */
87 return 0;
89 libm_hidden_def (fesetenv)