fenv.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /* Copyright (C) 1997-2026 Free Software Foundation, Inc.
  2. The GNU C Library is free software; you can redistribute it and/or
  3. modify it under the terms of the GNU Lesser General Public
  4. License as published by the Free Software Foundation; either
  5. version 2.1 of the License, or (at your option) any later version.
  6. The GNU C Library is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9. Lesser General Public License for more details.
  10. You should have received a copy of the GNU Lesser General Public
  11. License along with the GNU C Library; if not, see
  12. <https://www.gnu.org/licenses/>. */
  13. #ifndef _FENV_H
  14. # error "Never use <bits/fenv.h> directly; include <fenv.h> instead."
  15. #endif
  16. #include <features.h>
  17. #ifdef __CONFIG_E500__
  18. /* Define bits representing the exception. We use the bit positions of
  19. the appropriate bits in the SPEFSCR... */
  20. enum
  21. {
  22. FE_INEXACT = 1 << (63 - 42),
  23. #define FE_INEXACT FE_INEXACT
  24. FE_INVALID = 1 << (63 - 43),
  25. #define FE_INVALID FE_INVALID
  26. FE_DIVBYZERO = 1 << (63 - 44),
  27. #define FE_DIVBYZERO FE_DIVBYZERO
  28. FE_UNDERFLOW = 1 << (63 - 45),
  29. #define FE_UNDERFLOW FE_UNDERFLOW
  30. FE_OVERFLOW = 1 << (63 - 46)
  31. #define FE_OVERFLOW FE_OVERFLOW
  32. };
  33. #else /* PowerPC floating-point. */
  34. /* Define bits representing the exception. We use the bit positions of
  35. the appropriate bits in the FPSCR... */
  36. enum
  37. {
  38. FE_INEXACT =
  39. #define FE_INEXACT (1 << (31 - 6))
  40. FE_INEXACT,
  41. FE_DIVBYZERO =
  42. #define FE_DIVBYZERO (1 << (31 - 5))
  43. FE_DIVBYZERO,
  44. FE_UNDERFLOW =
  45. #define FE_UNDERFLOW (1 << (31 - 4))
  46. FE_UNDERFLOW,
  47. FE_OVERFLOW =
  48. #define FE_OVERFLOW (1 << (31 - 3))
  49. FE_OVERFLOW,
  50. /* ... except for FE_INVALID, for which we use bit 31. FE_INVALID
  51. actually corresponds to bits 7 through 12 and 21 through 23
  52. in the FPSCR, but we can't use that because the current draft
  53. says that it must be a power of 2. Instead we use bit 2 which
  54. is the summary bit for all the FE_INVALID exceptions, which
  55. kind of makes sense. */
  56. FE_INVALID =
  57. #define FE_INVALID (1 << (31 - 2))
  58. FE_INVALID,
  59. #ifdef __USE_GNU
  60. /* Breakdown of the FE_INVALID bits. Setting FE_INVALID on an
  61. input to a routine is equivalent to setting all of these bits;
  62. FE_INVALID will be set on output from a routine iff one of
  63. these bits is set. Note, though, that you can't disable or
  64. enable these exceptions individually. */
  65. /* Operation with a sNaN. */
  66. FE_INVALID_SNAN =
  67. # define FE_INVALID_SNAN (1 << (31 - 7))
  68. FE_INVALID_SNAN,
  69. /* Inf - Inf */
  70. FE_INVALID_ISI =
  71. # define FE_INVALID_ISI (1 << (31 - 8))
  72. FE_INVALID_ISI,
  73. /* Inf / Inf */
  74. FE_INVALID_IDI =
  75. # define FE_INVALID_IDI (1 << (31 - 9))
  76. FE_INVALID_IDI,
  77. /* 0 / 0 */
  78. FE_INVALID_ZDZ =
  79. # define FE_INVALID_ZDZ (1 << (31 - 10))
  80. FE_INVALID_ZDZ,
  81. /* Inf * 0 */
  82. FE_INVALID_IMZ =
  83. # define FE_INVALID_IMZ (1 << (31 - 11))
  84. FE_INVALID_IMZ,
  85. /* Comparison with a NaN. */
  86. FE_INVALID_COMPARE =
  87. # define FE_INVALID_COMPARE (1 << (31 - 12))
  88. FE_INVALID_COMPARE,
  89. /* Invalid operation flag for software (not set by hardware). */
  90. /* Note that some chips don't have this implemented, presumably
  91. because no-one expected anyone to write software for them %-). */
  92. FE_INVALID_SOFTWARE =
  93. # define FE_INVALID_SOFTWARE (1 << (31 - 21))
  94. FE_INVALID_SOFTWARE,
  95. /* Square root of negative number (including -Inf). */
  96. /* Note that some chips don't have this implemented. */
  97. FE_INVALID_SQRT =
  98. # define FE_INVALID_SQRT (1 << (31 - 22))
  99. FE_INVALID_SQRT,
  100. /* Conversion-to-integer of a NaN or a number too large or too small. */
  101. FE_INVALID_INTEGER_CONVERSION =
  102. # define FE_INVALID_INTEGER_CONVERSION (1 << (31 - 23))
  103. FE_INVALID_INTEGER_CONVERSION
  104. # define FE_ALL_INVALID \
  105. (FE_INVALID_SNAN | FE_INVALID_ISI | FE_INVALID_IDI | FE_INVALID_ZDZ \
  106. | FE_INVALID_IMZ | FE_INVALID_COMPARE | FE_INVALID_SOFTWARE \
  107. | FE_INVALID_SQRT | FE_INVALID_INTEGER_CONVERSION)
  108. #endif
  109. };
  110. #endif /* __CONFIG_E500__ */
  111. #define FE_ALL_EXCEPT \
  112. (FE_INEXACT | FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID)
  113. /* PowerPC chips support all of the four defined rounding modes. We
  114. use the bit pattern in the FPSCR as the values for the
  115. appropriate macros. */
  116. enum
  117. {
  118. FE_TONEAREST =
  119. #define FE_TONEAREST 0
  120. FE_TONEAREST,
  121. FE_TOWARDZERO =
  122. #define FE_TOWARDZERO 1
  123. FE_TOWARDZERO,
  124. FE_UPWARD =
  125. #define FE_UPWARD 2
  126. FE_UPWARD,
  127. FE_DOWNWARD =
  128. #define FE_DOWNWARD 3
  129. FE_DOWNWARD
  130. };
  131. /* Type representing exception flags. */
  132. typedef unsigned int fexcept_t;
  133. /* Type representing floating-point environment. We leave it as 'double'
  134. for efficiency reasons (rather than writing it to a 32-bit integer). */
  135. typedef double fenv_t;
  136. /* If the default argument is used we use this value. */
  137. extern const fenv_t __fe_dfl_env;
  138. #define FE_DFL_ENV (&__fe_dfl_env)
  139. #ifdef __USE_GNU
  140. /* Floating-point environment where all exceptions are enabled. Note that
  141. this is not sufficient to give you SIGFPE. */
  142. extern const fenv_t __fe_enabled_env;
  143. # define FE_ENABLED_ENV (&__fe_enabled_env)
  144. /* Floating-point environment with (processor-dependent) non-IEEE floating
  145. point. */
  146. extern const fenv_t __fe_nonieee_env;
  147. # define FE_NONIEEE_ENV (&__fe_nonieee_env)
  148. /* Floating-point environment with all exceptions enabled. Note that
  149. just evaluating this value does not change the processor exception mode.
  150. Passing this mask to fesetenv will result in a prctl syscall to change
  151. the MSR FE0/FE1 bits to "Precise Mode". On some processors this will
  152. result in slower floating point execution. This will last until an
  153. fenv or exception mask is installed that disables all FP exceptions. */
  154. # define FE_NOMASK_ENV FE_ENABLED_ENV
  155. /* Floating-point environment with all exceptions disabled. Note that
  156. just evaluating this value does not change the processor exception mode.
  157. Passing this mask to fesetenv will result in a prctl syscall to change
  158. the MSR FE0/FE1 bits to "Ignore Exceptions Mode". On most processors
  159. this allows the fastest possible floating point execution.*/
  160. # define FE_MASK_ENV FE_DFL_ENV
  161. #endif
  162. /* Type representing floating-point control modes. */
  163. typedef double femode_t;
  164. /* Default floating-point control modes. */
  165. extern const femode_t __fe_dfl_mode;
  166. # define FE_DFL_MODE (&__fe_dfl_mode)