Back to index

glibc  2.9
fraiseexcpt.c
Go to the documentation of this file.
00001 /* Raise given exceptions.
00002    Copyright (C) 2006 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 <float.h>
00022 #include <math.h>
00023 
00024 int
00025 feraiseexcept (int excepts)
00026 {
00027   /* Raise exceptions represented by EXCEPTS.  But we must raise only one
00028      signal at a time.  It is important that if the overflow/underflow
00029      exception and the divide by zero exception are given at the same
00030      time, the overflow/underflow exception follows the divide by zero
00031      exception.
00032 
00033      The Coldfire FPU allows an exception to be raised by asserting
00034      the associated EXC bit and then executing an arbitrary arithmetic
00035      instruction.  fmove.l is classified as an arithmetic instruction
00036      and suffices for this purpose.
00037 
00038      We therefore raise an exception by setting both the EXC and AEXC
00039      bit associated with the exception (the former being 6 bits to the
00040      left of the latter) and then loading the longword at (%sp) into an
00041      FP register.  */
00042 
00043   inline void
00044   raise_one_exception (int mask)
00045   {
00046     if (excepts & mask)
00047       {
00048        int fpsr;
00049        double unused;
00050 
00051        asm volatile ("fmove%.l %/fpsr,%0" : "=d" (fpsr));
00052        fpsr |= (mask << 6) | mask;
00053        asm volatile ("fmove%.l %0,%/fpsr" :: "d" (fpsr));
00054        asm volatile ("fmove%.l (%%sp),%0" : "=f" (unused));
00055       }
00056   }
00057 
00058   raise_one_exception (FE_INVALID);
00059   raise_one_exception (FE_DIVBYZERO);
00060   raise_one_exception (FE_OVERFLOW);
00061   raise_one_exception (FE_UNDERFLOW);
00062   raise_one_exception (FE_INEXACT);
00063 
00064   /* Success.  */
00065   return 0;
00066 }
00067 libm_hidden_def (feraiseexcept)