math_errf.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Single-precision math error handling.
  2. Copyright (C) 2017-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <math-barriers.h>
  16. #include "math_config.h"
  17. #if WANT_ERRNO
  18. # include <errno.h>
  19. /* NOINLINE reduces code size. */
  20. NOINLINE static float
  21. with_errnof (float y, int e)
  22. {
  23. errno = e;
  24. return y;
  25. }
  26. NOINLINE static int
  27. with_errnof_i (int y, int e)
  28. {
  29. errno = e;
  30. return y;
  31. }
  32. NOINLINE static long int
  33. with_errnof_li (long int y, int e)
  34. {
  35. errno = e;
  36. return y;
  37. }
  38. #else
  39. # define with_errnof(x, e) (x)
  40. # define with_errnof_i(x, x) (x)
  41. # define with_errnof_li(x, x) (x)
  42. #endif
  43. attribute_hidden float
  44. __math_edomf (float y)
  45. {
  46. return with_errnof (y, EDOM);
  47. }
  48. attribute_hidden float
  49. __math_erangef (float y)
  50. {
  51. return with_errnof (y, ERANGE);
  52. }
  53. /* NOINLINE prevents fenv semantics breaking optimizations. */
  54. NOINLINE static float
  55. xflowf (uint32_t sign, float y)
  56. {
  57. y = (sign ? -y : y) * y;
  58. return with_errnof (y, ERANGE);
  59. }
  60. attribute_hidden float
  61. __math_uflowf (uint32_t sign)
  62. {
  63. return xflowf (sign, 0x1p-95f);
  64. }
  65. #if WANT_ERRNO_UFLOW
  66. /* Underflows to zero in some non-nearest rounding mode, setting errno
  67. is valid even if the result is non-zero, but in the subnormal range. */
  68. attribute_hidden float
  69. __math_may_uflowf (uint32_t sign)
  70. {
  71. return xflowf (sign, 0x1.4p-75f);
  72. }
  73. #endif
  74. attribute_hidden float
  75. __math_oflowf (uint32_t sign)
  76. {
  77. return xflowf (sign, 0x1p97f);
  78. }
  79. attribute_hidden float
  80. __math_divzerof (uint32_t sign)
  81. {
  82. float y = 0;
  83. return with_errnof ((sign ? -1 : 1) / y, ERANGE);
  84. }
  85. attribute_hidden float
  86. __math_invalidf (float x)
  87. {
  88. float y = (x - x) / (x - x);
  89. return isnan (x) ? y : with_errnof (y, EDOM);
  90. }
  91. attribute_hidden int
  92. __math_invalidf_i (int x)
  93. {
  94. float y = 0.0f / 0.0f;
  95. math_force_eval (y);
  96. return with_errnof_i (x, EDOM);
  97. }
  98. attribute_hidden long int
  99. __math_invalidf_li (long int x)
  100. {
  101. float y = 0.0f / 0.0f;
  102. math_force_eval (y);
  103. return with_errnof_li (x, EDOM);
  104. }