fraiseexcpt.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. #include <fenv.h>
  18. #include <math.h>
  19. libm_hidden_proto(feraiseexcept)
  20. int
  21. feraiseexcept (int excepts)
  22. {
  23. /* Raise exceptions represented by EXPECTS. But we must raise only
  24. one signal at a time. It is important that if the overflow/underflow
  25. exception and the inexact exception are given at the same time,
  26. the overflow/underflow exception follows the inexact exception. */
  27. /* First: invalid exception. */
  28. if ((FE_INVALID & excepts) != 0)
  29. {
  30. /* One example of a invalid operation is 0.0 / 0.0. */
  31. double d;
  32. __asm__ __volatile__ ("fldz; fdiv %%st, %%st(0); fwait" : "=t" (d));
  33. (void) &d;
  34. }
  35. /* Next: division by zero. */
  36. if ((FE_DIVBYZERO & excepts) != 0)
  37. {
  38. double d;
  39. __asm__ __volatile__ ("fldz; fld1; fdivp %%st, %%st(1); fwait"
  40. : "=t" (d));
  41. (void) &d;
  42. }
  43. /* Next: overflow. */
  44. if ((FE_OVERFLOW & excepts) != 0)
  45. {
  46. /* There is no way to raise only the overflow flag. Do it the
  47. hard way. */
  48. fenv_t temp;
  49. /* Bah, we have to clear selected exceptions. Since there is no
  50. `fldsw' instruction we have to do it the hard way. */
  51. __asm__ __volatile__ ("fnstenv %0" : "=m" (*&temp));
  52. /* Set the relevant bits. */
  53. temp.__status_word |= FE_OVERFLOW;
  54. /* Put the new data in effect. */
  55. __asm__ __volatile__ ("fldenv %0" : : "m" (*&temp));
  56. /* And raise the exception. */
  57. __asm__ __volatile__ ("fwait");
  58. }
  59. /* Next: underflow. */
  60. if ((FE_UNDERFLOW & excepts) != 0)
  61. {
  62. /* There is no way to raise only the underflow flag. Do it the
  63. hard way. */
  64. fenv_t temp;
  65. /* Bah, we have to clear selected exceptions. Since there is no
  66. `fldsw' instruction we have to do it the hard way. */
  67. __asm__ __volatile__ ("fnstenv %0" : "=m" (*&temp));
  68. /* Set the relevant bits. */
  69. temp.__status_word |= FE_UNDERFLOW;
  70. /* Put the new data in effect. */
  71. __asm__ __volatile__ ("fldenv %0" : : "m" (*&temp));
  72. /* And raise the exception. */
  73. __asm__ __volatile__ ("fwait");
  74. }
  75. /* Last: inexact. */
  76. if ((FE_INEXACT & excepts) != 0)
  77. {
  78. /* There is no way to raise only the inexact flag. Do it the
  79. hard way. */
  80. fenv_t temp;
  81. /* Bah, we have to clear selected exceptions. Since there is no
  82. `fldsw' instruction we have to do it the hard way. */
  83. __asm__ __volatile__ ("fnstenv %0" : "=m" (*&temp));
  84. /* Set the relevant bits. */
  85. temp.__status_word |= FE_INEXACT;
  86. /* Put the new data in effect. */
  87. __asm__ __volatile__ ("fldenv %0" : : "m" (*&temp));
  88. /* And raise the exception. */
  89. __asm__ __volatile__ ("fwait");
  90. }
  91. /* Success. */
  92. return 0;
  93. }
  94. libm_hidden_def(feraiseexcept)