OLD | NEW |
1 /* origin: FreeBSD /usr/src/lib/msun/src/s_ccosh.c */ | 1 /* origin: FreeBSD /usr/src/lib/msun/src/s_ccosh.c */ |
2 /*- | 2 /*- |
3 * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl | 3 * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl |
4 * All rights reserved. | 4 * All rights reserved. |
5 * | 5 * |
6 * Redistribution and use in source and binary forms, with or without | 6 * Redistribution and use in source and binary forms, with or without |
7 * modification, are permitted provided that the following conditions | 7 * modification, are permitted provided that the following conditions |
8 * are met: | 8 * are met: |
9 * 1. Redistributions of source code must retain the above copyright | 9 * 1. Redistributions of source code must retain the above copyright |
10 * notice unmodified, this list of conditions, and the following | 10 * notice unmodified, this list of conditions, and the following |
(...skipping 20 matching lines...) Expand all Loading... |
31 * = cosh(x) cos(y) + i sinh(x) sin(y). | 31 * = cosh(x) cos(y) + i sinh(x) sin(y). |
32 * | 32 * |
33 * Exceptional values are noted in the comments within the source code. | 33 * Exceptional values are noted in the comments within the source code. |
34 * These values and the return value were taken from n1124.pdf. | 34 * These values and the return value were taken from n1124.pdf. |
35 */ | 35 */ |
36 | 36 |
37 #include "libm.h" | 37 #include "libm.h" |
38 | 38 |
39 static const double huge = 0x1p1023; | 39 static const double huge = 0x1p1023; |
40 | 40 |
41 double complex ccosh(double complex z) | 41 double complex ccosh(double complex z) { |
42 { | 42 double x, y, h; |
43 » double x, y, h; | 43 int32_t hx, hy, ix, iy, lx, ly; |
44 » int32_t hx, hy, ix, iy, lx, ly; | |
45 | 44 |
46 » x = creal(z); | 45 x = creal(z); |
47 » y = cimag(z); | 46 y = cimag(z); |
48 | 47 |
49 » EXTRACT_WORDS(hx, lx, x); | 48 EXTRACT_WORDS(hx, lx, x); |
50 » EXTRACT_WORDS(hy, ly, y); | 49 EXTRACT_WORDS(hy, ly, y); |
51 | 50 |
52 » ix = 0x7fffffff & hx; | 51 ix = 0x7fffffff & hx; |
53 » iy = 0x7fffffff & hy; | 52 iy = 0x7fffffff & hy; |
54 | 53 |
55 » /* Handle the nearly-non-exceptional cases where x and y are finite. */ | 54 /* Handle the nearly-non-exceptional cases where x and y are finite. */ |
56 » if (ix < 0x7ff00000 && iy < 0x7ff00000) { | 55 if (ix < 0x7ff00000 && iy < 0x7ff00000) { |
57 » » if ((iy | ly) == 0) | 56 if ((iy | ly) == 0) |
58 » » » return CMPLX(cosh(x), x * y); | 57 return CMPLX(cosh(x), x * y); |
59 » » if (ix < 0x40360000) /* small x: normal case */ | 58 if (ix < 0x40360000) /* small x: normal case */ |
60 » » » return CMPLX(cosh(x) * cos(y), sinh(x) * sin(y)); | 59 return CMPLX(cosh(x) * cos(y), sinh(x) * sin(y)); |
61 | 60 |
62 » » /* |x| >= 22, so cosh(x) ~= exp(|x|) */ | 61 /* |x| >= 22, so cosh(x) ~= exp(|x|) */ |
63 » » if (ix < 0x40862e42) { | 62 if (ix < 0x40862e42) { |
64 » » » /* x < 710: exp(|x|) won't overflow */ | 63 /* x < 710: exp(|x|) won't overflow */ |
65 » » » h = exp(fabs(x)) * 0.5; | 64 h = exp(fabs(x)) * 0.5; |
66 » » » return CMPLX(h * cos(y), copysign(h, x) * sin(y)); | 65 return CMPLX(h * cos(y), copysign(h, x) * sin(y)); |
67 » » } else if (ix < 0x4096bbaa) { | 66 } else if (ix < 0x4096bbaa) { |
68 » » » /* x < 1455: scale to avoid overflow */ | 67 /* x < 1455: scale to avoid overflow */ |
69 » » » z = __ldexp_cexp(CMPLX(fabs(x), y), -1); | 68 z = __ldexp_cexp(CMPLX(fabs(x), y), -1); |
70 » » » return CMPLX(creal(z), cimag(z) * copysign(1, x)); | 69 return CMPLX(creal(z), cimag(z) * copysign(1, x)); |
71 » » } else { | 70 } else { |
72 » » » /* x >= 1455: the result always overflows */ | 71 /* x >= 1455: the result always overflows */ |
73 » » » h = huge * x; | 72 h = huge * x; |
74 » » » return CMPLX(h * h * cos(y), h * sin(y)); | 73 return CMPLX(h * h * cos(y), h * sin(y)); |
75 » » } | 74 } |
76 » } | 75 } |
77 | 76 |
78 » /* | 77 /* |
79 » * cosh(+-0 +- I Inf) = dNaN + I sign(d(+-0, dNaN))0. | 78 * cosh(+-0 +- I Inf) = dNaN + I sign(d(+-0, dNaN))0. |
80 » * The sign of 0 in the result is unspecified. Choice = normally | 79 * The sign of 0 in the result is unspecified. Choice = normally |
81 » * the same as dNaN. Raise the invalid floating-point exception. | 80 * the same as dNaN. Raise the invalid floating-point exception. |
82 » * | 81 * |
83 » * cosh(+-0 +- I NaN) = d(NaN) + I sign(d(+-0, NaN))0. | 82 * cosh(+-0 +- I NaN) = d(NaN) + I sign(d(+-0, NaN))0. |
84 » * The sign of 0 in the result is unspecified. Choice = normally | 83 * The sign of 0 in the result is unspecified. Choice = normally |
85 » * the same as d(NaN). | 84 * the same as d(NaN). |
86 » */ | 85 */ |
87 » if ((ix | lx) == 0 && iy >= 0x7ff00000) | 86 if ((ix | lx) == 0 && iy >= 0x7ff00000) |
88 » » return CMPLX(y - y, copysign(0, x * (y - y))); | 87 return CMPLX(y - y, copysign(0, x * (y - y))); |
89 | 88 |
90 » /* | 89 /* |
91 » * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0. | 90 * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0. |
92 » * | 91 * |
93 » * cosh(NaN +- I 0) = d(NaN) + I sign(d(NaN, +-0))0. | 92 * cosh(NaN +- I 0) = d(NaN) + I sign(d(NaN, +-0))0. |
94 » * The sign of 0 in the result is unspecified. | 93 * The sign of 0 in the result is unspecified. |
95 » */ | 94 */ |
96 » if ((iy | ly) == 0 && ix >= 0x7ff00000) { | 95 if ((iy | ly) == 0 && ix >= 0x7ff00000) { |
97 » » if (((hx & 0xfffff) | lx) == 0) | 96 if (((hx & 0xfffff) | lx) == 0) |
98 » » » return CMPLX(x * x, copysign(0, x) * y); | 97 return CMPLX(x * x, copysign(0, x) * y); |
99 » » return CMPLX(x * x, copysign(0, (x + x) * y)); | 98 return CMPLX(x * x, copysign(0, (x + x) * y)); |
100 » } | 99 } |
101 | 100 |
102 » /* | 101 /* |
103 » * cosh(x +- I Inf) = dNaN + I dNaN. | 102 * cosh(x +- I Inf) = dNaN + I dNaN. |
104 » * Raise the invalid floating-point exception for finite nonzero x. | 103 * Raise the invalid floating-point exception for finite nonzero x. |
105 » * | 104 * |
106 » * cosh(x + I NaN) = d(NaN) + I d(NaN). | 105 * cosh(x + I NaN) = d(NaN) + I d(NaN). |
107 » * Optionally raises the invalid floating-point exception for finite | 106 * Optionally raises the invalid floating-point exception for finite |
108 » * nonzero x. Choice = don't raise (except for signaling NaNs). | 107 * nonzero x. Choice = don't raise (except for signaling NaNs). |
109 » */ | 108 */ |
110 » if (ix < 0x7ff00000 && iy >= 0x7ff00000) | 109 if (ix < 0x7ff00000 && iy >= 0x7ff00000) |
111 » » return CMPLX(y - y, x * (y - y)); | 110 return CMPLX(y - y, x * (y - y)); |
112 | 111 |
113 » /* | 112 /* |
114 » * cosh(+-Inf + I NaN) = +Inf + I d(NaN). | 113 * cosh(+-Inf + I NaN) = +Inf + I d(NaN). |
115 » * | 114 * |
116 » * cosh(+-Inf +- I Inf) = +Inf + I dNaN. | 115 * cosh(+-Inf +- I Inf) = +Inf + I dNaN. |
117 » * The sign of Inf in the result is unspecified. Choice = always +. | 116 * The sign of Inf in the result is unspecified. Choice = always +. |
118 » * Raise the invalid floating-point exception. | 117 * Raise the invalid floating-point exception. |
119 » * | 118 * |
120 » * cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y) | 119 * cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y) |
121 » */ | 120 */ |
122 » if (ix >= 0x7ff00000 && ((hx & 0xfffff) | lx) == 0) { | 121 if (ix >= 0x7ff00000 && ((hx & 0xfffff) | lx) == 0) { |
123 » » if (iy >= 0x7ff00000) | 122 if (iy >= 0x7ff00000) |
124 » » » return CMPLX(x * x, x * (y - y)); | 123 return CMPLX(x * x, x * (y - y)); |
125 » » return CMPLX((x * x) * cos(y), x * sin(y)); | 124 return CMPLX((x * x) * cos(y), x * sin(y)); |
126 » } | 125 } |
127 | 126 |
128 » /* | 127 /* |
129 » * cosh(NaN + I NaN) = d(NaN) + I d(NaN). | 128 * cosh(NaN + I NaN) = d(NaN) + I d(NaN). |
130 » * | 129 * |
131 » * cosh(NaN +- I Inf) = d(NaN) + I d(NaN). | 130 * cosh(NaN +- I Inf) = d(NaN) + I d(NaN). |
132 » * Optionally raises the invalid floating-point exception. | 131 * Optionally raises the invalid floating-point exception. |
133 » * Choice = raise. | 132 * Choice = raise. |
134 » * | 133 * |
135 » * cosh(NaN + I y) = d(NaN) + I d(NaN). | 134 * cosh(NaN + I y) = d(NaN) + I d(NaN). |
136 » * Optionally raises the invalid floating-point exception for finite | 135 * Optionally raises the invalid floating-point exception for finite |
137 » * nonzero y. Choice = don't raise (except for signaling NaNs). | 136 * nonzero y. Choice = don't raise (except for signaling NaNs). |
138 » */ | 137 */ |
139 » return CMPLX((x * x) * (y - y), (x + x) * (y - y)); | 138 return CMPLX((x * x) * (y - y), (x + x) * (y - y)); |
140 } | 139 } |
OLD | NEW |