fenv_private.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /* Private floating point rounding and exceptions handling. C-SKY version.
  2. Copyright (C) 2018-2025 Free Software Foundation, Inc.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library. If not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #ifndef CSKY_FENV_PRIVATE_H
  15. #define CSKY_FENV_PRIVATE_H 1
  16. #include <fenv.h>
  17. #include <fpu_control.h>
  18. #include "fenv_libc.h"
  19. static __always_inline void
  20. libc_feholdexcept_vfp (fenv_t *envp)
  21. {
  22. fpu_control_t fpsr, fpcr;
  23. _FPU_GETCW (fpcr);
  24. envp->__fpcr = fpcr;
  25. _FPU_GETFPSR (fpsr);
  26. envp->__fpsr = fpsr;
  27. /* Now set all exceptions to non-stop. */
  28. fpcr &= ~FE_ALL_EXCEPT;
  29. /* And clear all exception flags. */
  30. fpsr &= ~(FE_ALL_EXCEPT << CAUSE_SHIFT);
  31. _FPU_SETFPSR (fpsr);
  32. _FPU_SETCW (fpcr);
  33. }
  34. static __always_inline void
  35. libc_fesetround_vfp (int round)
  36. {
  37. fpu_control_t fpcr;
  38. _FPU_GETCW (fpcr);
  39. /* Set new rounding mode if different. */
  40. if (unlikely ((fpcr & FE_DOWNWARD) != round))
  41. _FPU_SETCW ((fpcr & ~FE_DOWNWARD) | round);
  42. }
  43. static __always_inline void
  44. libc_feholdexcept_setround_vfp (fenv_t *envp, int round)
  45. {
  46. fpu_control_t fpsr, fpcr;
  47. _FPU_GETCW (fpcr);
  48. envp->__fpcr = fpcr;
  49. _FPU_GETFPSR (fpsr);
  50. envp->__fpsr = fpsr;
  51. /* Clear exception flags, set all exceptions to non-stop,
  52. and set new rounding mode. */
  53. fpsr &= ~(FE_ALL_EXCEPT << CAUSE_SHIFT);
  54. _FPU_SETFPSR (fpsr);
  55. fpcr &= ~(FE_ALL_EXCEPT | FE_DOWNWARD);
  56. _FPU_SETCW (fpcr | round);
  57. }
  58. static __always_inline void
  59. libc_feholdsetround_vfp (fenv_t *envp, int round)
  60. {
  61. fpu_control_t fpcr;
  62. _FPU_GETCW (fpcr);
  63. envp->__fpcr = fpcr;
  64. /* Set new rounding mode if different. */
  65. if (unlikely ((fpcr & FE_DOWNWARD) != round))
  66. _FPU_SETCW ((fpcr & ~FE_DOWNWARD) | round);
  67. }
  68. static __always_inline void
  69. libc_feresetround_vfp (fenv_t *envp)
  70. {
  71. fpu_control_t fpcr, round;
  72. _FPU_GETCW (fpcr);
  73. /* Check whether rounding modes are different. */
  74. round = (envp->__fpcr ^ fpcr) & FE_DOWNWARD;
  75. /* Restore the rounding mode if it was changed. */
  76. if (unlikely (round != 0))
  77. _FPU_SETCW (fpcr ^ round);
  78. }
  79. static __always_inline int
  80. libc_fetestexcept_vfp (int ex)
  81. {
  82. fpu_control_t fpsr;
  83. _FPU_GETFPSR (fpsr);
  84. fpsr = fpsr >> CAUSE_SHIFT;
  85. return fpsr & ex & FE_ALL_EXCEPT;
  86. }
  87. static __always_inline void
  88. libc_fesetenv_vfp (const fenv_t *envp)
  89. {
  90. fpu_control_t fpcr, fpsr, new_fpcr, new_fpsr;
  91. _FPU_GETCW (fpcr);
  92. _FPU_GETFPSR (fpsr);
  93. new_fpcr = envp->__fpcr;
  94. new_fpsr = envp->__fpsr;
  95. if (unlikely (fpsr ^ new_fpsr) != 0)
  96. _FPU_SETFPSR (new_fpsr);
  97. if (unlikely (fpcr ^ new_fpcr) != 0)
  98. _FPU_SETCW (new_fpcr);
  99. }
  100. static __always_inline int
  101. libc_feupdateenv_test_vfp (const fenv_t *envp, int ex)
  102. {
  103. fpu_control_t fpcr, fpsr, new_fpcr, new_fpsr, excepts;
  104. _FPU_GETCW (fpcr);
  105. _FPU_GETFPSR (fpsr);
  106. /* Merge current exception flags with the saved fenv. */
  107. excepts = (fpsr >> CAUSE_SHIFT) & FE_ALL_EXCEPT;
  108. new_fpcr = envp->__fpcr;
  109. new_fpsr = envp->__fpsr | (excepts << CAUSE_SHIFT);
  110. /* Write FCR and FESR if different. */
  111. if (unlikely (fpsr ^ new_fpsr) != 0)
  112. _FPU_SETFPSR (new_fpsr);
  113. if (unlikely (fpcr ^ new_fpcr) != 0)
  114. _FPU_SETCW (new_fpcr);
  115. /* Raise the exceptions if enabled in the new FP state. */
  116. if (unlikely (excepts & new_fpcr))
  117. feraiseexcept (excepts);
  118. return excepts & ex;
  119. }
  120. static __always_inline void
  121. libc_feupdateenv_vfp (const fenv_t *envp)
  122. {
  123. libc_feupdateenv_test_vfp (envp, 0);
  124. }
  125. static __always_inline void
  126. libc_feholdsetround_vfp_ctx (struct rm_ctx *ctx, int r)
  127. {
  128. fpu_control_t fpcr, fpsr, round;
  129. _FPU_GETCW (fpcr);
  130. _FPU_GETFPSR (fpsr);
  131. ctx->updated_status = false;
  132. ctx->env.__fpcr = fpcr;
  133. ctx->env.__fpsr = fpsr;
  134. /* Check whether rounding modes are different. */
  135. round = (fpcr ^ r) & FE_DOWNWARD;
  136. /* Set the rounding mode if changed. */
  137. if (unlikely (round != 0))
  138. {
  139. ctx->updated_status = true;
  140. _FPU_SETCW (fpcr ^ round);
  141. }
  142. }
  143. static __always_inline void
  144. libc_feresetround_vfp_ctx (struct rm_ctx *ctx)
  145. {
  146. /* Restore the rounding mode if updated. */
  147. if (unlikely (ctx->updated_status))
  148. {
  149. fpu_control_t fpcr;
  150. _FPU_GETCW (fpcr);
  151. fpcr = (fpcr & ~FE_DOWNWARD) | (ctx->env.__fpcr & FE_DOWNWARD);
  152. _FPU_SETCW (fpcr);
  153. }
  154. }
  155. static __always_inline void
  156. libc_fesetenv_vfp_ctx (struct rm_ctx *ctx)
  157. {
  158. fpu_control_t fpcr, fpsr, new_fpcr, new_fpsr;
  159. _FPU_GETCW (fpcr);
  160. _FPU_GETFPSR (fpsr);
  161. new_fpcr = ctx->env.__fpcr;
  162. new_fpsr = ctx->env.__fpsr;
  163. if (unlikely (fpsr ^ new_fpsr) != 0)
  164. _FPU_SETFPSR (new_fpsr);
  165. if (unlikely (fpcr ^ new_fpcr) != 0)
  166. _FPU_SETCW (new_fpcr);
  167. }
  168. #define libc_feholdexcept libc_feholdexcept_vfp
  169. #define libc_feholdexceptf libc_feholdexcept_vfp
  170. #define libc_feholdexceptl libc_feholdexcept_vfp
  171. #define libc_fesetround libc_fesetround_vfp
  172. #define libc_fesetroundf libc_fesetround_vfp
  173. #define libc_fesetroundl libc_fesetround_vfp
  174. #define libc_feresetround libc_feresetround_vfp
  175. #define libc_feresetroundf libc_feresetround_vfp
  176. #define libc_feresetroundl libc_feresetround_vfp
  177. #define libc_feresetround_noex libc_fesetenv_vfp
  178. #define libc_feresetround_noexf libc_fesetenv_vfp
  179. #define libc_feresetround_noexl libc_fesetenv_vfp
  180. #define libc_feholdexcept_setround libc_feholdexcept_setround_vfp
  181. #define libc_feholdexcept_setroundf libc_feholdexcept_setround_vfp
  182. #define libc_feholdexcept_setroundl libc_feholdexcept_setround_vfp
  183. #define libc_feholdsetround libc_feholdsetround_vfp
  184. #define libc_feholdsetroundf libc_feholdsetround_vfp
  185. #define libc_feholdsetroundl libc_feholdsetround_vfp
  186. #define libc_fetestexcept libc_fetestexcept_vfp
  187. #define libc_fetestexceptf libc_fetestexcept_vfp
  188. #define libc_fetestexceptl libc_fetestexcept_vfp
  189. #define libc_fesetenv libc_fesetenv_vfp
  190. #define libc_fesetenvf libc_fesetenv_vfp
  191. #define libc_fesetenvl libc_fesetenv_vfp
  192. #define libc_feupdateenv libc_feupdateenv_vfp
  193. #define libc_feupdateenvf libc_feupdateenv_vfp
  194. #define libc_feupdateenvl libc_feupdateenv_vfp
  195. #define libc_feupdateenv_test libc_feupdateenv_test_vfp
  196. #define libc_feupdateenv_testf libc_feupdateenv_test_vfp
  197. #define libc_feupdateenv_testl libc_feupdateenv_test_vfp
  198. /* We have support for rounding mode context. */
  199. #define HAVE_RM_CTX 1
  200. #define libc_feholdsetround_ctx libc_feholdsetround_vfp_ctx
  201. #define libc_feresetround_ctx libc_feresetround_vfp_ctx
  202. #define libc_feresetround_noex_ctx libc_fesetenv_vfp_ctx
  203. #define libc_feholdsetroundf_ctx libc_feholdsetround_vfp_ctx
  204. #define libc_feresetroundf_ctx libc_feresetround_vfp_ctx
  205. #define libc_feresetround_noexf_ctx libc_fesetenv_vfp_ctx
  206. #define libc_feholdsetroundl_ctx libc_feholdsetround_vfp_ctx
  207. #define libc_feresetroundl_ctx libc_feresetround_vfp_ctx
  208. #define libc_feresetround_noexl_ctx libc_fesetenv_vfp_ctx
  209. #endif /* CSKY_FENV_PRIVATE_H */