fesetenv.c 2.2 KB

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