1 /* Raise given exceptions.
2 Copyright (C) 1997-2022 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 Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <fenv.h>
20 #include <float.h>
21 #include <math.h>
22 #include <signal.h>
23 #include <unistd.h>
24
25 int
__feraiseexcept(int excepts)26 __feraiseexcept (int excepts)
27 {
28 double tmp;
29 double dummy;
30
31 /* Raise exceptions represented by EXPECTS. But we must raise only
32 one signal at a time. It is important the if the overflow/underflow
33 exception and the inexact exception are given at the same time,
34 the overflow/underflow exception precedes the inexact exception. */
35
36 /* We do these bits in assembly to be certain GCC doesn't optimize
37 away something important. */
38
39 /* First: invalid exception. */
40 if (FE_INVALID & excepts)
41 {
42 /* One example of an invalid operation is 0 * Infinity. */
43 tmp = 0;
44 __asm__ __volatile__ ("frcpa.s0 %0,p1=f0,f0" : "=f" (tmp) : : "p1" );
45 }
46
47 /* Next: division by zero. */
48 if (FE_DIVBYZERO & excepts)
49 __asm__ __volatile__ ("frcpa.s0 %0,p1=f1,f0" : "=f" (tmp) : : "p1" );
50
51 /* Next: overflow. */
52 if (FE_OVERFLOW & excepts)
53 {
54 dummy = DBL_MAX;
55
56 __asm__ __volatile__ ("fadd.d.s0 %0=%1,%1" : "=f" (dummy) : "0" (dummy));
57 }
58
59 /* Next: underflow. */
60 if (FE_UNDERFLOW & excepts)
61 {
62 dummy = DBL_MIN;
63
64 __asm__ __volatile__ ("fnma.d.s0 %0=%1,%1,f0" : "=f" (tmp) : "f" (dummy));
65 }
66
67 /* Last: inexact. */
68 if (FE_INEXACT & excepts)
69 {
70 dummy = DBL_MAX;
71 __asm__ __volatile__ ("fsub.d.s0 %0=%1,f1" : "=f" (dummy) : "0" (dummy));
72 }
73
74 /* Success. */
75 return 0;
76 }
77 libm_hidden_def (__feraiseexcept)
78 weak_alias (__feraiseexcept, feraiseexcept)
79 libm_hidden_weak (feraiseexcept)
80