s_ccosh.c revision 284423
131921Sbrian/*- 231921Sbrian * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl 331921Sbrian * All rights reserved. 431921Sbrian * 531921Sbrian * Redistribution and use in source and binary forms, with or without 631921Sbrian * modification, are permitted provided that the following conditions 731921Sbrian * are met: 831921Sbrian * 1. Redistributions of source code must retain the above copyright 931921Sbrian * notice unmodified, this list of conditions, and the following 1031921Sbrian * disclaimer. 1131921Sbrian * 2. Redistributions in binary form must reproduce the above copyright 1231921Sbrian * notice, this list of conditions and the following disclaimer in the 1331921Sbrian * documentation and/or other materials provided with the distribution. 1431921Sbrian * 1531921Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1631921Sbrian * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1731921Sbrian * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1831921Sbrian * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1931921Sbrian * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 2031921Sbrian * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2131921Sbrian * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2231921Sbrian * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2331921Sbrian * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2431921Sbrian * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2531921Sbrian */ 2636314Sbrian 2730715Sbrian/* 2830715Sbrian * Hyperbolic cosine of a complex argument z = x + i y. 2926516Sbrian * 3028679Sbrian * cosh(z) = cosh(x+iy) 3136285Sbrian * = cosh(x) cos(y) + i sinh(x) sin(y). 3236285Sbrian * 3336285Sbrian * Exceptional values are noted in the comments within the source code. 3436285Sbrian * These values and the return value were taken from n1124.pdf. 3536285Sbrian * The sign of the result for some exceptional values is unspecified but 3636285Sbrian * must satisfy both cosh(conj(z)) == conj(cosh(z)) and cosh(-z) == cosh(z). 3736285Sbrian */ 3836285Sbrian 3936285Sbrian#include <sys/cdefs.h> 4036285Sbrian__FBSDID("$FreeBSD: head/lib/msun/src/s_ccosh.c 284423 2015-06-15 20:11:06Z tijl $"); 4136285Sbrian 4236285Sbrian#include <complex.h> 4336285Sbrian#include <math.h> 4436285Sbrian 4536285Sbrian#include "math_private.h" 4636285Sbrian 4736285Sbrianstatic const double huge = 0x1p1023; 4836285Sbrian 4936285Sbriandouble complex 506059Samuraiccosh(double complex z) 5136285Sbrian{ 5236285Sbrian double x, y, h; 5336285Sbrian int32_t hx, hy, ix, iy, lx, ly; 5436314Sbrian 5536314Sbrian x = creal(z); 5636285Sbrian y = cimag(z); 5726516Sbrian 5836285Sbrian EXTRACT_WORDS(hx, lx, x); 5936285Sbrian EXTRACT_WORDS(hy, ly, y); 6036285Sbrian 6136285Sbrian ix = 0x7fffffff & hx; 6236285Sbrian iy = 0x7fffffff & hy; 6336285Sbrian 6436285Sbrian /* Handle the nearly-non-exceptional cases where x and y are finite. */ 6536285Sbrian if (ix < 0x7ff00000 && iy < 0x7ff00000) { 6636285Sbrian if ((iy | ly) == 0) 6736285Sbrian return (CMPLX(cosh(x), x * y)); 6836285Sbrian if (ix < 0x40360000) /* |x| < 22: normal case */ 6936285Sbrian return (CMPLX(cosh(x) * cos(y), sinh(x) * sin(y))); 7036285Sbrian 7136285Sbrian /* |x| >= 22, so cosh(x) ~= exp(|x|) */ 7236285Sbrian if (ix < 0x40862e42) { 7336285Sbrian /* x < 710: exp(|x|) won't overflow */ 7436285Sbrian h = exp(fabs(x)) * 0.5; 7536285Sbrian return (CMPLX(h * cos(y), copysign(h, x) * sin(y))); 7636285Sbrian } else if (ix < 0x4096bbaa) { 7736285Sbrian /* x < 1455: scale to avoid overflow */ 7836285Sbrian z = __ldexp_cexp(CMPLX(fabs(x), y), -1); 7936285Sbrian return (CMPLX(creal(z), cimag(z) * copysign(1, x))); 8036285Sbrian } else { 8136285Sbrian /* x >= 1455: the result always overflows */ 8236285Sbrian h = huge * x; 8336314Sbrian return (CMPLX(h * h * cos(y), h * sin(y))); 8436314Sbrian } 8536314Sbrian } 8636314Sbrian 8736314Sbrian /* 8836314Sbrian * cosh(+-0 +- I Inf) = dNaN + I (+-)(+-)0. 8936314Sbrian * The sign of 0 in the result is unspecified. Choice = product 9036314Sbrian * of the signs of the argument. Raise the invalid floating-point 9136314Sbrian * exception. 9236314Sbrian * 93 * cosh(+-0 +- I NaN) = d(NaN) + I (+-)(+-)0. 94 * The sign of 0 in the result is unspecified. Choice = product 95 * of the signs of the argument. 96 */ 97 if ((ix | lx) == 0) /* && iy >= 0x7ff00000 */ 98 return (CMPLX(y - y, x * copysign(0, y))); 99 100 /* 101 * cosh(+-Inf +- I 0) = +Inf + I (+-)(+-)0. 102 * 103 * cosh(NaN +- I 0) = d(NaN) + I (+-)(+-)0. 104 * The sign of 0 in the result is unspecified. Choice = product 105 * of the signs of the argument. 106 */ 107 if ((iy | ly) == 0) /* && ix >= 0x7ff00000 */ 108 return (CMPLX(x * x, copysign(0, x) * y)); 109 110 /* 111 * cosh(x +- I Inf) = dNaN + I dNaN. 112 * Raise the invalid floating-point exception for finite nonzero x. 113 * 114 * cosh(x + I NaN) = d(NaN) + I d(NaN). 115 * Optionally raises the invalid floating-point exception for finite 116 * nonzero x. Choice = don't raise (except for signaling NaNs). 117 */ 118 if (ix < 0x7ff00000) /* && iy >= 0x7ff00000 */ 119 return (CMPLX(y - y, x * (y - y))); 120 121 /* 122 * cosh(+-Inf + I NaN) = +Inf + I d(NaN). 123 * 124 * cosh(+-Inf +- I Inf) = +Inf + I dNaN. 125 * The sign of Inf in the result is unspecified. Choice = always +. 126 * Raise the invalid floating-point exception. 127 * 128 * cosh(+-Inf + I y) = +Inf cos(y) +- I Inf sin(y) 129 */ 130 if (ix == 0x7ff00000 && lx == 0) { 131 if (iy >= 0x7ff00000) 132 return (CMPLX(INFINITY, x * (y - y))); 133 return (CMPLX(INFINITY * cos(y), x * sin(y))); 134 } 135 136 /* 137 * cosh(NaN + I NaN) = d(NaN) + I d(NaN). 138 * 139 * cosh(NaN +- I Inf) = d(NaN) + I d(NaN). 140 * Optionally raises the invalid floating-point exception. 141 * Choice = raise. 142 * 143 * cosh(NaN + I y) = d(NaN) + I d(NaN). 144 * Optionally raises the invalid floating-point exception for finite 145 * nonzero y. Choice = don't raise (except for signaling NaNs). 146 */ 147 return (CMPLX((x * x) * (y - y), (x + x) * (y - y))); 148} 149 150double complex 151ccos(double complex z) 152{ 153 154 /* ccos(z) = ccosh(I * z) */ 155 return (ccosh(CMPLX(-cimag(z), creal(z)))); 156} 157