125c28e83SPiotr Jasiukajtis /* 225c28e83SPiotr Jasiukajtis * CDDL HEADER START 325c28e83SPiotr Jasiukajtis * 425c28e83SPiotr Jasiukajtis * The contents of this file are subject to the terms of the 525c28e83SPiotr Jasiukajtis * Common Development and Distribution License (the "License"). 625c28e83SPiotr Jasiukajtis * You may not use this file except in compliance with the License. 725c28e83SPiotr Jasiukajtis * 825c28e83SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 925c28e83SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing. 1025c28e83SPiotr Jasiukajtis * See the License for the specific language governing permissions 1125c28e83SPiotr Jasiukajtis * and limitations under the License. 1225c28e83SPiotr Jasiukajtis * 1325c28e83SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each 1425c28e83SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1525c28e83SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the 1625c28e83SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying 1725c28e83SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner] 1825c28e83SPiotr Jasiukajtis * 1925c28e83SPiotr Jasiukajtis * CDDL HEADER END 2025c28e83SPiotr Jasiukajtis */ 2125c28e83SPiotr Jasiukajtis 2225c28e83SPiotr Jasiukajtis /* 2325c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 2425c28e83SPiotr Jasiukajtis */ 2525c28e83SPiotr Jasiukajtis /* 2625c28e83SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 2725c28e83SPiotr Jasiukajtis * Use is subject to license terms. 2825c28e83SPiotr Jasiukajtis */ 2925c28e83SPiotr Jasiukajtis 30ddc0e0b5SRichard Lowe #pragma weak __tanhl = tanhl 3125c28e83SPiotr Jasiukajtis 3225c28e83SPiotr Jasiukajtis /* 3325c28e83SPiotr Jasiukajtis * tanhl(x) returns the hyperbolic tangent of x 3425c28e83SPiotr Jasiukajtis * 3525c28e83SPiotr Jasiukajtis * Method : 3625c28e83SPiotr Jasiukajtis * 1. reduce x to non-negative: tanhl(-x) = - tanhl(x). 3725c28e83SPiotr Jasiukajtis * 2. 3825c28e83SPiotr Jasiukajtis * 0 < x <= small : tanhl(x) := x 3925c28e83SPiotr Jasiukajtis * -expm1l(-2x) 4025c28e83SPiotr Jasiukajtis * small < x <= 1 : tanhl(x) := -------------- 4125c28e83SPiotr Jasiukajtis * expm1l(-2x) + 2 4225c28e83SPiotr Jasiukajtis * 2 4325c28e83SPiotr Jasiukajtis * 1 <= x <= threshold : tanhl(x) := 1 - --------------- 4425c28e83SPiotr Jasiukajtis * expm1l(2x) + 2 4525c28e83SPiotr Jasiukajtis * threshold < x <= INF : tanhl(x) := 1. 4625c28e83SPiotr Jasiukajtis * 4725c28e83SPiotr Jasiukajtis * where 4825c28e83SPiotr Jasiukajtis * single : small = 1.e-5 threshold = 11.0 4925c28e83SPiotr Jasiukajtis * double : small = 1.e-10 threshold = 22.0 5025c28e83SPiotr Jasiukajtis * quad : small = 1.e-20 threshold = 45.0 5125c28e83SPiotr Jasiukajtis * 5225c28e83SPiotr Jasiukajtis * Note: threshold was chosen so that 5325c28e83SPiotr Jasiukajtis * fl(1.0+2/(expm1(2*threshold)+2)) == 1. 5425c28e83SPiotr Jasiukajtis * 5525c28e83SPiotr Jasiukajtis * Special cases: 5625c28e83SPiotr Jasiukajtis * tanhl(NaN) is NaN; 5725c28e83SPiotr Jasiukajtis * only tanhl(0.0)=0.0 is exact for finite argument. 5825c28e83SPiotr Jasiukajtis */ 5925c28e83SPiotr Jasiukajtis 6025c28e83SPiotr Jasiukajtis #include "libm.h" 6125c28e83SPiotr Jasiukajtis #include "longdouble.h" 6225c28e83SPiotr Jasiukajtis 6325c28e83SPiotr Jasiukajtis static const long double small = 1.0e-20L, one = 1.0, two = 2.0, 6425c28e83SPiotr Jasiukajtis #ifndef lint 6525c28e83SPiotr Jasiukajtis big = 1.0e+20L, 6625c28e83SPiotr Jasiukajtis #endif 6725c28e83SPiotr Jasiukajtis threshold = 45.0L; 6825c28e83SPiotr Jasiukajtis 6925c28e83SPiotr Jasiukajtis long double 7025c28e83SPiotr Jasiukajtis tanhl(long double x) { 7125c28e83SPiotr Jasiukajtis long double t, y, z; 7225c28e83SPiotr Jasiukajtis int signx; 73*61ec6b12SGordon Ross #ifndef lint 7425c28e83SPiotr Jasiukajtis volatile long double dummy; 75*61ec6b12SGordon Ross #endif 7625c28e83SPiotr Jasiukajtis 7725c28e83SPiotr Jasiukajtis if (isnanl(x)) 7825c28e83SPiotr Jasiukajtis return (x + x); /* x is NaN */ 7925c28e83SPiotr Jasiukajtis signx = signbitl(x); 8025c28e83SPiotr Jasiukajtis t = fabsl(x); 8125c28e83SPiotr Jasiukajtis z = one; 8225c28e83SPiotr Jasiukajtis if (t <= threshold) { 8325c28e83SPiotr Jasiukajtis if (t > one) 8425c28e83SPiotr Jasiukajtis z = one - two / (expm1l(t + t) + two); 8525c28e83SPiotr Jasiukajtis else if (t > small) { 8625c28e83SPiotr Jasiukajtis y = expm1l(-t - t); 8725c28e83SPiotr Jasiukajtis z = -y / (y + two); 8825c28e83SPiotr Jasiukajtis } else { 8925c28e83SPiotr Jasiukajtis #ifndef lint 9025c28e83SPiotr Jasiukajtis dummy = t + big; 9125c28e83SPiotr Jasiukajtis /* inexact if t != 0 */ 9225c28e83SPiotr Jasiukajtis #endif 9325c28e83SPiotr Jasiukajtis return (x); 9425c28e83SPiotr Jasiukajtis } 9525c28e83SPiotr Jasiukajtis } else if (!finitel(t)) 9625c28e83SPiotr Jasiukajtis return (copysignl(one, x)); 9725c28e83SPiotr Jasiukajtis else 9825c28e83SPiotr Jasiukajtis return (signx ? -z + small * small : z - small * small); 9925c28e83SPiotr Jasiukajtis return (signx ? -z : z); 10025c28e83SPiotr Jasiukajtis } 101