rtld: increase TLS storage space (bug 2566)
[dragonfly.git] / lib / libm / src / s_ccosh.c
1 /*-
2  * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD: head/lib/msun/src/s_ccosh.c 226599 2011-10-21 06:29:32Z das $
27  */
28
29 /*
30  * Hyperbolic cosine of a complex argument z = x + i y.
31  *
32  * cosh(z) = cosh(x+iy)
33  *         = cosh(x) cos(y) + i sinh(x) sin(y).
34  *
35  * Exceptional values are noted in the comments within the source code.
36  * These values and the return value were taken from n1124.pdf.
37  */
38
39 #include <complex.h>
40 #include <math.h>
41
42 #include "math_private.h"
43
44 static const double huge = 0x1p1023;
45
46 double complex
47 ccosh(double complex z)
48 {
49         double x, y, h;
50         int32_t hx, hy, ix, iy, lx, ly;
51
52         x = creal(z);
53         y = cimag(z);
54
55         EXTRACT_WORDS(hx, lx, x);
56         EXTRACT_WORDS(hy, ly, y);
57
58         ix = 0x7fffffff & hx;
59         iy = 0x7fffffff & hy;
60
61         /* Handle the nearly-non-exceptional cases where x and y are finite. */
62         if (ix < 0x7ff00000 && iy < 0x7ff00000) {
63                 if ((iy | ly) == 0)
64                         return (cpack(cosh(x), x * y));
65                 if (ix < 0x40360000)    /* small x: normal case */
66                         return (cpack(cosh(x) * cos(y), sinh(x) * sin(y)));
67
68                 /* |x| >= 22, so cosh(x) ~= exp(|x|) */
69                 if (ix < 0x40862e42) {
70                         /* x < 710: exp(|x|) won't overflow */
71                         h = exp(fabs(x)) * 0.5;
72                         return (cpack(h * cos(y), copysign(h, x) * sin(y)));
73                 } else if (ix < 0x4096bbaa) {
74                         /* x < 1455: scale to avoid overflow */
75                         z = __ldexp_cexp(cpack(fabs(x), y), -1);
76                         return (cpack(creal(z), cimag(z) * copysign(1, x)));
77                 } else {
78                         /* x >= 1455: the result always overflows */
79                         h = huge * x;
80                         return (cpack(h * h * cos(y), h * sin(y)));
81                 }
82         }
83
84         /*
85          * cosh(+-0 +- I Inf) = dNaN + I sign(d(+-0, dNaN))0.
86          * The sign of 0 in the result is unspecified.  Choice = normally
87          * the same as dNaN.  Raise the invalid floating-point exception.
88          *
89          * cosh(+-0 +- I NaN) = d(NaN) + I sign(d(+-0, NaN))0.
90          * The sign of 0 in the result is unspecified.  Choice = normally
91          * the same as d(NaN).
92          */
93         if ((ix | lx) == 0 && iy >= 0x7ff00000)
94                 return (cpack(y - y, copysign(0, x * (y - y))));
95
96         /*
97          * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0.
98          *
99          * cosh(NaN +- I 0)   = d(NaN) + I sign(d(NaN, +-0))0.
100          * The sign of 0 in the result is unspecified.
101          */
102         if ((iy | ly) == 0 && ix >= 0x7ff00000) {
103                 if (((hx & 0xfffff) | lx) == 0)
104                         return (cpack(x * x, copysign(0, x) * y));
105                 return (cpack(x * x, copysign(0, (x + x) * y)));
106         }
107
108         /*
109          * cosh(x +- I Inf) = dNaN + I dNaN.
110          * Raise the invalid floating-point exception for finite nonzero x.
111          *
112          * cosh(x + I NaN) = d(NaN) + I d(NaN).
113          * Optionally raises the invalid floating-point exception for finite
114          * nonzero x.  Choice = don't raise (except for signaling NaNs).
115          */
116         if (ix < 0x7ff00000 && iy >= 0x7ff00000)
117                 return (cpack(y - y, x * (y - y)));
118
119         /*
120          * cosh(+-Inf + I NaN)  = +Inf + I d(NaN).
121          *
122          * cosh(+-Inf +- I Inf) = +Inf + I dNaN.
123          * The sign of Inf in the result is unspecified.  Choice = always +.
124          * Raise the invalid floating-point exception.
125          *
126          * cosh(+-Inf + I y)   = +Inf cos(y) +- I Inf sin(y)
127          */
128         if (ix >= 0x7ff00000 && ((hx & 0xfffff) | lx) == 0) {
129                 if (iy >= 0x7ff00000)
130                         return (cpack(x * x, x * (y - y)));
131                 return (cpack((x * x) * cos(y), x * sin(y)));
132         }
133
134         /*
135          * cosh(NaN + I NaN)  = d(NaN) + I d(NaN).
136          *
137          * cosh(NaN +- I Inf) = d(NaN) + I d(NaN).
138          * Optionally raises the invalid floating-point exception.
139          * Choice = raise.
140          *
141          * cosh(NaN + I y)    = d(NaN) + I d(NaN).
142          * Optionally raises the invalid floating-point exception for finite
143          * nonzero y.  Choice = don't raise (except for signaling NaNs).
144          */
145         return (cpack((x * x) * (y - y), (x + x) * (y - y)));
146 }
147
148 double complex
149 ccos(double complex z)
150 {
151
152         /* ccos(z) = ccosh(I * z) */
153         return (ccosh(cpack(-cimag(z), creal(z))));
154 }