fenv_private.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /* Private floating point rounding and exceptions handling. AArch64 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 AARCH64_FENV_PRIVATE_H
  15. #define AARCH64_FENV_PRIVATE_H 1
  16. #include <fenv.h>
  17. #include <fpu_control.h>
  18. static __always_inline void
  19. libc_feholdexcept_aarch64 (fenv_t *envp)
  20. {
  21. fpu_control_t fpcr;
  22. fpu_control_t new_fpcr;
  23. fpu_fpsr_t fpsr;
  24. fpu_fpsr_t new_fpsr;
  25. _FPU_GETCW (fpcr);
  26. _FPU_GETFPSR (fpsr);
  27. envp->__fpcr = fpcr;
  28. envp->__fpsr = fpsr;
  29. /* Clear exception flags and set all exceptions to non-stop. */
  30. new_fpcr = fpcr & ~(FE_ALL_EXCEPT << FE_EXCEPT_SHIFT);
  31. new_fpsr = fpsr & ~FE_ALL_EXCEPT;
  32. if (unlikely (new_fpcr != fpcr))
  33. _FPU_SETCW (new_fpcr);
  34. if (new_fpsr != fpsr)
  35. _FPU_SETFPSR (new_fpsr);
  36. }
  37. #define libc_feholdexcept libc_feholdexcept_aarch64
  38. #define libc_feholdexceptf libc_feholdexcept_aarch64
  39. #define libc_feholdexceptl libc_feholdexcept_aarch64
  40. static __always_inline void
  41. libc_fesetround_aarch64 (int round)
  42. {
  43. fpu_control_t fpcr;
  44. _FPU_GETCW (fpcr);
  45. /* Check whether rounding modes are different. */
  46. round = (fpcr ^ round) & _FPU_FPCR_RM_MASK;
  47. /* Set new rounding mode if different. */
  48. if (unlikely (round != 0))
  49. _FPU_SETCW (fpcr ^ round);
  50. }
  51. #define libc_fesetround libc_fesetround_aarch64
  52. #define libc_fesetroundf libc_fesetround_aarch64
  53. #define libc_fesetroundl libc_fesetround_aarch64
  54. static __always_inline void
  55. libc_feholdexcept_setround_aarch64 (fenv_t *envp, int round)
  56. {
  57. fpu_control_t fpcr;
  58. fpu_control_t new_fpcr;
  59. fpu_fpsr_t fpsr;
  60. fpu_fpsr_t new_fpsr;
  61. _FPU_GETCW (fpcr);
  62. _FPU_GETFPSR (fpsr);
  63. envp->__fpcr = fpcr;
  64. envp->__fpsr = fpsr;
  65. /* Clear exception flags, set all exceptions to non-stop,
  66. and set new rounding mode. */
  67. new_fpcr = fpcr & ~((FE_ALL_EXCEPT << FE_EXCEPT_SHIFT) | _FPU_FPCR_RM_MASK);
  68. new_fpcr |= round;
  69. new_fpsr = fpsr & ~FE_ALL_EXCEPT;
  70. if (unlikely (new_fpcr != fpcr))
  71. _FPU_SETCW (new_fpcr);
  72. if (new_fpsr != fpsr)
  73. _FPU_SETFPSR (new_fpsr);
  74. }
  75. #define libc_feholdexcept_setround libc_feholdexcept_setround_aarch64
  76. #define libc_feholdexcept_setroundf libc_feholdexcept_setround_aarch64
  77. #define libc_feholdexcept_setroundl libc_feholdexcept_setround_aarch64
  78. static __always_inline int
  79. libc_fetestexcept_aarch64 (int ex)
  80. {
  81. fpu_fpsr_t fpsr;
  82. _FPU_GETFPSR (fpsr);
  83. return fpsr & ex & FE_ALL_EXCEPT;
  84. }
  85. #define libc_fetestexcept libc_fetestexcept_aarch64
  86. #define libc_fetestexceptf libc_fetestexcept_aarch64
  87. #define libc_fetestexceptl libc_fetestexcept_aarch64
  88. static __always_inline void
  89. libc_fesetenv_aarch64 (const fenv_t *envp)
  90. {
  91. fpu_control_t fpcr;
  92. fpu_control_t new_fpcr;
  93. _FPU_GETCW (fpcr);
  94. new_fpcr = envp->__fpcr;
  95. if (unlikely (fpcr != new_fpcr))
  96. _FPU_SETCW (new_fpcr);
  97. _FPU_SETFPSR (envp->__fpsr);
  98. }
  99. #define libc_fesetenv libc_fesetenv_aarch64
  100. #define libc_fesetenvf libc_fesetenv_aarch64
  101. #define libc_fesetenvl libc_fesetenv_aarch64
  102. #define libc_feresetround_noex libc_fesetenv_aarch64
  103. #define libc_feresetround_noexf libc_fesetenv_aarch64
  104. #define libc_feresetround_noexl libc_fesetenv_aarch64
  105. static __always_inline int
  106. libc_feupdateenv_test_aarch64 (const fenv_t *envp, int ex)
  107. {
  108. fpu_control_t fpcr;
  109. fpu_control_t new_fpcr;
  110. fpu_fpsr_t fpsr;
  111. fpu_fpsr_t new_fpsr;
  112. int excepts;
  113. _FPU_GETCW (fpcr);
  114. _FPU_GETFPSR (fpsr);
  115. /* Merge current exception flags with the saved fenv. */
  116. excepts = fpsr & FE_ALL_EXCEPT;
  117. new_fpcr = envp->__fpcr;
  118. new_fpsr = envp->__fpsr | excepts;
  119. if (unlikely (fpcr != new_fpcr))
  120. _FPU_SETCW (new_fpcr);
  121. if (fpsr != new_fpsr)
  122. _FPU_SETFPSR (new_fpsr);
  123. /* Raise the exceptions if enabled in the new FP state. */
  124. if (unlikely (excepts & (new_fpcr >> FE_EXCEPT_SHIFT)))
  125. feraiseexcept (excepts);
  126. return excepts & ex;
  127. }
  128. #define libc_feupdateenv_test libc_feupdateenv_test_aarch64
  129. #define libc_feupdateenv_testf libc_feupdateenv_test_aarch64
  130. #define libc_feupdateenv_testl libc_feupdateenv_test_aarch64
  131. static __always_inline void
  132. libc_feupdateenv_aarch64 (const fenv_t *envp)
  133. {
  134. libc_feupdateenv_test_aarch64 (envp, 0);
  135. }
  136. #define libc_feupdateenv libc_feupdateenv_aarch64
  137. #define libc_feupdateenvf libc_feupdateenv_aarch64
  138. #define libc_feupdateenvl libc_feupdateenv_aarch64
  139. static __always_inline void
  140. libc_feholdsetround_aarch64 (fenv_t *envp, int round)
  141. {
  142. fpu_control_t fpcr;
  143. fpu_fpsr_t fpsr;
  144. _FPU_GETCW (fpcr);
  145. _FPU_GETFPSR (fpsr);
  146. envp->__fpcr = fpcr;
  147. envp->__fpsr = fpsr;
  148. /* Check whether rounding modes are different. */
  149. round = (fpcr ^ round) & _FPU_FPCR_RM_MASK;
  150. /* Set new rounding mode if different. */
  151. if (unlikely (round != 0))
  152. _FPU_SETCW (fpcr ^ round);
  153. }
  154. #define libc_feholdsetround libc_feholdsetround_aarch64
  155. #define libc_feholdsetroundf libc_feholdsetround_aarch64
  156. #define libc_feholdsetroundl libc_feholdsetround_aarch64
  157. static __always_inline void
  158. libc_feresetround_aarch64 (fenv_t *envp)
  159. {
  160. fpu_control_t fpcr;
  161. int round;
  162. _FPU_GETCW (fpcr);
  163. /* Check whether rounding modes are different. */
  164. round = (envp->__fpcr ^ fpcr) & _FPU_FPCR_RM_MASK;
  165. /* Restore the rounding mode if it was changed. */
  166. if (unlikely (round != 0))
  167. _FPU_SETCW (fpcr ^ round);
  168. }
  169. #define libc_feresetround libc_feresetround_aarch64
  170. #define libc_feresetroundf libc_feresetround_aarch64
  171. #define libc_feresetroundl libc_feresetround_aarch64
  172. /* We have support for rounding mode context. */
  173. #define HAVE_RM_CTX 1
  174. static __always_inline void
  175. libc_feholdsetround_aarch64_ctx (struct rm_ctx *ctx, int r)
  176. {
  177. fpu_control_t fpcr;
  178. int round;
  179. _FPU_GETCW (fpcr);
  180. ctx->env.__fpcr = fpcr;
  181. /* Check whether rounding modes are different. */
  182. round = (fpcr ^ r) & _FPU_FPCR_RM_MASK;
  183. ctx->updated_status = round != 0;
  184. /* Set the rounding mode if changed. */
  185. if (unlikely (round != 0))
  186. _FPU_SETCW (fpcr ^ round);
  187. }
  188. #define libc_feholdsetround_ctx libc_feholdsetround_aarch64_ctx
  189. #define libc_feholdsetroundf_ctx libc_feholdsetround_aarch64_ctx
  190. #define libc_feholdsetroundl_ctx libc_feholdsetround_aarch64_ctx
  191. static __always_inline void
  192. libc_feresetround_aarch64_ctx (struct rm_ctx *ctx)
  193. {
  194. /* Restore the rounding mode if updated. */
  195. if (unlikely (ctx->updated_status))
  196. _FPU_SETCW (ctx->env.__fpcr);
  197. }
  198. #define libc_feresetround_ctx libc_feresetround_aarch64_ctx
  199. #define libc_feresetroundf_ctx libc_feresetround_aarch64_ctx
  200. #define libc_feresetroundl_ctx libc_feresetround_aarch64_ctx
  201. static __always_inline void
  202. libc_feholdsetround_noex_aarch64_ctx (struct rm_ctx *ctx, int r)
  203. {
  204. fpu_control_t fpcr;
  205. fpu_fpsr_t fpsr;
  206. int round;
  207. _FPU_GETCW (fpcr);
  208. _FPU_GETFPSR (fpsr);
  209. ctx->env.__fpcr = fpcr;
  210. ctx->env.__fpsr = fpsr;
  211. /* Check whether rounding modes are different. */
  212. round = (fpcr ^ r) & _FPU_FPCR_RM_MASK;
  213. ctx->updated_status = round != 0;
  214. /* Set the rounding mode if changed. */
  215. if (unlikely (round != 0))
  216. _FPU_SETCW (fpcr ^ round);
  217. }
  218. #define libc_feholdsetround_noex_ctx libc_feholdsetround_noex_aarch64_ctx
  219. #define libc_feholdsetround_noexf_ctx libc_feholdsetround_noex_aarch64_ctx
  220. #define libc_feholdsetround_noexl_ctx libc_feholdsetround_noex_aarch64_ctx
  221. static __always_inline void
  222. libc_feresetround_noex_aarch64_ctx (struct rm_ctx *ctx)
  223. {
  224. /* Restore the rounding mode if updated. */
  225. if (unlikely (ctx->updated_status))
  226. _FPU_SETCW (ctx->env.__fpcr);
  227. /* Write new FPSR to restore exception flags. */
  228. _FPU_SETFPSR (ctx->env.__fpsr);
  229. }
  230. #define libc_feresetround_noex_ctx libc_feresetround_noex_aarch64_ctx
  231. #define libc_feresetround_noexf_ctx libc_feresetround_noex_aarch64_ctx
  232. #define libc_feresetround_noexl_ctx libc_feresetround_noex_aarch64_ctx
  233. #endif