fraiseexcpt.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. int
  18. feraiseexcept (int excepts)
  19. {
  20. #if defined(__mcffpu__)
  21. /* The Coldfire FPU allows an exception to be raised by asserting
  22. the associated EXC bit and then executing an arbitrary arithmetic
  23. instruction. fmove.l is classified as an arithmetic instruction
  24. and suffices for this purpose.
  25. We therefore raise an exception by setting both the EXC and AEXC
  26. bit associated with the exception (the former being 6 bits to the
  27. left of the latter) and then loading the longword at (%sp) into an
  28. FP register. */
  29. inline void
  30. raise_one_exception (int mask)
  31. {
  32. if (excepts & mask)
  33. {
  34. int fpsr;
  35. double unused;
  36. __asm__ volatile ("fmove%.l %/fpsr,%0" : "=d" (fpsr));
  37. fpsr |= (mask << 6) | mask;
  38. __asm__ volatile ("fmove%.l %0,%/fpsr" :: "d" (fpsr));
  39. __asm__ volatile ("fmove%.l (%%sp),%0" : "=f" (unused));
  40. }
  41. }
  42. raise_one_exception (FE_INVALID);
  43. raise_one_exception (FE_DIVBYZERO);
  44. raise_one_exception (FE_OVERFLOW);
  45. raise_one_exception (FE_UNDERFLOW);
  46. raise_one_exception (FE_INEXACT);
  47. #else
  48. /* Raise exceptions represented by EXCEPTS. But we must raise only one
  49. signal at a time. It is important that if the overflow/underflow
  50. exception and the divide by zero exception are given at the same
  51. time, the overflow/underflow exception follows the divide by zero
  52. exception. */
  53. /* First: invalid exception. */
  54. if (excepts & FE_INVALID)
  55. {
  56. /* One example of an invalid operation is 0 * Infinity. */
  57. double d = HUGE_VAL;
  58. __asm__ __volatile__ ("fmul%.s %#0r0,%0; fnop" : "=f" (d) : "0" (d));
  59. }
  60. /* Next: division by zero. */
  61. if (excepts & FE_DIVBYZERO)
  62. {
  63. double d = 1.0;
  64. __asm__ __volatile__ ("fdiv%.s %#0r0,%0; fnop" : "=f" (d) : "0" (d));
  65. }
  66. /* Next: overflow. */
  67. if (excepts & FE_OVERFLOW)
  68. {
  69. long double d = LDBL_MAX;
  70. __asm__ __volatile__ ("fmul%.x %0,%0; fnop" : "=f" (d) : "0" (d));
  71. }
  72. /* Next: underflow. */
  73. if (excepts & FE_UNDERFLOW)
  74. {
  75. long double d = -LDBL_MAX;
  76. __asm__ __volatile__ ("fetox%.x %0; fnop" : "=f" (d) : "0" (d));
  77. }
  78. /* Last: inexact. */
  79. if (excepts & FE_INEXACT)
  80. {
  81. long double d = 1.0;
  82. __asm__ __volatile__ ("fdiv%.s %#0r3,%0; fnop" : "=f" (d) : "0" (d));
  83. }
  84. #endif
  85. /* Success. */
  86. return 0;
  87. }