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 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 30 #pragma weak sincospi = __sincospi 31 32 /* INDENT OFF */ 33 /* 34 * void sincospi(double x, double *s, double *c) 35 * *s = sin(pi*x); *c = cos(pi*x); 36 * 37 * Algorithm, 10/17/2002, K.C. Ng 38 * ------------------------------ 39 * Let y = |4x|, z = floor(y), and n = (int)(z mod 8.0) (displayed in binary). 40 * 1. If y == z, then x is a multiple of pi/4. Return the following values: 41 * --------------------------------------------------- 42 * n x mod 2 sin(x*pi) cos(x*pi) tan(x*pi) 43 * --------------------------------------------------- 44 * 000 0.00 +0 ___ +1 ___ +0 45 * 001 0.25 +\/0.5 +\/0.5 +1 46 * 010 0.50 +1 ___ +0 ___ +inf 47 * 011 0.75 +\/0.5 -\/0.5 -1 48 * 100 1.00 -0 ___ -1 ___ +0 49 * 101 1.25 -\/0.5 -\/0.5 +1 50 * 110 1.50 -1 ___ -0 ___ +inf 51 * 111 1.75 -\/0.5 +\/0.5 -1 52 * --------------------------------------------------- 53 * 2. Otherwise, 54 * --------------------------------------------------- 55 * n t sin(x*pi) cos(x*pi) tan(x*pi) 56 * --------------------------------------------------- 57 * 000 (y-z)/4 sinpi(t) cospi(t) tanpi(t) 58 * 001 (z+1-y)/4 cospi(t) sinpi(t) 1/tanpi(t) 59 * 010 (y-z)/4 cospi(t) -sinpi(t) -1/tanpi(t) 60 * 011 (z+1-y)/4 sinpi(t) -cospi(t) -tanpi(t) 61 * 100 (y-z)/4 -sinpi(t) -cospi(t) tanpi(t) 62 * 101 (z+1-y)/4 -cospi(t) -sinpi(t) 1/tanpi(t) 63 * 110 (y-z)/4 -cospi(t) sinpi(t) -1/tanpi(t) 64 * 111 (z+1-y)/4 -sinpi(t) cospi(t) -tanpi(t) 65 * --------------------------------------------------- 66 * 67 * NOTE. This program compute sinpi/cospi(t<0.25) by __k_sin/cos(pi*t, 0.0). 68 * This will return a result with error slightly more than one ulp (but less 69 * than 2 ulp). If one wants accurate result, one may break up pi*t in 70 * high (tpi_h) and low (tpi_l) parts and call __k_sin/cos(tip_h, tip_lo) 71 * instead. 72 */ 73 74 #include "libm.h" 75 #include "libm_synonyms.h" 76 #include "libm_protos.h" 77 #include "libm_macros.h" 78 #include <math.h> 79 #if defined(__SUNPRO_C) 80 #include <sunmath.h> 81 #endif 82 83 static const double 84 pi = 3.14159265358979323846, /* 400921FB,54442D18 */ 85 sqrth_h = 0.70710678118654757273731092936941422522068023681640625, 86 sqrth_l = -4.8336466567264565185935844299127932213411660131004e-17; 87 /* INDENT ON */ 88 89 void 90 sincospi(double x, double *s, double *c) { 91 double y, z, t; 92 int n, ix, k; 93 int hx = ((int *) &x)[HIWORD]; 94 unsigned h, lx = ((unsigned *) &x)[LOWORD]; 95 96 ix = hx & ~0x80000000; 97 n = (ix >> 20) - 0x3ff; 98 if (n >= 51) { /* |x| >= 2**51 */ 99 if (n >= 1024) 100 #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN) 101 *s = *c = ix >= 0x7ff80000 ? x : x - x; 102 /* assumes sparc-like QNaN */ 103 #else 104 *s = *c = x - x; 105 #endif 106 else { 107 if (n >= 53) { 108 *s = 0.0; 109 *c = 1.0; 110 } 111 else if (n == 52) { 112 if ((lx & 1) == 0) { 113 *s = 0.0; 114 *c = 1.0; 115 } 116 else { 117 *s = -0.0; 118 *c = -1.0; 119 } 120 } 121 else { /* n == 51 */ 122 if ((lx & 1) == 0) { 123 *s = 0.0; 124 *c = 1.0; 125 } 126 else { 127 *s = 1.0; 128 *c = 0.0; 129 } 130 if ((lx & 2) != 0) { 131 *s = -*s; 132 *c = -*c; 133 } 134 } 135 } 136 } 137 else if (n < -2) /* |x| < 0.25 */ 138 *s = __k_sincos(pi * fabs(x), 0.0, c); 139 else { 140 /* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */ 141 if (ix < 0x41C00000) { /* |x| < 2**29 */ 142 y = 4.0 * fabs(x); 143 n = (int) y; /* exact */ 144 z = (double) n; 145 k = z == y; 146 t = (y - z) * 0.25; 147 } 148 else { /* 2**29 <= |x| < 2**51 */ 149 y = fabs(x); 150 k = 50 - n; 151 n = lx >> k; 152 h = n << k; 153 ((unsigned *) &z)[LOWORD] = h; 154 ((int *) &z)[HIWORD] = ix; 155 k = h == lx; 156 t = y - z; 157 } 158 if (k) { /* x = N/4 */ 159 if ((n & 1) != 0) 160 *s = *c = sqrth_h + sqrth_l; 161 else 162 if ((n & 2) == 0) { 163 *s = 0.0; 164 *c = 1.0; 165 } 166 else { 167 *s = 1.0; 168 *c = 0.0; 169 } 170 y = (n & 2) == 0 ? 0.0 : 1.0; 171 if ((n & 4) != 0) 172 *s = -*s; 173 if (((n + 1) & 4) != 0) 174 *c = -*c; 175 } 176 else { 177 if ((n & 1) != 0) 178 t = 0.25 - t; 179 if (((n + (n & 1)) & 2) == 0) 180 *s = __k_sincos(pi * t, 0.0, c); 181 else 182 *c = __k_sincos(pi * t, 0.0, s); 183 if ((n & 4) != 0) 184 *s = -*s; 185 if (((n + 2) & 4) != 0) 186 *c = -*c; 187 } 188 } 189 if (hx < 0) 190 *s = -*s; 191 } 192