fenv.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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/sparc64/fenv.h,v 1.4 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 __uint64_t fexcept_t;
  36. /* Exception flags */
  37. #define FE_INVALID 0x00000200
  38. #define FE_DIVBYZERO 0x00000040
  39. #define FE_OVERFLOW 0x00000100
  40. #define FE_UNDERFLOW 0x00000080
  41. #define FE_INEXACT 0x00000020
  42. #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | \
  43. FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
  44. /*
  45. * Rounding modes
  46. *
  47. * We can't just use the hardware bit values here, because that would
  48. * make FE_UPWARD and FE_DOWNWARD negative, which is not allowed.
  49. */
  50. #define FE_TONEAREST 0x0
  51. #define FE_TOWARDZERO 0x1
  52. #define FE_UPWARD 0x2
  53. #define FE_DOWNWARD 0x3
  54. #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
  55. FE_UPWARD | FE_TOWARDZERO)
  56. #define _ROUND_SHIFT 30
  57. __BEGIN_DECLS
  58. /* Default floating-point environment */
  59. extern const fenv_t __fe_dfl_env;
  60. #define FE_DFL_ENV (&__fe_dfl_env)
  61. /* We need to be able to map status flag positions to mask flag positions */
  62. #define _FPUSW_SHIFT 18
  63. #define _ENABLE_MASK (FE_ALL_EXCEPT << _FPUSW_SHIFT)
  64. #define __ldxfsr(__r) __asm __volatile("ldx %0, %%fsr" : : "m" (__r))
  65. #define __stxfsr(__r) __asm __volatile("stx %%fsr, %0" : "=m" (*(__r)))
  66. __fenv_static __inline int
  67. feclearexcept(int __excepts)
  68. {
  69. fexcept_t __r;
  70. __stxfsr(&__r);
  71. __r &= ~__excepts;
  72. __ldxfsr(__r);
  73. return (0);
  74. }
  75. __fenv_static inline int
  76. fegetexceptflag(fexcept_t *__flagp, int __excepts)
  77. {
  78. fexcept_t __r;
  79. __stxfsr(&__r);
  80. *__flagp = __r & __excepts;
  81. return (0);
  82. }
  83. __fenv_static inline int
  84. fesetexceptflag(const fexcept_t *__flagp, int __excepts)
  85. {
  86. fexcept_t __r;
  87. __stxfsr(&__r);
  88. __r &= ~__excepts;
  89. __r |= *__flagp & __excepts;
  90. __ldxfsr(__r);
  91. return (0);
  92. }
  93. /*
  94. * In contrast with the ia64 platform, it seems to be worthwhile to
  95. * inline this function on sparc64 even when the arguments are not
  96. * compile-time constants. Perhaps this depends on the register window.
  97. */
  98. __fenv_static inline int
  99. feraiseexcept(int __excepts)
  100. {
  101. volatile double d;
  102. /*
  103. * With a compiler that supports the FENV_ACCESS pragma
  104. * properly, simple expressions like '0.0 / 0.0' should
  105. * be sufficient to generate traps. Unfortunately, we
  106. * need to bring a volatile variable into the equation
  107. * to prevent incorrect optimizations.
  108. */
  109. if (__excepts & FE_INVALID) {
  110. d = 0.0;
  111. d = 0.0 / d;
  112. }
  113. if (__excepts & FE_DIVBYZERO) {
  114. d = 0.0;
  115. d = 1.0 / d;
  116. }
  117. if (__excepts & FE_OVERFLOW) {
  118. d = 0x1.ffp1023;
  119. d *= 2.0;
  120. }
  121. if (__excepts & FE_UNDERFLOW) {
  122. d = 0x1p-1022;
  123. d /= 0x1p1023;
  124. }
  125. if (__excepts & FE_INEXACT) {
  126. d = 0x1p-1022;
  127. d += 1.0;
  128. }
  129. return (0);
  130. }
  131. __fenv_static inline int
  132. fetestexcept(int __excepts)
  133. {
  134. fexcept_t __r;
  135. __stxfsr(&__r);
  136. return (__r & __excepts);
  137. }
  138. __fenv_static inline int
  139. fegetround(void)
  140. {
  141. fenv_t __r;
  142. __stxfsr(&__r);
  143. return ((__r >> _ROUND_SHIFT) & _ROUND_MASK);
  144. }
  145. __fenv_static inline int
  146. fesetround(int __round)
  147. {
  148. fenv_t __r;
  149. if (__round & ~_ROUND_MASK)
  150. return (-1);
  151. __stxfsr(&__r);
  152. __r &= ~(_ROUND_MASK << _ROUND_SHIFT);
  153. __r |= __round << _ROUND_SHIFT;
  154. __ldxfsr(__r);
  155. return (0);
  156. }
  157. __fenv_static inline int
  158. fegetenv(fenv_t *__envp)
  159. {
  160. __stxfsr(__envp);
  161. return (0);
  162. }
  163. __fenv_static inline int
  164. feholdexcept(fenv_t *__envp)
  165. {
  166. fenv_t __r;
  167. __stxfsr(&__r);
  168. *__envp = __r;
  169. __r &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
  170. __ldxfsr(__r);
  171. return (0);
  172. }
  173. __fenv_static inline int
  174. fesetenv(const fenv_t *__envp)
  175. {
  176. __ldxfsr(*__envp);
  177. return (0);
  178. }
  179. __fenv_static inline int
  180. feupdateenv(const fenv_t *__envp)
  181. {
  182. fexcept_t __r;
  183. __stxfsr(&__r);
  184. __ldxfsr(*__envp);
  185. feraiseexcept(__r & FE_ALL_EXCEPT);
  186. return (0);
  187. }
  188. #if __BSD_VISIBLE
  189. /* We currently provide no external definitions of the functions below. */
  190. static inline int
  191. feenableexcept(int __mask)
  192. {
  193. fenv_t __old_r, __new_r;
  194. __stxfsr(&__old_r);
  195. __new_r = __old_r | ((__mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
  196. __ldxfsr(__new_r);
  197. return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
  198. }
  199. static inline int
  200. fedisableexcept(int __mask)
  201. {
  202. fenv_t __old_r, __new_r;
  203. __stxfsr(&__old_r);
  204. __new_r = __old_r & ~((__mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
  205. __ldxfsr(__new_r);
  206. return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
  207. }
  208. static inline int
  209. fegetexcept(void)
  210. {
  211. fenv_t __r;
  212. __stxfsr(&__r);
  213. return ((__r & _ENABLE_MASK) >> _FPUSW_SHIFT);
  214. }
  215. #endif /* __BSD_VISIBLE */
  216. __END_DECLS
  217. #endif /* !_FENV_H_ */