1 /* Install given floating-point environment.
2 Copyright (C) 1997-2025 Free Software Foundation, Inc.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library. If not, see
16 <https://www.gnu.org/licenses/>. */
17
18 #include <fenv.h>
19
20 int
fesetenv(const fenv_t * envp)21 fesetenv (const fenv_t *envp)
22 {
23 fenv_t temp;
24
25 /* Install the environment specified by ENVP. But there are a few
26 values which we do not want to come from the saved environment.
27 Therefore, we get the current environment and replace the values
28 we want to use from the environment specified by the parameter. */
29 #ifdef __mcoldfire__
30 __asm__ ("fmove%.l %/fpcr,%0" : "=dm" (temp.__control_register));
31 __asm__ ("fmove%.l %/fpsr,%0" : "=dm" (temp.__status_register));
32 __asm__ ("fmove%.l %/fpiar,%0" : "=dm" (temp.__instruction_address));
33 #else
34 __asm__ ("fmovem%.l %/fpcr/%/fpsr/%/fpiar,%0" : "=m" (*&temp));
35 #endif
36
37 temp.__status_register &= ~FE_ALL_EXCEPT;
38 temp.__control_register &= ~((FE_ALL_EXCEPT << 6) | FE_UPWARD);
39 if (envp == FE_DFL_ENV)
40 ;
41 else if (envp == FE_NOMASK_ENV)
42 temp.__control_register |= FE_ALL_EXCEPT << 6;
43 else
44 {
45 temp.__control_register |= (envp->__control_register
46 & ((FE_ALL_EXCEPT << 6) | FE_UPWARD));
47 temp.__status_register |= envp->__status_register & FE_ALL_EXCEPT;
48 }
49
50 #ifdef __mcoldfire__
51 __asm__ __volatile__ ("fmove%.l %0,%/fpiar"
52 :: "dm" (temp.__instruction_address));
53 __asm__ __volatile__ ("fmove%.l %0,%/fpcr"
54 :: "dm" (temp.__control_register));
55 __asm__ __volatile__ ("fmove%.l %0,%/fpsr"
56 :: "dm" (temp.__status_register));
57 #else
58 __asm__ __volatile__ ("fmovem%.l %0,%/fpcr/%/fpsr/%/fpiar" : : "m" (*&temp));
59 #endif
60
61 /* Success. */
62 return 0;
63 }
64