fenv.c 5.1 KB

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