78a881117e0a1c8854d6f06b3a45342b86cd8bd4
[platform/upstream/glibc.git] / sysdeps / ia64 / fpu / fraiseexcpt.c
1 /* Raise given exceptions.
2    Copyright (C) 1997, 1998, 2000 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4    Contributed by Jes Sorensen <Jes.Sorensen@cern.ch>, 2000.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Library General Public License as
8    published by the Free Software Foundation; either version 2 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Library General Public License for more details.
15
16    You should have received a copy of the GNU Library General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <fenv.h>
22 #include <math.h>
23 #include <signal.h>
24 #include <unistd.h>
25 #include <asm/fpu.h>
26
27 int
28 feraiseexcept (int excepts)
29 {
30   fenv_t fpsr;
31   double tmp;
32   double dummy;
33
34   /* Raise exceptions represented by EXPECTS.  But we must raise only
35      one signal at a time.  It is important the if the overflow/underflow
36      exception and the inexact exception are given at the same time,
37      the overflow/underflow exception precedes the inexact exception.  */
38
39   /* We do these bits in assembly to be certain GCC doesn't optimize
40      away something important.  */
41
42   /* First: invalid exception.  */
43   if (FE_INVALID & excepts)
44     {
45       /* One example of a invalid operation is 0 * Infinity.  */
46       tmp = 0;
47       __asm__ __volatile__ ("frcpa.s0 %0,p1=f0,f0" : "=f" (tmp) : : "p1" );
48     }
49
50   /* Next: division by zero.  */
51   if (FE_DIVBYZERO & excepts)
52     __asm__ __volatile__ ("frcpa.s0 %0,p1=f1,f0" : "=f" (tmp) : : "p1" );
53
54   /* XXX There seem to be no reliable way to generate
55      overflow/underflow exceptions without causing inexact exceptions
56      as well.  */
57   /* Next: overflow.  */
58   if (FE_OVERFLOW & excepts)
59     {
60       __asm__ __volatile__ ("mov.m %0=ar.fpsr" : "=r" (fpsr));
61       fpsr |= (FE_OVERFLOW << 13);
62       __asm__ __volatile__ ("mov.m ar.fpsr=%0" : : "r" (fpsr));
63
64       if (!((unsigned long int) fpsr & FE_OVERFLOW))
65         {
66           pid_t pid = getpid ();
67           kill (pid, SIGFPE);
68         }
69     }
70
71   /* Next: underflow.  */
72   if (FE_UNDERFLOW & excepts)
73     {
74       __asm__ __volatile__ ("mov.m %0=ar.fpsr" : "=r" (fpsr));
75       fpsr |= (FE_UNDERFLOW << 13);
76       __asm__ __volatile__ ("mov.m ar.fpsr=%0" : : "r" (fpsr));
77
78       if (!((unsigned long int) fpsr & FE_UNDERFLOW))
79         {
80           pid_t pid = getpid();
81           kill (pid, SIGFPE);
82         }
83   }
84
85   /* Last: inexact.  */
86   if (FE_INEXACT & excepts)
87     {
88       dummy = DBL_MAX;
89       __asm__ __volatile__ ("fadd.d.s0 %0=%1,f1" : "=f" (dummy) : "0" (dummy));
90     }
91
92   /* Success.  */
93   return 0;
94 }