chiark / gitweb /
eglibc (2.11.3-4+deb6u3) squeeze-lts; urgency=medium
[eglibc.git] / ports / sysdeps / arm / eabi / fraiseexcpt.c
1 /* Raise given exceptions.
2    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
3    This file is part of the GNU C Library.
4
5    The GNU C Library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License as published by the Free Software Foundation; either
8    version 2.1 of the License, or (at your option) any later version.
9
10    The GNU C Library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Lesser General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with GCC; see the file COPYING.  If not, write to the Free
17    Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA.  */
19
20 #include <fpu_control.h>
21 #include <fenv.h>
22 #include <float.h>
23
24 #include <unistd.h>
25 #include <ldsodefs.h>
26 #include <dl-procinfo.h>
27 #include <sysdep.h>
28
29 int
30 feraiseexcept (int excepts)
31 {
32   if (GLRO (dl_hwcap) & HWCAP_ARM_VFP)
33     {
34       int fpscr;
35       const float fp_zero = 0.0, fp_one = 1.0, fp_max = FLT_MAX,
36                   fp_min = FLT_MIN, fp_1e32 = 1.0e32f, fp_two = 2.0,
37                   fp_three = 3.0;
38
39       /* Raise exceptions represented by EXPECTS.  But we must raise only
40          one signal at a time.  It is important that if the overflow/underflow
41          exception and the inexact exception are given at the same time,
42          the overflow/underflow exception follows the inexact exception.  After
43          each exception we read from the fpscr, to force the exception to be
44          raised immediately.  */
45
46       /* There are additional complications because this file may be compiled
47          without VFP support enabled, and we also can't assume that the
48          assembler has VFP instructions enabled. To get around this we use the
49          generic coprocessor mnemonics and avoid asking GCC to put float values
50          in VFP registers.  */
51
52       /* First: invalid exception.  */
53       if (FE_INVALID & excepts)
54         __asm__ __volatile__ (
55           "ldc p10, cr0, %1\n\t"                        /* flds s0, %1  */
56           "cdp p10, 8, cr0, cr0, cr0, 0\n\t"            /* fdivs s0, s0, s0  */
57           "mrc p10, 7, %0, cr1, cr0, 0" : "=r" (fpscr)  /* fmrx %0, fpscr  */
58                                         : "m" (fp_zero)
59                                         : "s0");
60
61       /* Next: division by zero.  */
62       if (FE_DIVBYZERO & excepts)
63         __asm__ __volatile__ (
64           "ldc p10, cr0, %1\n\t"                        /* flds s0, %1  */
65           "ldcl p10, cr0, %2\n\t"                       /* flds s1, %2  */
66           "cdp p10, 8, cr0, cr0, cr0, 1\n\t"            /* fdivs s0, s0, s1  */
67           "mrc p10, 7, %0, cr1, cr0, 0" : "=r" (fpscr)  /* fmrx %0, fpscr  */
68                                         : "m" (fp_one), "m" (fp_zero)
69                                         : "s0", "s1");
70
71       /* Next: overflow.  */
72       if (FE_OVERFLOW & excepts)
73         /* There's no way to raise overflow without also raising inexact.  */
74         __asm__ __volatile__ (
75           "ldc p10, cr0, %1\n\t"                        /* flds s0, %1  */
76           "ldcl p10, cr0, %2\n\t"                       /* flds s1, %2  */
77           "cdp p10, 3, cr0, cr0, cr0, 1\n\t"            /* fadds s0, s0, s1  */
78           "mrc p10, 7, %0, cr1, cr0, 0" : "=r" (fpscr)  /* fmrx %0, fpscr  */
79                                         : "m" (fp_max), "m" (fp_1e32)
80                                         : "s0", "s1");
81
82       /* Next: underflow.  */
83       if (FE_UNDERFLOW & excepts)
84         __asm__ __volatile__ (
85           "ldc p10, cr0, %1\n\t"                        /* flds s0, %1  */
86           "ldcl p10, cr0, %2\n\t"                       /* flds s1, %2  */
87           "cdp p10, 8, cr0, cr0, cr0, 1\n\t"            /* fdivs s0, s0, s1  */
88           "mrc p10, 7, %0, cr1, cr0, 0" : "=r" (fpscr)  /* fmrx %0, fpscr  */
89                                         : "m" (fp_min), "m" (fp_three)
90                                         : "s0", "s1");
91
92       /* Last: inexact.  */
93       if (FE_INEXACT & excepts)
94         __asm__ __volatile__ (
95           "ldc p10, cr0, %1\n\t"                        /* flds s0, %1  */
96           "ldcl p10, cr0, %2\n\t"                       /* flds s1, %2  */
97           "cdp p10, 8, cr0, cr0, cr0, 1\n\t"            /* fdivs s0, s0, s1  */
98           "mrc p10, 7, %0, cr1, cr0, 0" : "=r" (fpscr)  /* fmrx %0, fpscr  */
99                                         : "m" (fp_two), "m" (fp_three)
100                                         : "s0", "s1");
101
102       /* Success.  */
103       return 0;
104     }
105
106   /* Unsupported, so fail.  */
107   return 1;
108 }
109
110 libm_hidden_def (feraiseexcept)