fenv_private.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* Private floating point rounding and exceptions handling. OpenRISC version.
  2. Copyright (C) 2024-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 OR1K_FENV_PRIVATE_H
  15. #define OR1K_FENV_PRIVATE_H 1
  16. #include <fenv.h>
  17. #include <fpu_control.h>
  18. static __always_inline void
  19. libc_feholdexcept_or1k (fenv_t *envp)
  20. {
  21. fpu_control_t cw;
  22. fpu_control_t cw_new;
  23. /* Get and store the environment. */
  24. _FPU_GETCW (cw);
  25. *envp = cw;
  26. /* Clear the exception status flags. */
  27. cw_new = cw & ~FE_ALL_EXCEPT;
  28. if (cw != cw_new)
  29. _FPU_SETCW (cw_new);
  30. }
  31. #define libc_feholdexcept libc_feholdexcept_or1k
  32. #define libc_feholdexceptf libc_feholdexcept_or1k
  33. #define libc_feholdexceptl libc_feholdexcept_or1k
  34. static __always_inline void
  35. libc_fesetround_or1k (int round)
  36. {
  37. fpu_control_t cw;
  38. fpu_control_t cw_new;
  39. _FPU_GETCW (cw);
  40. cw_new = cw & ~_FPU_FPCSR_RM_MASK;
  41. cw_new |= round;
  42. if (cw != cw_new)
  43. _FPU_SETCW (cw_new);
  44. }
  45. #define libc_fesetround libc_fesetround_or1k
  46. #define libc_fesetroundf libc_fesetround_or1k
  47. #define libc_fesetroundl libc_fesetround_or1k
  48. static __always_inline void
  49. libc_feholdexcept_setround_or1k (fenv_t *envp, int round)
  50. {
  51. fpu_control_t cw;
  52. fpu_control_t cw_new;
  53. /* Get and store the environment. */
  54. _FPU_GETCW (cw);
  55. *envp = cw;
  56. /* Clear the status flags and rounding mode. */
  57. cw_new = cw & ~(FE_ALL_EXCEPT | _FPU_FPCSR_RM_MASK);
  58. /* Set rounding mode. */
  59. cw_new |= round;
  60. if (cw != cw_new)
  61. _FPU_SETCW (cw_new);
  62. }
  63. #define libc_feholdexcept_setround libc_feholdexcept_setround_or1k
  64. #define libc_feholdexcept_setroundf libc_feholdexcept_setround_or1k
  65. #define libc_feholdexcept_setroundl libc_feholdexcept_setround_or1k
  66. static __always_inline int
  67. libc_fetestexcept_or1k (int ex)
  68. {
  69. fpu_control_t cw;
  70. /* Get current control word. */
  71. _FPU_GETCW (cw);
  72. /* Check if any of the queried exception flags are set. */
  73. return cw & ex & FE_ALL_EXCEPT;
  74. }
  75. #define libc_fetestexcept libc_fetestexcept_or1k
  76. #define libc_fetestexceptf libc_fetestexcept_or1k
  77. #define libc_fetestexceptl libc_fetestexcept_or1k
  78. static __always_inline void
  79. libc_fesetenv_or1k (const fenv_t *envp)
  80. {
  81. if (envp == FE_DFL_ENV)
  82. _FPU_SETCW (_FPU_DEFAULT);
  83. else
  84. _FPU_SETCW (*envp);
  85. }
  86. #define libc_fesetenv libc_fesetenv_or1k
  87. #define libc_fesetenvf libc_fesetenv_or1k
  88. #define libc_fesetenvl libc_fesetenv_or1k
  89. #define libc_feresetround_noex libc_fesetenv_or1k
  90. #define libc_feresetround_noexf libc_fesetenv_or1k
  91. #define libc_feresetround_noexl libc_fesetenv_or1k
  92. static __always_inline int
  93. libc_feupdateenv_test_or1k (const fenv_t *envp, int ex)
  94. {
  95. fpu_control_t cw;
  96. fpu_control_t cw_new;
  97. int excepts;
  98. /* Get current control word. */
  99. _FPU_GETCW (cw);
  100. /* Merge current exception flags with the passed fenv. */
  101. excepts = cw & FE_ALL_EXCEPT;
  102. cw_new = (envp == FE_DFL_ENV ? _FPU_DEFAULT : *envp) | excepts;
  103. if (unlikely (cw != cw_new))
  104. _FPU_SETCW (cw_new);
  105. /* Raise the exceptions if enabled in the new FP state. */
  106. if (unlikely (excepts))
  107. feraiseexcept (excepts);
  108. return excepts & ex;
  109. }
  110. #define libc_feupdateenv_test libc_feupdateenv_test_or1k
  111. #define libc_feupdateenv_testf libc_feupdateenv_test_or1k
  112. #define libc_feupdateenv_testl libc_feupdateenv_test_or1k
  113. static __always_inline void
  114. libc_feupdateenv_or1k (const fenv_t *envp)
  115. {
  116. libc_feupdateenv_test_or1k (envp, 0);
  117. }
  118. #define libc_feupdateenv libc_feupdateenv_or1k
  119. #define libc_feupdateenvf libc_feupdateenv_or1k
  120. #define libc_feupdateenvl libc_feupdateenv_or1k
  121. static __always_inline void
  122. libc_feholdsetround_or1k (fenv_t *envp, int round)
  123. {
  124. fpu_control_t cw;
  125. _FPU_GETCW (cw);
  126. *envp = cw;
  127. /* Check whether rounding modes are different. */
  128. round = (cw ^ round) & _FPU_FPCSR_RM_MASK;
  129. /* Set new rounding mode if different. */
  130. if (unlikely (round != 0))
  131. _FPU_SETCW (cw ^ round);
  132. }
  133. #define libc_feholdsetround libc_feholdsetround_or1k
  134. #define libc_feholdsetroundf libc_feholdsetround_or1k
  135. #define libc_feholdsetroundl libc_feholdsetround_or1k
  136. static __always_inline void
  137. libc_feresetround_or1k (fenv_t *envp)
  138. {
  139. fpu_control_t cw;
  140. int round;
  141. _FPU_GETCW (cw);
  142. /* Check whether rounding modes are different. */
  143. round = (*envp ^ cw) & _FPU_FPCSR_RM_MASK;
  144. /* Restore the rounding mode if it was changed. */
  145. if (unlikely (round != 0))
  146. _FPU_SETCW (cw ^ round);
  147. }
  148. #define libc_feresetround libc_feresetround_or1k
  149. #define libc_feresetroundf libc_feresetround_or1k
  150. #define libc_feresetroundl libc_feresetround_or1k
  151. #endif