fenv.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*-
  2. * Copyright (c) 2004-2005 David Schultz <das@FreeBSD.ORG>
  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.5 2010/02/03 20:23:47 kib Exp $
  27. */
  28. #include <sys/cdefs.h>
  29. #include <sys/types.h>
  30. #include <machine/fpu.h>
  31. #include <fenv.h>
  32. const fenv_t __fe_dfl_env = {
  33. { 0xffff0000 | __INITIAL_FPUCW__,
  34. 0xffff0000,
  35. 0xffffffff,
  36. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  37. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
  38. },
  39. __INITIAL_MXCSR__
  40. };
  41. int
  42. fesetexceptflag(const fexcept_t *flagp, int excepts)
  43. {
  44. fenv_t env;
  45. __fnstenv(&env.__x87);
  46. env.__x87.__status &= ~excepts;
  47. env.__x87.__status |= *flagp & excepts;
  48. __fldenv(env.__x87);
  49. __stmxcsr(&env.__mxcsr);
  50. env.__mxcsr &= ~excepts;
  51. env.__mxcsr |= *flagp & excepts;
  52. __ldmxcsr(env.__mxcsr);
  53. return (0);
  54. }
  55. int
  56. feraiseexcept(int excepts)
  57. {
  58. fexcept_t ex = excepts;
  59. fesetexceptflag(&ex, excepts);
  60. __fwait();
  61. return (0);
  62. }
  63. int
  64. fegetenv(fenv_t *envp)
  65. {
  66. __fnstenv(&envp->__x87);
  67. __stmxcsr(&envp->__mxcsr);
  68. /*
  69. * fnstenv masks all exceptions, so we need to restore the
  70. * control word to avoid this side effect.
  71. */
  72. __fldcw(envp->__x87.__control);
  73. return (0);
  74. }
  75. int
  76. feholdexcept(fenv_t *envp)
  77. {
  78. __uint32_t mxcsr;
  79. __stmxcsr(&mxcsr);
  80. __fnstenv(&envp->__x87);
  81. __fnclex();
  82. envp->__mxcsr = mxcsr;
  83. mxcsr &= ~FE_ALL_EXCEPT;
  84. mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
  85. __ldmxcsr(mxcsr);
  86. return (0);
  87. }
  88. int
  89. feupdateenv(const fenv_t *envp)
  90. {
  91. __uint32_t mxcsr;
  92. __uint16_t status;
  93. __fnstsw(&status);
  94. __stmxcsr(&mxcsr);
  95. fesetenv(envp);
  96. feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
  97. return (0);
  98. }
  99. int
  100. __feenableexcept(int mask)
  101. {
  102. __uint32_t mxcsr, omask;
  103. __uint16_t control;
  104. mask &= FE_ALL_EXCEPT;
  105. __fnstcw(&control);
  106. __stmxcsr(&mxcsr);
  107. omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
  108. control &= ~mask;
  109. __fldcw(control);
  110. mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
  111. __ldmxcsr(mxcsr);
  112. return (~omask);
  113. }
  114. int
  115. __fedisableexcept(int mask)
  116. {
  117. __uint32_t mxcsr, omask;
  118. __uint16_t control;
  119. mask &= FE_ALL_EXCEPT;
  120. __fnstcw(&control);
  121. __stmxcsr(&mxcsr);
  122. omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
  123. control |= mask;
  124. __fldcw(control);
  125. mxcsr |= mask << _SSE_EMASK_SHIFT;
  126. __ldmxcsr(mxcsr);
  127. return (~omask);
  128. }
  129. __weak_reference(__feenableexcept, feenableexcept);
  130. __weak_reference(__fedisableexcept, fedisableexcept);