Back to index

glibc  2.9
fraiseexcpt.c
Go to the documentation of this file.
00001 /* Raise given exceptions.
00002    Copyright (C) 2000, 2002 Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004    Contributed by Denis Joseph Barrow (djbarrow@de.ibm.com) and
00005    Martin Schwidefsky (schwidefsky@de.ibm.com).
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_libc.h>
00023 #include <float.h>
00024 #include <math.h>
00025 
00026 
00027 static __inline__ void
00028 fexceptdiv (float d, float e)
00029 {
00030   __asm__ __volatile__ ("debr %0,%1" : : "f" (d), "f" (e) );
00031 }
00032 
00033 static __inline__ void
00034 fexceptadd (float d, float e)
00035 {
00036   __asm__ __volatile__ ("aebr %0,%1" : : "f" (d), "f" (e) );
00037 }
00038 
00039 
00040 int
00041 feraiseexcept (int excepts)
00042 {
00043   /* Raise exceptions represented by EXPECTS.  But we must raise only
00044      one signal at a time.  It is important that if the overflow/underflow
00045      exception and the inexact exception are given at the same time,
00046      the overflow/underflow exception follows the inexact exception.  */
00047 
00048   /* First: invalid exception.  */
00049   if (FE_INVALID & excepts)
00050     fexceptdiv (0.0, 0.0);
00051 
00052   /* Next: division by zero.  */
00053   if (FE_DIVBYZERO & excepts)
00054     fexceptdiv (1.0, 0.0);
00055 
00056   /* Next: overflow.  */
00057   if (FE_OVERFLOW & excepts)
00058     /* I don't think we can do the same trick as intel so we will have
00059        to live with inexact coming also.  */
00060     fexceptadd (FLT_MAX, 1.0e32);
00061 
00062   /* Next: underflow.  */
00063   if (FE_UNDERFLOW & excepts)
00064     fexceptdiv (FLT_MIN, 3.0);
00065 
00066   /* Last: inexact.  */
00067   if (FE_INEXACT & excepts)
00068     fexceptdiv (2.0, 3.0);
00069 
00070   /* Success.  */
00071   return 0;
00072 }
00073 libm_hidden_def (feraiseexcept)