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 2005 Sun Microsystems, Inc.  All rights reserved.
  28  * Use is subject to license terms.
  29  */
  30 
  31 #pragma weak __sincos = sincos
  32 
  33 /*
  34  * sincos(x,s,c)
  35  * Accurate Table look-up algorithm by K.C. Ng, 2000.
  36  *
  37  * 1. Reduce x to x>0 by cos(-x)=cos(x), sin(-x)=-sin(x).
  38  * 2. For 0<= x < 8, let i = (64*x chopped)-10. Let d = x - a[i], where
  39  *    a[i] is a double that is close to (i+10.5)/64 (and hence |d|< 10.5/64)
  40  *    and such that sin(a[i]) and cos(a[i]) is close to a double (with error
  41  *    less than 2**-8 ulp). Then
  42  *
  43  *      cos(x) = cos(a[i]+d) = cos(a[i])cos(d) - sin(a[i])*sin(d)
  44  *             = TBL_cos_a[i]*(1+QQ1*d^2+QQ2*d^4) -
  45  *                      TBL_sin_a[i]*(d+PP1*d^3+PP2*d^5)
  46  *             = TBL_cos_a[i] + (TBL_cos_a[i]*d^2*(QQ1+QQ2*d^2) -
  47  *                      TBL_sin_a[i]*(d+PP1*d^3+PP2*d^5))
  48  *
  49  *      sin(x) = sin(a[i]+d) = sin(a[i])cos(d) + cos(a[i])*sin(d)
  50  *             = TBL_sin_a[i]*(1+QQ1*d^2+QQ2*d^4) +
  51  *                      TBL_cos_a[i]*(d+PP1*d^3+PP2*d^5)
  52  *             = TBL_sin_a[i] + (TBL_sin_a[i]*d^2*(QQ1+QQ2*d^2) +
  53  *                      TBL_cos_a[i]*(d+PP1*d^3+PP2*d^5))
  54  *
  55  *    Note: for x close to n*pi/2, special treatment is need for either
  56  *    sin or cos:
  57  *    i in [81, 100] (  pi/2 +-10.5/64 => tiny cos(x) = sin(pi/2-x)
  58  *    i in [181,200] (  pi   +-10.5/64 => tiny sin(x) = sin(pi-x)
  59  *    i in [282,301] (  3pi/2+-10.5/64 => tiny cos(x) = sin(x-3pi/2)
  60  *    i in [382,401] (  2pi  +-10.5/64 => tiny sin(x) = sin(x-2pi)
  61  *    i in [483,502] (  5pi/2+-10.5/64 => tiny cos(x) = sin(5pi/2-x)
  62  *
  63  * 3. For x >= 8.0, use kernel function __rem_pio2 to perform argument
  64  *    reduction and call __k_sincos_ to compute sin and cos.
  65  *
  66  * kernel function:
  67  *      __rem_pio2      ... argument reduction routine
  68  *      __k_sincos_     ... sine and cosine function on [-pi/4,pi/4]
  69  *
  70  * Method.
  71  *      Let S and C denote the sin and cos respectively on [-PI/4, +PI/4].
  72  *      1. Assume the argument x is reduced to y1+y2 = x-k*pi/2 in
  73  *         [-pi/2 , +pi/2], and let n = k mod 4.
  74  *      2. Let S=S(y1+y2), C=C(y1+y2). Depending on n, we have
  75  *
  76  *          n        sin(x)      cos(x)        tan(x)
  77  *     ----------------------------------------------------------
  78  *          0          S           C             S/C
  79  *          1          C          -S            -C/S
  80  *          2         -S          -C             S/C
  81  *          3         -C           S            -C/S
  82  *     ----------------------------------------------------------
  83  *
  84  * Special cases:
  85  *      Let trig be any of sin, cos, or tan.
  86  *      trig(+-INF)  is NaN, with signals;
  87  *      trig(NaN)    is that NaN;
  88  *
  89  * Accuracy:
  90  *      TRIG(x) returns trig(x) nearly rounded (less than 1 ulp)
  91  */
  92 
  93 #include "libm.h"
  94 
  95 /* BEGIN CSTYLED */
  96 static const double sc[] = {
  97 /* ONE  = */
  98         1.0,
  99 /* NONE = */ -1.0,
 100 
 101 /*
 102  * |sin(x) - (x+pp1*x^3+pp2*x^5)| <= 2^-58.79 for |x| < 0.008
 103  */
 104 /* PP1  = */-0.166666666666316558867252052378889521480627858683055567,
 105 /* PP2  = */.008333315652997472323564894248466758248475374977974017927,
 106 
 107 /*
 108  * |(sin(x) - (x+p1*x^3+...+p4*x^9)|
 109  * |------------------------------ | <= 2^-57.63 for |x| < 0.1953125
 110  * |                 x             |
 111  */
 112 /* P1   = */ -1.666666666666629669805215138920301589656e-0001,
 113 /* P2   = */ 8.333333332390951295683993455280336376663e-0003,
 114 /* P3   = */ -1.984126237997976692791551778230098403960e-0004,
 115 /* P4   = */ 2.753403624854277237649987622848330351110e-0006,
 116 
 117 /*
 118  * |cos(x) - (1+qq1*x^2+qq2*x^4)| <= 2^-55.99 for |x| <= 0.008 (0x3f80624d)
 119  */
 120 /* QQ1  = */-0.4999999999975492381842911981948418542742729,
 121 /* QQ2  = */0.041666542904352059294545209158357640398771740,
 122 /* Q1   = */ -0.5,
 123 /* Q2   = */ 4.166666666500350703680945520860748617445e-0002,
 124 /* Q3   = */ -1.388888596436972210694266290577848696006e-0003,
 125 /* Q4   = */ 2.478563078858589473679519517892953492192e-0005,
 126 /* PIO2_H    = */ 1.570796326794896557999,
 127 /* PIO2_L    = */ 6.123233995736765886130e-17,
 128 /* PIO2_L0   = */ 6.123233995727922165564e-17,
 129 /* PIO2_L1   = */ 8.843720566135701120255e-29,
 130 /* PI_H      = */ 3.1415926535897931159979634685,
 131 /* PI_L      = */ 1.22464679914735317722606593227425e-16,
 132 /* PI_L0     = */ 1.22464679914558443311283879205095e-16,
 133 /* PI_L1     = */ 1.768744113227140223300005233735517376e-28,
 134 /* PI3O2_H   = */ 4.712388980384689673997,
 135 /* PI3O2_L   = */ 1.836970198721029765839e-16,
 136 /* PI3O2_L0  = */ 1.836970198720396133587e-16,
 137 /* PI3O2_L1  = */ 6.336322524749201142226e-29,
 138 /* PI2_H     = */ 6.2831853071795862319959269370,
 139 /* PI2_L     = */ 2.44929359829470635445213186454850e-16,
 140 /* PI2_L0    = */ 2.44929359829116886622567758410190e-16,
 141 /* PI2_L1    = */ 3.537488226454280446600010467471034752e-28,
 142 /* PI5O2_H   = */ 7.853981633974482789995,
 143 /* PI5O2_L   = */ 3.061616997868382943065e-16,
 144 /* PI5O2_L0  = */ 3.061616997861941598865e-16,
 145 /* PI5O2_L1  = */ 6.441344200433640781982e-28,
 146 };
 147 /* END CSTYLED */
 148 
 149 #define ONE                     sc[0]
 150 #define PP1                     sc[2]
 151 #define PP2                     sc[3]
 152 #define P1                      sc[4]
 153 #define P2                      sc[5]
 154 #define P3                      sc[6]
 155 #define P4                      sc[7]
 156 #define QQ1                     sc[8]
 157 #define QQ2                     sc[9]
 158 #define Q1                      sc[10]
 159 #define Q2                      sc[11]
 160 #define Q3                      sc[12]
 161 #define Q4                      sc[13]
 162 #define PIO2_H                  sc[14]
 163 #define PIO2_L                  sc[15]
 164 #define PIO2_L0                 sc[16]
 165 #define PIO2_L1                 sc[17]
 166 #define PI_H                    sc[18]
 167 #define PI_L                    sc[19]
 168 #define PI_L0                   sc[20]
 169 #define PI_L1                   sc[21]
 170 #define PI3O2_H                 sc[22]
 171 #define PI3O2_L                 sc[23]
 172 #define PI3O2_L0                sc[24]
 173 #define PI3O2_L1                sc[25]
 174 #define PI2_H                   sc[26]
 175 #define PI2_L                   sc[27]
 176 #define PI2_L0                  sc[28]
 177 #define PI2_L1                  sc[29]
 178 #define PI5O2_H                 sc[30]
 179 #define PI5O2_L                 sc[31]
 180 #define PI5O2_L0                sc[32]
 181 #define PI5O2_L1                sc[33]
 182 #define PoS(x, z)               ((x * z) * (PP1 + z * PP2))
 183 #define PoL(x, z)               ((x * z) * ((P1 + z * P2) + (z * z) * \
 184         (P3 + z * P4)))
 185 
 186 extern const double _TBL_sincos[], _TBL_sincosx[];
 187 
 188 void
 189 sincos(double x, double *s, double *c)
 190 {
 191         double z, y[2], w, t, v, p, q;
 192         int i, j, n, hx, ix, lx;
 193 
 194         hx = ((int *)&x)[HIWORD];
 195         lx = ((int *)&x)[LOWORD];
 196         ix = hx & ~0x80000000;
 197 
 198         if (ix <= 0x3fc50000) {              /* |x| < 10.5/64 = 0.164062500 */
 199                 if (ix < 0x3e400000) {       /* |x| < 2**-27 */
 200                         if ((int)x == 0)
 201                                 *c = ONE;
 202 
 203                         *s = x;
 204                 } else {
 205                         z = x * x;
 206 
 207                         if (ix < 0x3f800000) {       /* |x| < 0.008 */
 208                                 q = z * (QQ1 + z * QQ2);
 209                                 p = PoS(x, z);
 210                         } else {
 211                                 q = z * ((Q1 + z * Q2) + (z * z) * (Q3 + z *
 212                                     Q4));
 213                                 p = PoL(x, z);
 214                         }
 215 
 216                         *c = ONE + q;
 217                         *s = x + p;
 218                 }
 219 
 220                 return;
 221         }
 222 
 223         n = ix >> 20;
 224         i = (((ix >> 12) & 0xff) | 0x100) >> (0x401 - n);
 225         j = i - 10;
 226 
 227         if (n < 0x402) {             /* |x| < 8 */
 228                 x = fabs(x);
 229                 v = x - _TBL_sincosx[j];
 230                 t = v * v;
 231                 w = _TBL_sincos[(j << 1)];
 232                 z = _TBL_sincos[(j << 1) + 1];
 233                 p = v + PoS(v, t);
 234                 q = t * (QQ1 + t * QQ2);
 235 
 236                 if ((((j - 81) ^ (j - 101)) | ((j - 282) ^ (j - 302)) | ((j -
 237                     483) ^ (j - 503)) | ((j - 181) ^ (j - 201)) | ((j - 382) ^
 238                     (j - 402))) < 0) {
 239                         if (j <= 101) {
 240                                 /* near pi/2, cos(x) = sin(pi/2-x) */
 241                                 t = w * q + z * p;
 242                                 *s = (hx >= 0) ? w + t : -w - t;
 243                                 p = PIO2_H - x;
 244                                 i = ix - 0x3ff921fb;
 245                                 x = p + PIO2_L;
 246 
 247                                 if ((i | ((lx - 0x54442D00) & 0xffffff00)) ==
 248                                     0) {
 249                                         /* very close to pi/2 */
 250                                         x = p + PIO2_L0;
 251                                         *c = x + PIO2_L1;
 252                                 } else {
 253                                         z = x * x;
 254 
 255                                         if (((ix - 0x3ff92000) >> 12) == 0) {
 256                                                 /* |pi/2-x|<2**-8 */
 257                                                 w = PIO2_L + PoS(x, z);
 258                                         } else {
 259                                                 w = PIO2_L + PoL(x, z);
 260                                         }
 261 
 262                                         *c = p + w;
 263                                 }
 264                         } else if (j <= 201) {
 265                                 /* near pi, sin(x) = sin(pi-x) */
 266                                 *c = z - (w * p - z * q);
 267                                 p = PI_H - x;
 268                                 i = ix - 0x400921fb;
 269                                 x = p + PI_L;
 270 
 271                                 if ((i | ((lx - 0x54442D00) & 0xffffff00)) ==
 272                                     0) {
 273                                         /* very close to pi */
 274                                         x = p + PI_L0;
 275                                         *s = (hx >= 0) ? x + PI_L1 : -(x +
 276                                             PI_L1);
 277                                 } else {
 278                                         z = x * x;
 279 
 280                                         if (((ix - 0x40092000) >> 11) == 0) {
 281                                                 /* |pi-x|<2**-8 */
 282                                                 w = PI_L + PoS(x, z);
 283                                         } else {
 284                                                 w = PI_L + PoL(x, z);
 285                                         }
 286 
 287                                         *s = (hx >= 0) ? p + w : -p - w;
 288                                 }
 289                         } else if (j <= 302) {
 290                                 /* near 3/2pi, cos(x)=sin(x-3/2pi) */
 291                                 t = w * q + z * p;
 292                                 *s = (hx >= 0) ? w + t : -w - t;
 293                                 p = x - PI3O2_H;
 294                                 i = ix - 0x4012D97C;
 295                                 x = p - PI3O2_L;
 296 
 297                                 if ((i | ((lx - 0x7f332100) & 0xffffff00)) ==
 298                                     0) {
 299                                         /* very close to 3/2pi */
 300                                         x = p - PI3O2_L0;
 301                                         *c = x - PI3O2_L1;
 302                                 } else {
 303                                         z = x * x;
 304 
 305                                         if (((ix - 0x4012D800) >> 9) == 0) {
 306                                                 /* |3/2pi-x|<2**-8 */
 307                                                 w = PoS(x, z) - PI3O2_L;
 308                                         } else {
 309                                                 w = PoL(x, z) - PI3O2_L;
 310                                         }
 311 
 312                                         *c = p + w;
 313                                 }
 314                         } else if (j <= 402) {
 315                                 /* near 2pi, sin(x)=sin(x-2pi) */
 316                                 *c = z - (w * p - z * q);
 317                                 p = x - PI2_H;
 318                                 i = ix - 0x401921fb;
 319                                 x = p - PI2_L;
 320 
 321                                 if ((i | ((lx - 0x54442D00) & 0xffffff00)) ==
 322                                     0) {
 323                                         /* very close to 2pi */
 324                                         x = p - PI2_L0;
 325                                         *s = (hx >= 0) ? x - PI2_L1 : -(x -
 326                                             PI2_L1);
 327                                 } else {
 328                                         z = x * x;
 329 
 330                                         if (((ix - 0x40192000) >> 10) == 0) {
 331                                                 /* |x-2pi|<2**-8 */
 332                                                 w = PoS(x, z) - PI2_L;
 333                                         } else {
 334                                                 w = PoL(x, z) - PI2_L;
 335                                         }
 336 
 337                                         *s = (hx >= 0) ? p + w : -p - w;
 338                                 }
 339                         } else {
 340                                 /* near 5pi/2, cos(x) = sin(5pi/2-x) */
 341                                 t = w * q + z * p;
 342                                 *s = (hx >= 0) ? w + t : -w - t;
 343                                 p = PI5O2_H - x;
 344                                 i = ix - 0x401F6A7A;
 345                                 x = p + PI5O2_L;
 346 
 347                                 if ((i | ((lx - 0x29553800) & 0xffffff00)) ==
 348                                     0) {
 349                                         /* very close to pi/2 */
 350                                         x = p + PI5O2_L0;
 351                                         *c = x + PI5O2_L1;
 352                                 } else {
 353                                         z = x * x;
 354 
 355                                         if (((ix - 0x401F6A7A) >> 7) == 0) {
 356                                                 /* |5pi/2-x|<2**-8 */
 357                                                 w = PI5O2_L + PoS(x, z);
 358                                         } else {
 359                                                 w = PI5O2_L + PoL(x, z);
 360                                         }
 361 
 362                                         *c = p + w;
 363                                 }
 364                         }
 365                 } else {
 366                         *c = z - (w * p - z * q);
 367                         t = w * q + z * p;
 368                         *s = (hx >= 0) ? w + t : -w - t;
 369                 }
 370 
 371                 return;
 372         }
 373 
 374         if (ix >= 0x7ff00000) {
 375                 *s = *c = x / x;
 376                 return;
 377         }
 378 
 379         /* argument reduction needed */
 380         n = __rem_pio2(x, y);
 381 
 382         switch (n & 3) {
 383         case 0:
 384                 *s = __k_sincos(y[0], y[1], c);
 385                 break;
 386         case 1:
 387                 *c = -__k_sincos(y[0], y[1], s);
 388                 break;
 389         case 2:
 390                 *s = -__k_sincos(y[0], y[1], c);
 391                 *c = -*c;
 392                 break;
 393         default:
 394                 *c = __k_sincos(y[0], y[1], s);
 395                 *s = -*s;
 396         }
 397 }