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 /* BEGIN CSTYLED */
  32 /*
  33  * __k_cosl( long double x;  long double y )
  34  * kernel cos 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  * Table look up algorithm
  39  *      1. by cos(-x) = cos(x), we may replace x by |x|
  40  *      2. if x < 25/128 = [0x3ffc4000, 0] = 0.15625 , then
  41  *           if x < 2^-57 (hx < 0x3fc60000 0), return 1.0 with inexact if x !=  0
  42  *           z = x*x;
  43  *           if x <= 1/128 = 2**-7 = 0.0078125
  44  *              cos(x)=1.0+z*(qq1+z*(qq2+z*(qq3+z*(qq4+z*qq5))))
  45  *           else
  46  *              cos(x)=1.0+z*(q1+ ... z*q8)
  47  *      3. else
  48  *              ht = (hx + 0x400)&0x7ffff800        (round x to a break point t)
  49  *              lt = 0
  50  *              i  = (hy-0x3ffc4000)>>11; (i<=64)
  51  *              x' = (x - t)+y                  (|x'| ~<= 2^-7
  52  *         By
  53  *              cos(t+x')
  54  *                = cos(t)cos(x')-sin(t)sin(x')
  55  *                = cos(t)(1+z*(qq1+z*qq2))-[sin(t)]*x*(1+z*(pp1+z*pp2))
  56  *                = cos(t) + [cos(t)]*(z*(qq1+z*qq2))-
  57  *                              [sin(t)]*x*(1+z*(pp1+z*pp2))
  58  *
  59  *         Thus,
  60  *              let a= _TBL_cos_hi[i], b = _TBL_cos_lo[i], c= _TBL_sin_hi[i],
  61  *              x = (x-t)+y
  62  *              z = x*x;
  63  *              cos(t+x) = a+(b+ (-c*x*(1+z*(pp1+z*pp2))+a*(z*(qq1+z*qq2)))
  64  */
  65 /* END CSTYLED */
  66 
  67 #include "libm.h"
  68 
  69 #include <sys/isa_defs.h>
  70 
  71 extern const long double _TBL_cosl_hi[], _TBL_cosl_lo[], _TBL_sinl_hi[];
  72 static const long double one = 1.0;
  73 
  74 /*
  75  * |sin(x) - (x+pp1*x^3+...+ pp5*x^11 )| <= 2^-122.32 for |x|<1/64
  76  */
  77 static const long double
  78         pp1 = -1.666666666666666666666666666586782940810e-0001L,
  79         pp2 = 8.333333333333333333333003723660929317540e-0003L,
  80         pp3 = -1.984126984126984076045903483778337804470e-0004L,
  81         pp4 = 2.755731922361906641319723106210900949413e-0006L,
  82         pp5 = -2.505198398570947019093998469135012057673e-0008L;
  83 
  84 /*
  85  *
  86  * |cos(x) - (1+q1*x^2+...+q8*x^16)| <= 2^-117.11 for |x|<= 0.15625
  87  */
  88 static const long double
  89         q1 = -4.999999999999999999999999999999756416975e-0001L,
  90         q2 = 4.166666666666666666666666664006066577258e-0002L,
  91         q3 = -1.388888888888888888888877700363937169637e-0003L,
  92         q4 = 2.480158730158730158494468463031814083559e-0005L,
  93         q5 = -2.755731922398586276322819250356005542871e-0007L,
  94         q6 = 2.087675698767424261441959760729854017855e-0009L,
  95         q7 = -1.147074481239662089072452129010790774761e-0011L,
  96         q8 = 4.777761647399651599730663422263531034782e-0014L;
  97 
  98 /*
  99  *
 100  * |cos(x) - (1+qq1*x^2+...+ qq5*x^10)| <= 2^-123.84 for |x|<=1/128
 101  */
 102 static const long double
 103         qq1 = -4.999999999999999999999999999999378373641e-0001L,
 104         qq2 = 4.166666666666666666666665478399327703130e-0002L,
 105         qq3 = -1.388888888888888888058211230618051613494e-0003L,
 106         qq4 = 2.480158730156105377771585658905303111866e-0005L,
 107         qq5 = -2.755728099762526325736488376695157008736e-0007L;
 108 
 109 
 110 long double
 111 __k_cosl(long double x, long double y)
 112 {
 113         long double a, t, z, w;
 114         int *pt = (int *)&t, *px = (int *)&x;
 115         int i, j, hx, ix;
 116 
 117         t = 1.0;
 118 #if defined(__i386) || defined(__amd64)
 119         XTOI(px, hx);
 120 #else
 121         hx = px[0];
 122 #endif
 123         ix = hx & 0x7fffffff;
 124 
 125         if (ix < 0x3ffc4000) {
 126                 if (ix < 0x3fc60000)
 127                         if ((i = (int)x) == 0)
 128                                 return (one);
 129 
 130                 /* generate inexact */
 131                 z = x * x;
 132 
 133                 if (ix < 0x3ff80000) /* 0.0078125 */
 134                         return (one + z * (qq1 + z * (qq2 + z * (qq3 + z *
 135                             (qq4 + z * qq5)))));
 136                 else
 137                         return (one + z * (q1 + z * (q2 + z * (q3 + z * (q4 +
 138                             z * (q5 + z * (q6 + z * (q7 + z * q8))))))));
 139         }
 140 
 141         j = (ix + 0x400) & 0x7ffff800;
 142         i = (j - 0x3ffc4000) >> 11;
 143 #if defined(__i386) || defined(__amd64)
 144         ITOX(j, pt);
 145 #else
 146         pt[0] = j;
 147 #endif
 148 
 149         if (hx > 0)
 150                 x = y - (t - x);
 151         else
 152                 x = (-y) - (t + x);
 153 
 154         a = _TBL_cosl_hi[i];
 155         z = x * x;
 156         t = z * (qq1 + z * (qq2 + z * (qq3 + z * (qq4 + z * qq5))));
 157         w = x * (one + z * (pp1 + z * (pp2 + z * (pp3 + z * (pp4 + z * pp5)))));
 158         t = _TBL_cosl_lo[i] - (_TBL_sinl_hi[i] * w - a * t);
 159         return (a + t);
 160 }