openlibm_fenv_arm.h 6.0 KB

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