fenv.c 3.9 KB

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