fenv.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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/i387/fenv.c,v 1.4 2010/02/03 20:23:47 kib Exp $
  27. */
  28. #include <sys/cdefs.h>
  29. #include <sys/types.h>
  30. #include <machine/npx.h>
  31. #include "fenv.h"
  32. const fenv_t __fe_dfl_env = {
  33. __INITIAL_NPXCW__,
  34. 0x0000,
  35. 0x0000,
  36. 0x1f80,
  37. 0xffffffff,
  38. { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff }
  40. };
  41. enum __sse_support __has_sse =
  42. #ifdef __SSE__
  43. __SSE_YES;
  44. #else
  45. __SSE_UNK;
  46. #endif
  47. #define getfl(x) __asm __volatile("pushfl\n\tpopl %0" : "=mr" (*(x)))
  48. #define setfl(x) __asm __volatile("pushl %0\n\tpopfl" : : "g" (x))
  49. #define cpuid_dx(x) __asm __volatile("pushl %%ebx\n\tmovl $1, %%eax\n\t" \
  50. "cpuid\n\tpopl %%ebx" \
  51. : "=d" (*(x)) : : "eax", "ecx")
  52. /*
  53. * Test for SSE support on this processor. We need to do this because
  54. * we need to use ldmxcsr/stmxcsr to get correct results if any part
  55. * of the program was compiled to use SSE floating-point, but we can't
  56. * use SSE on older processors.
  57. */
  58. int
  59. __test_sse(void)
  60. {
  61. int flag, nflag;
  62. int dx_features;
  63. /* Am I a 486? */
  64. getfl(&flag);
  65. nflag = flag ^ 0x200000;
  66. setfl(nflag);
  67. getfl(&nflag);
  68. if (flag != nflag) {
  69. /* Not a 486, so CPUID should work. */
  70. cpuid_dx(&dx_features);
  71. if (dx_features & 0x2000000) {
  72. __has_sse = __SSE_YES;
  73. return (1);
  74. }
  75. }
  76. __has_sse = __SSE_NO;
  77. return (0);
  78. }
  79. int
  80. fesetexceptflag(const fexcept_t *flagp, int excepts)
  81. {
  82. fenv_t env;
  83. __uint32_t mxcsr;
  84. __fnstenv(&env);
  85. env.__status &= ~excepts;
  86. env.__status |= *flagp & excepts;
  87. __fldenv(env);
  88. if (__HAS_SSE()) {
  89. __stmxcsr(&mxcsr);
  90. mxcsr &= ~excepts;
  91. mxcsr |= *flagp & excepts;
  92. __ldmxcsr(mxcsr);
  93. }
  94. return (0);
  95. }
  96. int
  97. feraiseexcept(int excepts)
  98. {
  99. fexcept_t ex = excepts;
  100. fesetexceptflag(&ex, excepts);
  101. __fwait();
  102. return (0);
  103. }
  104. int
  105. fegetenv(fenv_t *envp)
  106. {
  107. __uint32_t mxcsr;
  108. __fnstenv(envp);
  109. /*
  110. * fnstenv masks all exceptions, so we need to restore
  111. * the old control word to avoid this side effect.
  112. */
  113. __fldcw(envp->__control);
  114. if (__HAS_SSE()) {
  115. __stmxcsr(&mxcsr);
  116. __set_mxcsr(*envp, mxcsr);
  117. }
  118. return (0);
  119. }
  120. int
  121. feholdexcept(fenv_t *envp)
  122. {
  123. __uint32_t mxcsr;
  124. __fnstenv(envp);
  125. __fnclex();
  126. if (__HAS_SSE()) {
  127. __stmxcsr(&mxcsr);
  128. __set_mxcsr(*envp, mxcsr);
  129. mxcsr &= ~FE_ALL_EXCEPT;
  130. mxcsr |= FE_ALL_EXCEPT << _SSE_EMASK_SHIFT;
  131. __ldmxcsr(mxcsr);
  132. }
  133. return (0);
  134. }
  135. int
  136. feupdateenv(const fenv_t *envp)
  137. {
  138. __uint32_t mxcsr;
  139. __uint16_t status;
  140. __fnstsw(&status);
  141. if (__HAS_SSE())
  142. __stmxcsr(&mxcsr);
  143. else
  144. mxcsr = 0;
  145. fesetenv(envp);
  146. feraiseexcept((mxcsr | status) & FE_ALL_EXCEPT);
  147. return (0);
  148. }
  149. int
  150. __feenableexcept(int mask)
  151. {
  152. __uint32_t mxcsr, omask;
  153. __uint16_t control;
  154. mask &= FE_ALL_EXCEPT;
  155. __fnstcw(&control);
  156. if (__HAS_SSE())
  157. __stmxcsr(&mxcsr);
  158. else
  159. mxcsr = 0;
  160. omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
  161. control &= ~mask;
  162. __fldcw(control);
  163. if (__HAS_SSE()) {
  164. mxcsr &= ~(mask << _SSE_EMASK_SHIFT);
  165. __ldmxcsr(mxcsr);
  166. }
  167. return (~omask);
  168. }
  169. int
  170. __fedisableexcept(int mask)
  171. {
  172. __uint32_t mxcsr, omask;
  173. __uint16_t control;
  174. mask &= FE_ALL_EXCEPT;
  175. __fnstcw(&control);
  176. if (__HAS_SSE())
  177. __stmxcsr(&mxcsr);
  178. else
  179. mxcsr = 0;
  180. omask = (control | mxcsr >> _SSE_EMASK_SHIFT) & FE_ALL_EXCEPT;
  181. control |= mask;
  182. __fldcw(control);
  183. if (__HAS_SSE()) {
  184. mxcsr |= mask << _SSE_EMASK_SHIFT;
  185. __ldmxcsr(mxcsr);
  186. }
  187. return (~omask);
  188. }
  189. __weak_reference(__feenableexcept, feenableexcept);
  190. __weak_reference(__fedisableexcept, fedisableexcept);