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 * 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 46 * 100 1.00 -0 ___ -1 ___ +0 47 * 101 1.25 -\/0.5 -\/0.5 +1 48 * 110 1.50 -1 ___ -0 ___ +inf 49 * 111 1.75 -\/0.5 +\/0.5 -1 50 * --------------------------------------------------- 51 * 2. Otherwise, 52 * --------------------------------------------------- 53 * n t sin(x*pi) cos(x*pi) tan(x*pi) 54 * --------------------------------------------------- 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 #include <sys/isa_defs.h> 76 77 #define I(q, m) ((int *)&(q))[m] 78 #define U(q, m) ((unsigned *)&(q))[m] 79 #if defined(__i386) || defined(__amd64) 80 #define LDBL_MOST_SIGNIF_I(ld) ((I(ld, 2) << 16) | (0xffff & (I(ld, \ 81 1) >> 15))) 82 #define LDBL_LEAST_SIGNIF_U(ld) U(ld, 0) 83 #define PREC 64 84 #define PRECM1 63 85 #define PRECM2 62 86 87 static const long double twoPRECM2 = 9.223372036854775808000000000000000e+18L; 88 #else 89 #define LDBL_MOST_SIGNIF_I(ld) I(ld, 0) 90 #define LDBL_LEAST_SIGNIF_U(ld) U(ld, sizeof (long double) / \ 91 sizeof (int) - 1) 92 #define PREC 113 93 #define PRECM1 112 94 #define PRECM2 111 95 96 static const long double twoPRECM2 = 5.192296858534827628530496329220096e+33L; 97 #endif 98 99 static const long double zero = 0.0L, 100 quater = 0.25L, 101 one = 1.0L, 102 pi = 3.141592653589793238462643383279502884197e+0000L, 103 sqrth = 0.707106781186547524400844362104849039284835937688474, 104 tiny = 1.0e-100; 105 106 void 107 sincospil(long double x, long double *s, long double *c) 108 { 109 long double y, z, t; 110 int hx, n, k; 111 unsigned lx; 112 113 hx = LDBL_MOST_SIGNIF_I(x); 114 lx = LDBL_LEAST_SIGNIF_U(x); 115 k = ((hx & 0x7fff0000) >> 16) - 0x3fff; 116 117 if (k >= PRECM2) { /* |x| >= 2**(Prec-2) */ 118 if (k >= 16384) { 119 *s = *c = x - x; 120 } else { 121 if (k >= PREC) { 122 *s = zero; 123 *c = one; 124 } else if (k == PRECM1) { 125 if ((lx & 1) == 0) { 126 *s = zero; 127 *c = one; 128 } else { 129 *s = -zero; 130 *c = -one; 131 } 132 } else { /* k = Prec - 2 */ 133 if ((lx & 1) == 0) { 134 *s = zero; 135 *c = one; 136 } else { 137 *s = one; 138 *c = zero; 139 } 140 141 if ((lx & 2) != 0) { 142 *s = -*s; 143 *c = -*c; 144 } 145 } 146 } 147 } else if (k < -2) { /* |x| < 0.25 */ 148 *s = __k_sincosl(pi * fabsl(x), zero, c); 149 } else { 150 /* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */ 151 y = 4.0L * fabsl(x); 152 153 if (k < PRECM2) { 154 z = y + twoPRECM2; 155 n = LDBL_LEAST_SIGNIF_U(z) & 7; /* 3 LSb of z */ 156 t = z - twoPRECM2; 157 k = 0; 158 159 if (t == y) { 160 k = 1; 161 } else if (t > y) { 162 n -= 1; 163 t = quater + (y - t) * quater; 164 } else { 165 t = (y - t) * quater; 166 } 167 } else { /* k = Prec-3 */ 168 n = LDBL_LEAST_SIGNIF_U(y) & 7; /* 3 LSb of z */ 169 k = 1; 170 } 171 172 if (k) { /* x = N/4 */ 173 if ((n & 1) != 0) { 174 *s = *c = sqrth + tiny; 175 } else if ((n & 2) == 0) { 176 *s = zero; 177 *c = one; 178 } else { 179 *s = one; 180 *c = zero; 181 } 182 183 if ((n & 4) != 0) 184 *s = -*s; 185 186 if (((n + 1) & 4) != 0) 187 *c = -*c; 188 } else { 189 if ((n & 1) != 0) 190 t = quater - t; 191 192 if (((n + (n & 1)) & 2) == 0) 193 *s = __k_sincosl(pi * t, zero, c); 194 else 195 *c = __k_sincosl(pi * t, zero, s); 196 197 if ((n & 4) != 0) 198 *s = -*s; 199 200 if (((n + 2) & 4) != 0) 201 *c = -*c; 202 } 203 } 204 205 if (hx < 0) 206 *s = -*s; 207 } 208 209 #undef U 210 #undef LDBL_LEAST_SIGNIF_U 211 #undef I 212 #undef LDBL_MOST_SIGNIF_I