fenv_private.h 6.9 KB

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