openlibm_fenv_aarch64.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 <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 0x00000001
  38. #define FE_DIVBYZERO 0x00000002
  39. #define FE_OVERFLOW 0x00000004
  40. #define FE_UNDERFLOW 0x00000008
  41. #define FE_INEXACT 0x00000010
  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_UPWARD 0x1
  52. #define FE_DOWNWARD 0x2
  53. #define FE_TOWARDZERO 0x3
  54. #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
  55. FE_UPWARD | FE_TOWARDZERO)
  56. #define _ROUND_SHIFT 22
  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 8
  63. #define _ENABLE_MASK (FE_ALL_EXCEPT << _FPUSW_SHIFT)
  64. #define __mrs_fpcr(__r) __asm __volatile("mrs %0, fpcr" : "=r" (__r))
  65. #define __msr_fpcr(__r) __asm __volatile("msr fpcr, %0" : : "r" (__r))
  66. #define __mrs_fpsr(__r) __asm __volatile("mrs %0, fpsr" : "=r" (__r))
  67. #define __msr_fpsr(__r) __asm __volatile("msr fpsr, %0" : : "r" (__r))
  68. __fenv_static __inline int
  69. feclearexcept(int __excepts)
  70. {
  71. fexcept_t __r;
  72. __mrs_fpsr(__r);
  73. __r &= ~__excepts;
  74. __msr_fpsr(__r);
  75. return (0);
  76. }
  77. __fenv_static inline int
  78. fegetexceptflag(fexcept_t *__flagp, int __excepts)
  79. {
  80. fexcept_t __r;
  81. __mrs_fpsr(__r);
  82. *__flagp = __r & __excepts;
  83. return (0);
  84. }
  85. __fenv_static inline int
  86. fesetexceptflag(const fexcept_t *__flagp, int __excepts)
  87. {
  88. fexcept_t __r;
  89. __mrs_fpsr(__r);
  90. __r &= ~__excepts;
  91. __r |= *__flagp & __excepts;
  92. __msr_fpsr(__r);
  93. return (0);
  94. }
  95. __fenv_static inline int
  96. feraiseexcept(int __excepts)
  97. {
  98. fexcept_t __r;
  99. __mrs_fpsr(__r);
  100. __r |= __excepts;
  101. __msr_fpsr(__r);
  102. return (0);
  103. }
  104. __fenv_static inline int
  105. fetestexcept(int __excepts)
  106. {
  107. fexcept_t __r;
  108. __mrs_fpsr(__r);
  109. return (__r & __excepts);
  110. }
  111. __fenv_static inline int
  112. fegetround(void)
  113. {
  114. fenv_t __r;
  115. __mrs_fpcr(__r);
  116. return ((__r >> _ROUND_SHIFT) & _ROUND_MASK);
  117. }
  118. __fenv_static inline int
  119. fesetround(int __round)
  120. {
  121. fenv_t __r;
  122. if (__round & ~_ROUND_MASK)
  123. return (-1);
  124. __mrs_fpcr(__r);
  125. __r &= ~(_ROUND_MASK << _ROUND_SHIFT);
  126. __r |= __round << _ROUND_SHIFT;
  127. __msr_fpcr(__r);
  128. return (0);
  129. }
  130. __fenv_static inline int
  131. fegetenv(fenv_t *__envp)
  132. {
  133. fenv_t __r;
  134. __mrs_fpcr(__r);
  135. *__envp = __r & _ENABLE_MASK;
  136. __mrs_fpsr(__r);
  137. *__envp |= __r & (FE_ALL_EXCEPT | (_ROUND_MASK << _ROUND_SHIFT));
  138. return (0);
  139. }
  140. __fenv_static inline int
  141. feholdexcept(fenv_t *__envp)
  142. {
  143. fenv_t __r;
  144. __mrs_fpcr(__r);
  145. *__envp = __r & _ENABLE_MASK;
  146. __r &= ~(_ENABLE_MASK);
  147. __msr_fpcr(__r);
  148. __mrs_fpsr(__r);
  149. *__envp |= __r & (FE_ALL_EXCEPT | (_ROUND_MASK << _ROUND_SHIFT));
  150. __r &= ~(_ENABLE_MASK);
  151. __msr_fpsr(__r);
  152. return (0);
  153. }
  154. __fenv_static inline int
  155. fesetenv(const fenv_t *__envp)
  156. {
  157. __msr_fpcr((*__envp) & _ENABLE_MASK);
  158. __msr_fpsr((*__envp) & (FE_ALL_EXCEPT | (_ROUND_MASK << _ROUND_SHIFT)));
  159. return (0);
  160. }
  161. __fenv_static inline int
  162. feupdateenv(const fenv_t *__envp)
  163. {
  164. fexcept_t __r;
  165. __mrs_fpsr(__r);
  166. fesetenv(__envp);
  167. feraiseexcept(__r & FE_ALL_EXCEPT);
  168. return (0);
  169. }
  170. #if __BSD_VISIBLE
  171. /* We currently provide no external definitions of the functions below. */
  172. static inline int
  173. feenableexcept(int __mask)
  174. {
  175. fenv_t __old_r, __new_r;
  176. __mrs_fpcr(__old_r);
  177. __new_r = __old_r | ((__mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
  178. __msr_fpcr(__new_r);
  179. return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
  180. }
  181. static inline int
  182. fedisableexcept(int __mask)
  183. {
  184. fenv_t __old_r, __new_r;
  185. __mrs_fpcr(__old_r);
  186. __new_r = __old_r & ~((__mask & FE_ALL_EXCEPT) << _FPUSW_SHIFT);
  187. __msr_fpcr(__new_r);
  188. return ((__old_r >> _FPUSW_SHIFT) & FE_ALL_EXCEPT);
  189. }
  190. static inline int
  191. fegetexcept(void)
  192. {
  193. fenv_t __r;
  194. __mrs_fpcr(__r);
  195. return ((__r & _ENABLE_MASK) >> _FPUSW_SHIFT);
  196. }
  197. #endif /* __BSD_VISIBLE */
  198. __END_DECLS
  199. #endif /* !_FENV_H_ */