2013-06-01 Janus Weil <janus@gcc.gnu.org>
[official-gcc.git] / libquadmath / math / ctanq.c
blobd390511ca2008c3f10162dc76e765a6200eb1973
1 /* Complex tangent 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.
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.
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.
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/>. */
20 #include "quadmath-imp.h"
22 #ifdef HAVE_FENV_H
23 # include <fenv.h>
24 #endif
27 __complex128
28 ctanq (__complex128 x)
30 __complex128 res;
32 if (__builtin_expect (!finiteq (__real__ x) || !finiteq (__imag__ x), 0))
34 if (__quadmath_isinf_nsq (__imag__ x))
36 __real__ res = copysignq (0.0Q, __real__ x);
37 __imag__ res = copysignq (1.0Q, __imag__ x);
39 else if (__real__ x == 0.0Q)
41 res = x;
43 else
45 __real__ res = nanq ("");
46 __imag__ res = nanq ("");
48 #ifdef HAVE_FENV_H
49 if (__quadmath_isinf_nsq (__real__ x))
50 feraiseexcept (FE_INVALID);
51 #endif
54 else
56 __float128 sinrx, cosrx;
57 __float128 den;
58 const int t = (int) ((FLT128_MAX_EXP - 1) * M_LN2q / 2.0Q);
59 int rcls = fpclassifyq (__real__ x);
61 /* tan(x+iy) = (sin(2x) + i*sinh(2y))/(cos(2x) + cosh(2y))
62 = (sin(x)*cos(x) + i*sinh(y)*cosh(y)/(cos(x)^2 + sinh(y)^2). */
64 if (__builtin_expect (rcls != QUADFP_SUBNORMAL, 1))
66 sincosq (__real__ x, &sinrx, &cosrx);
68 else
70 sinrx = __real__ x;
71 cosrx = 1.0Q;
74 if (fabsq (__imag__ x) > t)
76 /* Avoid intermediate overflow when the real part of the
77 result may be subnormal. Ignoring negligible terms, the
78 imaginary part is +/- 1, the real part is
79 sin(x)*cos(x)/sinh(y)^2 = 4*sin(x)*cos(x)/exp(2y). */
80 __float128 exp_2t = expq (2 * t);
82 __imag__ res = copysignq (1.0Q, __imag__ x);
83 __real__ res = 4 * sinrx * cosrx;
84 __imag__ x = fabsq (__imag__ x);
85 __imag__ x -= t;
86 __real__ res /= exp_2t;
87 if (__imag__ x > t)
89 /* Underflow (original imaginary part of x has absolute
90 value > 2t). */
91 __real__ res /= exp_2t;
93 else
94 __real__ res /= expq (2 * __imag__ x);
96 else
98 __float128 sinhix, coshix;
99 if (fabsq (__imag__ x) > FLT128_MIN)
101 sinhix = sinhq (__imag__ x);
102 coshix = coshq (__imag__ x);
104 else
106 sinhix = __imag__ x;
107 coshix = 1.0Q;
110 if (fabsq (sinhix) > fabsq (cosrx) * FLT128_EPSILON)
111 den = cosrx * cosrx + sinhix * sinhix;
112 else
113 den = cosrx * cosrx;
114 __real__ res = sinrx * cosrx / den;
115 __imag__ res = sinhix * coshix / den;
119 return res;