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 
  32 /*
  33  * __k_sin( double x;  double y )
  34  * kernel sin 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  *
  38  * Accurate Table look-up algorithm by K.C. Ng, May, 1995.
  39  *
  40  * Algorithm: see __sincos.c
  41  */
  42 
  43 #include "libm.h"
  44 
  45 static const double sc[] = {
  46 /* ONE  = */
  47         1.0,
  48 /* NONE = */ -1.0,
  49 
  50 /*
  51  * |sin(x) - (x+pp1*x^3+pp2*x^5)| <= 2^-58.79 for |x| < 0.008
  52  */
  53 /* PP1  = */-0.166666666666316558867252052378889521480627858683055567,
  54 /* PP2  = */.008333315652997472323564894248466758248475374977974017927,
  55 
  56 /*
  57  * |(sin(x) - (x+p1*x^3+...+p4*x^9)|
  58  * |------------------------------ | <= 2^-57.63 for |x| < 0.1953125
  59  * |                 x             |
  60  */
  61 /* P1   = */ -1.666666666666629669805215138920301589656e-0001,
  62 /* P2   = */ 8.333333332390951295683993455280336376663e-0003,
  63 /* P3   = */ -1.984126237997976692791551778230098403960e-0004,
  64 /* P4   = */ 2.753403624854277237649987622848330351110e-0006,
  65 
  66 /*
  67  * |cos(x) - (1+qq1*x^2+qq2*x^4)| <= 2^-55.99 for |x| <= 0.008 (0x3f80624d)
  68  */
  69 /* QQ1  = */-0.4999999999975492381842911981948418542742729,
  70 /* QQ2  = */0.041666542904352059294545209158357640398771740,
  71 
  72 /*
  73  * |cos(x) - (1+q1*x^2+...+q4*x^8)| <= 2^-55.86 for |x| <= 0.1640625 (10.5/64)
  74  */
  75 /* Q1   = */ -0.5,
  76 /* Q2   = */ 4.166666666500350703680945520860748617445e-0002,
  77 /* Q3   = */ -1.388888596436972210694266290577848696006e-0003,
  78 /* Q4   = */ 2.478563078858589473679519517892953492192e-0005,
  79 };
  80 
  81 
  82 #define ONE             sc[0]
  83 #define NONE            sc[1]
  84 #define PP1             sc[2]
  85 #define PP2             sc[3]
  86 #define P1              sc[4]
  87 #define P2              sc[5]
  88 #define P3              sc[6]
  89 #define P4              sc[7]
  90 #define QQ1             sc[8]
  91 #define QQ2             sc[9]
  92 #define Q1              sc[10]
  93 #define Q2              sc[11]
  94 #define Q3              sc[12]
  95 #define Q4              sc[13]
  96 
  97 extern const double _TBL_sincos[], _TBL_sincosx[];
  98 
  99 double
 100 __k_sin(double x, double y)
 101 {
 102         double z, w, s, v, p, q;
 103         int i, j, n, hx, ix;
 104 
 105         hx = ((int *)&x)[HIWORD];
 106         ix = hx & ~0x80000000;
 107 
 108         if (ix <= 0x3fc50000) {              /* |x| < 10.5/64 = 0.164062500 */
 109                 if (ix < 0x3e400000) /* |x| < 2**-27 */
 110                         if ((int)x == 0)
 111                                 return (x + y);
 112 
 113                 z = x * x;
 114 
 115                 if (ix < 0x3f800000) /* |x| < 0.008  */
 116                         p = (x * z) * (PP1 + z * PP2) + y;
 117                 else
 118                         p = (x * z) * ((P1 + z * P2) + (z * z) * (P3 + z *
 119                             P4)) + y;
 120 
 121                 return (x + p);
 122         } else {                        /* 0.164062500 < |x| < ~pi/4 */
 123                 n = ix >> 20;
 124                 i = (((ix >> 12) & 0xff) | 0x100) >> (0x401 - n);
 125                 j = i - 10;
 126 
 127                 if (hx < 0)
 128                         v = -y - (_TBL_sincosx[j] + x);
 129                 else
 130                         v = y - (_TBL_sincosx[j] - x);
 131 
 132                 s = v * v;
 133                 j <<= 1;
 134                 w = _TBL_sincos[j];
 135                 z = _TBL_sincos[j + 1];
 136                 p = s * (PP1 + s * PP2);
 137                 q = s * (QQ1 + s * QQ2);
 138                 p = v + v * p;
 139                 s = w * q + z * p;
 140                 return ((hx >= 0) ? w + s : -(w + s));
 141         }
 142 }