1 /* Raise given exceptions.
2    Copyright (C) 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 <fpu_control.h>
21 #include <float.h>
22 
23 int
__feraiseexcept(int excepts)24 __feraiseexcept (int excepts)
25 {
26   const float fp_zero = 0.0f;
27   const float fp_one = 1.0f;
28   const float fp_max = FLT_MAX;
29   const float fp_min = FLT_MIN;
30   const float fp_1e32 = 1.0e32f;
31   const float fp_two = 2.0f;
32   const float fp_three = 3.0f;
33 
34   /* Raise exceptions represented by EXPECTS.  But we must raise only
35      one signal at a time.  It is important that if the overflow/underflow
36      exception and the inexact exception are given at the same time,
37      the overflow/underflow exception follows the inexact exception.  */
38 
39   /* First: invalid exception.  */
40   if (FE_INVALID & excepts)
41     __asm__ __volatile__("fdiv.s $f0,%0,%0\n\t"
42 			 :
43 			 : "f"(fp_zero)
44 			 : "$f0");
45 
46   /* Next: division by zero.  */
47   if (FE_DIVBYZERO & excepts)
48     __asm__ __volatile__("fdiv.s $f0,%0,%1\n\t"
49 			 :
50 			 : "f"(fp_one), "f"(fp_zero)
51 			 : "$f0");
52 
53   /* Next: overflow.  */
54   if (FE_OVERFLOW & excepts)
55     /* There's no way to raise overflow without also raising inexact.  */
56     __asm__ __volatile__("fadd.s $f0,%0,%1\n\t"
57 			 :
58 			 : "f"(fp_max), "f"(fp_1e32)
59 			 : "$f0");
60 
61   /* Next: underflow.  */
62   if (FE_UNDERFLOW & excepts)
63     __asm__ __volatile__("fdiv.s $f0,%0,%1\n\t"
64 			 :
65 			 : "f"(fp_min), "f"(fp_three)
66 			 : "$f0");
67 
68   /* Last: inexact.  */
69   if (FE_INEXACT & excepts)
70     __asm__ __volatile__("fdiv.s $f0, %0, %1\n\t"
71 			 :
72 			 : "f"(fp_two), "f"(fp_three)
73 			 : "$f0");
74 
75   /* Success.  */
76   return 0;
77 }
78 
79 libm_hidden_def (__feraiseexcept) weak_alias (__feraiseexcept, feraiseexcept)
80 libm_hidden_weak (feraiseexcept)
81