Print this page
11210 libm should be cstyle(1ONBLD) clean
   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  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
  23  */

  24 /*
  25  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
  26  * Use is subject to license terms.
  27  */
  28 
  29 #pragma weak __cos = cos
  30 
  31 /* INDENT OFF */
  32 /*
  33  * cos(x)
  34  * Accurate Table look-up algorithm by K.C. Ng, May, 1995.
  35  *
  36  * Algorithm: see sincos.c
  37  */
  38 
  39 #include "libm.h"
  40 
  41 static const double sc[] = {
  42 /* ONE  = */  1.0,

  43 /* NONE = */ -1.0,

  44 /*
  45  * |sin(x) - (x+pp1*x^3+pp2*x^5)| <= 2^-58.79 for |x| < 0.008
  46  */
  47 /* PP1  = */ -0.166666666666316558867252052378889521480627858683055567,
  48 /* PP2  = */   .008333315652997472323564894248466758248475374977974017927,

  49 /*
  50  * |(sin(x) - (x+p1*x^3+...+p4*x^9)|
  51  * |------------------------------ | <= 2^-57.63 for |x| < 0.1953125
  52  * |                 x             |
  53  */
  54 /* P1   = */ -1.666666666666629669805215138920301589656e-0001,
  55 /* P2   = */  8.333333332390951295683993455280336376663e-0003,
  56 /* P3   = */ -1.984126237997976692791551778230098403960e-0004,
  57 /* P4   = */  2.753403624854277237649987622848330351110e-0006,

  58 /*
  59  * |cos(x) - (1+qq1*x^2+qq2*x^4)| <= 2^-55.99 for |x| <= 0.008 (0x3f80624d)
  60  */
  61 /* QQ1  = */ -0.4999999999975492381842911981948418542742729,
  62 /* QQ2  = */  0.041666542904352059294545209158357640398771740,
  63 /* Q1   = */ -0.5,
  64 /* Q2   = */  4.166666666500350703680945520860748617445e-0002,
  65 /* Q3   = */ -1.388888596436972210694266290577848696006e-0003,
  66 /* Q4   = */  2.478563078858589473679519517892953492192e-0005,
  67 /* PIO2_H    = */  1.570796326794896557999,
  68 /* PIO2_L    = */  6.123233995736765886130e-17,
  69 /* PIO2_L0   = */  6.123233995727922165564e-17,
  70 /* PIO2_L1   = */  8.843720566135701120255e-29,
  71 /* PI3O2_H   = */  4.712388980384689673997,
  72 /* PI3O2_L   = */  1.836970198721029765839e-16,
  73 /* PI3O2_L0  = */  1.836970198720396133587e-16,
  74 /* PI3O2_L1  = */  6.336322524749201142226e-29,
  75 /* PI5O2_H   = */  7.853981633974482789995,
  76 /* PI5O2_L   = */  3.061616997868382943065e-16,
  77 /* PI5O2_L0  = */  3.061616997861941598865e-16,
  78 /* PI5O2_L1  = */  6.441344200433640781982e-28,
  79 };
  80 /* INDENT ON */
  81 
  82 #define ONE             sc[0]
  83 #define PP1             sc[2]
  84 #define PP2             sc[3]
  85 #define P1              sc[4]
  86 #define P2              sc[5]
  87 #define P3              sc[6]
  88 #define P4              sc[7]
  89 #define QQ1             sc[8]
  90 #define QQ2             sc[9]
  91 #define Q1              sc[10]
  92 #define Q2              sc[11]
  93 #define Q3              sc[12]
  94 #define Q4              sc[13]
  95 #define PIO2_H          sc[14]
  96 #define PIO2_L          sc[15]
  97 #define PIO2_L0         sc[16]
  98 #define PIO2_L1         sc[17]
  99 #define PI3O2_H         sc[18]
 100 #define PI3O2_L         sc[19]
 101 #define PI3O2_L0        sc[20]
 102 #define PI3O2_L1        sc[21]
 103 #define PI5O2_H         sc[22]
 104 #define PI5O2_L         sc[23]
 105 #define PI5O2_L0        sc[24]
 106 #define PI5O2_L1        sc[25]
 107 
 108 extern const double _TBL_sincos[], _TBL_sincosx[];
 109 
 110 double
 111 cos(double x) {

 112         double  z, y[2], w, s, v, p, q;
 113         int     i, j, n, hx, ix, lx;
 114 
 115         hx = ((int *)&x)[HIWORD];
 116         lx = ((int *)&x)[LOWORD];
 117         ix = hx & ~0x80000000;
 118 
 119         if (ix <= 0x3fc50000) {      /* |x| < 10.5/64 = 0.164062500 */
 120                 if (ix < 0x3e400000) {       /* |x| < 2**-27 */
 121                         if ((int)x == 0)
 122                                 return (ONE);
 123                 }

 124                 z = x * x;

 125                 if (ix < 0x3f800000) /* |x| < 0.008 */
 126                         w = z * (QQ1 + z * QQ2);
 127                 else
 128                         w = z * ((Q1 + z * Q2) + (z * z) * (Q3 + z * Q4));

 129                 return (ONE + w);
 130         }
 131 
 132         /* for 0.164062500 < x < M, */
 133         n = ix >> 20;

 134         if (n < 0x402) {     /* x < 8 */
 135                 i = (((ix >> 12) & 0xff) | 0x100) >> (0x401 - n);
 136                 j = i - 10;
 137                 x = fabs(x);
 138                 v = x - _TBL_sincosx[j];

 139                 if (((j - 81) ^ (j - 101)) < 0) {
 140                         /* near pi/2, cos(pi/2-x)=sin(x) */
 141                         p = PIO2_H - x;
 142                         i = ix - 0x3ff921fb;
 143                         x = p + PIO2_L;

 144                         if ((i | ((lx - 0x54442D00) & 0xffffff00)) == 0) {
 145                                 /* very close to pi/2 */
 146                                 x = p + PIO2_L0;
 147                                 return (x + PIO2_L1);
 148                         }

 149                         z = x * x;

 150                         if (((ix - 0x3ff92000) >> 12) == 0) {
 151                                 /* |pi/2-x|<2**-8 */
 152                                 w = PIO2_L + (z * x) * (PP1 + z * PP2);
 153                         } else {
 154                                 w = PIO2_L + (z * x) * ((P1 + z * P2) +
 155                                     (z * z) * (P3 + z * P4));
 156                         }

 157                         return (p + w);
 158                 }

 159                 s = v * v;

 160                 if (((j - 282) ^ (j - 302)) < 0) {
 161                         /* near 3/2pi, cos(x-3/2pi)=sin(x) */
 162                         p = x - PI3O2_H;
 163                         i = ix - 0x4012D97C;
 164                         x = p - PI3O2_L;

 165                         if ((i | ((lx - 0x7f332100) & 0xffffff00)) == 0) {
 166                                 /* very close to 3/2pi */
 167                                 x = p - PI3O2_L0;
 168                                 return (x - PI3O2_L1);
 169                         }

 170                         z = x * x;

 171                         if (((ix - 0x4012D800) >> 9) == 0) {
 172                                 /* |x-3/2pi|<2**-8 */
 173                                 w = (z * x) * (PP1 + z * PP2) - PI3O2_L;
 174                         } else {
 175                                 w = (z * x) * ((P1 + z * P2) + (z * z)
 176                                     * (P3 + z * P4)) - PI3O2_L;
 177                         }

 178                         return (p + w);
 179                 }

 180                 if (((j - 483) ^ (j - 503)) < 0) {
 181                         /* near 5pi/2, cos(5pi/2-x)=sin(x) */
 182                         p = PI5O2_H - x;
 183                         i = ix - 0x401F6A7A;
 184                         x = p + PI5O2_L;

 185                         if ((i | ((lx - 0x29553800) & 0xffffff00)) == 0) {
 186                                 /* very close to pi/2 */
 187                                 x = p + PI5O2_L0;
 188                                 return (x + PI5O2_L1);
 189                         }

 190                         z = x * x;

 191                         if (((ix - 0x401F6A7A) >> 7) == 0) {
 192                                 /* |pi/2-x|<2**-8 */
 193                                 w = PI5O2_L + (z * x) * (PP1 + z * PP2);
 194                         } else {
 195                                 w = PI5O2_L + (z * x) * ((P1 + z * P2) +
 196                                     (z * z) * (P3 + z * P4));
 197                         }

 198                         return (p + w);
 199                 }

 200                 j <<= 1;
 201                 w = _TBL_sincos[j];
 202                 z = _TBL_sincos[j+1];
 203                 p = v + (v * s) * (PP1 + s * PP2);
 204                 q = s * (QQ1 + s * QQ2);
 205                 return (z - (w * p - z * q));
 206         }
 207 
 208         if (ix >= 0x7ff00000)        /* cos(Inf or NaN) is NaN */
 209                 return (x / x);
 210 
 211         /* argument reduction needed */
 212         n = __rem_pio2(x, y);

 213         switch (n & 3) {
 214         case 0:
 215                 return (__k_cos(y[0], y[1]));
 216         case 1:
 217                 return (-__k_sin(y[0], y[1]));
 218         case 2:
 219                 return (-__k_cos(y[0], y[1]));
 220         default:
 221                 return (__k_sin(y[0], y[1]));
 222         }
 223 }
   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 __cos = cos
  32 
  33 
  34 /*
  35  * cos(x)
  36  * Accurate Table look-up algorithm by K.C. Ng, May, 1995.
  37  *
  38  * Algorithm: see sincos.c
  39  */
  40 
  41 #include "libm.h"
  42 
  43 static const double sc[] = {
  44 /* ONE  = */
  45         1.0,
  46 /* NONE = */ -1.0,
  47 
  48 /*
  49  * |sin(x) - (x+pp1*x^3+pp2*x^5)| <= 2^-58.79 for |x| < 0.008
  50  */
  51 /* PP1  = */-0.166666666666316558867252052378889521480627858683055567,
  52 /* PP2  = */.008333315652997472323564894248466758248475374977974017927,
  53 
  54 /*
  55  * |(sin(x) - (x+p1*x^3+...+p4*x^9)|
  56  * |------------------------------ | <= 2^-57.63 for |x| < 0.1953125
  57  * |                 x             |
  58  */
  59 /* P1   = */ -1.666666666666629669805215138920301589656e-0001,
  60 /* P2   = */ 8.333333332390951295683993455280336376663e-0003,
  61 /* P3   = */ -1.984126237997976692791551778230098403960e-0004,
  62 /* P4   = */ 2.753403624854277237649987622848330351110e-0006,
  63 
  64 /*
  65  * |cos(x) - (1+qq1*x^2+qq2*x^4)| <= 2^-55.99 for |x| <= 0.008 (0x3f80624d)
  66  */
  67 /* QQ1  = */-0.4999999999975492381842911981948418542742729,
  68 /* QQ2  = */0.041666542904352059294545209158357640398771740,
  69 /* Q1   = */ -0.5,
  70 /* Q2   = */ 4.166666666500350703680945520860748617445e-0002,
  71 /* Q3   = */ -1.388888596436972210694266290577848696006e-0003,
  72 /* Q4   = */ 2.478563078858589473679519517892953492192e-0005,
  73 /* PIO2_H    = */ 1.570796326794896557999,
  74 /* PIO2_L    = */ 6.123233995736765886130e-17,
  75 /* PIO2_L0   = */ 6.123233995727922165564e-17,
  76 /* PIO2_L1   = */ 8.843720566135701120255e-29,
  77 /* PI3O2_H   = */ 4.712388980384689673997,
  78 /* PI3O2_L   = */ 1.836970198721029765839e-16,
  79 /* PI3O2_L0  = */ 1.836970198720396133587e-16,
  80 /* PI3O2_L1  = */ 6.336322524749201142226e-29,
  81 /* PI5O2_H   = */ 7.853981633974482789995,
  82 /* PI5O2_L   = */ 3.061616997868382943065e-16,
  83 /* PI5O2_L0  = */ 3.061616997861941598865e-16,
  84 /* PI5O2_L1  = */ 6.441344200433640781982e-28,
  85 };
  86 
  87 
  88 #define ONE             sc[0]
  89 #define PP1             sc[2]
  90 #define PP2             sc[3]
  91 #define P1              sc[4]
  92 #define P2              sc[5]
  93 #define P3              sc[6]
  94 #define P4              sc[7]
  95 #define QQ1             sc[8]
  96 #define QQ2             sc[9]
  97 #define Q1              sc[10]
  98 #define Q2              sc[11]
  99 #define Q3              sc[12]
 100 #define Q4              sc[13]
 101 #define PIO2_H          sc[14]
 102 #define PIO2_L          sc[15]
 103 #define PIO2_L0         sc[16]
 104 #define PIO2_L1         sc[17]
 105 #define PI3O2_H         sc[18]
 106 #define PI3O2_L         sc[19]
 107 #define PI3O2_L0        sc[20]
 108 #define PI3O2_L1        sc[21]
 109 #define PI5O2_H         sc[22]
 110 #define PI5O2_L         sc[23]
 111 #define PI5O2_L0        sc[24]
 112 #define PI5O2_L1        sc[25]
 113 
 114 extern const double _TBL_sincos[], _TBL_sincosx[];
 115 
 116 double
 117 cos(double x)
 118 {
 119         double z, y[2], w, s, v, p, q;
 120         int i, j, n, hx, ix, lx;
 121 
 122         hx = ((int *)&x)[HIWORD];
 123         lx = ((int *)&x)[LOWORD];
 124         ix = hx & ~0x80000000;
 125 
 126         if (ix <= 0x3fc50000) {              /* |x| < 10.5/64 = 0.164062500 */
 127                 if (ix < 0x3e400000) {       /* |x| < 2**-27 */
 128                         if ((int)x == 0)
 129                                 return (ONE);
 130                 }
 131 
 132                 z = x * x;
 133 
 134                 if (ix < 0x3f800000) /* |x| < 0.008 */
 135                         w = z * (QQ1 + z * QQ2);
 136                 else
 137                         w = z * ((Q1 + z * Q2) + (z * z) * (Q3 + z * Q4));
 138 
 139                 return (ONE + w);
 140         }
 141 
 142         /* for 0.164062500 < x < M, */
 143         n = ix >> 20;
 144 
 145         if (n < 0x402) {             /* x < 8 */
 146                 i = (((ix >> 12) & 0xff) | 0x100) >> (0x401 - n);
 147                 j = i - 10;
 148                 x = fabs(x);
 149                 v = x - _TBL_sincosx[j];
 150 
 151                 if (((j - 81) ^ (j - 101)) < 0) {
 152                         /* near pi/2, cos(pi/2-x)=sin(x) */
 153                         p = PIO2_H - x;
 154                         i = ix - 0x3ff921fb;
 155                         x = p + PIO2_L;
 156 
 157                         if ((i | ((lx - 0x54442D00) & 0xffffff00)) == 0) {
 158                                 /* very close to pi/2 */
 159                                 x = p + PIO2_L0;
 160                                 return (x + PIO2_L1);
 161                         }
 162 
 163                         z = x * x;
 164 
 165                         if (((ix - 0x3ff92000) >> 12) == 0) {
 166                                 /* |pi/2-x|<2**-8 */
 167                                 w = PIO2_L + (z * x) * (PP1 + z * PP2);
 168                         } else {
 169                                 w = PIO2_L + (z * x) * ((P1 + z * P2) + (z *
 170                                     z) * (P3 + z * P4));
 171                         }
 172 
 173                         return (p + w);
 174                 }
 175 
 176                 s = v * v;
 177 
 178                 if (((j - 282) ^ (j - 302)) < 0) {
 179                         /* near 3/2pi, cos(x-3/2pi)=sin(x) */
 180                         p = x - PI3O2_H;
 181                         i = ix - 0x4012D97C;
 182                         x = p - PI3O2_L;
 183 
 184                         if ((i | ((lx - 0x7f332100) & 0xffffff00)) == 0) {
 185                                 /* very close to 3/2pi */
 186                                 x = p - PI3O2_L0;
 187                                 return (x - PI3O2_L1);
 188                         }
 189 
 190                         z = x * x;
 191 
 192                         if (((ix - 0x4012D800) >> 9) == 0) {
 193                                 /* |x-3/2pi|<2**-8 */
 194                                 w = (z * x) * (PP1 + z * PP2) - PI3O2_L;
 195                         } else {
 196                                 w = (z * x) * ((P1 + z * P2) + (z * z) * (P3 +
 197                                     z * P4)) - PI3O2_L;
 198                         }
 199 
 200                         return (p + w);
 201                 }
 202 
 203                 if (((j - 483) ^ (j - 503)) < 0) {
 204                         /* near 5pi/2, cos(5pi/2-x)=sin(x) */
 205                         p = PI5O2_H - x;
 206                         i = ix - 0x401F6A7A;
 207                         x = p + PI5O2_L;
 208 
 209                         if ((i | ((lx - 0x29553800) & 0xffffff00)) == 0) {
 210                                 /* very close to pi/2 */
 211                                 x = p + PI5O2_L0;
 212                                 return (x + PI5O2_L1);
 213                         }
 214 
 215                         z = x * x;
 216 
 217                         if (((ix - 0x401F6A7A) >> 7) == 0) {
 218                                 /* |pi/2-x|<2**-8 */
 219                                 w = PI5O2_L + (z * x) * (PP1 + z * PP2);
 220                         } else {
 221                                 w = PI5O2_L + (z * x) * ((P1 + z * P2) + (z *
 222                                     z) * (P3 + z * P4));
 223                         }
 224 
 225                         return (p + w);
 226                 }
 227 
 228                 j <<= 1;
 229                 w = _TBL_sincos[j];
 230                 z = _TBL_sincos[j + 1];
 231                 p = v + (v * s) * (PP1 + s * PP2);
 232                 q = s * (QQ1 + s * QQ2);
 233                 return (z - (w * p - z * q));
 234         }
 235 
 236         if (ix >= 0x7ff00000)                /* cos(Inf or NaN) is NaN */
 237                 return (x / x);
 238 
 239         /* argument reduction needed */
 240         n = __rem_pio2(x, y);
 241 
 242         switch (n & 3) {
 243         case 0:
 244                 return (__k_cos(y[0], y[1]));
 245         case 1:
 246                 return (-__k_sin(y[0], y[1]));
 247         case 2:
 248                 return (-__k_cos(y[0], y[1]));
 249         default:
 250                 return (__k_sin(y[0], y[1]));
 251         }
 252 }