openlibm_fenv_s390.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*-
  2. * Copyright (c) 2016 Dan Horák <dan[at]danny.cz>
  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 __uint32_t fenv_t;
  35. typedef __uint32_t fexcept_t;
  36. /* Exception flags */
  37. #define FE_INEXACT 0x080000
  38. #define FE_UNDERFLOW 0x100000
  39. #define FE_OVERFLOW 0x200000
  40. #define FE_DIVBYZERO 0x400000
  41. #define FE_INVALID 0x800000 /* all types of invalid FP ops */
  42. #define FE_ALL_EXCEPT (FE_INVALID | FE_DIVBYZERO | FE_INEXACT | FE_OVERFLOW | FE_UNDERFLOW)
  43. /* Rounding modes */
  44. #define FE_TONEAREST 0x0000
  45. #define FE_TOWARDZERO 0x0001
  46. #define FE_UPWARD 0x0002
  47. #define FE_DOWNWARD 0x0003
  48. #define _ROUND_MASK (FE_TONEAREST | FE_DOWNWARD | \
  49. FE_UPWARD | FE_TOWARDZERO)
  50. __BEGIN_DECLS
  51. /* Default floating-point environment */
  52. extern const fenv_t __fe_dfl_env;
  53. #define FE_DFL_ENV (&__fe_dfl_env)
  54. /* We need to be able to map status flag positions to mask flag positions */
  55. #define _FPC_EXC_MASK_SHIFT 8
  56. #define _ENABLE_MASK ((FE_DIVBYZERO | FE_INEXACT | FE_INVALID | \
  57. FE_OVERFLOW | FE_UNDERFLOW) << _FPC_EXC_MASK_SHIFT)
  58. /* Macros for accessing the hardware control word. */
  59. #define _FPU_GETCW(cw) __asm__ __volatile__ ("efpc %0,0" : "=d" (cw))
  60. #define _FPU_SETCW(cw) __asm__ __volatile__ ("sfpc %0,0" : : "d" (cw))
  61. __fenv_static inline int
  62. feclearexcept(int __excepts)
  63. {
  64. fexcept_t __r;
  65. if (__excepts & FE_INVALID)
  66. __excepts |= FE_ALL_EXCEPT;
  67. _FPU_GETCW(__r);
  68. __r &= ~__excepts;
  69. _FPU_SETCW(__r);
  70. return (0);
  71. }
  72. __fenv_static inline int
  73. fegetexceptflag(fexcept_t *__flagp, int __excepts)
  74. {
  75. fexcept_t __r;
  76. _FPU_GETCW(__r);
  77. *__flagp = __r & __excepts;
  78. return (0);
  79. }
  80. __fenv_static inline int
  81. fesetexceptflag(const fexcept_t *__flagp, int __excepts)
  82. {
  83. fexcept_t __r;
  84. if (__excepts & FE_INVALID)
  85. __excepts |= FE_ALL_EXCEPT;
  86. _FPU_GETCW(__r);
  87. __r &= ~__excepts;
  88. __r |= *__flagp & __excepts;
  89. _FPU_SETCW(__r);
  90. return (0);
  91. }
  92. __fenv_static inline int
  93. feraiseexcept(int __excepts)
  94. {
  95. fexcept_t __r;
  96. _FPU_GETCW(__r);
  97. __r |= __excepts;
  98. _FPU_SETCW(__r);
  99. return (0);
  100. }
  101. __fenv_static inline int
  102. fetestexcept(int __excepts)
  103. {
  104. fexcept_t __r;
  105. _FPU_GETCW(__r);
  106. return (__r & __excepts);
  107. }
  108. __fenv_static inline int
  109. fegetround(void)
  110. {
  111. fexcept_t __r;
  112. _FPU_GETCW(__r);
  113. return (__r & _ROUND_MASK);
  114. }
  115. __fenv_static inline int
  116. fesetround(int __round)
  117. {
  118. fexcept_t __r;
  119. if (__round & ~_ROUND_MASK)
  120. return (-1);
  121. _FPU_GETCW(__r);
  122. __r &= ~_ROUND_MASK;
  123. __r |= __round;
  124. _FPU_SETCW(__r);
  125. return (0);
  126. }
  127. __fenv_static inline int
  128. fegetenv(fenv_t *__envp)
  129. {
  130. _FPU_GETCW(*__envp);
  131. return (0);
  132. }
  133. __fenv_static inline int
  134. feholdexcept(fenv_t *__envp)
  135. {
  136. fexcept_t __r;
  137. _FPU_GETCW(__r);
  138. *__envp = __r;
  139. __r &= ~(FE_ALL_EXCEPT | _ENABLE_MASK);
  140. _FPU_SETCW(__r);
  141. return (0);
  142. }
  143. __fenv_static inline int
  144. fesetenv(const fenv_t *__envp)
  145. {
  146. _FPU_SETCW(*__envp);
  147. return (0);
  148. }
  149. __fenv_static inline int
  150. feupdateenv(const fenv_t *__envp)
  151. {
  152. fexcept_t __r;
  153. _FPU_GETCW(__r);
  154. __r &= FE_ALL_EXCEPT;
  155. __r |= *__envp;
  156. _FPU_SETCW(__r);
  157. return (0);
  158. }
  159. #if __BSD_VISIBLE
  160. /* We currently provide no external definitions of the functions below. */
  161. static inline int
  162. feenableexcept(int __mask)
  163. {
  164. fenv_t __r;
  165. fenv_t __oldmask;
  166. _FPU_GETCW(__r);
  167. __oldmask = __r;
  168. __r |= (__mask & FE_ALL_EXCEPT) << _FPC_EXC_MASK_SHIFT;
  169. _FPU_SETCW(__r);
  170. return ((__oldmask & _ENABLE_MASK) >> _FPC_EXC_MASK_SHIFT);
  171. }
  172. static inline int
  173. fedisableexcept(int __mask)
  174. {
  175. fenv_t __r;
  176. fenv_t __oldmask;
  177. _FPU_GETCW(__r);
  178. __oldmask = __r;
  179. __r &= ~((__mask & FE_ALL_EXCEPT) << _FPC_EXC_MASK_SHIFT);
  180. _FPU_SETCW(__r);
  181. return ((__oldmask & _ENABLE_MASK) >> _FPC_EXC_MASK_SHIFT);
  182. }
  183. static inline int
  184. fegetexcept(void)
  185. {
  186. fexcept_t __r;
  187. _FPU_GETCW(__r);
  188. return (__r & (_ENABLE_MASK >> _FPC_EXC_MASK_SHIFT));
  189. }
  190. #endif /* __BSD_VISIBLE */
  191. __END_DECLS
  192. #endif /* !_FENV_H_ */