1 /* Install given floating-point environment.
2    Copyright (C) 1997-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_libc.h>
20 
21 int
__fesetenv(const fenv_t * envp)22 __fesetenv (const fenv_t *envp)
23 {
24   unsigned long int fpcr;
25   fenv_t env;
26 
27   /* Magic encoding of default values: high bit set (never possible for a
28      user-space address) is not indirect.  And we don't even have to get
29      rid of it since we mask things around just below.  */
30   if ((long int) envp >= 0)
31     env = *envp;
32   else
33     env = (unsigned long int) envp;
34 
35   /* Reset the rounding mode with the hardware fpcr.  Note that the following
36      system call is an implied trap barrier for our modification.  */
37   __asm__ __volatile__ ("excb; mf_fpcr %0" : "=f" (fpcr));
38   fpcr = (fpcr & ~FPCR_ROUND_MASK) | (env & FPCR_ROUND_MASK);
39   __asm__ __volatile__ ("mt_fpcr %0" : : "f" (fpcr));
40 
41   /* Reset the exception status and mask with the kernel's FP code.  */
42   __ieee_set_fp_control (env & SWCR_ALL_MASK);
43 
44   /* Success.  */
45   return 0;
46 }
47 
48 #include <shlib-compat.h>
49 #if SHLIB_COMPAT (libm, GLIBC_2_1, GLIBC_2_2)
50 strong_alias (__fesetenv, __old_fesetenv)
51 compat_symbol (libm, __old_fesetenv, fesetenv, GLIBC_2_1);
52 #endif
53 
54 libm_hidden_def (__fesetenv)
55 libm_hidden_ver (__fesetenv, fesetenv)
56 versioned_symbol (libm, __fesetenv, fesetenv, GLIBC_2_2);
57