fenv_private.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /* Private floating point rounding and exceptions handling. ARM VFP version.
  2. Copyright (C) 2014-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 ARM_FENV_PRIVATE_H
  15. #define ARM_FENV_PRIVATE_H 1
  16. #include <fenv.h>
  17. #include <fpu_control.h>
  18. static __always_inline void
  19. libc_feholdexcept_vfp (fenv_t *envp)
  20. {
  21. fpu_control_t fpscr;
  22. _FPU_GETCW (fpscr);
  23. envp->__cw = fpscr;
  24. /* Clear exception flags and set all exceptions to non-stop. */
  25. fpscr &= ~_FPU_MASK_EXCEPT;
  26. _FPU_SETCW (fpscr);
  27. }
  28. static __always_inline void
  29. libc_fesetround_vfp (int round)
  30. {
  31. fpu_control_t fpscr;
  32. _FPU_GETCW (fpscr);
  33. /* Set new rounding mode if different. */
  34. if (unlikely ((fpscr & _FPU_MASK_RM) != round))
  35. _FPU_SETCW ((fpscr & ~_FPU_MASK_RM) | round);
  36. }
  37. static __always_inline void
  38. libc_feholdexcept_setround_vfp (fenv_t *envp, int round)
  39. {
  40. fpu_control_t fpscr;
  41. _FPU_GETCW (fpscr);
  42. envp->__cw = fpscr;
  43. /* Clear exception flags, set all exceptions to non-stop,
  44. and set new rounding mode. */
  45. fpscr &= ~(_FPU_MASK_EXCEPT | _FPU_MASK_RM);
  46. _FPU_SETCW (fpscr | round);
  47. }
  48. static __always_inline void
  49. libc_feholdsetround_vfp (fenv_t *envp, int round)
  50. {
  51. fpu_control_t fpscr;
  52. _FPU_GETCW (fpscr);
  53. envp->__cw = fpscr;
  54. /* Set new rounding mode if different. */
  55. if (unlikely ((fpscr & _FPU_MASK_RM) != round))
  56. _FPU_SETCW ((fpscr & ~_FPU_MASK_RM) | round);
  57. }
  58. static __always_inline void
  59. libc_feresetround_vfp (fenv_t *envp)
  60. {
  61. fpu_control_t fpscr, round;
  62. _FPU_GETCW (fpscr);
  63. /* Check whether rounding modes are different. */
  64. round = (envp->__cw ^ fpscr) & _FPU_MASK_RM;
  65. /* Restore the rounding mode if it was changed. */
  66. if (unlikely (round != 0))
  67. _FPU_SETCW (fpscr ^ round);
  68. }
  69. static __always_inline int
  70. libc_fetestexcept_vfp (int ex)
  71. {
  72. fpu_control_t fpscr;
  73. _FPU_GETCW (fpscr);
  74. return fpscr & ex & FE_ALL_EXCEPT;
  75. }
  76. static __always_inline void
  77. libc_fesetenv_vfp (const fenv_t *envp)
  78. {
  79. fpu_control_t fpscr, new_fpscr;
  80. _FPU_GETCW (fpscr);
  81. new_fpscr = envp->__cw;
  82. /* Write new FPSCR if different (ignoring NZCV flags). */
  83. if (unlikely (((fpscr ^ new_fpscr) & ~_FPU_MASK_NZCV) != 0))
  84. _FPU_SETCW (new_fpscr);
  85. }
  86. static __always_inline int
  87. libc_feupdateenv_test_vfp (const fenv_t *envp, int ex)
  88. {
  89. fpu_control_t fpscr, new_fpscr;
  90. int excepts;
  91. _FPU_GETCW (fpscr);
  92. /* Merge current exception flags with the saved fenv. */
  93. excepts = fpscr & FE_ALL_EXCEPT;
  94. new_fpscr = envp->__cw | excepts;
  95. /* Write new FPSCR if different (ignoring NZCV flags). */
  96. if (unlikely (((fpscr ^ new_fpscr) & ~_FPU_MASK_NZCV) != 0))
  97. _FPU_SETCW (new_fpscr);
  98. /* Raise the exceptions if enabled in the new FP state. */
  99. if (unlikely (excepts & (new_fpscr >> FE_EXCEPT_SHIFT)))
  100. feraiseexcept (excepts);
  101. return excepts & ex;
  102. }
  103. static __always_inline void
  104. libc_feupdateenv_vfp (const fenv_t *envp)
  105. {
  106. libc_feupdateenv_test_vfp (envp, 0);
  107. }
  108. static __always_inline void
  109. libc_feholdsetround_vfp_ctx (struct rm_ctx *ctx, int r)
  110. {
  111. fpu_control_t fpscr, round;
  112. _FPU_GETCW (fpscr);
  113. ctx->updated_status = false;
  114. ctx->env.__cw = fpscr;
  115. /* Check whether rounding modes are different. */
  116. round = (fpscr ^ r) & _FPU_MASK_RM;
  117. /* Set the rounding mode if changed. */
  118. if (unlikely (round != 0))
  119. {
  120. ctx->updated_status = true;
  121. _FPU_SETCW (fpscr ^ round);
  122. }
  123. }
  124. static __always_inline void
  125. libc_feresetround_vfp_ctx (struct rm_ctx *ctx)
  126. {
  127. /* Restore the rounding mode if updated. */
  128. if (unlikely (ctx->updated_status))
  129. {
  130. fpu_control_t fpscr;
  131. _FPU_GETCW (fpscr);
  132. fpscr = (fpscr & ~_FPU_MASK_RM) | (ctx->env.__cw & _FPU_MASK_RM);
  133. _FPU_SETCW (fpscr);
  134. }
  135. }
  136. static __always_inline void
  137. libc_fesetenv_vfp_ctx (struct rm_ctx *ctx)
  138. {
  139. fpu_control_t fpscr, new_fpscr;
  140. _FPU_GETCW (fpscr);
  141. new_fpscr = ctx->env.__cw;
  142. /* Write new FPSCR if different (ignoring NZCV flags). */
  143. if (unlikely (((fpscr ^ new_fpscr) & ~_FPU_MASK_NZCV) != 0))
  144. _FPU_SETCW (new_fpscr);
  145. }
  146. #ifndef __SOFTFP__
  147. # define libc_feholdexcept libc_feholdexcept_vfp
  148. # define libc_feholdexceptf libc_feholdexcept_vfp
  149. # define libc_feholdexceptl libc_feholdexcept_vfp
  150. # define libc_fesetround libc_fesetround_vfp
  151. # define libc_fesetroundf libc_fesetround_vfp
  152. # define libc_fesetroundl libc_fesetround_vfp
  153. # define libc_feresetround libc_feresetround_vfp
  154. # define libc_feresetroundf libc_feresetround_vfp
  155. # define libc_feresetroundl libc_feresetround_vfp
  156. # define libc_feresetround_noex libc_fesetenv_vfp
  157. # define libc_feresetround_noexf libc_fesetenv_vfp
  158. # define libc_feresetround_noexl libc_fesetenv_vfp
  159. # define libc_feholdexcept_setround libc_feholdexcept_setround_vfp
  160. # define libc_feholdexcept_setroundf libc_feholdexcept_setround_vfp
  161. # define libc_feholdexcept_setroundl libc_feholdexcept_setround_vfp
  162. # define libc_feholdsetround libc_feholdsetround_vfp
  163. # define libc_feholdsetroundf libc_feholdsetround_vfp
  164. # define libc_feholdsetroundl libc_feholdsetround_vfp
  165. # define libc_fetestexcept libc_fetestexcept_vfp
  166. # define libc_fetestexceptf libc_fetestexcept_vfp
  167. # define libc_fetestexceptl libc_fetestexcept_vfp
  168. # define libc_fesetenv libc_fesetenv_vfp
  169. # define libc_fesetenvf libc_fesetenv_vfp
  170. # define libc_fesetenvl libc_fesetenv_vfp
  171. # define libc_feupdateenv libc_feupdateenv_vfp
  172. # define libc_feupdateenvf libc_feupdateenv_vfp
  173. # define libc_feupdateenvl libc_feupdateenv_vfp
  174. # define libc_feupdateenv_test libc_feupdateenv_test_vfp
  175. # define libc_feupdateenv_testf libc_feupdateenv_test_vfp
  176. # define libc_feupdateenv_testl libc_feupdateenv_test_vfp
  177. /* We have support for rounding mode context. */
  178. #define HAVE_RM_CTX 1
  179. # define libc_feholdsetround_ctx libc_feholdsetround_vfp_ctx
  180. # define libc_feresetround_ctx libc_feresetround_vfp_ctx
  181. # define libc_feresetround_noex_ctx libc_fesetenv_vfp_ctx
  182. # define libc_feholdsetroundf_ctx libc_feholdsetround_vfp_ctx
  183. # define libc_feresetroundf_ctx libc_feresetround_vfp_ctx
  184. # define libc_feresetround_noexf_ctx libc_fesetenv_vfp_ctx
  185. # define libc_feholdsetroundl_ctx libc_feholdsetround_vfp_ctx
  186. # define libc_feresetroundl_ctx libc_feresetround_vfp_ctx
  187. # define libc_feresetround_noexl_ctx libc_fesetenv_vfp_ctx
  188. #endif
  189. #endif /* ARM_FENV_PRIVATE_H */