ctanhf.c revision 25c28e83beb90e7c80452a7c818c5e6f73a07dc8
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER START
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * The contents of this file are subject to the terms of the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Common Development and Distribution License (the "License").
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You may not use this file except in compliance with the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * See the License for the specific language governing permissions
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * and limitations under the License.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * CDDL HEADER END
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Use is subject to license terms.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#pragma weak ctanhf = __ctanhf
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#include "libm.h" /* expf/expm1f/fabsf/sincosf/sinf/tanhf */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#include "complex_wrapper.h"
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT OFF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtisstatic const float four = 4.0F, two = 2.0F, one = 1.0F, zero = 0.0F;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/* INDENT ON */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtisfcomplex
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtisctanhf(fcomplex z) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis float r, u, v, t, x, y, S, C;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis int hx, ix, hy, iy;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis fcomplex ans;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis x = F_RE(z);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis y = F_IM(z);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis hx = THE_WORD(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis ix = hx & 0x7fffffff;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis hy = THE_WORD(y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis iy = hy & 0x7fffffff;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis x = fabsf(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis y = fabsf(y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (iy == 0) { /* ctanh(x,0) = (x,0) for x = 0 or NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = tanhf(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = zero;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (iy >= 0x7f800000) { /* y is inf or NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix < 0x7f800000) /* catanh(finite x,inf/nan) is nan */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = F_IM(ans) = y - y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else if (ix == 0x7f800000) { /* x is inf */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = one;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = zero;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = x + y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = y - y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (ix >= 0x41600000) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * |x| > 14 = prec/2 (14,28,34,60)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ctanh z ~ 1 + i (sin2y)/(exp(2x))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = one;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (iy < 0x7f000000) /* t = sin(2y) */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis S = sinf(y + y);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis (void) sincosf(y, &S, &C);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis S = (S + S) * C;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix >= 0x7f000000) { /* |x| > max/2 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix >= 0x7f800000) { /* |x| is inf or NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (ix > 0x7f800000) /* x is NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = F_IM(ans) = x + y;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = zero * S; /* x is inf */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = S * expf(-x); /* underflow */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = (S + S) * expf(-(x + x));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* 2 sin 2y / exp(2x) */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* INDENT OFF */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * t*t+2t
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * ctanh z = ---------------------------
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * t*t+[4(t+1)(cos y)](cos y)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * [4(t+1)(cos y)]*(sin y)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * i --------------------------
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * t*t+[4(t+1)(cos y)](cos y)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* INDENT ON */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis (void) sincosf(y, &S, &C);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis t = expm1f(x + x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis r = (four * C) * (t + one);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis u = t * t;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis v = one / (u + r * C);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = (u + two * t) * v;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = (r * S) * v;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hx < 0)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_RE(ans) = -F_RE(ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (hy < 0)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis F_IM(ans) = -F_IM(ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (ans);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis}