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  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  27  * Use is subject to license terms.
  28  */
  29 
  30 /*
  31  * long double __k_sinl(long double x, long double y);
  32  * kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.785398164
  33  * Input x is assumed to be bounded by ~pi/4 in magnitude.
  34  * Input y is the tail of x.
  35  *
  36  * Table look up algorithm
  37  *      1. by sin(-x) = -sin(x), need only to consider positive x
  38  *      2. if x < 25/128 = [0x3ffc9000,0,0,0] = 0.1953125 , then
  39  *           if x < 2^-57 (hx < 0x3fc60000,0,0,0), return x (inexact if x!= 0)
  40  *           z = x*x;
  41  *           if x <= 1/64 = 2**-6
  42  *              sin(x) = x + (y+(x*z)*(p1 + z*p2))
  43  *           else
  44  *              sin(x) = x + (y+(x*z)*(p1 + z*(p2 + z*(p3 + z*p4))))
  45  *      3. else
  46  *              ht = (hx + 0x400)&0x7ffff800        (round x to a break point t)
  47  *              lt = 0
  48  *              i  = (hy-0x3ffc4000)>>11; (i<=64)
  49  *              x' = (x - t)+y                  (|x'| ~<= 2^-7
  50  *         By
  51  *              sin(t+x')
  52  *                = sin(t)cos(x')+cos(t)sin(x')
  53  *                = sin(t)(1+z*(qq1+z*qq2))+[cos(t)]*x*(1+z*(pp1+z*pp2))
  54  *                = sin(t) + [sin(t)]*(z*(qq1+z*qq2))+
  55  *                              [cos(t)]*x*(1+z*(pp1+z*pp2))
  56  *
  57  *         Thus,
  58  *              let a= _TBL_sin_hi[i], b = _TBL_sin_lo[i], c= _TBL_cos_hi[i],
  59  *              x = (x-t)+y
  60  *              z = x*x;
  61  *              sin(t+x) = a+(b+ ((c*x)*(1+z*(pp1+z*pp2))+a*(z*(qq1+z*qq2)))
  62  */
  63 
  64 #include "libm.h"
  65 
  66 extern const long double _TBL_sinl_hi[], _TBL_sinl_lo[], _TBL_cosl_hi[];
  67 static const long double
  68 one     = 1.0L,
  69 /*
  70  *                   3           11       -122.32
  71  * |sin(x) - (x+pp1*x +...+ pp5*x  )| <= 2        for |x|<1/64
  72  */
  73         pp1     = -1.666666666666666666666666666586782940810e-0001L,
  74         pp2     = +8.333333333333333333333003723660929317540e-0003L,
  75         pp3     = -1.984126984126984076045903483778337804470e-0004L,
  76         pp4     = +2.755731922361906641319723106210900949413e-0006L,
  77         pp5     = -2.505198398570947019093998469135012057673e-0008L,
  78 /*
  79  * |(sin(x) - (x+p1*x^3+...+p8*x^17)|
  80  * |------------------------------- | <= 2^-116.17 for |x|<0.1953125
  81  * |                 x              |
  82  */
  83         p1      = -1.666666666666666666666666666666211262297e-0001L,
  84         p2      = +8.333333333333333333333333301497876908541e-0003L,
  85         p3      = -1.984126984126984126984041302881180621922e-0004L,
  86         p4      = +2.755731922398589064100587351307269621093e-0006L,
  87         p5      = -2.505210838544163129378906953765595393873e-0008L,
  88         p6      = +1.605904383643244375050998243778534074273e-0010L,
  89         p7      = -7.647162722800685516901456114270824622699e-0013L,
  90         p8      = +2.810046428661902961725428841068844462603e-0015L,
  91 /*
  92  *                   2           10        -123.84
  93  * |cos(x) - (1+qq1*x +...+ qq5*x  )| <= 2        for |x|<=1/128
  94  */
  95         qq1     = -4.999999999999999999999999999999378373641e-0001L,
  96         qq2     = +4.166666666666666666666665478399327703130e-0002L,
  97         qq3     = -1.388888888888888888058211230618051613494e-0003L,
  98         qq4     = +2.480158730156105377771585658905303111866e-0005L,
  99         qq5     = -2.755728099762526325736488376695157008736e-0007L;
 100 
 101 #define i0      0
 102 
 103 long double
 104 __k_sinl(long double x, long double y) {
 105         long double a, t, z, w;
 106         int *pt = (int *) &t, *px = (int *) &x;
 107         int i, j, hx, ix;
 108 
 109         t = 1.0L;
 110         hx = px[i0];
 111         ix = hx & 0x7fffffff;
 112         if (ix < 0x3ffc9000) {
 113                 *(3 - i0 + (int *) &t) = -1;        /* one-ulp */
 114                 *(2 + (int *) &t) = -1;     /* one-ulp */
 115                 *(1 + (int *) &t) = -1;     /* one-ulp */
 116                 *(i0 + (int *) &t) -= 1;    /* one-ulp */
 117                 if (ix < 0x3fc60000)
 118                         if (((int) (x * t)) < 1)
 119                                 return (x);     /* inexact and underflow */
 120                 z = x * x;
 121                 t = z * (p1 + z * (p2 + z * (p3 + z * (p4 + z * (p5 +
 122                         z * (p6 + z * (p7 + z * p8)))))));
 123                 t = y + x * t;
 124                 return (x + t);
 125         }
 126         j = (ix + 0x400) & 0x7ffff800;
 127         i = (j - 0x3ffc4000) >> 11;
 128         pt[i0] = j;
 129         if (hx > 0)
 130                 x = y - (t - x);
 131         else
 132                 x = (-y) - (t + x);
 133         a = _TBL_sinl_hi[i];
 134         z = x * x;
 135         t = z * (qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5))));
 136         w = x * (one + z * (pp1 + z * (pp2 + z * (pp3 + z * (pp4 + z * pp5)))));
 137         t = _TBL_cosl_hi[i] * w + a * t;
 138         t += _TBL_sinl_lo[i];
 139         if (hx < 0)
 140                 return (-a - t);
 141         else
 142                 return (a + t);
 143 }