openlibm_fenv_mips.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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$
  27. */
  28. #ifndef _FENV_H_
  29. #define _FENV_H_
  30. #include <stdint.h>
  31. #include "cdefs-compat.h"
  32. #ifndef __fenv_static
  33. #define __fenv_static static
  34. #endif
  35. typedef uint32_t fenv_t;
  36. typedef uint32_t fexcept_t;
  37. /* Exception flags */
  38. #ifdef __mips_soft_float
  39. #define _FPUSW_SHIFT 16
  40. #define FE_INVALID 0x0001
  41. #define FE_DIVBYZERO 0x0002
  42. #define FE_OVERFLOW 0x0004
  43. #define FE_UNDERFLOW 0x0008
  44. #define FE_INEXACT 0x0010
  45. #else
  46. #define _FCSR_CAUSE_SHIFT 10
  47. #define FE_INVALID 0x0040
  48. #define FE_DIVBYZERO 0x0020
  49. #define FE_OVERFLOW 0x0010
  50. #define FE_UNDERFLOW 0x0008
  51. #define FE_INEXACT 0x0004
  52. #endif
  53. #define FE_ALL_EXCEPT (FE_DIVBYZERO | FE_INEXACT | \
  54. FE_INVALID | FE_OVERFLOW | FE_UNDERFLOW)
  55. /* Rounding modes */
  56. #define FE_TONEAREST 0x0000
  57. #define FE_TOWARDZERO 0x0001
  58. #define FE_UPWARD 0x0002
  59. #define FE_DOWNWARD 0x0003
  60. #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
  61. FE_UPWARD | FE_TOWARDZERO)
  62. __BEGIN_DECLS
  63. /* Default floating-point environment */
  64. extern const fenv_t __fe_dfl_env;
  65. #define FE_DFL_ENV (&__fe_dfl_env)
  66. /* We need to be able to map status flag positions to mask flag positions */
  67. #define _ENABLE_SHIFT 5
  68. #define _ENABLE_MASK (FE_ALL_EXCEPT << _ENABLE_SHIFT)
  69. #ifndef __mips_soft_float
  70. #define __cfc1(__fcsr) __asm __volatile("cfc1 %0, $31" : "=r" (__fcsr))
  71. #define __ctc1(__fcsr) __asm __volatile("ctc1 %0, $31" :: "r" (__fcsr))
  72. #endif
  73. #ifdef __mips_soft_float
  74. int feclearexcept(int __excepts);
  75. int fegetexceptflag(fexcept_t *__flagp, int __excepts);
  76. int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
  77. int feraiseexcept(int __excepts);
  78. int fetestexcept(int __excepts);
  79. int fegetround(void);
  80. int fesetround(int __round);
  81. int fegetenv(fenv_t *__envp);
  82. int feholdexcept(fenv_t *__envp);
  83. int fesetenv(const fenv_t *__envp);
  84. int feupdateenv(const fenv_t *__envp);
  85. #else
  86. __fenv_static inline int
  87. feclearexcept(int __excepts)
  88. {
  89. fexcept_t fcsr;
  90. __excepts &= FE_ALL_EXCEPT;
  91. __cfc1(fcsr);
  92. fcsr &= ~(__excepts | (__excepts << _FCSR_CAUSE_SHIFT));
  93. __ctc1(fcsr);
  94. return (0);
  95. }
  96. __fenv_static inline int
  97. fegetexceptflag(fexcept_t *__flagp, int __excepts)
  98. {
  99. fexcept_t fcsr;
  100. __excepts &= FE_ALL_EXCEPT;
  101. __cfc1(fcsr);
  102. *__flagp = fcsr & __excepts;
  103. return (0);
  104. }
  105. __fenv_static inline int
  106. fesetexceptflag(const fexcept_t *__flagp, int __excepts)
  107. {
  108. fexcept_t fcsr;
  109. __excepts &= FE_ALL_EXCEPT;
  110. __cfc1(fcsr);
  111. fcsr &= ~__excepts;
  112. fcsr |= *__flagp & __excepts;
  113. __ctc1(fcsr);
  114. return (0);
  115. }
  116. __fenv_static inline int
  117. feraiseexcept(int __excepts)
  118. {
  119. fexcept_t fcsr;
  120. __excepts &= FE_ALL_EXCEPT;
  121. __cfc1(fcsr);
  122. fcsr |= __excepts | (__excepts << _FCSR_CAUSE_SHIFT);
  123. __ctc1(fcsr);
  124. return (0);
  125. }
  126. __fenv_static inline int
  127. fetestexcept(int __excepts)
  128. {
  129. fexcept_t fcsr;
  130. __excepts &= FE_ALL_EXCEPT;
  131. __cfc1(fcsr);
  132. return (fcsr & __excepts);
  133. }
  134. __fenv_static inline int
  135. fegetround(void)
  136. {
  137. fexcept_t fcsr;
  138. __cfc1(fcsr);
  139. return (fcsr & _ROUND_MASK);
  140. }
  141. __fenv_static inline int
  142. fesetround(int __round)
  143. {
  144. fexcept_t fcsr;
  145. if (__round & ~_ROUND_MASK)
  146. return (-1);
  147. __cfc1(fcsr);
  148. fcsr &= ~_ROUND_MASK;
  149. fcsr |= __round;
  150. __ctc1(fcsr);
  151. return (0);
  152. }
  153. __fenv_static inline int
  154. fegetenv(fenv_t *__envp)
  155. {
  156. __cfc1(*__envp);
  157. return (0);
  158. }
  159. __fenv_static inline int
  160. feholdexcept(fenv_t *__envp)
  161. {
  162. fexcept_t fcsr;
  163. __cfc1(fcsr);
  164. *__envp = fcsr;
  165. fcsr &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
  166. __ctc1(fcsr);
  167. return (0);
  168. }
  169. __fenv_static inline int
  170. fesetenv(const fenv_t *__envp)
  171. {
  172. __ctc1(*__envp);
  173. return (0);
  174. }
  175. __fenv_static inline int
  176. feupdateenv(const fenv_t *__envp)
  177. {
  178. fexcept_t fcsr;
  179. __cfc1(fcsr);
  180. fesetenv(__envp);
  181. feraiseexcept(fcsr);
  182. return (0);
  183. }
  184. #endif /* !__mips_soft_float */
  185. #if __BSD_VISIBLE
  186. /* We currently provide no external definitions of the functions below. */
  187. #ifdef __mips_soft_float
  188. int feenableexcept(int __mask);
  189. int fedisableexcept(int __mask);
  190. int fegetexcept(void);
  191. #else
  192. static inline int
  193. feenableexcept(int __mask)
  194. {
  195. fenv_t __old_fcsr, __new_fcsr;
  196. __cfc1(__old_fcsr);
  197. __new_fcsr = __old_fcsr | (__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT;
  198. __ctc1(__new_fcsr);
  199. return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
  200. }
  201. static inline int
  202. fedisableexcept(int __mask)
  203. {
  204. fenv_t __old_fcsr, __new_fcsr;
  205. __cfc1(__old_fcsr);
  206. __new_fcsr = __old_fcsr & ~((__mask & FE_ALL_EXCEPT) << _ENABLE_SHIFT);
  207. __ctc1(__new_fcsr);
  208. return ((__old_fcsr >> _ENABLE_SHIFT) & FE_ALL_EXCEPT);
  209. }
  210. static inline int
  211. fegetexcept(void)
  212. {
  213. fexcept_t fcsr;
  214. __cfc1(fcsr);
  215. return ((fcsr & _ENABLE_MASK) >> _ENABLE_SHIFT);
  216. }
  217. #endif /* !__mips_soft_float */
  218. #endif /* __BSD_VISIBLE */
  219. __END_DECLS
  220. #endif /* !_FENV_H_ */