fraiseexcpt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /* Raise given exceptions.
  2. Copyright (C) 1997,99,2000,01,02 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #include <fenv.h>
  17. #include <math.h>
  18. libm_hidden_proto(feraiseexcept)
  19. int
  20. feraiseexcept (int excepts)
  21. {
  22. /* Raise exceptions represented by EXPECTS. But we must raise only
  23. one signal at a time. It is important that if the overflow/underflow
  24. exception and the inexact exception are given at the same time,
  25. the overflow/underflow exception follows the inexact exception. */
  26. /* First: invalid exception. */
  27. if ((FE_INVALID & excepts) != 0)
  28. {
  29. /* One example of a invalid operation is 0.0 / 0.0. */
  30. double d;
  31. __asm__ __volatile__ ("fldz; fdiv %%st, %%st(0); fwait" : "=t" (d));
  32. (void) &d;
  33. }
  34. /* Next: division by zero. */
  35. if ((FE_DIVBYZERO & excepts) != 0)
  36. {
  37. double d;
  38. __asm__ __volatile__ ("fldz; fld1; fdivp %%st, %%st(1); fwait"
  39. : "=t" (d));
  40. (void) &d;
  41. }
  42. /* Next: overflow. */
  43. if ((FE_OVERFLOW & excepts) != 0)
  44. {
  45. /* There is no way to raise only the overflow flag. Do it the
  46. hard way. */
  47. fenv_t temp;
  48. /* Bah, we have to clear selected exceptions. Since there is no
  49. `fldsw' instruction we have to do it the hard way. */
  50. __asm__ __volatile__ ("fnstenv %0" : "=m" (*&temp));
  51. /* Set the relevant bits. */
  52. temp.__status_word |= FE_OVERFLOW;
  53. /* Put the new data in effect. */
  54. __asm__ __volatile__ ("fldenv %0" : : "m" (*&temp));
  55. /* And raise the exception. */
  56. __asm__ __volatile__ ("fwait");
  57. }
  58. /* Next: underflow. */
  59. if ((FE_UNDERFLOW & excepts) != 0)
  60. {
  61. /* There is no way to raise only the underflow flag. Do it the
  62. hard way. */
  63. fenv_t temp;
  64. /* Bah, we have to clear selected exceptions. Since there is no
  65. `fldsw' instruction we have to do it the hard way. */
  66. __asm__ __volatile__ ("fnstenv %0" : "=m" (*&temp));
  67. /* Set the relevant bits. */
  68. temp.__status_word |= FE_UNDERFLOW;
  69. /* Put the new data in effect. */
  70. __asm__ __volatile__ ("fldenv %0" : : "m" (*&temp));
  71. /* And raise the exception. */
  72. __asm__ __volatile__ ("fwait");
  73. }
  74. /* Last: inexact. */
  75. if ((FE_INEXACT & excepts) != 0)
  76. {
  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. }
  93. libm_hidden_def(feraiseexcept)