Print this page
11210 libm should be cstyle(1ONBLD) clean


   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  * void sincospil(long double x, long double *s, long double *c)
  32  * *s = sinl(pi*x); *c = cosl(pi*x);
  33  *
  34  * Algorithm, 10/17/2002, K.C. Ng
  35  * ------------------------------
  36  * Let y = |4x|, z = floor(y), and n = (int)(z mod 8.0) (displayed in binary).
  37  *      1. If y == z, then x is a multiple of pi/4. Return the following values:
  38  *             ---------------------------------------------------
  39  *               n  x mod 2    sin(x*pi)    cos(x*pi)   tan(x*pi)
  40  *             ---------------------------------------------------
  41  *              000  0.00       +0 ___       +1 ___      +0
  42  *              001  0.25       +\/0.5       +\/0.5      +1
  43  *              010  0.50       +1 ___       +0 ___      +inf
  44  *              011  0.75       +\/0.5       -\/0.5      -1


  54  *              000  (y-z)/4     sinpi(t)     cospi(t)    tanpi(t)
  55  *              001  (z+1-y)/4   cospi(t)     sinpi(t)    1/tanpi(t)
  56  *              010  (y-z)/4     cospi(t)    -sinpi(t)   -1/tanpi(t)
  57  *              011  (z+1-y)/4   sinpi(t)    -cospi(t)   -tanpi(t)
  58  *              100  (y-z)/4    -sinpi(t)    -cospi(t)    tanpi(t)
  59  *              101  (z+1-y)/4  -cospi(t)    -sinpi(t)    1/tanpi(t)
  60  *              110  (y-z)/4    -cospi(t)     sinpi(t)   -1/tanpi(t)
  61  *              111  (z+1-y)/4  -sinpi(t)     cospi(t)   -tanpi(t)
  62  *             ---------------------------------------------------
  63  *
  64  * NOTE. This program compute sinpi/cospi(t<0.25) by __k_sin/cos(pi*t, 0.0).
  65  * This will return a result with error slightly more than one ulp (but less
  66  * than 2 ulp). If one wants accurate result,  one may break up pi*t in
  67  * high (tpi_h) and low (tpi_l) parts and call __k_sin/cos(tip_h, tip_lo)
  68  * instead.
  69  */
  70 
  71 #include "libm.h"
  72 #include "longdouble.h"
  73 
  74 #define I(q, m) ((int *) &(q))[m]
  75 #define U(q, m) ((unsigned *) &(q))[m]
  76 #if defined(__LITTLE_ENDIAN) || defined(__x86)
  77 #define LDBL_MOST_SIGNIF_I(ld)  ((I(ld, 2) << 16) | (0xffff & (I(ld, 1) >> 15)))

  78 #define LDBL_LEAST_SIGNIF_U(ld) U(ld, 0)
  79 #define PREC    64
  80 #define PRECM1  63
  81 #define PRECM2  62

  82 static const long double twoPRECM2 = 9.223372036854775808000000000000000e+18L;
  83 #else
  84 #define LDBL_MOST_SIGNIF_I(ld)  I(ld, 0)
  85 #define LDBL_LEAST_SIGNIF_U(ld) U(ld, sizeof (long double) / sizeof (int) - 1)

  86 #define PREC    113
  87 #define PRECM1  112
  88 #define PRECM2  111

  89 static const long double twoPRECM2 = 5.192296858534827628530496329220096e+33L;
  90 #endif
  91 
  92 static const long double
  93 zero    = 0.0L,
  94 quater  = 0.25L,
  95 one     = 1.0L,
  96 pi      = 3.141592653589793238462643383279502884197e+0000L,
  97 sqrth   = 0.707106781186547524400844362104849039284835937688474,
  98 tiny    = 1.0e-100;
  99 
 100 void
 101 sincospil(long double x, long double *s, long double *c) {

 102         long double y, z, t;
 103         int hx, n, k;
 104         unsigned lx;
 105 
 106         hx = LDBL_MOST_SIGNIF_I(x);
 107         lx = LDBL_LEAST_SIGNIF_U(x);
 108         k = ((hx & 0x7fff0000) >> 16) - 0x3fff;

 109         if (k >= PRECM2) {           /* |x| >= 2**(Prec-2) */
 110                 if (k >= 16384) {
 111                         *s = *c = x - x;
 112                 } else {
 113                         if (k >= PREC) {
 114                                 *s = zero;
 115                                 *c = one;
 116                         } else if (k == PRECM1) {
 117                                 if ((lx & 1) == 0) {
 118                                         *s = zero;
 119                                         *c = one;
 120                                 } else {
 121                                         *s = -zero;
 122                                         *c = -one;
 123                                 }
 124                         } else {        /* k = Prec - 2 */
 125                                 if ((lx & 1) == 0) {
 126                                         *s = zero;
 127                                         *c = one;
 128                                 } else {
 129                                         *s = one;
 130                                         *c = zero;
 131                                 }

 132                                 if ((lx & 2) != 0) {
 133                                         *s = -*s;
 134                                         *c = -*c;
 135                                 }
 136                         }
 137                 }
 138         } else if (k < -2)   /* |x| < 0.25 */
 139                 *s = __k_sincosl(pi * fabsl(x), zero, c);
 140         else {
 141                 /* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */
 142                 y = 4.0L * fabsl(x);

 143                 if (k < PRECM2) {
 144                         z = y + twoPRECM2;
 145                         n = LDBL_LEAST_SIGNIF_U(z) & 7;     /* 3 LSb of z */
 146                         t = z - twoPRECM2;
 147                         k = 0;
 148                         if (t == y)

 149                                 k = 1;
 150                         else if (t > y) {
 151                                 n -= 1;
 152                                 t = quater + (y - t) * quater;
 153                         } else
 154                                 t = (y - t) * quater;

 155                 } else {        /* k = Prec-3 */
 156                         n = LDBL_LEAST_SIGNIF_U(y) & 7;     /* 3 LSb of z */
 157                         k = 1;
 158                 }

 159                 if (k) {        /* x = N/4 */
 160                         if ((n & 1) != 0)
 161                                 *s = *c = sqrth + tiny;
 162                         else
 163                                 if ((n & 2) == 0) {
 164                                         *s = zero;
 165                                         *c = one;
 166                                 } else {
 167                                         *s = one;
 168                                         *c = zero;
 169                                 }

 170                         if ((n & 4) != 0)
 171                                 *s = -*s;

 172                         if (((n + 1) & 4) != 0)
 173                                 *c = -*c;
 174                 } else {
 175                         if ((n & 1) != 0)
 176                                 t = quater - t;

 177                         if (((n + (n & 1)) & 2) == 0)
 178                                 *s = __k_sincosl(pi * t, zero, c);
 179                         else
 180                                 *c = __k_sincosl(pi * t, zero, s);

 181                         if ((n & 4) != 0)
 182                                 *s = -*s;

 183                         if (((n + 2) & 4) != 0)
 184                                 *c = -*c;
 185                 }
 186         }

 187         if (hx < 0)
 188                 *s = -*s;
 189 }

 190 #undef U
 191 #undef LDBL_LEAST_SIGNIF_U
 192 #undef I
 193 #undef LDBL_MOST_SIGNIF_I


   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  * void sincospil(long double x, long double *s, long double *c)
  33  * *s = sinl(pi*x); *c = cosl(pi*x);
  34  *
  35  * Algorithm, 10/17/2002, K.C. Ng
  36  * ------------------------------
  37  * Let y = |4x|, z = floor(y), and n = (int)(z mod 8.0) (displayed in binary).
  38  *      1. If y == z, then x is a multiple of pi/4. Return the following values:
  39  *             ---------------------------------------------------
  40  *               n  x mod 2    sin(x*pi)    cos(x*pi)   tan(x*pi)
  41  *             ---------------------------------------------------
  42  *              000  0.00       +0 ___       +1 ___      +0
  43  *              001  0.25       +\/0.5       +\/0.5      +1
  44  *              010  0.50       +1 ___       +0 ___      +inf
  45  *              011  0.75       +\/0.5       -\/0.5      -1


  55  *              000  (y-z)/4     sinpi(t)     cospi(t)    tanpi(t)
  56  *              001  (z+1-y)/4   cospi(t)     sinpi(t)    1/tanpi(t)
  57  *              010  (y-z)/4     cospi(t)    -sinpi(t)   -1/tanpi(t)
  58  *              011  (z+1-y)/4   sinpi(t)    -cospi(t)   -tanpi(t)
  59  *              100  (y-z)/4    -sinpi(t)    -cospi(t)    tanpi(t)
  60  *              101  (z+1-y)/4  -cospi(t)    -sinpi(t)    1/tanpi(t)
  61  *              110  (y-z)/4    -cospi(t)     sinpi(t)   -1/tanpi(t)
  62  *              111  (z+1-y)/4  -sinpi(t)     cospi(t)   -tanpi(t)
  63  *             ---------------------------------------------------
  64  *
  65  * NOTE. This program compute sinpi/cospi(t<0.25) by __k_sin/cos(pi*t, 0.0).
  66  * This will return a result with error slightly more than one ulp (but less
  67  * than 2 ulp). If one wants accurate result,  one may break up pi*t in
  68  * high (tpi_h) and low (tpi_l) parts and call __k_sin/cos(tip_h, tip_lo)
  69  * instead.
  70  */
  71 
  72 #include "libm.h"
  73 #include "longdouble.h"
  74 
  75 #define I(q, m)                         ((int *)&(q))[m]
  76 #define U(q, m)                         ((unsigned *)&(q))[m]
  77 #if defined(__LITTLE_ENDIAN) || defined(__x86)
  78 #define LDBL_MOST_SIGNIF_I(ld)          ((I(ld, 2) << 16) | (0xffff & (I(ld, \
  79         1) >> 15)))
  80 #define LDBL_LEAST_SIGNIF_U(ld)         U(ld, 0)
  81 #define PREC                            64
  82 #define PRECM1                          63
  83 #define PRECM2                          62
  84 
  85 static const long double twoPRECM2 = 9.223372036854775808000000000000000e+18L;
  86 #else
  87 #define LDBL_MOST_SIGNIF_I(ld)          I(ld, 0)
  88 #define LDBL_LEAST_SIGNIF_U(ld)         U(ld, sizeof (long double) / \
  89         sizeof (int) - 1)
  90 #define PREC                            113
  91 #define PRECM1                          112
  92 #define PRECM2                          111
  93 
  94 static const long double twoPRECM2 = 5.192296858534827628530496329220096e+33L;
  95 #endif
  96 
  97 static const long double zero = 0.0L,
  98         quater = 0.25L,
  99         one = 1.0L,
 100         pi = 3.141592653589793238462643383279502884197e+0000L,
 101         sqrth = 0.707106781186547524400844362104849039284835937688474,
 102         tiny = 1.0e-100;

 103 
 104 void
 105 sincospil(long double x, long double *s, long double *c)
 106 {
 107         long double y, z, t;
 108         int hx, n, k;
 109         unsigned lx;
 110 
 111         hx = LDBL_MOST_SIGNIF_I(x);
 112         lx = LDBL_LEAST_SIGNIF_U(x);
 113         k = ((hx & 0x7fff0000) >> 16) - 0x3fff;
 114 
 115         if (k >= PRECM2) {           /* |x| >= 2**(Prec-2) */
 116                 if (k >= 16384) {
 117                         *s = *c = x - x;
 118                 } else {
 119                         if (k >= PREC) {
 120                                 *s = zero;
 121                                 *c = one;
 122                         } else if (k == PRECM1) {
 123                                 if ((lx & 1) == 0) {
 124                                         *s = zero;
 125                                         *c = one;
 126                                 } else {
 127                                         *s = -zero;
 128                                         *c = -one;
 129                                 }
 130                         } else {        /* k = Prec - 2 */
 131                                 if ((lx & 1) == 0) {
 132                                         *s = zero;
 133                                         *c = one;
 134                                 } else {
 135                                         *s = one;
 136                                         *c = zero;
 137                                 }
 138 
 139                                 if ((lx & 2) != 0) {
 140                                         *s = -*s;
 141                                         *c = -*c;
 142                                 }
 143                         }
 144                 }
 145         } else if (k < -2) {         /* |x| < 0.25 */
 146                 *s = __k_sincosl(pi * fabsl(x), zero, c);
 147         } else {
 148                 /* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */
 149                 y = 4.0L * fabsl(x);
 150 
 151                 if (k < PRECM2) {
 152                         z = y + twoPRECM2;
 153                         n = LDBL_LEAST_SIGNIF_U(z) & 7;     /* 3 LSb of z */
 154                         t = z - twoPRECM2;
 155                         k = 0;
 156 
 157                         if (t == y) {
 158                                 k = 1;
 159                         } else if (t > y) {
 160                                 n -= 1;
 161                                 t = quater + (y - t) * quater;
 162                         } else {
 163                                 t = (y - t) * quater;
 164                         }
 165                 } else {                                /* k = Prec-3 */
 166                         n = LDBL_LEAST_SIGNIF_U(y) & 7;     /* 3 LSb of z */
 167                         k = 1;
 168                 }
 169 
 170                 if (k) {                /* x = N/4 */
 171                         if ((n & 1) != 0) {
 172                                 *s = *c = sqrth + tiny;
 173                         } else if ((n & 2) == 0) {

 174                                 *s = zero;
 175                                 *c = one;
 176                         } else {
 177                                 *s = one;
 178                                 *c = zero;
 179                         }
 180 
 181                         if ((n & 4) != 0)
 182                                 *s = -*s;
 183 
 184                         if (((n + 1) & 4) != 0)
 185                                 *c = -*c;
 186                 } else {
 187                         if ((n & 1) != 0)
 188                                 t = quater - t;
 189 
 190                         if (((n + (n & 1)) & 2) == 0)
 191                                 *s = __k_sincosl(pi * t, zero, c);
 192                         else
 193                                 *c = __k_sincosl(pi * t, zero, s);
 194 
 195                         if ((n & 4) != 0)
 196                                 *s = -*s;
 197 
 198                         if (((n + 2) & 4) != 0)
 199                                 *c = -*c;
 200                 }
 201         }
 202 
 203         if (hx < 0)
 204                 *s = -*s;
 205 }
 206 
 207 #undef U
 208 #undef LDBL_LEAST_SIGNIF_U
 209 #undef I
 210 #undef LDBL_MOST_SIGNIF_I