openlibm_fenv_amd64.h 6.0 KB

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