1 /* Raise given exceptions.
2    Copyright (C) 1997-2025 Free Software Foundation, Inc.
3 
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8 
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library.  If not, see
16    <https://www.gnu.org/licenses/>.  */
17 
18 #include <fenv.h>
19 #include <float.h>
20 #include <math.h>
21 
22 int
feraiseexcept(int excepts)23 feraiseexcept (int excepts)
24 {
25 #if defined(__mcffpu__)
26 
27   /* The Coldfire FPU allows an exception to be raised by asserting
28      the associated EXC bit and then executing an arbitrary arithmetic
29      instruction.  fmove.l is classified as an arithmetic instruction
30      and suffices for this purpose.
31 
32      We therefore raise an exception by setting both the EXC and AEXC
33      bit associated with the exception (the former being 6 bits to the
34      left of the latter) and then loading the longword at (%sp) into an
35      FP register.  */
36 
37   inline void
38   raise_one_exception (int mask)
39   {
40     if (excepts & mask)
41       {
42 	int fpsr;
43 	double unused;
44 
45 	__asm__ volatile ("fmove%.l %/fpsr,%0" : "=d" (fpsr));
46 	fpsr |= (mask << 6) | mask;
47 	__asm__ volatile ("fmove%.l %0,%/fpsr" :: "d" (fpsr));
48 	__asm__ volatile ("fmove%.l (%%sp),%0" : "=f" (unused));
49       }
50   }
51 
52   raise_one_exception (FE_INVALID);
53   raise_one_exception (FE_DIVBYZERO);
54   raise_one_exception (FE_OVERFLOW);
55   raise_one_exception (FE_UNDERFLOW);
56   raise_one_exception (FE_INEXACT);
57 
58 #else
59   /* Raise exceptions represented by EXCEPTS.  But we must raise only one
60      signal at a time.  It is important that if the overflow/underflow
61      exception and the divide by zero exception are given at the same
62      time, the overflow/underflow exception follows the divide by zero
63      exception.  */
64 
65   /* First: invalid exception.  */
66   if (excepts & FE_INVALID)
67     {
68       /* One example of an invalid operation is 0 * Infinity.  */
69       double d = HUGE_VAL;
70       __asm__ __volatile__ ("fmul%.s %#0r0,%0; fnop" : "=f" (d) : "0" (d));
71     }
72 
73   /* Next: division by zero.  */
74   if (excepts & FE_DIVBYZERO)
75     {
76       double d = 1.0;
77       __asm__ __volatile__ ("fdiv%.s %#0r0,%0; fnop" : "=f" (d) : "0" (d));
78     }
79 
80   /* Next: overflow.  */
81   if (excepts & FE_OVERFLOW)
82     {
83       long double d = LDBL_MAX;
84 
85       __asm__ __volatile__ ("fmul%.x %0,%0; fnop" : "=f" (d) : "0" (d));
86     }
87 
88   /* Next: underflow.  */
89   if (excepts & FE_UNDERFLOW)
90     {
91       long double d = -LDBL_MAX;
92 
93       __asm__ __volatile__ ("fetox%.x %0; fnop" : "=f" (d) : "0" (d));
94     }
95 
96   /* Last: inexact.  */
97   if (excepts & FE_INEXACT)
98     {
99       long double d = 1.0;
100       __asm__ __volatile__ ("fdiv%.s %#0r3,%0; fnop" : "=f" (d) : "0" (d));
101     }
102 
103 #endif
104   /* Success.  */
105   return 0;
106 }
107