1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  24  */
  25 
  26 /*
  27  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 
  32 /*
  33  * __k_tan( double x;  double y; int k )
  34  * kernel tan/cotan function on [-pi/4, pi/4], pi/4 ~ 0.785398164
  35  * Input x is assumed to be bounded by ~pi/4 in magnitude.
  36  * Input y is the tail of x.
  37  * Input k indicate -- tan if k=0; else -1/tan
  38  *
  39  * Table look up algorithm
  40  *      1. by tan(-x) = -tan(x), need only to consider positive x
  41  *      2. if x < 5/32 = [0x3fc40000, 0] = 0.15625 , then
  42  *           if x < 2^-27 (hx < 0x3e400000 0), set w=x with inexact if x !=  0
  43  *           else
  44  *              z = x*x;
  45  *              w = x + (y+(x*z)*(t1+z*(t2+z*(t3+z*(t4+z*(t5+z*t6))))))
  46  *         return (k == 0)? w: 1/w;
  47  *      3. else
  48  *              ht = (hx + 0x4000)&0x7fff8000       (round x to a break point t)
  49  *              lt = 0
  50  *              i  = (hy-0x3fc40000)>>15; (i<=64)
  51  *              x' = (x - t)+y                  (|x'| ~<= 2^-7)
  52  *         By
  53  *              tan(t+x')
  54  *                = (tan(t)+tan(x'))/(1-tan(x')tan(t))
  55  *         We have
  56  *                           sin(x')+tan(t)*(tan(t)*sin(x'))
  57  *                = tan(t) + -------------------------------    for k=0
  58  *                              cos(x') - tan(t)*sin(x')
  59  *
  60  *                           cos(x') - tan(t)*sin(x')
  61  *                = - --------------------------------------    for k=1
  62  *                     tan(t) + tan(t)*(cos(x')-1) + sin(x')
  63  *
  64  *
  65  *         where        tan(t) is from the table,
  66  *                      sin(x') = x + pp1*x^3 + pp2*x^5
  67  *                      cos(x') = 1 + qq1*x^2 + qq2*x^4
  68  */
  69 
  70 #include "libm.h"
  71 
  72 extern const double _TBL_tan_hi[], _TBL_tan_lo[];
  73 
  74 static const double q[] = {
  75 /* one  = */
  76         1.0,
  77 
  78 /*
  79  *                       2       2       -59.56
  80  * |sin(x) - pp1*x*(pp2+x *(pp3+x )| <= 2        for |x|<1/64
  81  */
  82 /* pp1  = */ 8.33326120969096230395312119298978359438478946686e-0003,
  83 /* pp2  = */ 1.20001038589438965215025680596868692381425944526e+0002,
  84 /* pp3  = */ -2.00001730975089451192161504877731204032897949219e+0001,
  85 
  86 /*
  87  *                   2      2        -56.19
  88  * |cos(x) - (1+qq1*x (qq2+x ))| <= 2        for |x|<=1/128
  89  */
  90 /* qq1  = */ 4.16665486385721928197511942926212213933467864990e-0002,
  91 /* qq2  = */ -1.20000339921340035687080671777948737144470214844e+0001,
  92 
  93 /*
  94  * |tan(x) - PF(x)|
  95  * |--------------| <= 2^-58.57 for |x|<0.15625
  96  * |      x       |
  97  *
  98  * where (let z = x*x)
  99  *      PF(x) = x + (t1*x*z)(t2 + z(t3 + z))(t4 + z)(t5 + z(t6 + z))
 100  */
 101 /* t1 = */ 3.71923358986516816929168705030406272271648049355e-0003,
 102 /* t2 = */ 6.02645120354857866118436504621058702468872070312e+0000,
 103 /* t3 = */ 2.42627327587398156083509093150496482849121093750e+0000,
 104 /* t4 = */ 2.44968983934252770851003333518747240304946899414e+0000,
 105 /* t5 = */ 6.07089252571767978849948121933266520500183105469e+0000,
 106 /* t6 = */ -2.49403756995593761658369658107403665781021118164e+0000,
 107 };
 108 
 109 #define one             q[0]
 110 #define pp1             q[1]
 111 #define pp2             q[2]
 112 #define pp3             q[3]
 113 #define qq1             q[4]
 114 #define qq2             q[5]
 115 #define t1              q[6]
 116 #define t2              q[7]
 117 #define t3              q[8]
 118 #define t4              q[9]
 119 #define t5              q[10]
 120 #define t6              q[11]
 121 
 122 
 123 double
 124 __k_tan(double x, double y, int k)
 125 {
 126         double a, t, z, w = 0.0L, s, c, r, rh, xh, xl;
 127         int i, j, hx, ix;
 128 
 129         t = one;
 130         hx = ((int *)&x)[HIWORD];
 131         ix = hx & 0x7fffffff;
 132 
 133         if (ix < 0x3fc40000) {                       /* 0.15625 */
 134                 if (ix < 0x3e400000) {               /* 2^-27 */
 135                         if ((i = (int)x) == 0)  /* generate inexact */
 136                                 w = x;
 137 
 138                         t = y;
 139                 } else {
 140                         z = x * x;
 141                         t = y + (((t1 * x) * z) * (t2 + z * (t3 + z))) * ((t4 +
 142                             z) * (t5 + z * (t6 + z)));
 143                         w = x + t;
 144                 }
 145 
 146                 if (k == 0)
 147                         return (w);
 148 
 149                 /*
 150                  * Compute -1/(x+T) with great care
 151                  * Let r = -1/(x+T), rh = r chopped to 20 bits.
 152                  * Also let xh  = x+T chopped to 20 bits, xl = (x-xh)+T. Then
 153                  *   -1/(x+T)   = rh + (-1/(x+T)-rh) = rh + r*(1+rh*(x+T))
 154                  *              = rh + r*((1+rh*xh)+rh*xl).
 155                  */
 156                 rh = r = -one / w;
 157                 ((int *)&rh)[LOWORD] = 0;
 158                 xh = w;
 159                 ((int *)&xh)[LOWORD] = 0;
 160                 xl = (x - xh) + t;
 161                 return (rh + r * ((one + rh * xh) + rh * xl));
 162         }
 163 
 164         j = (ix + 0x4000) & 0x7fff8000;
 165         i = (j - 0x3fc40000) >> 15;
 166         ((int *)&t)[HIWORD] = j;
 167 
 168         if (hx > 0)
 169                 x = y - (t - x);
 170         else
 171                 x = -y - (t + x);
 172 
 173         a = _TBL_tan_hi[i];
 174         z = x * x;
 175         s = (pp1 * x) * (pp2 + z * (pp3 + z));  /* sin(x) */
 176         t = (qq1 * z) * (qq2 + z);              /* cos(x) - 1 */
 177 
 178         if (k == 0) {
 179                 w = a * s;
 180                 t = _TBL_tan_lo[i] + (s + a * w) / (one - (w - t));
 181                 return (hx < 0 ? -a - t : a + t);
 182         } else {
 183                 w = s + a * t;
 184                 c = w + _TBL_tan_lo[i];
 185                 t = a * s - t;
 186 
 187                 /*
 188                  * Now try to compute [(1-T)/(a+c)] accurately
 189                  *
 190                  * Let r = 1/(a+c), rh = (1-T)*r chopped to 20 bits.
 191                  * Also let xh = a+c chopped to 20 bits, xl = (a-xh)+c. Then
 192                  *      (1-T)/(a+c) = rh + ((1-T)/(a+c)-rh)
 193                  *              = rh + r*(1-T-rh*(a+c))
 194                  *              = rh + r*((1-T-rh*xh)-rh*xl)
 195                  *              = rh + r*(((1-rh*xh)-T)-rh*xl)
 196                  */
 197                 r = one / (a + c);
 198                 rh = (one - t) * r;
 199                 ((int *)&rh)[LOWORD] = 0;
 200                 xh = a + c;
 201                 ((int *)&xh)[LOWORD] = 0;
 202                 xl = (a - xh) + c;
 203                 z = rh + r * (((one - rh * xh) - t) - rh * xl);
 204                 return (hx >= 0 ? -z : z);
 205         }
 206 }