fenv_private.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* Internal math stuff. MIPS version.
  2. Copyright (C) 2013-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 MIPS_FENV_PRIVATE_H
  15. #define MIPS_FENV_PRIVATE_H 1
  16. /* Inline functions to speed up the math library implementation. The
  17. default versions of these routines are in generic/fenv_private.h
  18. and call fesetround, feholdexcept, etc. These routines use inlined
  19. code instead. */
  20. #include <fenv.h>
  21. #include "fenv_libc.h"
  22. #include <fpu_control.h>
  23. #define _FPU_MASK_ALL (_FPU_MASK_V | _FPU_MASK_Z | _FPU_MASK_O \
  24. |_FPU_MASK_U | _FPU_MASK_I | FE_ALL_EXCEPT)
  25. static __always_inline void
  26. libc_feholdexcept_mips (fenv_t *envp)
  27. {
  28. fpu_control_t cw;
  29. /* Save the current state. */
  30. _FPU_GETCW (cw);
  31. envp->__fp_control_register = cw;
  32. /* Clear all exception enable bits and flags. */
  33. cw &= ~(_FPU_MASK_ALL);
  34. _FPU_SETCW (cw);
  35. }
  36. #define libc_feholdexcept libc_feholdexcept_mips
  37. #define libc_feholdexceptf libc_feholdexcept_mips
  38. #define libc_feholdexceptl libc_feholdexcept_mips
  39. static __always_inline void
  40. libc_fesetround_mips (int round)
  41. {
  42. fpu_control_t cw;
  43. /* Get current state. */
  44. _FPU_GETCW (cw);
  45. /* Set rounding bits. */
  46. cw &= ~_FPU_RC_MASK;
  47. cw |= round;
  48. /* Set new state. */
  49. _FPU_SETCW (cw);
  50. }
  51. #define libc_fesetround libc_fesetround_mips
  52. #define libc_fesetroundf libc_fesetround_mips
  53. #define libc_fesetroundl libc_fesetround_mips
  54. static __always_inline void
  55. libc_feholdexcept_setround_mips (fenv_t *envp, int round)
  56. {
  57. fpu_control_t cw;
  58. /* Save the current state. */
  59. _FPU_GETCW (cw);
  60. envp->__fp_control_register = cw;
  61. /* Clear all exception enable bits and flags. */
  62. cw &= ~(_FPU_MASK_ALL);
  63. /* Set rounding bits. */
  64. cw &= ~_FPU_RC_MASK;
  65. cw |= round;
  66. /* Set new state. */
  67. _FPU_SETCW (cw);
  68. }
  69. #define libc_feholdexcept_setround libc_feholdexcept_setround_mips
  70. #define libc_feholdexcept_setroundf libc_feholdexcept_setround_mips
  71. #define libc_feholdexcept_setroundl libc_feholdexcept_setround_mips
  72. #define libc_feholdsetround libc_feholdexcept_setround_mips
  73. #define libc_feholdsetroundf libc_feholdexcept_setround_mips
  74. #define libc_feholdsetroundl libc_feholdexcept_setround_mips
  75. static __always_inline void
  76. libc_fesetenv_mips (fenv_t *envp)
  77. {
  78. fpu_control_t cw __attribute__ ((unused));
  79. /* Read current state to flush fpu pipeline. */
  80. _FPU_GETCW (cw);
  81. _FPU_SETCW (envp->__fp_control_register);
  82. }
  83. #define libc_fesetenv libc_fesetenv_mips
  84. #define libc_fesetenvf libc_fesetenv_mips
  85. #define libc_fesetenvl libc_fesetenv_mips
  86. static __always_inline int
  87. libc_feupdateenv_test_mips (fenv_t *envp, int excepts)
  88. {
  89. /* int ret = fetestexcept (excepts); feupdateenv (envp); return ret; */
  90. int cw, temp;
  91. /* Get current control word. */
  92. _FPU_GETCW (cw);
  93. /* Set flag bits (which are accumulative), and *also* set the
  94. cause bits. The setting of the cause bits is what actually causes
  95. the hardware to generate the exception, if the corresponding enable
  96. bit is set as well. */
  97. temp = cw & FE_ALL_EXCEPT;
  98. temp |= envp->__fp_control_register | (temp << CAUSE_SHIFT);
  99. /* Set new state. */
  100. _FPU_SETCW (temp);
  101. return cw & excepts & FE_ALL_EXCEPT;
  102. }
  103. #define libc_feupdateenv_test libc_feupdateenv_test_mips
  104. #define libc_feupdateenv_testf libc_feupdateenv_test_mips
  105. #define libc_feupdateenv_testl libc_feupdateenv_test_mips
  106. static __always_inline void
  107. libc_feupdateenv_mips (fenv_t *envp)
  108. {
  109. libc_feupdateenv_test_mips (envp, 0);
  110. }
  111. #define libc_feupdateenv libc_feupdateenv_mips
  112. #define libc_feupdateenvf libc_feupdateenv_mips
  113. #define libc_feupdateenvl libc_feupdateenv_mips
  114. #define libc_feresetround libc_feupdateenv_mips
  115. #define libc_feresetroundf libc_feupdateenv_mips
  116. #define libc_feresetroundl libc_feupdateenv_mips
  117. static __always_inline int
  118. libc_fetestexcept_mips (int excepts)
  119. {
  120. int cw;
  121. /* Get current control word. */
  122. _FPU_GETCW (cw);
  123. return cw & excepts & FE_ALL_EXCEPT;
  124. }
  125. #define libc_fetestexcept libc_fetestexcept_mips
  126. #define libc_fetestexceptf libc_fetestexcept_mips
  127. #define libc_fetestexceptl libc_fetestexcept_mips
  128. /* Enable support for rounding mode context. */
  129. #define HAVE_RM_CTX 1
  130. static __always_inline void
  131. libc_feholdexcept_setround_mips_ctx (struct rm_ctx *ctx, int round)
  132. {
  133. fpu_control_t old, new;
  134. /* Save the current state. */
  135. _FPU_GETCW (old);
  136. ctx->env.__fp_control_register = old;
  137. /* Clear all exception enable bits and flags. */
  138. new = old & ~(_FPU_MASK_ALL);
  139. /* Set rounding bits. */
  140. new = (new & ~_FPU_RC_MASK) | round;
  141. if (__glibc_unlikely (new != old))
  142. {
  143. _FPU_SETCW (new);
  144. ctx->updated_status = true;
  145. }
  146. else
  147. ctx->updated_status = false;
  148. }
  149. #define libc_feholdexcept_setround_ctx libc_feholdexcept_setround_mips_ctx
  150. #define libc_feholdexcept_setroundf_ctx libc_feholdexcept_setround_mips_ctx
  151. #define libc_feholdexcept_setroundl_ctx libc_feholdexcept_setround_mips_ctx
  152. static __always_inline void
  153. libc_fesetenv_mips_ctx (struct rm_ctx *ctx)
  154. {
  155. libc_fesetenv_mips (&ctx->env);
  156. }
  157. #define libc_fesetenv_ctx libc_fesetenv_mips_ctx
  158. #define libc_fesetenvf_ctx libc_fesetenv_mips_ctx
  159. #define libc_fesetenvl_ctx libc_fesetenv_mips_ctx
  160. static __always_inline void
  161. libc_feupdateenv_mips_ctx (struct rm_ctx *ctx)
  162. {
  163. if (__glibc_unlikely (ctx->updated_status))
  164. libc_feupdateenv_test_mips (&ctx->env, 0);
  165. }
  166. #define libc_feupdateenv_ctx libc_feupdateenv_mips_ctx
  167. #define libc_feupdateenvf_ctx libc_feupdateenv_mips_ctx
  168. #define libc_feupdateenvl_ctx libc_feupdateenv_mips_ctx
  169. #define libc_feresetround_ctx libc_feupdateenv_mips_ctx
  170. #define libc_feresetroundf_ctx libc_feupdateenv_mips_ctx
  171. #define libc_feresetroundl_ctx libc_feupdateenv_mips_ctx
  172. static __always_inline void
  173. libc_feholdsetround_mips_ctx (struct rm_ctx *ctx, int round)
  174. {
  175. fpu_control_t old, new;
  176. /* Save the current state. */
  177. _FPU_GETCW (old);
  178. ctx->env.__fp_control_register = old;
  179. /* Set rounding bits. */
  180. new = (old & ~_FPU_RC_MASK) | round;
  181. if (__glibc_unlikely (new != old))
  182. {
  183. _FPU_SETCW (new);
  184. ctx->updated_status = true;
  185. }
  186. else
  187. ctx->updated_status = false;
  188. }
  189. #define libc_feholdsetround_ctx libc_feholdsetround_mips_ctx
  190. #define libc_feholdsetroundf_ctx libc_feholdsetround_mips_ctx
  191. #define libc_feholdsetroundl_ctx libc_feholdsetround_mips_ctx
  192. #endif