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