xref: /netbsd-src/external/gpl3/gcc/dist/libquadmath/math/csinhq.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1*181254a7Smrg /* Complex sine hyperbole function for float types.
2*181254a7Smrg    Copyright (C) 1997-2018 Free Software Foundation, Inc.
3*181254a7Smrg    This file is part of the GNU C Library.
4*181254a7Smrg    Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997.
5*181254a7Smrg 
6*181254a7Smrg    The GNU C Library is free software; you can redistribute it and/or
7*181254a7Smrg    modify it under the terms of the GNU Lesser General Public
8*181254a7Smrg    License as published by the Free Software Foundation; either
9*181254a7Smrg    version 2.1 of the License, or (at your option) any later version.
10*181254a7Smrg 
11*181254a7Smrg    The GNU C Library is distributed in the hope that it will be useful,
12*181254a7Smrg    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*181254a7Smrg    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14*181254a7Smrg    Lesser General Public License for more details.
15*181254a7Smrg 
16*181254a7Smrg    You should have received a copy of the GNU Lesser General Public
17*181254a7Smrg    License along with the GNU C Library; if not, see
18*181254a7Smrg    <http://www.gnu.org/licenses/>.  */
19*181254a7Smrg 
20*181254a7Smrg #include "quadmath-imp.h"
21*181254a7Smrg 
22*181254a7Smrg __complex128
csinhq(__complex128 x)23*181254a7Smrg csinhq (__complex128 x)
24*181254a7Smrg {
25*181254a7Smrg   __complex128 retval;
26*181254a7Smrg   int negate = signbitq (__real__ x);
27*181254a7Smrg   int rcls = fpclassifyq (__real__ x);
28*181254a7Smrg   int icls = fpclassifyq (__imag__ x);
29*181254a7Smrg 
30*181254a7Smrg   __real__ x = fabsq (__real__ x);
31*181254a7Smrg 
32*181254a7Smrg   if (__glibc_likely (rcls >= QUADFP_ZERO))
33*181254a7Smrg     {
34*181254a7Smrg       /* Real part is finite.  */
35*181254a7Smrg       if (__glibc_likely (icls >= QUADFP_ZERO))
36*181254a7Smrg 	{
37*181254a7Smrg 	  /* Imaginary part is finite.  */
38*181254a7Smrg 	  const int t = (int) ((FLT128_MAX_EXP - 1) * M_LN2q);
39*181254a7Smrg 	  __float128 sinix, cosix;
40*181254a7Smrg 
41*181254a7Smrg 	  if (__glibc_likely (fabsq (__imag__ x) > FLT128_MIN))
42*181254a7Smrg 	    {
43*181254a7Smrg 	      sincosq (__imag__ x, &sinix, &cosix);
44*181254a7Smrg 	    }
45*181254a7Smrg 	  else
46*181254a7Smrg 	    {
47*181254a7Smrg 	      sinix = __imag__ x;
48*181254a7Smrg 	      cosix = 1;
49*181254a7Smrg 	    }
50*181254a7Smrg 
51*181254a7Smrg 	  if (negate)
52*181254a7Smrg 	    cosix = -cosix;
53*181254a7Smrg 
54*181254a7Smrg 	  if (fabsq (__real__ x) > t)
55*181254a7Smrg 	    {
56*181254a7Smrg 	      __float128 exp_t = expq (t);
57*181254a7Smrg 	      __float128 rx = fabsq (__real__ x);
58*181254a7Smrg 	      if (signbitq (__real__ x))
59*181254a7Smrg 		cosix = -cosix;
60*181254a7Smrg 	      rx -= t;
61*181254a7Smrg 	      sinix *= exp_t / 2;
62*181254a7Smrg 	      cosix *= exp_t / 2;
63*181254a7Smrg 	      if (rx > t)
64*181254a7Smrg 		{
65*181254a7Smrg 		  rx -= t;
66*181254a7Smrg 		  sinix *= exp_t;
67*181254a7Smrg 		  cosix *= exp_t;
68*181254a7Smrg 		}
69*181254a7Smrg 	      if (rx > t)
70*181254a7Smrg 		{
71*181254a7Smrg 		  /* Overflow (original real part of x > 3t).  */
72*181254a7Smrg 		  __real__ retval = FLT128_MAX * cosix;
73*181254a7Smrg 		  __imag__ retval = FLT128_MAX * sinix;
74*181254a7Smrg 		}
75*181254a7Smrg 	      else
76*181254a7Smrg 		{
77*181254a7Smrg 		  __float128 exp_val = expq (rx);
78*181254a7Smrg 		  __real__ retval = exp_val * cosix;
79*181254a7Smrg 		  __imag__ retval = exp_val * sinix;
80*181254a7Smrg 		}
81*181254a7Smrg 	    }
82*181254a7Smrg 	  else
83*181254a7Smrg 	    {
84*181254a7Smrg 	      __real__ retval = sinhq (__real__ x) * cosix;
85*181254a7Smrg 	      __imag__ retval = coshq (__real__ x) * sinix;
86*181254a7Smrg 	    }
87*181254a7Smrg 
88*181254a7Smrg 	  math_check_force_underflow_complex (retval);
89*181254a7Smrg 	}
90*181254a7Smrg       else
91*181254a7Smrg 	{
92*181254a7Smrg 	  if (rcls == QUADFP_ZERO)
93*181254a7Smrg 	    {
94*181254a7Smrg 	      /* Real part is 0.0.  */
95*181254a7Smrg 	      __real__ retval = copysignq (0, negate ? -1 : 1);
96*181254a7Smrg 	      __imag__ retval = __imag__ x - __imag__ x;
97*181254a7Smrg 	    }
98*181254a7Smrg 	  else
99*181254a7Smrg 	    {
100*181254a7Smrg 	      __real__ retval = nanq ("");
101*181254a7Smrg 	      __imag__ retval = nanq ("");
102*181254a7Smrg 
103*181254a7Smrg 	      feraiseexcept (FE_INVALID);
104*181254a7Smrg 	    }
105*181254a7Smrg 	}
106*181254a7Smrg     }
107*181254a7Smrg   else if (rcls == QUADFP_INFINITE)
108*181254a7Smrg     {
109*181254a7Smrg       /* Real part is infinite.  */
110*181254a7Smrg       if (__glibc_likely (icls > QUADFP_ZERO))
111*181254a7Smrg 	{
112*181254a7Smrg 	  /* Imaginary part is finite.  */
113*181254a7Smrg 	  __float128 sinix, cosix;
114*181254a7Smrg 
115*181254a7Smrg 	  if (__glibc_likely (fabsq (__imag__ x) > FLT128_MIN))
116*181254a7Smrg 	    {
117*181254a7Smrg 	      sincosq (__imag__ x, &sinix, &cosix);
118*181254a7Smrg 	    }
119*181254a7Smrg 	  else
120*181254a7Smrg 	    {
121*181254a7Smrg 	      sinix = __imag__ x;
122*181254a7Smrg 	      cosix = 1;
123*181254a7Smrg 	    }
124*181254a7Smrg 
125*181254a7Smrg 	  __real__ retval = copysignq (HUGE_VALQ, cosix);
126*181254a7Smrg 	  __imag__ retval = copysignq (HUGE_VALQ, sinix);
127*181254a7Smrg 
128*181254a7Smrg 	  if (negate)
129*181254a7Smrg 	    __real__ retval = -__real__ retval;
130*181254a7Smrg 	}
131*181254a7Smrg       else if (icls == QUADFP_ZERO)
132*181254a7Smrg 	{
133*181254a7Smrg 	  /* Imaginary part is 0.0.  */
134*181254a7Smrg 	  __real__ retval = negate ? -HUGE_VALQ : HUGE_VALQ;
135*181254a7Smrg 	  __imag__ retval = __imag__ x;
136*181254a7Smrg 	}
137*181254a7Smrg       else
138*181254a7Smrg 	{
139*181254a7Smrg 	  __real__ retval = HUGE_VALQ;
140*181254a7Smrg 	  __imag__ retval = __imag__ x - __imag__ x;
141*181254a7Smrg 	}
142*181254a7Smrg     }
143*181254a7Smrg   else
144*181254a7Smrg     {
145*181254a7Smrg       __real__ retval = nanq ("");
146*181254a7Smrg       __imag__ retval = __imag__ x == 0 ? __imag__ x : nanq ("");
147*181254a7Smrg     }
148*181254a7Smrg 
149*181254a7Smrg   return retval;
150*181254a7Smrg }
151