feupdateenv.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Copyright (C) 2009-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. feupdateenv (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. int excepts;
  24. _FPU_GETCW (fpcr);
  25. _FPU_GETFPSR (fpsr);
  26. excepts = fpsr & FE_ALL_EXCEPT;
  27. if ((envp != FE_DFL_ENV) && (envp != FE_NOMASK_ENV))
  28. {
  29. fpcr_new = envp->__fpcr;
  30. fpsr_new = envp->__fpsr | excepts;
  31. if (fpcr != fpcr_new)
  32. _FPU_SETCW (fpcr_new);
  33. if (fpsr != fpsr_new)
  34. _FPU_SETFPSR (fpsr_new);
  35. if (excepts & (fpcr_new >> FE_EXCEPT_SHIFT))
  36. return feraiseexcept (excepts);
  37. return 0;
  38. }
  39. fpcr_new = fpcr & _FPU_RESERVED;
  40. fpsr_new = fpsr & (_FPU_FPSR_RESERVED | FE_ALL_EXCEPT);
  41. if (envp == FE_DFL_ENV)
  42. {
  43. fpcr_new |= _FPU_DEFAULT;
  44. fpsr_new |= _FPU_FPSR_DEFAULT;
  45. }
  46. else
  47. {
  48. fpcr_new |= _FPU_FPCR_IEEE;
  49. fpsr_new |= _FPU_FPSR_IEEE;
  50. }
  51. _FPU_SETFPSR (fpsr_new);
  52. if (fpcr != fpcr_new)
  53. {
  54. _FPU_SETCW (fpcr_new);
  55. /* Trapping exceptions are optional in AArch64; the relevant enable
  56. bits in FPCR are RES0 hence the absence of support can be detected
  57. by reading back the FPCR and comparing with the required value. */
  58. _FPU_GETCW (updated_fpcr);
  59. if (fpcr_new & ~updated_fpcr)
  60. return 1;
  61. }
  62. if (excepts & (fpcr_new >> FE_EXCEPT_SHIFT))
  63. return feraiseexcept (excepts);
  64. return 0;
  65. }