fenv.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*-
  2. * Copyright (c) 2004 David Schultz <[email protected]>
  3. * Copyright (c) 2013 Andrew Turner <[email protected]>
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. *
  27. * $FreeBSD$
  28. */
  29. //VBS
  30. #define __fenv_static
  31. #include <openlibm_fenv.h>
  32. #include <openlibm_acle_compat_arm.h>
  33. #if __ARM_ARCH >= 6
  34. #define FENV_ARMv6
  35. #endif
  36. /* When SOFTFP_ABI is defined we are using the softfp ABI. */
  37. #if defined(__VFP_FP__) && !defined(__ARM_PCS_VFP)
  38. #define SOFTFP_ABI
  39. #endif
  40. #ifndef FENV_MANGLE
  41. /*
  42. * Hopefully the system ID byte is immutable, so it's valid to use
  43. * this as a default environment.
  44. */
  45. const fenv_t __fe_dfl_env = 0;
  46. #endif
  47. /* If this is a non-mangled softfp version special processing is required */
  48. #if defined(FENV_MANGLE) || !defined(SOFTFP_ABI) || !defined(FENV_ARMv6)
  49. /*
  50. * The following macros map between the softfloat emulator's flags and
  51. * the hardware's FPSR. The hardware this file was written for doesn't
  52. * have rounding control bits, so we stick those in the system ID byte.
  53. */
  54. #ifndef __ARM_PCS_VFP
  55. #define __set_env(env, flags, mask, rnd) env = ((flags) \
  56. | (mask)<<_FPUSW_SHIFT \
  57. | (rnd) << 24)
  58. #define __env_flags(env) ((env) & FE_ALL_EXCEPT)
  59. #define __env_mask(env) (((env) >> _FPUSW_SHIFT) \
  60. & FE_ALL_EXCEPT)
  61. #define __env_round(env) (((env) >> 24) & _ROUND_MASK)
  62. #include <openlibm_fenv_softfloat.h>
  63. #endif
  64. #ifdef __GNUC_GNU_INLINE__
  65. #error "This file must be compiled with C99 'inline' semantics"
  66. #endif
  67. extern inline DLLEXPORT int feclearexcept(int __excepts);
  68. extern inline DLLEXPORT int fegetexceptflag(fexcept_t *__flagp, int __excepts);
  69. extern inline DLLEXPORT int fesetexceptflag(const fexcept_t *__flagp, int __excepts);
  70. extern inline DLLEXPORT int feraiseexcept(int __excepts);
  71. extern inline DLLEXPORT int fetestexcept(int __excepts);
  72. extern inline DLLEXPORT int fegetround(void);
  73. extern inline DLLEXPORT int fesetround(int __round);
  74. extern inline DLLEXPORT int fegetenv(fenv_t *__envp);
  75. extern inline DLLEXPORT int feholdexcept(fenv_t *__envp);
  76. extern inline DLLEXPORT int fesetenv(const fenv_t *__envp);
  77. extern inline DLLEXPORT int feupdateenv(const fenv_t *__envp);
  78. extern inline DLLEXPORT int feenableexcept(int __mask);
  79. extern inline DLLEXPORT int fedisableexcept(int __mask);
  80. extern inline DLLEXPORT int fegetexcept(void);
  81. #else /* !FENV_MANGLE && SOFTFP_ABI */
  82. /* Set by libc when the VFP unit is enabled */
  83. extern int _libc_arm_fpu_present;
  84. int __softfp_feclearexcept(int __excepts);
  85. int __softfp_fegetexceptflag(fexcept_t *__flagp, int __excepts);
  86. int __softfp_fesetexceptflag(const fexcept_t *__flagp, int __excepts);
  87. int __softfp_feraiseexcept(int __excepts);
  88. int __softfp_fetestexcept(int __excepts);
  89. int __softfp_fegetround(void);
  90. int __softfp_fesetround(int __round);
  91. int __softfp_fegetenv(fenv_t *__envp);
  92. int __softfp_feholdexcept(fenv_t *__envp);
  93. int __softfp_fesetenv(const fenv_t *__envp);
  94. int __softfp_feupdateenv(const fenv_t *__envp);
  95. int __softfp_feenableexcept(int __mask);
  96. int __softfp_fedisableexcept(int __mask);
  97. int __softfp_fegetexcept(void);
  98. int __vfp_feclearexcept(int __excepts);
  99. int __vfp_fegetexceptflag(fexcept_t *__flagp, int __excepts);
  100. int __vfp_fesetexceptflag(const fexcept_t *__flagp, int __excepts);
  101. int __vfp_feraiseexcept(int __excepts);
  102. int __vfp_fetestexcept(int __excepts);
  103. int __vfp_fegetround(void);
  104. int __vfp_fesetround(int __round);
  105. int __vfp_fegetenv(fenv_t *__envp);
  106. int __vfp_feholdexcept(fenv_t *__envp);
  107. int __vfp_fesetenv(const fenv_t *__envp);
  108. int __vfp_feupdateenv(const fenv_t *__envp);
  109. int __vfp_feenableexcept(int __mask);
  110. int __vfp_fedisableexcept(int __mask);
  111. int __vfp_fegetexcept(void);
  112. static int
  113. __softfp_round_to_vfp(int round)
  114. {
  115. switch (round) {
  116. case FE_TONEAREST:
  117. default:
  118. return VFP_FE_TONEAREST;
  119. case FE_TOWARDZERO:
  120. return VFP_FE_TOWARDZERO;
  121. case FE_UPWARD:
  122. return VFP_FE_UPWARD;
  123. case FE_DOWNWARD:
  124. return VFP_FE_DOWNWARD;
  125. }
  126. }
  127. static int
  128. __softfp_round_from_vfp(int round)
  129. {
  130. switch (round) {
  131. case VFP_FE_TONEAREST:
  132. default:
  133. return FE_TONEAREST;
  134. case VFP_FE_TOWARDZERO:
  135. return FE_TOWARDZERO;
  136. case VFP_FE_UPWARD:
  137. return FE_UPWARD;
  138. case VFP_FE_DOWNWARD:
  139. return FE_DOWNWARD;
  140. }
  141. }
  142. DLLEXPORT
  143. int feclearexcept(int __excepts)
  144. {
  145. if (_libc_arm_fpu_present)
  146. __vfp_feclearexcept(__excepts);
  147. __softfp_feclearexcept(__excepts);
  148. return (0);
  149. }
  150. DLLEXPORT
  151. int fegetexceptflag(fexcept_t *__flagp, int __excepts)
  152. {
  153. fexcept_t __vfp_flagp;
  154. __vfp_flagp = 0;
  155. if (_libc_arm_fpu_present)
  156. __vfp_fegetexceptflag(&__vfp_flagp, __excepts);
  157. __softfp_fegetexceptflag(__flagp, __excepts);
  158. *__flagp |= __vfp_flagp;
  159. return (0);
  160. }
  161. DLLEXPORT
  162. int fesetexceptflag(const fexcept_t *__flagp, int __excepts)
  163. {
  164. if (_libc_arm_fpu_present)
  165. __vfp_fesetexceptflag(__flagp, __excepts);
  166. __softfp_fesetexceptflag(__flagp, __excepts);
  167. return (0);
  168. }
  169. DLLEXPORT
  170. int feraiseexcept(int __excepts)
  171. {
  172. if (_libc_arm_fpu_present)
  173. __vfp_feraiseexcept(__excepts);
  174. __softfp_feraiseexcept(__excepts);
  175. return (0);
  176. }
  177. DLLEXPORT
  178. int fetestexcept(int __excepts)
  179. {
  180. int __got_excepts;
  181. __got_excepts = 0;
  182. if (_libc_arm_fpu_present)
  183. __got_excepts = __vfp_fetestexcept(__excepts);
  184. __got_excepts |= __softfp_fetestexcept(__excepts);
  185. return (__got_excepts);
  186. }
  187. DLLEXPORT
  188. int fegetround(void)
  189. {
  190. if (_libc_arm_fpu_present)
  191. return __softfp_round_from_vfp(__vfp_fegetround());
  192. return __softfp_fegetround();
  193. }
  194. DLLEXPORT
  195. int fesetround(int __round)
  196. {
  197. if (_libc_arm_fpu_present)
  198. __vfp_fesetround(__softfp_round_to_vfp(__round));
  199. __softfp_fesetround(__round);
  200. return (0);
  201. }
  202. DLLEXPORT
  203. int fegetenv(fenv_t *__envp)
  204. {
  205. fenv_t __vfp_envp;
  206. __vfp_envp = 0;
  207. if (_libc_arm_fpu_present)
  208. __vfp_fegetenv(&__vfp_envp);
  209. __softfp_fegetenv(__envp);
  210. *__envp |= __vfp_envp;
  211. return (0);
  212. }
  213. DLLEXPORT
  214. int feholdexcept(fenv_t *__envp)
  215. {
  216. fenv_t __vfp_envp;
  217. __vfp_envp = 0;
  218. if (_libc_arm_fpu_present)
  219. __vfp_feholdexcept(&__vfp_envp);
  220. __softfp_feholdexcept(__envp);
  221. *__envp |= __vfp_envp;
  222. return (0);
  223. }
  224. DLLEXPORT
  225. int fesetenv(const fenv_t *__envp)
  226. {
  227. if (_libc_arm_fpu_present)
  228. __vfp_fesetenv(__envp);
  229. __softfp_fesetenv(__envp);
  230. return (0);
  231. }
  232. DLLEXPORT
  233. int feupdateenv(const fenv_t *__envp)
  234. {
  235. if (_libc_arm_fpu_present)
  236. __vfp_feupdateenv(__envp);
  237. __softfp_feupdateenv(__envp);
  238. return (0);
  239. }
  240. DLLEXPORT
  241. int feenableexcept(int __mask)
  242. {
  243. int __unmasked;
  244. __unmasked = 0;
  245. if (_libc_arm_fpu_present)
  246. __unmasked = __vfp_feenableexcept(__mask);
  247. __unmasked |= __softfp_feenableexcept(__mask);
  248. return (__unmasked);
  249. }
  250. DLLEXPORT
  251. int fedisableexcept(int __mask)
  252. {
  253. int __unmasked;
  254. __unmasked = 0;
  255. if (_libc_arm_fpu_present)
  256. __unmasked = __vfp_fedisableexcept(__mask);
  257. __unmasked |= __softfp_fedisableexcept(__mask);
  258. return (__unmasked);
  259. }
  260. DLLEXPORT
  261. int fegetexcept(void)
  262. {
  263. int __unmasked;
  264. __unmasked = 0;
  265. if (_libc_arm_fpu_present)
  266. __unmasked = __vfp_fegetexcept();
  267. __unmasked |= __softfp_fegetexcept();
  268. return (__unmasked);
  269. }
  270. #endif