fenv_private.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Private floating point rounding and exceptions handling. RISC-V 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 RISCV_FENV_PRIVATE_H
  15. #define RISCV_FENV_PRIVATE_H 1
  16. #include <fenv.h>
  17. #include <fpu_control.h>
  18. #include "get-rounding-mode.h"
  19. static __always_inline int
  20. riscv_getround (void)
  21. {
  22. return get_rounding_mode ();
  23. }
  24. static __always_inline void
  25. riscv_setround (int rm)
  26. {
  27. __asm__ volatile ("fsrm %z0" : : "rJ" (rm));
  28. }
  29. static __always_inline int
  30. riscv_getflags (void)
  31. {
  32. int flags;
  33. __asm__ volatile ("frflags %0" : "=r" (flags));
  34. return flags;
  35. }
  36. static __always_inline void
  37. riscv_setflags (int flags)
  38. {
  39. __asm__ volatile ("fsflags %z0" : : "rJ" (flags));
  40. }
  41. static __always_inline void
  42. libc_feholdexcept_riscv (fenv_t *envp)
  43. {
  44. __asm__ volatile ("csrrc %0, fcsr, %1" : "=r" (*envp) : "i" (FE_ALL_EXCEPT));
  45. }
  46. #define libc_feholdexcept libc_feholdexcept_riscv
  47. #define libc_feholdexceptf libc_feholdexcept_riscv
  48. #define libc_feholdexceptl libc_feholdexcept_riscv
  49. static __always_inline void
  50. libc_fesetround_riscv (int round)
  51. {
  52. riscv_setround (round);
  53. }
  54. #define libc_fesetround libc_fesetround_riscv
  55. #define libc_fesetroundf libc_fesetround_riscv
  56. #define libc_fesetroundl libc_fesetround_riscv
  57. static __always_inline void
  58. libc_feholdexcept_setround_riscv (fenv_t *envp, int round)
  59. {
  60. libc_feholdexcept_riscv (envp);
  61. libc_fesetround_riscv (round);
  62. }
  63. #define libc_feholdexcept_setround libc_feholdexcept_setround_riscv
  64. #define libc_feholdexcept_setroundf libc_feholdexcept_setround_riscv
  65. #define libc_feholdexcept_setroundl libc_feholdexcept_setround_riscv
  66. static __always_inline int
  67. libc_fetestexcept_riscv (int ex)
  68. {
  69. return riscv_getflags () & ex;
  70. }
  71. #define libc_fetestexcept libc_fetestexcept_riscv
  72. #define libc_fetestexceptf libc_fetestexcept_riscv
  73. #define libc_fetestexceptl libc_fetestexcept_riscv
  74. static __always_inline void
  75. libc_fesetenv_riscv (const fenv_t *envp)
  76. {
  77. long int env = (envp != FE_DFL_ENV ? *envp : 0);
  78. _FPU_SETCW (env);
  79. }
  80. #define libc_fesetenv libc_fesetenv_riscv
  81. #define libc_fesetenvf libc_fesetenv_riscv
  82. #define libc_fesetenvl libc_fesetenv_riscv
  83. #define libc_feresetround_noex libc_fesetenv_riscv
  84. #define libc_feresetround_noexf libc_fesetenv_riscv
  85. #define libc_feresetround_noexl libc_fesetenv_riscv
  86. static __always_inline int
  87. libc_feupdateenv_test_riscv (const fenv_t *envp, int ex)
  88. {
  89. fenv_t env = *envp;
  90. int flags = riscv_getflags ();
  91. __asm__ volatile ("csrw fcsr, %z0" : : "rJ" (env | flags));
  92. return flags & ex;
  93. }
  94. #define libc_feupdateenv_test libc_feupdateenv_test_riscv
  95. #define libc_feupdateenv_testf libc_feupdateenv_test_riscv
  96. #define libc_feupdateenv_testl libc_feupdateenv_test_riscv
  97. static __always_inline void
  98. libc_feupdateenv_riscv (const fenv_t *envp)
  99. {
  100. long int env = (envp != FE_DFL_ENV ? *envp : 0);
  101. _FPU_SETCW (env | riscv_getflags ());
  102. }
  103. #define libc_feupdateenv libc_feupdateenv_riscv
  104. #define libc_feupdateenvf libc_feupdateenv_riscv
  105. #define libc_feupdateenvl libc_feupdateenv_riscv
  106. static __always_inline void
  107. libc_feholdsetround_riscv (fenv_t *envp, int round)
  108. {
  109. /* Note this implementation makes an improperly-formatted fenv_t and
  110. so should only be used in conjunction with libc_feresetround. */
  111. int old_round;
  112. __asm__ volatile ("csrrw %0, frm, %z1" : "=r" (old_round) : "rJ" (round));
  113. *envp = old_round;
  114. }
  115. #define libc_feholdsetround libc_feholdsetround_riscv
  116. #define libc_feholdsetroundf libc_feholdsetround_riscv
  117. #define libc_feholdsetroundl libc_feholdsetround_riscv
  118. static __always_inline void
  119. libc_feresetround_riscv (fenv_t *envp)
  120. {
  121. /* Note this implementation takes an improperly-formatted fenv_t and
  122. so should only be used in conjunction with libc_feholdsetround. */
  123. riscv_setround (*envp);
  124. }
  125. #define libc_feresetround libc_feresetround_riscv
  126. #define libc_feresetroundf libc_feresetround_riscv
  127. #define libc_feresetroundl libc_feresetround_riscv
  128. #endif