fesetenv.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (C) 1997-2025 Free Software Foundation, Inc.
  2. The GNU C Library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Lesser General Public License as
  4. published by the Free Software Foundation; either version 2.1 of the
  5. License, or (at your option) any later version.
  6. The GNU C Library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public
  11. License along with the GNU C Library; if not, see
  12. <https://www.gnu.org/licenses/>. */
  13. #include <fenv.h>
  14. #include <fpu_control.h>
  15. int
  16. fesetenv (const fenv_t *envp)
  17. {
  18. fpu_control_t fpcr;
  19. fpu_control_t fpcr_new;
  20. fpu_control_t updated_fpcr;
  21. fpu_fpsr_t fpsr;
  22. fpu_fpsr_t fpsr_new;
  23. _FPU_GETCW (fpcr);
  24. if ((envp != FE_DFL_ENV) && (envp != FE_NOMASK_ENV))
  25. {
  26. /* The new FPCR/FPSR are valid, so don't merge the reserved flags. */
  27. fpcr_new = envp->__fpcr;
  28. if (fpcr != fpcr_new)
  29. _FPU_SETCW (fpcr_new);
  30. _FPU_SETFPSR (envp->__fpsr);
  31. return 0;
  32. }
  33. _FPU_GETFPSR (fpsr);
  34. fpcr_new = fpcr & _FPU_RESERVED;
  35. fpsr_new = fpsr & _FPU_FPSR_RESERVED;
  36. if (envp == FE_DFL_ENV)
  37. {
  38. fpcr_new |= _FPU_DEFAULT;
  39. fpsr_new |= _FPU_FPSR_DEFAULT;
  40. }
  41. else
  42. {
  43. fpcr_new |= _FPU_FPCR_IEEE;
  44. fpsr_new |= _FPU_FPSR_IEEE;
  45. }
  46. _FPU_SETFPSR (fpsr_new);
  47. if (fpcr != fpcr_new)
  48. {
  49. _FPU_SETCW (fpcr_new);
  50. /* Trapping exceptions are optional in AArch64; the relevant enable
  51. bits in FPCR are RES0 hence the absence of support can be detected
  52. by reading back the FPCR and comparing with the required value. */
  53. _FPU_GETCW (updated_fpcr);
  54. return fpcr_new & ~updated_fpcr;
  55. }
  56. return 0;
  57. }