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