fenv.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/ia64/fenv.h,v 1.5 2011/10/10 15:43:09 das Exp $
  27. */
  28. #ifndef _FENV_H_
  29. #define _FENV_H_
  30. #include <sys/_types.h>
  31. #ifndef __fenv_static
  32. #define __fenv_static static
  33. #endif
  34. typedef __uint64_t fenv_t;
  35. typedef __uint16_t fexcept_t;
  36. /* Exception flags */
  37. #define FE_INVALID 0x01
  38. #define FE_DENORMAL 0x02
  39. #define FE_DIVBYZERO 0x04
  40. #define FE_OVERFLOW 0x08
  41. #define FE_UNDERFLOW 0x10
  42. #define FE_INEXACT 0x20
  43. #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_DENORMAL | FE_INEXACT | \
  44. FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
  45. /* Rounding modes */
  46. #define FE_TONEAREST 0x0000
  47. #define FE_DOWNWARD 0x0400
  48. #define FE_UPWARD 0x0800
  49. #define FE_TOWARDZERO 0x0c00
  50. #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
  51. FE_UPWARD | FE_TOWARDZERO)
  52. __BEGIN_DECLS
  53. /* Default floating-point environment */
  54. extern const fenv_t __fe_dfl_env;
  55. #define FE_DFL_ENV (&__fe_dfl_env)
  56. #define _FPUSW_SHIFT 13
  57. #define __stfpsr(__r) __asm __volatile("mov %0=ar.fpsr" : "=r" (*(__r)))
  58. #define __ldfpsr(__r) __asm __volatile("mov ar.fpsr=%0;;" : : "r" (__r))
  59. __fenv_static inline int
  60. feclearexcept(int __excepts)
  61. {
  62. fenv_t __fpsr;
  63. __stfpsr(&__fpsr);
  64. __fpsr &= ~((fenv_t)__excepts << _FPUSW_SHIFT);
  65. __ldfpsr(__fpsr);
  66. return (0);
  67. }
  68. __fenv_static inline int
  69. fegetexceptflag(fexcept_t *__flagp, int __excepts)
  70. {
  71. fenv_t __fpsr;
  72. __stfpsr(&__fpsr);
  73. *__flagp = (fexcept_t)(__fpsr >> _FPUSW_SHIFT) & __excepts;
  74. return (0);
  75. }
  76. __fenv_static inline int
  77. fesetexceptflag(const fexcept_t *__flagp, int __excepts)
  78. {
  79. fenv_t __fpsr;
  80. __stfpsr(&__fpsr);
  81. __fpsr &= ~((fenv_t)__excepts << _FPUSW_SHIFT);
  82. __fpsr |= (fenv_t)(__excepts & *__flagp) << _FPUSW_SHIFT;
  83. __ldfpsr(__fpsr);
  84. return (0);
  85. }
  86. /*
  87. * It is worthwhile to use the inline version of this function iff it
  88. * is called with arguments that are compile-time constants (due to
  89. * dead code elimination). Unfortunately, gcc isn't smart enough to
  90. * figure this out automatically, and there's no way to tell it.
  91. * We assume that constant arguments will be the common case.
  92. */
  93. __fenv_static inline int
  94. feraiseexcept(int __excepts)
  95. {
  96. volatile double d;
  97. /*
  98. * With a compiler that supports the FENV_ACCESS pragma
  99. * properly, simple expressions like '0.0 / 0.0' should
  100. * be sufficient to generate traps. Unfortunately, we
  101. * need to bring a volatile variable into the equation
  102. * to prevent incorrect optimizations.
  103. */
  104. if (__excepts & FE_INVALID) {
  105. d = 0.0;
  106. d = 0.0 / d;
  107. }
  108. if (__excepts & FE_DIVBYZERO) {
  109. d = 0.0;
  110. d = 1.0 / d;
  111. }
  112. if (__excepts & FE_OVERFLOW) {
  113. d = 0x1.ffp1023;
  114. d *= 2.0;
  115. }
  116. if (__excepts & FE_UNDERFLOW) {
  117. d = 0x1p-1022;
  118. d /= 0x1p1023;
  119. }
  120. if (__excepts & FE_INEXACT) {
  121. d = 0x1p-1022;
  122. d += 1.0;
  123. }
  124. return (0);
  125. }
  126. __fenv_static inline int
  127. fetestexcept(int __excepts)
  128. {
  129. fenv_t __fpsr;
  130. __stfpsr(&__fpsr);
  131. return ((__fpsr >> _FPUSW_SHIFT) & __excepts);
  132. }
  133. __fenv_static inline int
  134. fegetround(void)
  135. {
  136. fenv_t __fpsr;
  137. __stfpsr(&__fpsr);
  138. return (__fpsr & _ROUND_MASK);
  139. }
  140. __fenv_static inline int
  141. fesetround(int __round)
  142. {
  143. fenv_t __fpsr;
  144. if (__round & ~_ROUND_MASK)
  145. return (-1);
  146. __stfpsr(&__fpsr);
  147. __fpsr &= ~_ROUND_MASK;
  148. __fpsr |= __round;
  149. __ldfpsr(__fpsr);
  150. return (0);
  151. }
  152. __fenv_static inline int
  153. fegetenv(fenv_t *__envp)
  154. {
  155. __stfpsr(__envp);
  156. return (0);
  157. }
  158. __fenv_static inline int
  159. feholdexcept(fenv_t *__envp)
  160. {
  161. fenv_t __fpsr;
  162. __stfpsr(&__fpsr);
  163. *__envp = __fpsr;
  164. __fpsr &= ~((fenv_t)FE_ALL_EXCEPT << _FPUSW_SHIFT);
  165. __fpsr |= FE_ALL_EXCEPT;
  166. __ldfpsr(__fpsr);
  167. return (0);
  168. }
  169. __fenv_static inline int
  170. fesetenv(const fenv_t *__envp)
  171. {
  172. __ldfpsr(*__envp);
  173. return (0);
  174. }
  175. int feupdateenv(const fenv_t *__envp);
  176. #if __BSD_VISIBLE
  177. /* We currently provide no external definitions of the functions below. */
  178. static inline int
  179. feenableexcept(int __mask)
  180. {
  181. fenv_t __newfpsr, __oldfpsr;
  182. __stfpsr(&__oldfpsr);
  183. __newfpsr = __oldfpsr & ~(__mask & FE_ALL_EXCEPT);
  184. __ldfpsr(__newfpsr);
  185. return (~__oldfpsr & FE_ALL_EXCEPT);
  186. }
  187. static inline int
  188. fedisableexcept(int __mask)
  189. {
  190. fenv_t __newfpsr, __oldfpsr;
  191. __stfpsr(&__oldfpsr);
  192. __newfpsr = __oldfpsr | (__mask & FE_ALL_EXCEPT);
  193. __ldfpsr(__newfpsr);
  194. return (~__oldfpsr & FE_ALL_EXCEPT);
  195. }
  196. static inline int
  197. fegetexcept(void)
  198. {
  199. fenv_t __fpsr;
  200. __stfpsr(&__fpsr);
  201. return (~__fpsr & FE_ALL_EXCEPT);
  202. }
  203. #endif /* __BSD_VISIBLE */
  204. __END_DECLS
  205. #endif /* !_FENV_H_ */