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 /* long double sinpil(long double x),


  31  * return long double precision sinl(pi*x).
  32  *
  33  * Algorithm, 10/17/2002, K.C. Ng
  34  * ------------------------------
  35  * Let y = |4x|, z = floor(y), and n = (int)(z mod 8.0) (displayed in binary).
  36  *      1. If y == z, then x is a multiple of pi/4. Return the following values:
  37  *             ---------------------------------------------------
  38  *               n  x mod 2    sin(x*pi)    cos(x*pi)   tan(x*pi)
  39  *             ---------------------------------------------------
  40  *              000  0.00       +0 ___       +1 ___      +0
  41  *              001  0.25       +\/0.5       +\/0.5      +1
  42  *              010  0.50       +1 ___       +0 ___      +inf
  43  *              011  0.75       +\/0.5       -\/0.5      -1
  44  *              100  1.00       -0 ___       -1 ___      +0
  45  *              101  1.25       -\/0.5       -\/0.5      +1
  46  *              110  1.50       -1 ___       -0 ___      +inf
  47  *              111  1.75       -\/0.5       +\/0.5      -1
  48  *             ---------------------------------------------------
  49  *      2. Otherwise,
  50  *             ---------------------------------------------------
  51  *               n     t        sin(x*pi)    cos(x*pi)   tan(x*pi)
  52  *             ---------------------------------------------------
  53  *              000  (y-z)/4     sinpi(t)     cospi(t)    tanpi(t)
  54  *              001  (z+1-y)/4   cospi(t)     sinpi(t)    1/tanpi(t)
  55  *              010  (y-z)/4     cospi(t)    -sinpi(t)   -1/tanpi(t)
  56  *              011  (z+1-y)/4   sinpi(t)    -cospi(t)   -tanpi(t)
  57  *              100  (y-z)/4    -sinpi(t)    -cospi(t)    tanpi(t)
  58  *              101  (z+1-y)/4  -cospi(t)    -sinpi(t)    1/tanpi(t)
  59  *              110  (y-z)/4    -cospi(t)     sinpi(t)   -1/tanpi(t)
  60  *              111  (z+1-y)/4  -sinpi(t)     cospi(t)   -tanpi(t)
  61  *             ---------------------------------------------------
  62  *
  63  * NOTE. This program compute sinpi/cospi(t<0.25) by __k_sin/cos(pi*t, 0.0).
  64  * This will return a result with error slightly more than one ulp (but less
  65  * than 2 ulp). If one wants accurate result,  one may break up pi*t in
  66  * high (tpi_h) and low (tpi_l) parts and call __k_sin/cos(tip_h, tip_lo)
  67  * instead.
  68  */

  69 
  70 #include "libm.h"
  71 #include "longdouble.h"
  72 
  73 #include <sys/isa_defs.h>
  74 
  75 #define I(q, m) ((int *) &(q))[m]
  76 #define U(q, m) ((unsigned *) &(q))[m]
  77 #if defined(__i386) || defined(__amd64)
  78 #define LDBL_MOST_SIGNIF_I(ld)  ((I(ld, 2) << 16) | (0xffff & (I(ld, 1) >> 15)))

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

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

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

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

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

 110         if (k >= PRECM2) {           /* |x| >= 2**(Prec-2) */
 111                 if (k >= 16384)
 112                         y = x - x;
 113                 else {
 114                         if (k >= PREC)
 115                                 y = zero;
 116                         else if (k == PRECM1)
 117                                 y = (lx & 1) == 0 ? zero: -zero;
 118                         else {  /* k = Prec - 2 */
 119                                 y = (lx & 1) == 0 ? zero : one;

 120                                 if ((lx & 2) != 0)
 121                                         y = -y;
 122                         }
 123                 }
 124         }
 125         else if (k < -2)     /* |x| < 0.25 */
 126                 y = __k_sinl(pi * fabsl(x), zero);
 127         else {
 128                 /* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */
 129                 y = 4.0L * fabsl(x);

 130                 if (k < PRECM2) {
 131                         z = y + twoPRECM2;
 132                         n = LDBL_LEAST_SIGNIF_U(z) & 7;     /* 3 LSb of z */
 133                         t = z - twoPRECM2;
 134                         k = 0;
 135                         if (t == y)

 136                                 k = 1;
 137                         else if (t > y) {
 138                                 n -= 1;
 139                                 t = quater + (y - t) * quater;
 140                         }
 141                         else
 142                                 t = (y - t) * quater;
 143                 }
 144                 else {  /* k = Prec-3 */
 145                         n = LDBL_LEAST_SIGNIF_U(y) & 7;     /* 3 LSb of z */
 146                         k = 1;
 147                 }

 148                 if (k) {        /* x = N/4 */
 149                         if ((n & 1) != 0)
 150                                 y = sqrth + tiny;
 151                         else
 152                                 y = (n & 2) == 0 ? zero : one;

 153                         if ((n & 4) != 0)
 154                                 y = -y;
 155                 }
 156                 else {
 157                         if ((n & 1) != 0)
 158                                 t = quater - t;

 159                         if (((n + (n & 1)) & 2) == 0)
 160                                 y = __k_sinl(pi * t, zero);
 161                         else
 162                                 y = __k_cosl(pi * t, zero);

 163                         if ((n & 4) != 0)
 164                                 y = -y;
 165                 }
 166         }
 167         return hx >= 0 ? y : -y;

 168 }

 169 #undef U
 170 #undef LDBL_LEAST_SIGNIF_U
 171 #undef I
 172 #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 /* BEGIN CSTYLED */
  32 /*
  33  * long double sinpil(long double x),
  34  * return long double precision sinl(pi*x).
  35  *
  36  * Algorithm, 10/17/2002, K.C. Ng
  37  * ------------------------------
  38  * Let y = |4x|, z = floor(y), and n = (int)(z mod 8.0) (displayed in binary).
  39  *      1. If y == z, then x is a multiple of pi/4. Return the following values:
  40  *             ---------------------------------------------------
  41  *               n  x mod 2    sin(x*pi)    cos(x*pi)   tan(x*pi)
  42  *             ---------------------------------------------------
  43  *              000  0.00       +0 ___       +1 ___      +0
  44  *              001  0.25       +\/0.5       +\/0.5      +1
  45  *              010  0.50       +1 ___       +0 ___      +inf
  46  *              011  0.75       +\/0.5       -\/0.5      -1
  47  *              100  1.00       -0 ___       -1 ___      +0
  48  *              101  1.25       -\/0.5       -\/0.5      +1
  49  *              110  1.50       -1 ___       -0 ___      +inf
  50  *              111  1.75       -\/0.5       +\/0.5      -1
  51  *             ---------------------------------------------------
  52  *      2. Otherwise,
  53  *             ---------------------------------------------------
  54  *               n     t        sin(x*pi)    cos(x*pi)   tan(x*pi)
  55  *             ---------------------------------------------------
  56  *              000  (y-z)/4     sinpi(t)     cospi(t)    tanpi(t)
  57  *              001  (z+1-y)/4   cospi(t)     sinpi(t)    1/tanpi(t)
  58  *              010  (y-z)/4     cospi(t)    -sinpi(t)   -1/tanpi(t)
  59  *              011  (z+1-y)/4   sinpi(t)    -cospi(t)   -tanpi(t)
  60  *              100  (y-z)/4    -sinpi(t)    -cospi(t)    tanpi(t)
  61  *              101  (z+1-y)/4  -cospi(t)    -sinpi(t)    1/tanpi(t)
  62  *              110  (y-z)/4    -cospi(t)     sinpi(t)   -1/tanpi(t)
  63  *              111  (z+1-y)/4  -sinpi(t)     cospi(t)   -tanpi(t)
  64  *             ---------------------------------------------------
  65  *
  66  * NOTE. This program compute sinpi/cospi(t<0.25) by __k_sin/cos(pi*t, 0.0).
  67  * This will return a result with error slightly more than one ulp (but less
  68  * than 2 ulp). If one wants accurate result,  one may break up pi*t in
  69  * high (tpi_h) and low (tpi_l) parts and call __k_sin/cos(tip_h, tip_lo)
  70  * instead.
  71  */
  72 /* END CSTYLED */
  73 
  74 #include "libm.h"
  75 #include "longdouble.h"
  76 
  77 #include <sys/isa_defs.h>
  78 
  79 #define I(q, m)                         ((int *)&(q))[m]
  80 #define U(q, m)                         ((unsigned *)&(q))[m]
  81 #if defined(__i386) || defined(__amd64)
  82 #define LDBL_MOST_SIGNIF_I(ld)          ((I(ld, 2) << 16) | (0xffff & (I(ld, \
  83         1) >> 15)))
  84 #define LDBL_LEAST_SIGNIF_U(ld)         U(ld, 0)
  85 #define PREC                            64
  86 #define PRECM1                          63
  87 #define PRECM2                          62
  88 
  89 static const long double twoPRECM2 = 9.223372036854775808000000000000000e+18L;
  90 #else
  91 #define LDBL_MOST_SIGNIF_I(ld)          I(ld, 0)
  92 #define LDBL_LEAST_SIGNIF_U(ld)         U(ld, sizeof (long double) / \
  93         sizeof (int) - 1)
  94 #define PREC                            113
  95 #define PRECM1                          112
  96 #define PRECM2                          111
  97 
  98 static const long double twoPRECM2 = 5.192296858534827628530496329220096e+33L;
  99 #endif
 100 
 101 static const long double zero = 0.0L,
 102         quater = 0.25L,
 103         one = 1.0L,
 104         pi = 3.141592653589793238462643383279502884197e+0000L,
 105         sqrth = 0.707106781186547524400844362104849039284835937688474,
 106         tiny = 1.0e-100;

 107 
 108 long double
 109 sinpil(long double x)
 110 {
 111         long double y, z, t;
 112         int hx, n, k;
 113         unsigned lx;
 114 
 115         hx = LDBL_MOST_SIGNIF_I(x);
 116         lx = LDBL_LEAST_SIGNIF_U(x);
 117         k = ((hx & 0x7fff0000) >> 16) - 0x3fff;
 118 
 119         if (k >= PRECM2) {           /* |x| >= 2**(Prec-2) */
 120                 if (k >= 16384) {
 121                         y = x - x;
 122                 } else {
 123                         if (k >= PREC) {
 124                                 y = zero;
 125                         } else if (k == PRECM1) {
 126                                 y = (lx & 1) == 0 ? zero : -zero;
 127                         } else { /* k = Prec - 2 */
 128                                 y = (lx & 1) == 0 ? zero : one;
 129 
 130                                 if ((lx & 2) != 0)
 131                                         y = -y;
 132                         }
 133                 }
 134         } else if (k < -2) {         /* |x| < 0.25 */

 135                 y = __k_sinl(pi * fabsl(x), zero);
 136         } else {
 137                 /* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */
 138                 y = 4.0L * fabsl(x);
 139 
 140                 if (k < PRECM2) {
 141                         z = y + twoPRECM2;
 142                         n = LDBL_LEAST_SIGNIF_U(z) & 7;     /* 3 LSb of z */
 143                         t = z - twoPRECM2;
 144                         k = 0;
 145 
 146                         if (t == y) {
 147                                 k = 1;
 148                         } else if (t > y) {
 149                                 n -= 1;
 150                                 t = quater + (y - t) * quater;
 151                         } else {

 152                                 t = (y - t) * quater;
 153                         }
 154                 } else { /* k = Prec-3 */
 155                         n = LDBL_LEAST_SIGNIF_U(y) & 7;     /* 3 LSb of z */
 156                         k = 1;
 157                 }
 158 
 159                 if (k) {                /* x = N/4 */
 160                         if ((n & 1) != 0)
 161                                 y = sqrth + tiny;
 162                         else
 163                                 y = (n & 2) == 0 ? zero : one;
 164 
 165                         if ((n & 4) != 0)
 166                                 y = -y;
 167                 } else {

 168                         if ((n & 1) != 0)
 169                                 t = quater - t;
 170 
 171                         if (((n + (n & 1)) & 2) == 0)
 172                                 y = __k_sinl(pi * t, zero);
 173                         else
 174                                 y = __k_cosl(pi * t, zero);
 175 
 176                         if ((n & 4) != 0)
 177                                 y = -y;
 178                 }
 179         }
 180 
 181         return (hx >= 0 ? y : -y);
 182 }
 183 
 184 #undef U
 185 #undef LDBL_LEAST_SIGNIF_U
 186 #undef I
 187 #undef LDBL_MOST_SIGNIF_I