fesetenv.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* Install given floating-point environment.
  2. Copyright (C) 2001-2017 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. <http://www.gnu.org/licenses/>. */
  14. #include <fenv.h>
  15. #include <fpu_control.h>
  16. #include <assert.h>
  17. libm_hidden_proto(fesetenv)
  18. /* All exceptions, including the x86-specific "denormal operand"
  19. exception. */
  20. #define FE_ALL_EXCEPT_X86 (FE_ALL_EXCEPT | __FE_DENORM)
  21. int
  22. fesetenv (const fenv_t *envp)
  23. {
  24. fenv_t temp;
  25. /* Install the environment specified by ENVP. But there are a few
  26. values which we do not want to come from the saved environment.
  27. Therefore, we get the current environment and replace the values
  28. we want to use from the environment specified by the parameter. */
  29. __asm__ ("fnstenv %0\n"
  30. "stmxcsr %1" : "=m" (*&temp), "=m" (*&temp.__mxcsr));
  31. if (envp == FE_DFL_ENV)
  32. {
  33. temp.__control_word |= FE_ALL_EXCEPT_X86;
  34. temp.__control_word &= ~FE_TOWARDZERO;
  35. temp.__control_word |= _FPU_EXTENDED;
  36. temp.__status_word &= ~FE_ALL_EXCEPT_X86;
  37. temp.__eip = 0;
  38. temp.__cs_selector = 0;
  39. temp.__opcode = 0;
  40. temp.__data_offset = 0;
  41. temp.__data_selector = 0;
  42. /* Clear SSE exceptions. */
  43. temp.__mxcsr &= ~FE_ALL_EXCEPT_X86;
  44. /* Set mask for SSE MXCSR. */
  45. temp.__mxcsr |= (FE_ALL_EXCEPT_X86 << 7);
  46. /* Set rounding to FE_TONEAREST. */
  47. temp.__mxcsr &= ~ 0x6000;
  48. temp.__mxcsr |= (FE_TONEAREST << 3);
  49. /* Clear the FZ and DAZ bits. */
  50. temp.__mxcsr &= ~0x8040;
  51. }
  52. else if (envp == FE_NOMASK_ENV)
  53. {
  54. temp.__control_word &= ~(FE_ALL_EXCEPT | FE_TOWARDZERO);
  55. /* Keep the "denormal operand" exception masked. */
  56. temp.__control_word |= __FE_DENORM;
  57. temp.__control_word |= _FPU_EXTENDED;
  58. temp.__status_word &= ~FE_ALL_EXCEPT_X86;
  59. temp.__eip = 0;
  60. temp.__cs_selector = 0;
  61. temp.__opcode = 0;
  62. temp.__data_offset = 0;
  63. temp.__data_selector = 0;
  64. /* Clear SSE exceptions. */
  65. temp.__mxcsr &= ~FE_ALL_EXCEPT_X86;
  66. /* Set mask for SSE MXCSR. */
  67. /* Set rounding to FE_TONEAREST. */
  68. temp.__mxcsr &= ~ 0x6000;
  69. temp.__mxcsr |= (FE_TONEAREST << 3);
  70. /* Do not mask exceptions. */
  71. temp.__mxcsr &= ~(FE_ALL_EXCEPT << 7);
  72. /* Keep the "denormal operand" exception masked. */
  73. temp.__mxcsr |= (__FE_DENORM << 7);
  74. /* Clear the FZ and DAZ bits. */
  75. temp.__mxcsr &= ~0x8040;
  76. }
  77. else
  78. {
  79. temp.__control_word &= ~(FE_ALL_EXCEPT_X86
  80. | FE_TOWARDZERO
  81. | _FPU_EXTENDED);
  82. temp.__control_word |= (envp->__control_word
  83. & (FE_ALL_EXCEPT_X86
  84. | FE_TOWARDZERO
  85. | _FPU_EXTENDED));
  86. temp.__status_word &= ~FE_ALL_EXCEPT_X86;
  87. temp.__status_word |= envp->__status_word & FE_ALL_EXCEPT_X86;
  88. temp.__eip = envp->__eip;
  89. temp.__cs_selector = envp->__cs_selector;
  90. temp.__opcode = envp->__opcode;
  91. temp.__data_offset = envp->__data_offset;
  92. temp.__data_selector = envp->__data_selector;
  93. temp.__mxcsr = envp->__mxcsr;
  94. }
  95. __asm__ ("fldenv %0\n"
  96. "ldmxcsr %1" : : "m" (temp), "m" (temp.__mxcsr));
  97. /* Success. */
  98. return 0;
  99. }
  100. libm_hidden_def(fesetenv)