fraiseexcpt.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* Raise given exceptions.
  2. Copyright (C) 1997-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. #include <fenv.h>
  15. #include <float.h>
  16. #include <math.h>
  17. #include "math-barriers.h"
  18. int
  19. feraiseexcept (int excepts)
  20. {
  21. static const struct {
  22. double zero, one, max, min, pi;
  23. } c = {
  24. 0.0, 1.0, DBL_MAX, DBL_MIN, M_PI
  25. };
  26. double d;
  27. /* Raise exceptions represented by EXPECTS. But we must raise only
  28. one signal at a time. It is important the if the overflow/underflow
  29. exception and the inexact exception are given at the same time,
  30. the overflow/underflow exception follows the inexact exception. */
  31. /* First: invalid exception. */
  32. if ((FE_INVALID & excepts) != 0)
  33. {
  34. /* One example of an invalid operation is 0/0. */
  35. __asm__ ("" : "=e" (d) : "0" (c.zero));
  36. d /= c.zero;
  37. math_force_eval (d);
  38. }
  39. /* Next: division by zero. */
  40. if ((FE_DIVBYZERO & excepts) != 0)
  41. {
  42. __asm__ ("" : "=e" (d) : "0" (c.one));
  43. d /= c.zero;
  44. math_force_eval (d);
  45. }
  46. /* Next: overflow. */
  47. if ((FE_OVERFLOW & excepts) != 0)
  48. {
  49. __asm__ ("" : "=e" (d) : "0" (c.max));
  50. d *= d;
  51. math_force_eval (d);
  52. }
  53. /* Next: underflow. */
  54. if ((FE_UNDERFLOW & excepts) != 0)
  55. {
  56. __asm__ ("" : "=e" (d) : "0" (c.min));
  57. d *= d;
  58. math_force_eval (d);
  59. }
  60. /* Last: inexact. */
  61. if ((FE_INEXACT & excepts) != 0)
  62. {
  63. __asm__ ("" : "=e" (d) : "0" (c.one));
  64. d /= c.pi;
  65. math_force_eval (d);
  66. }
  67. /* Success. */
  68. return 0;
  69. }