s_tanhl.c revision 260145
11573Srgrimes/* from: FreeBSD: head/lib/msun/src/s_tanhl.c XXX */ 21573Srgrimes 31573Srgrimes/* @(#)s_tanh.c 5.1 93/09/24 */ 41573Srgrimes/* 51573Srgrimes * ==================================================== 61573Srgrimes * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved. 71573Srgrimes * 81573Srgrimes * Developed at SunPro, a Sun Microsystems, Inc. business. 91573Srgrimes * Permission to use, copy, modify, and distribute this 101573Srgrimes * software is freely granted, provided that this notice 111573Srgrimes * is preserved. 121573Srgrimes * ==================================================== 131573Srgrimes */ 141573Srgrimes 151573Srgrimes#include <sys/cdefs.h> 161573Srgrimes__FBSDID("$FreeBSD: head/lib/msun/src/s_tanhl.c 260145 2013-12-31 23:59:33Z kargl $"); 171573Srgrimes 181573Srgrimes/* 191573Srgrimes * See s_tanh.c for complete comments. 201573Srgrimes * 211573Srgrimes * Converted to long double by Bruce D. Evans. 221573Srgrimes */ 231573Srgrimes 241573Srgrimes#include <float.h> 251573Srgrimes#ifdef __i386__ 261573Srgrimes#include <ieeefp.h> 271573Srgrimes#endif 281573Srgrimes 291573Srgrimes#include "math.h" 301573Srgrimes#include "math_private.h" 311573Srgrimes#include "fpmath.h" 321573Srgrimes#include "k_expl.h" 331573Srgrimes 341573Srgrimes#if LDBL_MAX_EXP != 0x4000 351573Srgrimes/* We also require the usual expsign encoding. */ 361573Srgrimes#error "Unsupported long double format" 371573Srgrimes#endif 381573Srgrimes 391573Srgrimes#define BIAS (LDBL_MAX_EXP - 1) 4090045Sobrien 4190045Sobrienstatic const volatile double tiny = 1.0e-300; 421573Srgrimesstatic const double one = 1.0; 431573Srgrimes#if LDBL_MANT_DIG == 64 441573Srgrimes/* 451573Srgrimes * Domain [-0.25, 0.25], range ~[-1.6304e-22, 1.6304e-22]: 461573Srgrimes * |tanh(x)/x - t(x)| < 2**-72.3 471573Srgrimes */ 481573Srgrimesstatic const union IEEEl2bits 491573SrgrimesT3u = LD80C(0xaaaaaaaaaaaaaa9f, -2, -3.33333333333333333017e-1L); 501573Srgrimes#define T3 T3u.e 511573Srgrimesstatic const double 521573SrgrimesT5 = 1.3333333333333314e-1, /* 0x1111111111110a.0p-55 */ 531573SrgrimesT7 = -5.3968253968210485e-2, /* -0x1ba1ba1ba1a1a1.0p-57 */ 541573SrgrimesT9 = 2.1869488531393817e-2, /* 0x1664f488172022.0p-58 */ 551573SrgrimesT11 = -8.8632352345964591e-3, /* -0x1226e34bc138d5.0p-59 */ 561573SrgrimesT13 = 3.5921169709993771e-3, /* 0x1d6d371d3e400f.0p-61 */ 571573SrgrimesT15 = -1.4555786415756001e-3, /* -0x17d923aa63814d.0p-62 */ 581573SrgrimesT17 = 5.8645267876296793e-4, /* 0x13378589b85aa7.0p-63 */ 591573SrgrimesT19 = -2.1121033571392224e-4; /* -0x1baf0af80c4090.0p-65 */ 601573Srgrimes#elif LDBL_MANT_DIG == 113 611573Srgrimes/* 621573Srgrimes * Domain [-0.25, 0.25], range ~[-2.4211e-37, 2.4211e-37]: 638870Srgrimes * |tanh(x)/x - t(x)| < 2**121.6 641573Srgrimes */ 651573Srgrimesstatic const long double 661573SrgrimesT3 = -3.33333333333333333333333333333332980e-1L, /* -0x1555555555555555555555555554e.0p-114L */ 671573SrgrimesT5 = 1.33333333333333333333333333332707260e-1L, /* 0x1111111111111111111111110ab7b.0p-115L */ 68132817StjrT7 = -5.39682539682539682539682535723482314e-2L, /* -0x1ba1ba1ba1ba1ba1ba1ba17b5fc98.0p-117L */ 69132817StjrT9 = 2.18694885361552028218693591149061717e-2L, /* 0x1664f4882c10f9f32d6b1a12a25e5.0p-118L */ 70132817StjrT11 = -8.86323552990219656883762347736381851e-3L, /* -0x1226e355e6c23c8f5a5a0f386cb4d.0p-119L */ 71132817StjrT13 = 3.59212803657248101358314398220822722e-3L, /* 0x1d6d3d0e157ddfb403ad3637442c6.0p-121L */ 72132817StjrT15 = -1.45583438705131796512568010348874662e-3L; /* -0x17da36452b75e150c44cc34253b34.0p-122L */ 73132817Stjrstatic const double 74132817StjrT17 = 5.9002744094556621e-4, /* 0x1355824803668e.0p-63 */ 75132817StjrT19 = -2.3912911424260516e-4, /* -0x1f57d7734c8dde.0p-65 */ 76132817StjrT21 = 9.6915379535512898e-5, /* 0x1967e18ad6a6ca.0p-66 */ 77132817StjrT23 = -3.9278322983156353e-5, /* -0x1497d8e6b75729.0p-67 */ 781573SrgrimesT25 = 1.5918887220143869e-5, /* 0x10b1319998cafa.0p-68 */ 791573SrgrimesT27 = -6.4514295231630956e-6, /* -0x1b0f2b71b218eb.0p-70 */ 801573SrgrimesT29 = 2.6120754043964365e-6, /* 0x15e963a3cf3a39.0p-71 */ 811573SrgrimesT31 = -1.0407567231003314e-6, /* -0x1176041e656869.0p-72 */ 821573SrgrimesT33 = 3.4744117554063574e-7; /* 0x1750fe732cab9c.0p-74 */ 831573Srgrimes#endif /* LDBL_MANT_DIG == 64 */ 841573Srgrimes 85132817Stjrstatic inline long double 861573Srgrimesdivl(long double a, long double b, long double c, long double d, 87132817Stjr long double e, long double f) 881573Srgrimes{ 891573Srgrimes long double inv, r; 901573Srgrimes float fr, fw; 911573Srgrimes 92132817Stjr _2sumF(a, c); 931573Srgrimes b = b + c; 9419276Sache _2sumF(d, f); 9519276Sache e = e + f; 961573Srgrimes 971573Srgrimes inv = 1 / (d + e); 981573Srgrimes 991573Srgrimes r = (a + b) * inv; 1001573Srgrimes fr = r; 1011573Srgrimes r = fr; 1021573Srgrimes 1031573Srgrimes fw = d + e; 1041573Srgrimes e = d - fw + e; 1051573Srgrimes d = fw; 1061573Srgrimes 1071573Srgrimes r = r + (a - d * r + b - e * r) * inv; 1081573Srgrimes 1091573Srgrimes return r; 1101573Srgrimes} 1111573Srgrimes 1121573Srgrimeslong double 1131573Srgrimestanhl(long double x) 1141573Srgrimes{ 1151573Srgrimes long double hi,lo,s,x2,x4,z; 116132817Stjr double dx2; 117132817Stjr int16_t jx,ix; 118132817Stjr 119132817Stjr GET_LDBL_EXPSIGN(jx,x); 1201573Srgrimes ix = jx&0x7fff; 121132817Stjr 1221573Srgrimes /* x is INF or NaN */ 1231573Srgrimes if(ix>=0x7fff) { 1241573Srgrimes if (jx>=0) return one/x+one; /* tanh(+-inf)=+-1 */ 1251573Srgrimes else return one/x-one; /* tanh(NaN) = NaN */ 1261573Srgrimes } 1271573Srgrimes 128132817Stjr ENTERI(); 1291573Srgrimes 1301573Srgrimes /* |x| < 40 */ 1311573Srgrimes if (ix < 0x4004 || fabsl(x) < 40) { /* |x|<40 */ 1321573Srgrimes if (__predict_false(ix<BIAS-(LDBL_MANT_DIG+1)/2)) { /* |x|<TINY */ 1331573Srgrimes /* tanh(+-0) = +0; tanh(tiny) = tiny(-+) with inexact: */ 1341573Srgrimes return (x == 0 ? x : (0x1p200 * x - x) * 0x1p-200); 135132817Stjr } 1361573Srgrimes if (ix<0x3ffd) { /* |x|<0.25 */ 1371573Srgrimes x2 = x*x; 1381573Srgrimes#if LDBL_MANT_DIG == 64 1391573Srgrimes x4 = x2*x2; 1401573Srgrimes RETURNI(((T19*x2 + T17)*x4 + (T15*x2 + T13))*(x2*x*x2*x4*x4) + 1411573Srgrimes ((T11*x2 + T9)*x4 + (T7*x2 + T5))*(x2*x*x2) + 1421573Srgrimes T3*(x2*x) + x); 1431573Srgrimes#elif LDBL_MANT_DIG == 113 1441573Srgrimes dx2 = x2; 1451573Srgrimes#if 0 14690045Sobrien RETURNI(((((((((((((((T33*dx2 + T31)*dx2 + T29)*dx2 + T27)*dx2 + 147158812Sache T25)*x2 + T23)*x2 + T21)*x2 + T19)*x2 + T17)*x2 + 14890045Sobrien T15)*x2 + T13)*x2 + T11)*x2 + T9)*x2 + T7)*x2 + T5)* 14990045Sobrien (x2*x*x2) + 150132817Stjr T3*(x2*x) + x); 1511573Srgrimes#else 15290045Sobrien long double q = ((((((((((((((T33*dx2 + T31)*dx2 + T29)*dx2 + T27)*dx2 + 1531573Srgrimes T25)*x2 + T23)*x2 + T21)*x2 + T19)*x2 + T17)*x2 + 15490045Sobrien T15)*x2 + T13)*x2 + T11)*x2 + T9)*x2 + T7)*x2 + T5)* 155158812Sache (x2*x*x2); 156158812Sache RETURNI(q + T3*(x2*x) + x); 157158812Sache#endif 158158812Sache#endif 159158812Sache } 16074963Speter k_hexpl(2*fabsl(x), &hi, &lo); 16190045Sobrien if (ix<0x4001 && fabsl(x) < 1.5) /* |x|<1.5 */ 162158812Sache z = divl(hi, lo, -0.5, hi, lo, 0.5); 163158812Sache else 16490045Sobrien z = one - one/(lo+0.5+hi); 1651573Srgrimes /* |x| >= 40, return +-1 */ 16690045Sobrien } else { 1671573Srgrimes z = one - tiny; /* raise inexact flag */ 1681573Srgrimes } 1691573Srgrimes s = 1; 170159294Sdelphij if (jx<0) s = -1; 1711573Srgrimes RETURNI(s*z); 172159294Sdelphij} 173158812Sache