fenv.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #define _fenv_static
  31. #include "fenv.h"
  32. #ifdef __GNUC_GNU_INLINE__
  33. #error "This file must be compiled with C99 'inline' semantics"
  34. #endif
  35. const fenv_t __fe_dfl_env = {
  36. { 0xffff0000 | __INITIAL_FPUCW__,
  37. 0xffff0000,
  38. 0xffffffff,
  39. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  40. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
  41. },
  42. __INITIAL_MXCSR__
  43. };
  44. extern inline DLLEXPORT int feclearexcept(int __excepts);
  45. extern inline DLLEXPORT int fegetexceptflag(fexcept_t *__flagp, int __excepts);
  46. DLLEXPORT int
  47. fesetexceptflag(const fexcept_t *flagp, int excepts)
  48. {
  49. fenv_t env;
  50. __fnstenv(&env.__x87);
  51. env.__x87.__status &= ~excepts;
  52. env.__x87.__status |= *flagp & excepts;
  53. __fldenv(env.__x87);
  54. __stmxcsr(&env.__mxcsr);
  55. env.__mxcsr &= ~excepts;
  56. env.__mxcsr |= *flagp & excepts;
  57. __ldmxcsr(env.__mxcsr);
  58. return (0);
  59. }
  60. DLLEXPORT int
  61. feraiseexcept(int excepts)
  62. {
  63. fexcept_t ex = excepts;
  64. fesetexceptflag(&ex, excepts);
  65. __fwait();
  66. return (0);
  67. }
  68. extern inline DLLEXPORT int fetestexcept(int __excepts);
  69. extern inline DLLEXPORT int fegetround(void);
  70. extern inline DLLEXPORT int fesetround(int __round);
  71. DLLEXPORT int
  72. fegetenv(fenv_t *envp)
  73. {
  74. __fnstenv(&envp->__x87);
  75. __stmxcsr(&envp->__mxcsr);
  76. /*
  77. * fnstenv masks all exceptions, so we need to restore the
  78. * control word to avoid this side effect.
  79. */
  80. __fldcw(envp->__x87.__control);
  81. return (0);
  82. }
  83. DLLEXPORT int
  84. feholdexcept(fenv_t *envp)
  85. {
  86. uint32_t mxcsr;
  87. __stmxcsr(&mxcsr);
  88. __fnstenv(&envp->__x87);
  89. __fnclex();
  90. envp->__mxcsr = mxcsr;
  91. mxcsr &= ~FE_ALL_EXCEPT;
  92. mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
  93. __ldmxcsr(mxcsr);
  94. return (0);
  95. }
  96. extern inline DLLEXPORT int fesetenv(const fenv_t *__envp);
  97. DLLEXPORT int
  98. feupdateenv(const fenv_t *envp)
  99. {
  100. uint32_t mxcsr;
  101. uint16_t status;
  102. __fnstsw(&status);
  103. __stmxcsr(&mxcsr);
  104. fesetenv(envp);
  105. feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
  106. return (0);
  107. }
  108. int
  109. __feenableexcept(int mask)
  110. {
  111. uint32_t mxcsr, omask;
  112. uint16_t control;
  113. mask &= FE_ALL_EXCEPT;
  114. __fnstcw(&control);
  115. __stmxcsr(&mxcsr);
  116. omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
  117. control &= ~mask;
  118. __fldcw(control);
  119. mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
  120. __ldmxcsr(mxcsr);
  121. return (omask);
  122. }
  123. int
  124. __fedisableexcept(int mask)
  125. {
  126. uint32_t mxcsr, omask;
  127. uint16_t control;
  128. mask &= FE_ALL_EXCEPT;
  129. __fnstcw(&control);
  130. __stmxcsr(&mxcsr);
  131. omask = ~(control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
  132. control |= mask;
  133. __fldcw(control);
  134. mxcsr |= mask << _SSE_EMASK_SHIFT;
  135. __ldmxcsr(mxcsr);
  136. return (omask);
  137. }
  138. __weak_reference(__feenableexcept, feenableexcept);
  139. __weak_reference(__fedisableexcept, fedisableexcept);