fraiseexcpt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Raise given exceptions.
  2. Copyright (C) 2001-2017 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. <http://www.gnu.org/licenses/>. */
  14. #include <fenv.h>
  15. #include <math.h>
  16. int
  17. feraiseexcept (int excepts)
  18. {
  19. /* Raise exceptions represented by EXPECTS. But we must raise only
  20. one signal at a time. It is important that if the overflow/underflow
  21. exception and the inexact exception are given at the same time,
  22. the overflow/underflow exception follows the inexact exception. */
  23. /* First: invalid exception. */
  24. if ((FE_INVALID & excepts) != 0)
  25. {
  26. /* One example of an invalid operation is 0.0 / 0.0. */
  27. float f = 0.0;
  28. __asm__ __volatile__ ("divss %0, %0 " : : "x" (f));
  29. (void) &f;
  30. }
  31. /* Next: division by zero. */
  32. if ((FE_DIVBYZERO & excepts) != 0)
  33. {
  34. float f = 1.0;
  35. float g = 0.0;
  36. __asm__ __volatile__ ("divss %1, %0" : : "x" (f), "x" (g));
  37. (void) &f;
  38. }
  39. /* Next: overflow. */
  40. if ((FE_OVERFLOW & excepts) != 0)
  41. {
  42. /* XXX: Is it ok to only set the x87 FPU? */
  43. /* There is no way to raise only the overflow flag. Do it the
  44. hard way. */
  45. fenv_t temp;
  46. /* Bah, we have to clear selected exceptions. Since there is no
  47. `fldsw' instruction we have to do it the hard way. */
  48. __asm__ __volatile__ ("fnstenv %0" : "=m" (*&temp));
  49. /* Set the relevant bits. */
  50. temp.__status_word |= FE_OVERFLOW;
  51. /* Put the new data in effect. */
  52. __asm__ __volatile__ ("fldenv %0" : : "m" (*&temp));
  53. /* And raise the exception. */
  54. __asm__ __volatile__ ("fwait");
  55. }
  56. /* Next: underflow. */
  57. if ((FE_UNDERFLOW & excepts) != 0)
  58. {
  59. /* XXX: Is it ok to only set the x87 FPU? */
  60. /* There is no way to raise only the underflow flag. Do it the
  61. hard way. */
  62. fenv_t temp;
  63. /* Bah, we have to clear selected exceptions. Since there is no
  64. `fldsw' instruction we have to do it the hard way. */
  65. __asm__ __volatile__ ("fnstenv %0" : "=m" (*&temp));
  66. /* Set the relevant bits. */
  67. temp.__status_word |= FE_UNDERFLOW;
  68. /* Put the new data in effect. */
  69. __asm__ __volatile__ ("fldenv %0" : : "m" (*&temp));
  70. /* And raise the exception. */
  71. __asm__ __volatile__ ("fwait");
  72. }
  73. /* Last: inexact. */
  74. if ((FE_INEXACT & excepts) != 0)
  75. {
  76. /* XXX: Is it ok to only set the x87 FPU? */
  77. /* There is no way to raise only the inexact flag. Do it the
  78. hard way. */
  79. fenv_t temp;
  80. /* Bah, we have to clear selected exceptions. Since there is no
  81. `fldsw' instruction we have to do it the hard way. */
  82. __asm__ __volatile__ ("fnstenv %0" : "=m" (*&temp));
  83. /* Set the relevant bits. */
  84. temp.__status_word |= FE_INEXACT;
  85. /* Put the new data in effect. */
  86. __asm__ __volatile__ ("fldenv %0" : : "m" (*&temp));
  87. /* And raise the exception. */
  88. __asm__ __volatile__ ("fwait");
  89. }
  90. /* Success. */
  91. return 0;
  92. }