fenv.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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.h,v 1.7 2010/02/03 20:23:47 kib Exp $
  27. */
  28. #ifndef _FENV_H_
  29. #define _FENV_H_
  30. #include <sys/cdefs.h>
  31. #include <sys/_types.h>
  32. /*
  33. * To preserve binary compatibility with FreeBSD 5.3, we pack the
  34. * mxcsr into some reserved fields, rather than changing sizeof(fenv_t).
  35. */
  36. typedef struct {
  37. __uint16_t __control;
  38. __uint16_t __mxcsr_hi;
  39. __uint16_t __status;
  40. __uint16_t __mxcsr_lo;
  41. __uint32_t __tag;
  42. char __other[16];
  43. } fenv_t;
  44. #define __get_mxcsr(env) (((env).__mxcsr_hi << 16) | \
  45. ((env).__mxcsr_lo))
  46. #define __set_mxcsr(env, x) do { \
  47. (env).__mxcsr_hi = (__uint32_t)(x) >> 16; \
  48. (env).__mxcsr_lo = (__uint16_t)(x); \
  49. } while (0)
  50. typedef __uint16_t fexcept_t;
  51. /* Exception flags */
  52. #define FE_INVALID 0x01
  53. #define FE_DENORMAL 0x02
  54. #define FE_DIVBYZERO 0x04
  55. #define FE_OVERFLOW 0x08
  56. #define FE_UNDERFLOW 0x10
  57. #define FE_INEXACT 0x20
  58. #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
  59. FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
  60. /* Rounding modes */
  61. #define FE_TONEAREST 0x0000
  62. #define FE_DOWNWARD 0x0400
  63. #define FE_UPWARD 0x0800
  64. #define FE_TOWARDZERO 0x0c00
  65. #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
  66. FE_UPWARD | FE_TOWARDZERO)
  67. /*
  68. * As compared to the x87 control word, the SSE unit's control word
  69. * has the rounding control bits offset by 3 and the exception mask
  70. * bits offset by 7.
  71. */
  72. #define _SSE_ROUND_SHIFT 3
  73. #define _SSE_EMASK_SHIFT 7
  74. __BEGIN_DECLS
  75. /* After testing for SSE support once, we cache the result in __has_sse. */
  76. enum __sse_support { __SSE_YES, __SSE_NO, __SSE_UNK };
  77. extern enum __sse_support __has_sse;
  78. int __test_sse(void);
  79. #ifdef __SSE__
  80. #define __HAS_SSE() 1
  81. #else
  82. #define __HAS_SSE() (__has_sse == __SSE_YES || \
  83. (__has_sse == __SSE_UNK && __test_sse()))
  84. #endif
  85. /* Default floating-point environment */
  86. extern const fenv_t __fe_dfl_env;
  87. #define FE_DFL_ENV (&__fe_dfl_env)
  88. #define __fldcw(__cw) __asm __volatile("fldcw %0" : : "m" (__cw))
  89. #define __fldenv(__env) __asm __volatile("fldenv %0" : : "m" (__env))
  90. #define __fldenvx(__env) __asm __volatile("fldenv %0" : : "m" (__env) \
  91. : "st", "st(1)", "st(2)", "st(3)", "st(4)", \
  92. "st(5)", "st(6)", "st(7)")
  93. #define __fnclex() __asm __volatile("fnclex")
  94. #define __fnstenv(__env) __asm __volatile("fnstenv %0" : "=m" (*(__env)))
  95. #define __fnstcw(__cw) __asm __volatile("fnstcw %0" : "=m" (*(__cw)))
  96. #define __fnstsw(__sw) __asm __volatile("fnstsw %0" : "=am" (*(__sw)))
  97. #define __fwait() __asm __volatile("fwait")
  98. #define __ldmxcsr(__csr) __asm __volatile("ldmxcsr %0" : : "m" (__csr))
  99. #define __stmxcsr(__csr) __asm __volatile("stmxcsr %0" : "=m" (*(__csr)))
  100. static __inline int
  101. feclearexcept(int __excepts)
  102. {
  103. fenv_t __env;
  104. __uint32_t __mxcsr;
  105. if (__excepts == FE_ALL_EXCEPT) {
  106. __fnclex();
  107. } else {
  108. __fnstenv(&__env);
  109. __env.__status &= ~__excepts;
  110. __fldenv(__env);
  111. }
  112. if (__HAS_SSE()) {
  113. __stmxcsr(&__mxcsr);
  114. __mxcsr &= ~__excepts;
  115. __ldmxcsr(__mxcsr);
  116. }
  117. return (0);
  118. }
  119. static __inline int
  120. fegetexceptflag(fexcept_t *__flagp, int __excepts)
  121. {
  122. __uint32_t __mxcsr;
  123. __uint16_t __status;
  124. __fnstsw(&__status);
  125. if (__HAS_SSE())
  126. __stmxcsr(&__mxcsr);
  127. else
  128. __mxcsr = 0;
  129. *__flagp = (__mxcsr | __status) & __excepts;
  130. return (0);
  131. }
  132. int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
  133. int feraiseexcept(int __excepts);
  134. static __inline int
  135. fetestexcept(int __excepts)
  136. {
  137. __uint32_t __mxcsr;
  138. __uint16_t __status;
  139. __fnstsw(&__status);
  140. if (__HAS_SSE())
  141. __stmxcsr(&__mxcsr);
  142. else
  143. __mxcsr = 0;
  144. return ((__status | __mxcsr) & __excepts);
  145. }
  146. static __inline int
  147. fegetround(void)
  148. {
  149. __uint16_t __control;
  150. /*
  151. * We assume that the x87 and the SSE unit agree on the
  152. * rounding mode. Reading the control word on the x87 turns
  153. * out to be about 5 times faster than reading it on the SSE
  154. * unit on an Opteron 244.
  155. */
  156. __fnstcw(&__control);
  157. return (__control & _ROUND_MASK);
  158. }
  159. static __inline int
  160. fesetround(int __round)
  161. {
  162. __uint32_t __mxcsr;
  163. __uint16_t __control;
  164. if (__round & ~_ROUND_MASK)
  165. return (-1);
  166. __fnstcw(&__control);
  167. __control &= ~_ROUND_MASK;
  168. __control |= __round;
  169. __fldcw(__control);
  170. if (__HAS_SSE()) {
  171. __stmxcsr(&__mxcsr);
  172. __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
  173. __mxcsr |= __round << _SSE_ROUND_SHIFT;
  174. __ldmxcsr(__mxcsr);
  175. }
  176. return (0);
  177. }
  178. int fegetenv(fenv_t *__envp);
  179. int feholdexcept(fenv_t *__envp);
  180. static __inline int
  181. fesetenv(const fenv_t *__envp)
  182. {
  183. fenv_t __env = *__envp;
  184. __uint32_t __mxcsr;
  185. __mxcsr = __get_mxcsr(__env);
  186. __set_mxcsr(__env, 0xffffffff);
  187. /*
  188. * XXX Using fldenvx() instead of fldenv() tells the compiler that this
  189. * instruction clobbers the i387 register stack. This happens because
  190. * we restore the tag word from the saved environment. Normally, this
  191. * would happen anyway and we wouldn't care, because the ABI allows
  192. * function calls to clobber the i387 regs. However, fesetenv() is
  193. * inlined, so we need to be more careful.
  194. */
  195. __fldenvx(__env);
  196. if (__HAS_SSE())
  197. __ldmxcsr(__mxcsr);
  198. return (0);
  199. }
  200. int feupdateenv(const fenv_t *__envp);
  201. #if __BSD_VISIBLE
  202. int feenableexcept(int __mask);
  203. int fedisableexcept(int __mask);
  204. static __inline int
  205. fegetexcept(void)
  206. {
  207. __uint16_t __control;
  208. /*
  209. * We assume that the masks for the x87 and the SSE unit are
  210. * the same.
  211. */
  212. __fnstcw(&__control);
  213. return (~__control & FE_ALL_EXCEPT);
  214. }
  215. #endif /* __BSD_VISIBLE */
  216. __END_DECLS
  217. #endif /* !_FENV_H_ */