1 /* Raise given exceptions.
2    Copyright (C) 2000-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_libc.h>
20 #include <float.h>
21 #include <math.h>
22 
23 
24 static __inline__ void
fexceptdiv(float d,float e)25 fexceptdiv (float d, float e)
26 {
27   __asm__ __volatile__ ("debr %0,%1" : : "f" (d), "f" (e) );
28 }
29 
30 static __inline__ void
fexceptadd(float d,float e)31 fexceptadd (float d, float e)
32 {
33   __asm__ __volatile__ ("aebr %0,%1" : : "f" (d), "f" (e) );
34 }
35 
36 #ifdef HAVE_S390_MIN_Z196_ZARCH_ASM_SUPPORT
37 static __inline__ void
fexceptround(double e)38 fexceptround (double e)
39 {
40   float d;
41   /* Load rounded from double to float with M3 = round toward 0, M4 = Suppress
42      IEEE-inexact exception.
43      In case of e=0x1p128 and the overflow-mask bit is zero, only the
44      IEEE-overflow flag is set. If overflow-mask bit is one, DXC field is set to
45      0x20 "IEEE overflow, exact".
46      In case of e=0x1p-150 and the underflow-mask bit is zero, only the
47      IEEE-underflow flag is set. If underflow-mask bit is one, DXC field is set
48      to 0x10 "IEEE underflow, exact".
49      This instruction is available with a zarch machine >= z196.  */
50   __asm__ __volatile__ ("ledbra %0,5,%1,4" : "=f" (d) : "f" (e) );
51 }
52 #endif
53 
54 int
__feraiseexcept(int excepts)55 __feraiseexcept (int excepts)
56 {
57   /* Raise exceptions represented by EXPECTS.  But we must raise only
58      one signal at a time.  It is important that if the overflow/underflow
59      exception and the inexact exception are given at the same time,
60      the overflow/underflow exception follows the inexact exception.  */
61 
62   /* First: invalid exception.  */
63   if (FE_INVALID & excepts)
64     fexceptdiv (0.0, 0.0);
65 
66   /* Next: division by zero.  */
67   if (FE_DIVBYZERO & excepts)
68     fexceptdiv (1.0, 0.0);
69 
70   /* Next: overflow.  */
71   if (FE_OVERFLOW & excepts)
72     {
73 #ifdef HAVE_S390_MIN_Z196_ZARCH_ASM_SUPPORT
74       fexceptround (0x1p128);
75 #else
76       /* If overflow-mask bit is zero, both IEEE-overflow and IEEE-inexact flags
77 	 are set.  If overflow-mask bit is one, DXC field is set to 0x2C "IEEE
78 	 overflow, inexact and incremented".  */
79       fexceptadd (FLT_MAX, 1.0e32);
80 #endif
81     }
82 
83   /* Next: underflow.  */
84   if (FE_UNDERFLOW & excepts)
85     {
86 #ifdef HAVE_S390_MIN_Z196_ZARCH_ASM_SUPPORT
87       fexceptround (0x1p-150);
88 #else
89       /* If underflow-mask bit is zero, both IEEE-underflow and IEEE-inexact
90 	 flags are set.  If underflow-mask bit is one, DXC field is set to 0x1C
91 	 "IEEE underflow, inexact and incremented".  */
92       fexceptdiv (FLT_MIN, 3.0);
93 #endif
94     }
95 
96   /* Last: inexact.  */
97   if (FE_INEXACT & excepts)
98     fexceptdiv (2.0, 3.0);
99 
100   /* Success.  */
101   return 0;
102 }
103 libm_hidden_def (__feraiseexcept)
104 weak_alias (__feraiseexcept, feraiseexcept)
105 libm_hidden_weak (feraiseexcept)
106