fenv.c 5.2 KB

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