github.com/afumu/libc@v0.0.6/musl/src/complex/ccoshf.c (about)

     1  /* origin: FreeBSD /usr/src/lib/msun/src/s_ccoshf.c */
     2  /*-
     3   * Copyright (c) 2005 Bruce D. Evans and Steven G. Kargl
     4   * All rights reserved.
     5   *
     6   * Redistribution and use in source and binary forms, with or without
     7   * modification, are permitted provided that the following conditions
     8   * are met:
     9   * 1. Redistributions of source code must retain the above copyright
    10   *    notice unmodified, this list of conditions, and the following
    11   *    disclaimer.
    12   * 2. Redistributions in binary form must reproduce the above copyright
    13   *    notice, this list of conditions and the following disclaimer in the
    14   *    documentation and/or other materials provided with the distribution.
    15   *
    16   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
    17   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
    18   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
    19   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
    20   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
    21   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    22   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    23   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    25   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    26   */
    27  /*
    28   * Hyperbolic cosine of a complex argument.  See s_ccosh.c for details.
    29   */
    30  
    31  #include "complex_impl.h"
    32  
    33  static const float huge = 0x1p127;
    34  
    35  float complex ccoshf(float complex z)
    36  {
    37  	float x, y, h;
    38  	int32_t hx, hy, ix, iy;
    39  
    40  	x = crealf(z);
    41  	y = cimagf(z);
    42  
    43  	GET_FLOAT_WORD(hx, x);
    44  	GET_FLOAT_WORD(hy, y);
    45  
    46  	ix = 0x7fffffff & hx;
    47  	iy = 0x7fffffff & hy;
    48  
    49  	if (ix < 0x7f800000 && iy < 0x7f800000) {
    50  		if (iy == 0)
    51  			return CMPLXF(coshf(x), x * y);
    52  		if (ix < 0x41100000)    /* small x: normal case */
    53  			return CMPLXF(coshf(x) * cosf(y), sinhf(x) * sinf(y));
    54  
    55  		/* |x| >= 9, so cosh(x) ~= exp(|x|) */
    56  		if (ix < 0x42b17218) {
    57  			/* x < 88.7: expf(|x|) won't overflow */
    58  			h = expf(fabsf(x)) * 0.5f;
    59  			return CMPLXF(h * cosf(y), copysignf(h, x) * sinf(y));
    60  		} else if (ix < 0x4340b1e7) {
    61  			/* x < 192.7: scale to avoid overflow */
    62  			z = __ldexp_cexpf(CMPLXF(fabsf(x), y), -1);
    63  			return CMPLXF(crealf(z), cimagf(z) * copysignf(1, x));
    64  		} else {
    65  			/* x >= 192.7: the result always overflows */
    66  			h = huge * x;
    67  			return CMPLXF(h * h * cosf(y), h * sinf(y));
    68  		}
    69  	}
    70  
    71  	if (ix == 0 && iy >= 0x7f800000)
    72  		return CMPLXF(y - y, copysignf(0, x * (y - y)));
    73  
    74  	if (iy == 0 && ix >= 0x7f800000) {
    75  		if ((hx & 0x7fffff) == 0)
    76  			return CMPLXF(x * x, copysignf(0, x) * y);
    77  		return CMPLXF(x * x, copysignf(0, (x + x) * y));
    78  	}
    79  
    80  	if (ix < 0x7f800000 && iy >= 0x7f800000)
    81  		return CMPLXF(y - y, x * (y - y));
    82  
    83  	if (ix >= 0x7f800000 && (hx & 0x7fffff) == 0) {
    84  		if (iy >= 0x7f800000)
    85  			return CMPLXF(x * x, x * (y - y));
    86  		return CMPLXF((x * x) * cosf(y), x * sinf(y));
    87  	}
    88  
    89  	return CMPLXF((x * x) * (y - y), (x + x) * (y - y));
    90  }