openlibm_fenv_i387.h 6.8 KB

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