Back to index

glibc  2.9
fraiseexcpt.c
Go to the documentation of this file.
00001 /* Raise given exceptions.
00002    Copyright (C) 1997, 1998, 2000, 2001, 2002, 2007
00003    Free Software Foundation, Inc.
00004    This file is part of the GNU C Library.
00005    Contributed by Jes Sorensen <Jes.Sorensen@cern.ch>, 2000.
00006 
00007    The GNU C Library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2.1 of the License, or (at your option) any later version.
00011 
00012    The GNU C Library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016 
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with the GNU C Library; if not, write to the Free
00019    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00020    02111-1307 USA.  */
00021 
00022 #include <fenv.h>
00023 #include <float.h>
00024 #include <math.h>
00025 #include <signal.h>
00026 #include <unistd.h>
00027 
00028 int
00029 feraiseexcept (int excepts)
00030 {
00031   double tmp;
00032   double dummy;
00033 
00034   /* Raise exceptions represented by EXPECTS.  But we must raise only
00035      one signal at a time.  It is important the if the overflow/underflow
00036      exception and the inexact exception are given at the same time,
00037      the overflow/underflow exception precedes the inexact exception.  */
00038 
00039   /* We do these bits in assembly to be certain GCC doesn't optimize
00040      away something important.  */
00041 
00042   /* First: invalid exception.  */
00043   if (FE_INVALID & excepts)
00044     {
00045       /* One example of a invalid operation is 0 * Infinity.  */
00046       tmp = 0;
00047       __asm__ __volatile__ ("frcpa.s0 %0,p1=f0,f0" : "=f" (tmp) : : "p1" );
00048     }
00049 
00050   /* Next: division by zero.  */
00051   if (FE_DIVBYZERO & excepts)
00052     __asm__ __volatile__ ("frcpa.s0 %0,p1=f1,f0" : "=f" (tmp) : : "p1" );
00053 
00054   /* Next: overflow.  */
00055   if (FE_OVERFLOW & excepts)
00056     {
00057       dummy = DBL_MAX;
00058 
00059       __asm__ __volatile__ ("fadd.d.s0 %0=%1,%1" : "=f" (dummy) : "0" (dummy));
00060     }
00061 
00062   /* Next: underflow.  */
00063   if (FE_UNDERFLOW & excepts)
00064     {
00065       dummy = DBL_MIN;
00066 
00067       __asm__ __volatile__ ("fnma.d.s0 %0=%1,%1,f0" : "=f" (tmp) : "f" (dummy));
00068   }
00069 
00070   /* Last: inexact.  */
00071   if (FE_INEXACT & excepts)
00072     {
00073       dummy = DBL_MAX;
00074       __asm__ __volatile__ ("fsub.d.s0 %0=%1,f1" : "=f" (dummy) : "0" (dummy));
00075     }
00076 
00077   /* Success.  */
00078   return 0;
00079 }
00080 libm_hidden_def (feraiseexcept)