Back to index

glibc  2.9
fraiseexcpt.c
Go to the documentation of this file.
00001 /* Raise given exceptions.
00002    Copyright (C) 2000, 2002, 2004 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Alexandre Oliva <aoliva@redhat.com>
00005    based on corresponding file in the M68K port.
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 <shlib-compat.h>
00026 
00027 int
00028 __feraiseexcept (int excepts)
00029 {
00030   /* Raise exceptions represented by EXCEPTS.  But we must raise only one
00031      signal at a time.  It is important that if the overflow/underflow
00032      exception and the divide by zero exception are given at the same
00033      time, the overflow/underflow exception follows the divide by zero
00034      exception.  */
00035 
00036   /* First: invalid exception.  */
00037   if (excepts & FE_INVALID)
00038     {
00039       /* One example of a invalid operation is 0 * Infinity.  */
00040       float x = HUGE_VALF, y = 0.0f;
00041       __asm__ __volatile__ ("fmul %1,%0" : "+f" (x) : "f" (y));
00042     }
00043 
00044   /* Next: division by zero.  */
00045   if (excepts & FE_DIVBYZERO)
00046     {
00047       float x = 1.0f, y = 0.0f;
00048       __asm__ __volatile__ ("fdiv %1,%0" : "+f" (x) : "f" (y));
00049     }
00050 
00051   /* Next: overflow.  */
00052   if (excepts & FE_OVERFLOW)
00053     {
00054       float x = FLT_MAX;
00055 
00056       __asm__ __volatile__ ("fmul %0,%0" : "+f" (x));
00057     }
00058 
00059   /* Next: underflow.  */
00060   if (excepts & FE_UNDERFLOW)
00061     {
00062       float x = -FLT_MIN;
00063 
00064       __asm__ __volatile__ ("fmul %0,%0" : "+f" (x));
00065     }
00066 
00067   /* Last: inexact.  */
00068   if (excepts & FE_INEXACT)
00069     {
00070       float x = 1.0f, y = 3.0f;
00071       __asm__ __volatile__ ("fdiv %1,%0" : "=f" (x) : "f" (y));
00072     }
00073 
00074   /* Success.  */
00075   return 0;
00076 }
00077 
00078 libm_hidden_ver (__feraiseexcept, feraiseexcept)
00079 versioned_symbol (libm, __feraiseexcept, feraiseexcept, GLIBC_2_2);