| 1 | /* Install given floating-point environment. |
| 2 | Copyright (C) 1997-2024 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 | |
| 21 | int |
| 22 | __fesetenv (const fenv_t *envp) |
| 23 | { |
| 24 | union { unsigned long long buf[4]; fenv_t env; } temp; |
| 25 | unsigned long long *bufptr; |
| 26 | |
| 27 | /* Install the environment specified by ENVP. But there are a few |
| 28 | values which we do not want to come from the saved environment. |
| 29 | Therefore, we get the current environment and replace the values |
| 30 | we want to use from the environment specified by the parameter. */ |
| 31 | bufptr = temp.buf; |
| 32 | __asm__ ( |
| 33 | "fstd %%fr0,0(%1)\n" |
| 34 | : "=m" (temp) : "r" (bufptr) : "%r0" ); |
| 35 | |
| 36 | temp.env.__status_word &= ~(FE_ALL_EXCEPT |
| 37 | | (FE_ALL_EXCEPT << 27) |
| 38 | | FE_DOWNWARD); |
| 39 | if (envp == FE_DFL_ENV) |
| 40 | temp.env.__status_word = 0; |
| 41 | else if (envp == FE_NOMASK_ENV) |
| 42 | temp.env.__status_word |= FE_ALL_EXCEPT; |
| 43 | else |
| 44 | temp.env.__status_word |= (envp->__status_word |
| 45 | & (FE_ALL_EXCEPT |
| 46 | | FE_DOWNWARD |
| 47 | | (FE_ALL_EXCEPT << 27))); |
| 48 | |
| 49 | /* Load the new environment. We use bufptr again since the |
| 50 | initial asm has modified the value of the register and here |
| 51 | we take advantage of that to load in reverse order so fr0 |
| 52 | is loaded last and T-Bit is enabled. */ |
| 53 | __asm__ ( |
| 54 | "fldd 0(%1),%%fr0\n" |
| 55 | : : "m" (temp), "r" (bufptr) : "%r0" ); |
| 56 | |
| 57 | /* Success. */ |
| 58 | return 0; |
| 59 | } |
| 60 | libm_hidden_def (__fesetenv) |
| 61 | weak_alias (__fesetenv, fesetenv) |
| 62 | libm_hidden_weak (fesetenv) |
| 63 | |