fenv.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*-
  2. * Copyright (c) 2004-2005 David Schultz <[email protected]>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  18. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  22. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  23. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  24. * SUCH DAMAGE.
  25. *
  26. * $FreeBSD: src/lib/msun/amd64/fenv.c,v 1.8 2011/10/21 06:25:31 das Exp $
  27. */
  28. #include "bsd_fpu.h"
  29. #include "math_private.h"
  30. #ifdef _WIN32
  31. #define __fenv_static OLM_DLLEXPORT
  32. #endif
  33. #include <openlibm_fenv.h>
  34. #ifdef __GNUC_GNU_INLINE__
  35. #error "This file must be compiled with C99 'inline' semantics"
  36. #endif
  37. const fenv_t __fe_dfl_env = {
  38. { 0xffff0000 | __INITIAL_FPUCW__,
  39. 0xffff0000,
  40. 0xffffffff,
  41. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  42. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
  43. },
  44. __INITIAL_MXCSR__
  45. };
  46. extern inline OLM_DLLEXPORT int feclearexcept(int __excepts);
  47. extern inline OLM_DLLEXPORT int fegetexceptflag(fexcept_t *__flagp, int __excepts);
  48. OLM_DLLEXPORT int
  49. fesetexceptflag(const fexcept_t *flagp, int excepts)
  50. {
  51. fenv_t env;
  52. __fnstenv(&env.__x87);
  53. env.__x87.__status &= ~excepts;
  54. env.__x87.__status |= *flagp & excepts;
  55. __fldenv(env.__x87);
  56. __stmxcsr(&env.__mxcsr);
  57. env.__mxcsr &= ~excepts;
  58. env.__mxcsr |= *flagp & excepts;
  59. __ldmxcsr(env.__mxcsr);
  60. return (0);
  61. }
  62. OLM_DLLEXPORT int
  63. feraiseexcept(int excepts)
  64. {
  65. fexcept_t ex = excepts;
  66. fesetexceptflag(&ex, excepts);
  67. __fwait();
  68. return (0);
  69. }
  70. extern inline OLM_DLLEXPORT int fetestexcept(int __excepts);
  71. extern inline OLM_DLLEXPORT int fegetround(void);
  72. extern inline OLM_DLLEXPORT int fesetround(int __round);
  73. OLM_DLLEXPORT int
  74. fegetenv(fenv_t *envp)
  75. {
  76. __fnstenv(&envp->__x87);
  77. __stmxcsr(&envp->__mxcsr);
  78. /*
  79. * fnstenv masks all exceptions, so we need to restore the
  80. * control word to avoid this side effect.
  81. */
  82. __fldcw(envp->__x87.__control);
  83. return (0);
  84. }
  85. OLM_DLLEXPORT int
  86. feholdexcept(fenv_t *envp)
  87. {
  88. uint32_t mxcsr;
  89. __stmxcsr(&mxcsr);
  90. __fnstenv(&envp->__x87);
  91. __fnclex();
  92. envp->__mxcsr = mxcsr;
  93. mxcsr &= ~FE_ALL_EXCEPT;
  94. mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
  95. __ldmxcsr(mxcsr);
  96. return (0);
  97. }
  98. extern inline OLM_DLLEXPORT int fesetenv(const fenv_t *__envp);
  99. OLM_DLLEXPORT int
  100. feupdateenv(const fenv_t *envp)
  101. {
  102. uint32_t mxcsr;
  103. uint16_t status;
  104. __fnstsw(&status);
  105. __stmxcsr(&mxcsr);
  106. fesetenv(envp);
  107. feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
  108. return (0);
  109. }
  110. int
  111. feenableexcept(int mask)
  112. {
  113. uint32_t mxcsr, omask;
  114. uint16_t control;
  115. mask &= FE_ALL_EXCEPT;
  116. __fnstcw(&control);
  117. __stmxcsr(&mxcsr);
  118. omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
  119. control &= ~mask;
  120. __fldcw(control);
  121. mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
  122. __ldmxcsr(mxcsr);
  123. return (omask);
  124. }
  125. int
  126. fedisableexcept(int mask)
  127. {
  128. uint32_t mxcsr, omask;
  129. uint16_t control;
  130. mask &= FE_ALL_EXCEPT;
  131. __fnstcw(&control);
  132. __stmxcsr(&mxcsr);
  133. omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
  134. control |= mask;
  135. __fldcw(control);
  136. mxcsr |= mask << _SSE_EMASK_SHIFT;
  137. __ldmxcsr(mxcsr);
  138. return (omask);
  139. }