Fix gnat.dg/opt39.adb on hppa.
[official-gcc.git] / libquadmath / math / cexpq.c
blob73bb4858b022fd543bfa0451b956f66bdbc7783c
1 /* Return value of complex exponential function for a float type.
2 Copyright (C) 1997-2018 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 __complex128
23 cexpq (__complex128 x)
25 __complex128 retval;
26 int rcls = fpclassifyq (__real__ x);
27 int icls = fpclassifyq (__imag__ x);
29 if (__glibc_likely (rcls >= QUADFP_ZERO))
31 /* Real part is finite. */
32 if (__glibc_likely (icls >= QUADFP_ZERO))
34 /* Imaginary part is finite. */
35 const int t = (int) ((FLT128_MAX_EXP - 1) * M_LN2q);
36 __float128 sinix, cosix;
38 if (__glibc_likely (fabsq (__imag__ x) > FLT128_MIN))
40 sincosq (__imag__ x, &sinix, &cosix);
42 else
44 sinix = __imag__ x;
45 cosix = 1;
48 if (__real__ x > t)
50 __float128 exp_t = expq (t);
51 __real__ x -= t;
52 sinix *= exp_t;
53 cosix *= exp_t;
54 if (__real__ x > t)
56 __real__ x -= t;
57 sinix *= exp_t;
58 cosix *= exp_t;
61 if (__real__ x > t)
63 /* Overflow (original real part of x > 3t). */
64 __real__ retval = FLT128_MAX * cosix;
65 __imag__ retval = FLT128_MAX * sinix;
67 else
69 __float128 exp_val = expq (__real__ x);
70 __real__ retval = exp_val * cosix;
71 __imag__ retval = exp_val * sinix;
73 math_check_force_underflow_complex (retval);
75 else
77 /* If the imaginary part is +-inf or NaN and the real part
78 is not +-inf the result is NaN + iNaN. */
79 __real__ retval = nanq ("");
80 __imag__ retval = nanq ("");
82 feraiseexcept (FE_INVALID);
85 else if (__glibc_likely (rcls == QUADFP_INFINITE))
87 /* Real part is infinite. */
88 if (__glibc_likely (icls >= QUADFP_ZERO))
90 /* Imaginary part is finite. */
91 __float128 value = signbitq (__real__ x) ? 0 : HUGE_VALQ;
93 if (icls == QUADFP_ZERO)
95 /* Imaginary part is 0.0. */
96 __real__ retval = value;
97 __imag__ retval = __imag__ x;
99 else
101 __float128 sinix, cosix;
103 if (__glibc_likely (fabsq (__imag__ x) > FLT128_MIN))
105 sincosq (__imag__ x, &sinix, &cosix);
107 else
109 sinix = __imag__ x;
110 cosix = 1;
113 __real__ retval = copysignq (value, cosix);
114 __imag__ retval = copysignq (value, sinix);
117 else if (signbitq (__real__ x) == 0)
119 __real__ retval = HUGE_VALQ;
120 __imag__ retval = __imag__ x - __imag__ x;
122 else
124 __real__ retval = 0;
125 __imag__ retval = copysignq (0, __imag__ x);
128 else
130 /* If the real part is NaN the result is NaN + iNaN unless the
131 imaginary part is zero. */
132 __real__ retval = nanq ("");
133 if (icls == QUADFP_ZERO)
134 __imag__ retval = __imag__ x;
135 else
137 __imag__ retval = nanq ("");
139 if (rcls != QUADFP_NAN || icls != QUADFP_NAN)
140 feraiseexcept (FE_INVALID);
144 return retval;