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