Back to index

glibc  2.9
fraiseexcpt.c
Go to the documentation of this file.
00001 /* Raise given exceptions.
00002    Copyright (C) 2001, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <fenv.h>
00021 #include <math.h>
00022 
00023 int
00024 feraiseexcept (int excepts)
00025 {
00026   /* Raise exceptions represented by EXPECTS.  But we must raise only
00027      one signal at a time.  It is important that if the overflow/underflow
00028      exception and the inexact exception are given at the same time,
00029      the overflow/underflow exception follows the inexact exception.  */
00030 
00031   /* First: invalid exception.  */
00032   if ((FE_INVALID & excepts) != 0)
00033     {
00034       /* One example of a invalid operation is 0.0 / 0.0.  */
00035       float f = 0.0;
00036 
00037       __asm__ __volatile__ ("divss %0, %0 " : : "x" (f));
00038       (void) &f;
00039     }
00040 
00041   /* Next: division by zero.  */
00042   if ((FE_DIVBYZERO & excepts) != 0)
00043     {
00044       float f = 1.0;
00045       float g = 0.0;
00046 
00047       __asm__ __volatile__ ("divss %1, %0" : : "x" (f), "x" (g));
00048       (void) &f;
00049     }
00050 
00051   /* Next: overflow.  */
00052   if ((FE_OVERFLOW & excepts) != 0)
00053     {
00054       /* XXX: Is it ok to only set the x87 FPU?  */
00055       /* There is no way to raise only the overflow flag.  Do it the
00056         hard way.  */
00057       fenv_t temp;
00058 
00059       /* Bah, we have to clear selected exceptions.  Since there is no
00060         `fldsw' instruction we have to do it the hard way.  */
00061       __asm__ __volatile__ ("fnstenv %0" : "=m" (*&temp));
00062 
00063       /* Set the relevant bits.  */
00064       temp.__status_word |= FE_OVERFLOW;
00065 
00066       /* Put the new data in effect.  */
00067       __asm__ __volatile__ ("fldenv %0" : : "m" (*&temp));
00068 
00069       /* And raise the exception.  */
00070       __asm__ __volatile__ ("fwait");
00071     }
00072 
00073   /* Next: underflow.  */
00074   if ((FE_UNDERFLOW & excepts) != 0)
00075     {
00076       /* XXX: Is it ok to only set the x87 FPU?  */
00077       /* There is no way to raise only the underflow flag.  Do it the
00078         hard way.  */
00079       fenv_t temp;
00080 
00081       /* Bah, we have to clear selected exceptions.  Since there is no
00082         `fldsw' instruction we have to do it the hard way.  */
00083       __asm__ __volatile__ ("fnstenv %0" : "=m" (*&temp));
00084 
00085       /* Set the relevant bits.  */
00086       temp.__status_word |= FE_UNDERFLOW;
00087 
00088       /* Put the new data in effect.  */
00089       __asm__ __volatile__ ("fldenv %0" : : "m" (*&temp));
00090 
00091       /* And raise the exception.  */
00092       __asm__ __volatile__ ("fwait");
00093     }
00094 
00095   /* Last: inexact.  */
00096   if ((FE_INEXACT & excepts) != 0)
00097     {
00098       /* XXX: Is it ok to only set the x87 FPU?  */
00099       /* There is no way to raise only the inexact flag.  Do it the
00100         hard way.  */
00101       fenv_t temp;
00102 
00103       /* Bah, we have to clear selected exceptions.  Since there is no
00104         `fldsw' instruction we have to do it the hard way.  */
00105       __asm__ __volatile__ ("fnstenv %0" : "=m" (*&temp));
00106 
00107       /* Set the relevant bits.  */
00108       temp.__status_word |= FE_INEXACT;
00109 
00110       /* Put the new data in effect.  */
00111       __asm__ __volatile__ ("fldenv %0" : : "m" (*&temp));
00112 
00113       /* And raise the exception.  */
00114       __asm__ __volatile__ ("fwait");
00115     }
00116 
00117   /* Success.  */
00118   return 0;
00119 }
00120 libm_hidden_def (feraiseexcept)