1*25c28e83SPiotr Jasiukajtis /* 2*25c28e83SPiotr Jasiukajtis * CDDL HEADER START 3*25c28e83SPiotr Jasiukajtis * 4*25c28e83SPiotr Jasiukajtis * The contents of this file are subject to the terms of the 5*25c28e83SPiotr Jasiukajtis * Common Development and Distribution License (the "License"). 6*25c28e83SPiotr Jasiukajtis * You may not use this file except in compliance with the License. 7*25c28e83SPiotr Jasiukajtis * 8*25c28e83SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*25c28e83SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing. 10*25c28e83SPiotr Jasiukajtis * See the License for the specific language governing permissions 11*25c28e83SPiotr Jasiukajtis * and limitations under the License. 12*25c28e83SPiotr Jasiukajtis * 13*25c28e83SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each 14*25c28e83SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*25c28e83SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the 16*25c28e83SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying 17*25c28e83SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner] 18*25c28e83SPiotr Jasiukajtis * 19*25c28e83SPiotr Jasiukajtis * CDDL HEADER END 20*25c28e83SPiotr Jasiukajtis */ 21*25c28e83SPiotr Jasiukajtis 22*25c28e83SPiotr Jasiukajtis /* 23*25c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved. 24*25c28e83SPiotr Jasiukajtis */ 25*25c28e83SPiotr Jasiukajtis /* 26*25c28e83SPiotr Jasiukajtis * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 27*25c28e83SPiotr Jasiukajtis * Use is subject to license terms. 28*25c28e83SPiotr Jasiukajtis */ 29*25c28e83SPiotr Jasiukajtis 30*25c28e83SPiotr Jasiukajtis /* INDENT OFF */ 31*25c28e83SPiotr Jasiukajtis /* 32*25c28e83SPiotr Jasiukajtis * __k_tanl( long double x; long double y; int k ) 33*25c28e83SPiotr Jasiukajtis * kernel tan/cotan function on [-pi/4, pi/4], pi/4 ~ 0.785398164 34*25c28e83SPiotr Jasiukajtis * Input x is assumed to be bounded by ~pi/4 in magnitude. 35*25c28e83SPiotr Jasiukajtis * Input y is the tail of x. 36*25c28e83SPiotr Jasiukajtis * Input k indicate -- tan if k=0; else -1/tan 37*25c28e83SPiotr Jasiukajtis * 38*25c28e83SPiotr Jasiukajtis * Table look up algorithm 39*25c28e83SPiotr Jasiukajtis * 1. by tan(-x) = -tan(x), need only to consider positive x 40*25c28e83SPiotr Jasiukajtis * 2. if x < 5/32 = [0x3ffc4000, 0] = 0.15625 , then 41*25c28e83SPiotr Jasiukajtis * if x < 2^-57 (hx < 0x3fc40000 0), set w=x with inexact if x != 0 42*25c28e83SPiotr Jasiukajtis * else 43*25c28e83SPiotr Jasiukajtis * z = x*x; 44*25c28e83SPiotr Jasiukajtis * w = x + (y+(x*z)*(t1+z*(t2+z*(t3+z*(t4+z*(t5+z*t6)))))) 45*25c28e83SPiotr Jasiukajtis * return (k == 0 ? w : 1/w); 46*25c28e83SPiotr Jasiukajtis * 3. else 47*25c28e83SPiotr Jasiukajtis * ht = (hx + 0x400)&0x7ffff800 (round x to a break point t) 48*25c28e83SPiotr Jasiukajtis * lt = 0 49*25c28e83SPiotr Jasiukajtis * i = (hy-0x3ffc4000)>>11; (i<=64) 50*25c28e83SPiotr Jasiukajtis * x' = (x - t)+y (|x'| ~<= 2^-7) 51*25c28e83SPiotr Jasiukajtis * By 52*25c28e83SPiotr Jasiukajtis * tan(t+x') 53*25c28e83SPiotr Jasiukajtis * = (tan(t)+tan(x'))/(1-tan(x')tan(t)) 54*25c28e83SPiotr Jasiukajtis * We have 55*25c28e83SPiotr Jasiukajtis * sin(x')+tan(t)*(tan(t)*sin(x')) 56*25c28e83SPiotr Jasiukajtis * = tan(t) + ------------------------------- for k=0 57*25c28e83SPiotr Jasiukajtis * cos(x') - tan(t)*sin(x') 58*25c28e83SPiotr Jasiukajtis * 59*25c28e83SPiotr Jasiukajtis * cos(x') - tan(t)*sin(x') 60*25c28e83SPiotr Jasiukajtis * = - -------------------------------------- for k=1 61*25c28e83SPiotr Jasiukajtis * tan(t) + tan(t)*(cos(x')-1) + sin(x') 62*25c28e83SPiotr Jasiukajtis * 63*25c28e83SPiotr Jasiukajtis * 64*25c28e83SPiotr Jasiukajtis * where tan(t) is from the table, 65*25c28e83SPiotr Jasiukajtis * sin(x') = x + pp1*x^3 + ...+ pp5*x^11 66*25c28e83SPiotr Jasiukajtis * cos(x') = 1 + qq1*x^2 + ...+ qq5*x^10 67*25c28e83SPiotr Jasiukajtis */ 68*25c28e83SPiotr Jasiukajtis 69*25c28e83SPiotr Jasiukajtis #include "libm.h" 70*25c28e83SPiotr Jasiukajtis 71*25c28e83SPiotr Jasiukajtis #include <sys/isa_defs.h> 72*25c28e83SPiotr Jasiukajtis 73*25c28e83SPiotr Jasiukajtis extern const long double _TBL_tanl_hi[], _TBL_tanl_lo[]; 74*25c28e83SPiotr Jasiukajtis static const long double 75*25c28e83SPiotr Jasiukajtis one = 1.0, 76*25c28e83SPiotr Jasiukajtis /* 77*25c28e83SPiotr Jasiukajtis * |sin(x) - (x+pp1*x^3+...+ pp5*x^11)| <= 2^-122.32 for |x|<1/64 78*25c28e83SPiotr Jasiukajtis */ 79*25c28e83SPiotr Jasiukajtis pp1 = -1.666666666666666666666666666586782940810e-0001L, 80*25c28e83SPiotr Jasiukajtis pp2 = 8.333333333333333333333003723660929317540e-0003L, 81*25c28e83SPiotr Jasiukajtis pp3 = -1.984126984126984076045903483778337804470e-0004L, 82*25c28e83SPiotr Jasiukajtis pp4 = 2.755731922361906641319723106210900949413e-0006L, 83*25c28e83SPiotr Jasiukajtis pp5 = -2.505198398570947019093998469135012057673e-0008L, 84*25c28e83SPiotr Jasiukajtis /* 85*25c28e83SPiotr Jasiukajtis * 2 10 -123.84 86*25c28e83SPiotr Jasiukajtis * |cos(x) - (1+qq1*x +...+ qq5*x )| <= 2 for |x|<=1/128 87*25c28e83SPiotr Jasiukajtis */ 88*25c28e83SPiotr Jasiukajtis qq1 = -4.999999999999999999999999999999378373641e-0001L, 89*25c28e83SPiotr Jasiukajtis qq2 = 4.166666666666666666666665478399327703130e-0002L, 90*25c28e83SPiotr Jasiukajtis qq3 = -1.388888888888888888058211230618051613494e-0003L, 91*25c28e83SPiotr Jasiukajtis qq4 = 2.480158730156105377771585658905303111866e-0005L, 92*25c28e83SPiotr Jasiukajtis qq5 = -2.755728099762526325736488376695157008736e-0007L, 93*25c28e83SPiotr Jasiukajtis /* 94*25c28e83SPiotr Jasiukajtis * |tan(x) - (x+t1*x^3+...+t6*x^13)| 95*25c28e83SPiotr Jasiukajtis * |------------------------------ | <= 2^-59.73 for |x|<0.15625 96*25c28e83SPiotr Jasiukajtis * | x | 97*25c28e83SPiotr Jasiukajtis */ 98*25c28e83SPiotr Jasiukajtis t1 = 3.333333333333333333333333333333423342490e-0001L, 99*25c28e83SPiotr Jasiukajtis t2 = 1.333333333333333333333333333093838744537e-0001L, 100*25c28e83SPiotr Jasiukajtis t3 = 5.396825396825396825396827906318682662250e-0002L, 101*25c28e83SPiotr Jasiukajtis t4 = 2.186948853615520282185576976994418486911e-0002L, 102*25c28e83SPiotr Jasiukajtis t5 = 8.863235529902196573354554519991152936246e-0003L, 103*25c28e83SPiotr Jasiukajtis t6 = 3.592128036572480064652191427543994878790e-0003L, 104*25c28e83SPiotr Jasiukajtis t7 = 1.455834387051455257856833807581901305474e-0003L, 105*25c28e83SPiotr Jasiukajtis t8 = 5.900274409318599857829983256201725587477e-0004L, 106*25c28e83SPiotr Jasiukajtis t9 = 2.391291152117265181501116961901122362937e-0004L, 107*25c28e83SPiotr Jasiukajtis t10 = 9.691533169382729742394024173194981882375e-0005L, 108*25c28e83SPiotr Jasiukajtis t11 = 3.927994733186415603228178184225780859951e-0005L, 109*25c28e83SPiotr Jasiukajtis t12 = 1.588300018848323824227640064883334101288e-0005L, 110*25c28e83SPiotr Jasiukajtis t13 = 6.916271223396808311166202285131722231723e-0006L; 111*25c28e83SPiotr Jasiukajtis /* INDENT ON */ 112*25c28e83SPiotr Jasiukajtis long double 113*25c28e83SPiotr Jasiukajtis __k_tanl(long double x, long double y, int k) { 114*25c28e83SPiotr Jasiukajtis long double a, t, z, w = 0.0, s, c; 115*25c28e83SPiotr Jasiukajtis int *pt = (int *) &t, *px = (int *) &x; 116*25c28e83SPiotr Jasiukajtis int i, j, hx, ix; 117*25c28e83SPiotr Jasiukajtis 118*25c28e83SPiotr Jasiukajtis t = 1.0; 119*25c28e83SPiotr Jasiukajtis #if defined(__i386) || defined(__amd64) 120*25c28e83SPiotr Jasiukajtis XTOI(px, hx); 121*25c28e83SPiotr Jasiukajtis #else 122*25c28e83SPiotr Jasiukajtis hx = px[0]; 123*25c28e83SPiotr Jasiukajtis #endif 124*25c28e83SPiotr Jasiukajtis ix = hx & 0x7fffffff; 125*25c28e83SPiotr Jasiukajtis if (ix < 0x3ffc4000) { 126*25c28e83SPiotr Jasiukajtis if (ix < 0x3fc60000) { 127*25c28e83SPiotr Jasiukajtis if ((i = (int) x) == 0) /* generate inexact */ 128*25c28e83SPiotr Jasiukajtis w = x; 129*25c28e83SPiotr Jasiukajtis } else { 130*25c28e83SPiotr Jasiukajtis z = x * x; 131*25c28e83SPiotr Jasiukajtis if (ix < 0x3ff30000) /* 2**-12 */ 132*25c28e83SPiotr Jasiukajtis t = z * (t1 + z * (t2 + z * (t3 + z * t4))); 133*25c28e83SPiotr Jasiukajtis else 134*25c28e83SPiotr Jasiukajtis t = z * (t1 + z * (t2 + z * (t3 + z * (t4 + 135*25c28e83SPiotr Jasiukajtis z * (t5 + z * (t6 + z * (t7 + z * 136*25c28e83SPiotr Jasiukajtis (t8 + z * (t9 + z * (t10 + z * (t11 + 137*25c28e83SPiotr Jasiukajtis z * (t12 + z * t13)))))))))))); 138*25c28e83SPiotr Jasiukajtis t = y + x * t; 139*25c28e83SPiotr Jasiukajtis w = x + t; 140*25c28e83SPiotr Jasiukajtis } 141*25c28e83SPiotr Jasiukajtis return (k == 0 ? w : -one / w); 142*25c28e83SPiotr Jasiukajtis } 143*25c28e83SPiotr Jasiukajtis j = (ix + 0x400) & 0x7ffff800; 144*25c28e83SPiotr Jasiukajtis i = (j - 0x3ffc4000) >> 11; 145*25c28e83SPiotr Jasiukajtis #if defined(__i386) || defined(__amd64) 146*25c28e83SPiotr Jasiukajtis ITOX(j, pt); 147*25c28e83SPiotr Jasiukajtis #else 148*25c28e83SPiotr Jasiukajtis pt[0] = j; 149*25c28e83SPiotr Jasiukajtis #endif 150*25c28e83SPiotr Jasiukajtis if (hx > 0) 151*25c28e83SPiotr Jasiukajtis x = y - (t - x); 152*25c28e83SPiotr Jasiukajtis else 153*25c28e83SPiotr Jasiukajtis x = (-y) - (t + x); 154*25c28e83SPiotr Jasiukajtis a = _TBL_tanl_hi[i]; 155*25c28e83SPiotr Jasiukajtis z = x * x; 156*25c28e83SPiotr Jasiukajtis /* cos(x)-1 */ 157*25c28e83SPiotr Jasiukajtis t = z * (qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5)))); 158*25c28e83SPiotr Jasiukajtis /* sin(x) */ 159*25c28e83SPiotr Jasiukajtis s = x * (one + z * (pp1 + z * (pp2 + z * (pp3 + z * (pp4 + z * 160*25c28e83SPiotr Jasiukajtis pp5))))); 161*25c28e83SPiotr Jasiukajtis if (k == 0) { 162*25c28e83SPiotr Jasiukajtis w = a * s; 163*25c28e83SPiotr Jasiukajtis t = _TBL_tanl_lo[i] + (s + a * w) / (one - (w - t)); 164*25c28e83SPiotr Jasiukajtis return (hx < 0 ? -a - t : a + t); 165*25c28e83SPiotr Jasiukajtis } else { 166*25c28e83SPiotr Jasiukajtis w = s + a * t; 167*25c28e83SPiotr Jasiukajtis c = w + _TBL_tanl_lo[i]; 168*25c28e83SPiotr Jasiukajtis z = (one - (a * s - t)); 169*25c28e83SPiotr Jasiukajtis return (hx >= 0 ? z / (-a - c) : z / (a + c)); 170*25c28e83SPiotr Jasiukajtis } 171*25c28e83SPiotr Jasiukajtis } 172