fenv.h 5.9 KB

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