Merge remote-tracking branch 'origin/master'
[unleashed/lotheac.git] / usr / src / lib / libm / common / C / sincospi.c
blob473c87274ca923f9788a103f3e20385f5f4aac02
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
27 * Use is subject to license terms.
30 /* INDENT OFF */
32 * void sincospi(double x, double *s, double *c)
33 * *s = sin(pi*x); *c = cos(pi*x);
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 * ---------------------------------------------------
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.
72 #include "libm.h"
73 #include "libm_protos.h"
74 #include "libm_macros.h"
75 #include <math.h>
77 static const double
78 pi = 3.14159265358979323846, /* 400921FB,54442D18 */
79 sqrth_h = 0.70710678118654757273731092936941422522068023681640625,
80 sqrth_l = -4.8336466567264565185935844299127932213411660131004e-17;
81 /* INDENT ON */
83 void
84 sincospi(double x, double *s, double *c)
86 double y, z, t;
87 int n, ix, k;
88 int hx = ((int *)&x)[HIWORD];
89 unsigned h, lx = ((unsigned *)&x)[LOWORD];
91 ix = hx & ~0x80000000;
92 n = (ix >> 20) - 0x3ff;
93 if (n >= 51) { /* |x| >= 2**51 */
94 if (n >= 1024) {
95 #if defined(FPADD_TRAPS_INCOMPLETE_ON_NAN)
96 *s = *c = ix >= 0x7ff80000 ? x : x - x;
97 /* assumes sparc-like QNaN */
98 #else
99 *s = *c = x - x;
100 #endif
101 } else {
102 if (n >= 53) {
103 *s = 0.0;
104 *c = 1.0;
105 } else if (n == 52) {
106 if ((lx & 1) == 0) {
107 *s = 0.0;
108 *c = 1.0;
109 } else {
110 *s = -0.0;
111 *c = -1.0;
113 } else { /* n == 51 */
114 if ((lx & 1) == 0) {
115 *s = 0.0;
116 *c = 1.0;
117 } else {
118 *s = 1.0;
119 *c = 0.0;
121 if ((lx & 2) != 0) {
122 *s = -*s;
123 *c = -*c;
127 } else if (n < -2) /* |x| < 0.25 */
128 *s = __k_sincos(pi * fabs(x), 0.0, c);
129 else {
130 /* y = |4x|, z = floor(y), and n = (int)(z mod 8.0) */
131 if (ix < 0x41C00000) { /* |x| < 2**29 */
132 y = 4.0 * fabs(x);
133 n = (int)y; /* exact */
134 z = (double)n;
135 k = z == y;
136 t = (y - z) * 0.25;
137 } else { /* 2**29 <= |x| < 2**51 */
138 y = fabs(x);
139 k = 50 - n;
140 n = lx >> k;
141 h = n << k;
142 ((unsigned *)&z)[LOWORD] = h;
143 ((int *)&z)[HIWORD] = ix;
144 k = h == lx;
145 t = y - z;
147 if (k) { /* x = N/4 */
148 if ((n & 1) != 0) {
149 *s = *c = sqrth_h + sqrth_l;
150 } else {
151 if ((n & 2) == 0) {
152 *s = 0.0;
153 *c = 1.0;
154 } else {
155 *s = 1.0;
156 *c = 0.0;
159 if ((n & 4) != 0)
160 *s = -*s;
161 if (((n + 1) & 4) != 0)
162 *c = -*c;
163 } else {
164 if ((n & 1) != 0)
165 t = 0.25 - t;
166 if (((n + (n & 1)) & 2) == 0)
167 *s = __k_sincos(pi * t, 0.0, c);
168 else
169 *c = __k_sincos(pi * t, 0.0, s);
170 if ((n & 4) != 0)
171 *s = -*s;
172 if (((n + 2) & 4) != 0)
173 *c = -*c;
176 if (hx < 0)
177 *s = -*s;