fenv.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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/amd64/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. typedef struct {
  33. struct {
  34. __uint32_t __control;
  35. __uint32_t __status;
  36. __uint32_t __tag;
  37. char __other[16];
  38. } __x87;
  39. __uint32_t __mxcsr;
  40. } fenv_t;
  41. typedef __uint16_t fexcept_t;
  42. /* Exception flags */
  43. #define FE_INVALID 0x01
  44. #define FE_DENORMAL 0x02
  45. #define FE_DIVBYZERO 0x04
  46. #define FE_OVERFLOW 0x08
  47. #define FE_UNDERFLOW 0x10
  48. #define FE_INEXACT 0x20
  49. #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
  50. FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
  51. /* Rounding modes */
  52. #define FE_TONEAREST 0x0000
  53. #define FE_DOWNWARD 0x0400
  54. #define FE_UPWARD 0x0800
  55. #define FE_TOWARDZERO 0x0c00
  56. #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
  57. FE_UPWARD | FE_TOWARDZERO)
  58. /*
  59. * As compared to the x87 control word, the SSE unit's control word
  60. * has the rounding control bits offset by 3 and the exception mask
  61. * bits offset by 7.
  62. */
  63. #define _SSE_ROUND_SHIFT 3
  64. #define _SSE_EMASK_SHIFT 7
  65. __BEGIN_DECLS
  66. /* Default floating-point environment */
  67. extern const fenv_t __fe_dfl_env;
  68. #define FE_DFL_ENV (&__fe_dfl_env)
  69. #define __fldcw(__cw) __asm __volatile("fldcw %0" : : "m" (__cw))
  70. #define __fldenv(__env) __asm __volatile("fldenv %0" : : "m" (__env))
  71. #define __fldenvx(__env) __asm __volatile("fldenv %0" : : "m" (__env) \
  72. : "st", "st(1)", "st(2)", "st(3)", "st(4)", \
  73. "st(5)", "st(6)", "st(7)")
  74. #define __fnclex() __asm __volatile("fnclex")
  75. #define __fnstenv(__env) __asm __volatile("fnstenv %0" : "=m" (*(__env)))
  76. #define __fnstcw(__cw) __asm __volatile("fnstcw %0" : "=m" (*(__cw)))
  77. #define __fnstsw(__sw) __asm __volatile("fnstsw %0" : "=am" (*(__sw)))
  78. #define __fwait() __asm __volatile("fwait")
  79. #define __ldmxcsr(__csr) __asm __volatile("ldmxcsr %0" : : "m" (__csr))
  80. #define __stmxcsr(__csr) __asm __volatile("stmxcsr %0" : "=m" (*(__csr)))
  81. static __inline int
  82. feclearexcept(int __excepts)
  83. {
  84. fenv_t __env;
  85. if (__excepts == FE_ALL_EXCEPT) {
  86. __fnclex();
  87. } else {
  88. __fnstenv(&__env.__x87);
  89. __env.__x87.__status &= ~__excepts;
  90. __fldenv(__env.__x87);
  91. }
  92. __stmxcsr(&__env.__mxcsr);
  93. __env.__mxcsr &= ~__excepts;
  94. __ldmxcsr(__env.__mxcsr);
  95. return (0);
  96. }
  97. static __inline int
  98. fegetexceptflag(fexcept_t *__flagp, int __excepts)
  99. {
  100. __uint32_t __mxcsr;
  101. __uint16_t __status;
  102. __stmxcsr(&__mxcsr);
  103. __fnstsw(&__status);
  104. *__flagp = (__mxcsr | __status) & __excepts;
  105. return (0);
  106. }
  107. int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
  108. int feraiseexcept(int __excepts);
  109. static __inline int
  110. fetestexcept(int __excepts)
  111. {
  112. __uint32_t __mxcsr;
  113. __uint16_t __status;
  114. __stmxcsr(&__mxcsr);
  115. __fnstsw(&__status);
  116. return ((__status | __mxcsr) & __excepts);
  117. }
  118. static __inline int
  119. fegetround(void)
  120. {
  121. __uint16_t __control;
  122. /*
  123. * We assume that the x87 and the SSE unit agree on the
  124. * rounding mode. Reading the control word on the x87 turns
  125. * out to be about 5 times faster than reading it on the SSE
  126. * unit on an Opteron 244.
  127. */
  128. __fnstcw(&__control);
  129. return (__control & _ROUND_MASK);
  130. }
  131. static __inline int
  132. fesetround(int __round)
  133. {
  134. __uint32_t __mxcsr;
  135. __uint16_t __control;
  136. if (__round & ~_ROUND_MASK)
  137. return (-1);
  138. __fnstcw(&__control);
  139. __control &= ~_ROUND_MASK;
  140. __control |= __round;
  141. __fldcw(__control);
  142. __stmxcsr(&__mxcsr);
  143. __mxcsr &= ~(_ROUND_MASK << _SSE_ROUND_SHIFT);
  144. __mxcsr |= __round << _SSE_ROUND_SHIFT;
  145. __ldmxcsr(__mxcsr);
  146. return (0);
  147. }
  148. int fegetenv(fenv_t *__envp);
  149. int feholdexcept(fenv_t *__envp);
  150. static __inline int
  151. fesetenv(const fenv_t *__envp)
  152. {
  153. /*
  154. * XXX Using fldenvx() instead of fldenv() tells the compiler that this
  155. * instruction clobbers the i387 register stack. This happens because
  156. * we restore the tag word from the saved environment. Normally, this
  157. * would happen anyway and we wouldn't care, because the ABI allows
  158. * function calls to clobber the i387 regs. However, fesetenv() is
  159. * inlined, so we need to be more careful.
  160. */
  161. __fldenvx(__envp->__x87);
  162. __ldmxcsr(__envp->__mxcsr);
  163. return (0);
  164. }
  165. int feupdateenv(const fenv_t *__envp);
  166. #if __BSD_VISIBLE
  167. int feenableexcept(int __mask);
  168. int fedisableexcept(int __mask);
  169. static __inline int
  170. fegetexcept(void)
  171. {
  172. __uint16_t __control;
  173. /*
  174. * We assume that the masks for the x87 and the SSE unit are
  175. * the same.
  176. */
  177. __fnstcw(&__control);
  178. return (~__control & FE_ALL_EXCEPT);
  179. }
  180. #endif /* __BSD_VISIBLE */
  181. __END_DECLS
  182. #endif /* !_FENV_H_ */