tanhl.c revision 61ec6b12089c560a32ebd9efdbb057ff92665e60
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
ddc0e0b53c661f6e439e3b7072b3ef353eadb4afRichard Lowe#pragma weak __tanhl = tanhl
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis/*
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * tanhl(x) returns the hyperbolic tangent of x
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Method :
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 1. reduce x to non-negative: tanhl(-x) = - tanhl(x).
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 0 < x <= small : tanhl(x) := x
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * -expm1l(-2x)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * small < x <= 1 : tanhl(x) := --------------
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * expm1l(-2x) + 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * 1 <= x <= threshold : tanhl(x) := 1 - ---------------
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * expm1l(2x) + 2
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * threshold < x <= INF : tanhl(x) := 1.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * where
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * single : small = 1.e-5 threshold = 11.0
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * double : small = 1.e-10 threshold = 22.0
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * quad : small = 1.e-20 threshold = 45.0
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Note: threshold was chosen so that
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * fl(1.0+2/(expm1(2*threshold)+2)) == 1.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis *
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * Special cases:
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * tanhl(NaN) is NaN;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis * only tanhl(0.0)=0.0 is exact for finite argument.
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#include "libm.h"
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#include "longdouble.h"
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtisstatic const long double small = 1.0e-20L, one = 1.0, two = 2.0,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#ifndef lint
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis big = 1.0e+20L,
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#endif
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis threshold = 45.0L;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtislong double
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtistanhl(long double x) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis long double t, y, z;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis int signx;
61ec6b12089c560a32ebd9efdbb057ff92665e60Gordon Ross#ifndef lint
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis volatile long double dummy;
61ec6b12089c560a32ebd9efdbb057ff92665e60Gordon Ross#endif
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (isnanl(x))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (x + x); /* x is NaN */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis signx = signbitl(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis t = fabsl(x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis z = one;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (t <= threshold) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis if (t > one)
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis z = one - two / (expm1l(t + t) + two);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else if (t > small) {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis y = expm1l(-t - t);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis z = -y / (y + two);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else {
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#ifndef lint
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis dummy = t + big;
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis /* inexact if t != 0 */
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis#endif
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (x);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis }
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis } else if (!finitel(t))
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (copysignl(one, x));
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis else
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (signx ? -z + small * small : z - small * small);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis return (signx ? -z : z);
25c28e83beb90e7c80452a7c818c5e6f73a07dc8Piotr Jasiukajtis}