fenv.c 5.3 KB

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