125c28e83SPiotr Jasiukajtis /*
225c28e83SPiotr Jasiukajtis * CDDL HEADER START
325c28e83SPiotr Jasiukajtis *
425c28e83SPiotr Jasiukajtis * The contents of this file are subject to the terms of the
525c28e83SPiotr Jasiukajtis * Common Development and Distribution License (the "License").
625c28e83SPiotr Jasiukajtis * You may not use this file except in compliance with the License.
725c28e83SPiotr Jasiukajtis *
825c28e83SPiotr Jasiukajtis * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
925c28e83SPiotr Jasiukajtis * or http://www.opensolaris.org/os/licensing.
1025c28e83SPiotr Jasiukajtis * See the License for the specific language governing permissions
1125c28e83SPiotr Jasiukajtis * and limitations under the License.
1225c28e83SPiotr Jasiukajtis *
1325c28e83SPiotr Jasiukajtis * When distributing Covered Code, include this CDDL HEADER in each
1425c28e83SPiotr Jasiukajtis * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1525c28e83SPiotr Jasiukajtis * If applicable, add the following below this CDDL HEADER, with the
1625c28e83SPiotr Jasiukajtis * fields enclosed by brackets "[]" replaced with your own identifying
1725c28e83SPiotr Jasiukajtis * information: Portions Copyright [yyyy] [name of copyright owner]
1825c28e83SPiotr Jasiukajtis *
1925c28e83SPiotr Jasiukajtis * CDDL HEADER END
2025c28e83SPiotr Jasiukajtis */
2125c28e83SPiotr Jasiukajtis /*
2225c28e83SPiotr Jasiukajtis * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
2325c28e83SPiotr Jasiukajtis */
2425c28e83SPiotr Jasiukajtis /*
2525c28e83SPiotr Jasiukajtis * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
2625c28e83SPiotr Jasiukajtis * Use is subject to license terms.
2725c28e83SPiotr Jasiukajtis */
2825c28e83SPiotr Jasiukajtis
29*ddc0e0b5SRichard Lowe #pragma weak __sin = sin
3025c28e83SPiotr Jasiukajtis
3125c28e83SPiotr Jasiukajtis /* INDENT OFF */
3225c28e83SPiotr Jasiukajtis /*
3325c28e83SPiotr Jasiukajtis * sin(x)
3425c28e83SPiotr Jasiukajtis * Accurate Table look-up algorithm by K.C. Ng, May, 1995.
3525c28e83SPiotr Jasiukajtis *
3625c28e83SPiotr Jasiukajtis * Algorithm: see sincos.c
3725c28e83SPiotr Jasiukajtis */
3825c28e83SPiotr Jasiukajtis
3925c28e83SPiotr Jasiukajtis #include "libm.h"
4025c28e83SPiotr Jasiukajtis
4125c28e83SPiotr Jasiukajtis static const double sc[] = {
4225c28e83SPiotr Jasiukajtis /* ONE = */ 1.0,
4325c28e83SPiotr Jasiukajtis /* NONE = */ -1.0,
4425c28e83SPiotr Jasiukajtis /*
4525c28e83SPiotr Jasiukajtis * |sin(x) - (x+pp1*x^3+pp2*x^5)| <= 2^-58.79 for |x| < 0.008
4625c28e83SPiotr Jasiukajtis */
4725c28e83SPiotr Jasiukajtis /* PP1 = */ -0.166666666666316558867252052378889521480627858683055567,
4825c28e83SPiotr Jasiukajtis /* PP2 = */ .008333315652997472323564894248466758248475374977974017927,
4925c28e83SPiotr Jasiukajtis /*
5025c28e83SPiotr Jasiukajtis * |(sin(x) - (x+p1*x^3+...+p4*x^9)|
5125c28e83SPiotr Jasiukajtis * |------------------------------ | <= 2^-57.63 for |x| < 0.1953125
5225c28e83SPiotr Jasiukajtis * | x |
5325c28e83SPiotr Jasiukajtis */
5425c28e83SPiotr Jasiukajtis /* P1 = */ -1.666666666666629669805215138920301589656e-0001,
5525c28e83SPiotr Jasiukajtis /* P2 = */ 8.333333332390951295683993455280336376663e-0003,
5625c28e83SPiotr Jasiukajtis /* P3 = */ -1.984126237997976692791551778230098403960e-0004,
5725c28e83SPiotr Jasiukajtis /* P4 = */ 2.753403624854277237649987622848330351110e-0006,
5825c28e83SPiotr Jasiukajtis /*
5925c28e83SPiotr Jasiukajtis * |cos(x) - (1+qq1*x^2+qq2*x^4)| <= 2^-55.99 for |x| <= 0.008 (0x3f80624d)
6025c28e83SPiotr Jasiukajtis */
6125c28e83SPiotr Jasiukajtis /* QQ1 = */ -0.4999999999975492381842911981948418542742729,
6225c28e83SPiotr Jasiukajtis /* QQ2 = */ 0.041666542904352059294545209158357640398771740,
6325c28e83SPiotr Jasiukajtis /* PI_H = */ 3.1415926535897931159979634685,
6425c28e83SPiotr Jasiukajtis /* PI_L = */ 1.22464679914735317722606593227425e-16,
6525c28e83SPiotr Jasiukajtis /* PI_L0 = */ 1.22464679914558443311283879205095e-16,
6625c28e83SPiotr Jasiukajtis /* PI_L1 = */ 1.768744113227140223300005233735517376e-28,
6725c28e83SPiotr Jasiukajtis /* PI2_H = */ 6.2831853071795862319959269370,
6825c28e83SPiotr Jasiukajtis /* PI2_L = */ 2.44929359829470635445213186454850e-16,
6925c28e83SPiotr Jasiukajtis /* PI2_L0 = */ 2.44929359829116886622567758410190e-16,
7025c28e83SPiotr Jasiukajtis /* PI2_L1 = */ 3.537488226454280446600010467471034752e-28,
7125c28e83SPiotr Jasiukajtis };
7225c28e83SPiotr Jasiukajtis /* INDENT ON */
7325c28e83SPiotr Jasiukajtis
7425c28e83SPiotr Jasiukajtis #define ONEA sc
7525c28e83SPiotr Jasiukajtis #define ONE sc[0]
7625c28e83SPiotr Jasiukajtis #define NONE sc[1]
7725c28e83SPiotr Jasiukajtis #define PP1 sc[2]
7825c28e83SPiotr Jasiukajtis #define PP2 sc[3]
7925c28e83SPiotr Jasiukajtis #define P1 sc[4]
8025c28e83SPiotr Jasiukajtis #define P2 sc[5]
8125c28e83SPiotr Jasiukajtis #define P3 sc[6]
8225c28e83SPiotr Jasiukajtis #define P4 sc[7]
8325c28e83SPiotr Jasiukajtis #define QQ1 sc[8]
8425c28e83SPiotr Jasiukajtis #define QQ2 sc[9]
8525c28e83SPiotr Jasiukajtis #define PI_H sc[10]
8625c28e83SPiotr Jasiukajtis #define PI_L sc[11]
8725c28e83SPiotr Jasiukajtis #define PI_L0 sc[12]
8825c28e83SPiotr Jasiukajtis #define PI_L1 sc[13]
8925c28e83SPiotr Jasiukajtis #define PI2_H sc[14]
9025c28e83SPiotr Jasiukajtis #define PI2_L sc[15]
9125c28e83SPiotr Jasiukajtis #define PI2_L0 sc[16]
9225c28e83SPiotr Jasiukajtis #define PI2_L1 sc[17]
9325c28e83SPiotr Jasiukajtis
9425c28e83SPiotr Jasiukajtis extern const double _TBL_sincos[], _TBL_sincosx[];
9525c28e83SPiotr Jasiukajtis
9625c28e83SPiotr Jasiukajtis double
sin(double x)9725c28e83SPiotr Jasiukajtis sin(double x) {
9825c28e83SPiotr Jasiukajtis double z, y[2], w, s, v, p, q;
9925c28e83SPiotr Jasiukajtis int i, j, n, hx, ix, lx;
10025c28e83SPiotr Jasiukajtis
10125c28e83SPiotr Jasiukajtis hx = ((int *)&x)[HIWORD];
10225c28e83SPiotr Jasiukajtis lx = ((int *)&x)[LOWORD];
10325c28e83SPiotr Jasiukajtis ix = hx & ~0x80000000;
10425c28e83SPiotr Jasiukajtis
10525c28e83SPiotr Jasiukajtis if (ix <= 0x3fc50000) { /* |x| < .1640625 */
10625c28e83SPiotr Jasiukajtis if (ix < 0x3e400000) /* |x| < 2**-27 */
10725c28e83SPiotr Jasiukajtis if ((int)x == 0)
10825c28e83SPiotr Jasiukajtis return (x);
10925c28e83SPiotr Jasiukajtis z = x * x;
11025c28e83SPiotr Jasiukajtis if (ix < 0x3f800000) /* |x| < 2**-8 */
11125c28e83SPiotr Jasiukajtis w = (z * x) * (PP1 + z * PP2);
11225c28e83SPiotr Jasiukajtis else
11325c28e83SPiotr Jasiukajtis w = (x * z) * ((P1 + z * P2) + (z * z) * (P3 + z * P4));
11425c28e83SPiotr Jasiukajtis return (x + w);
11525c28e83SPiotr Jasiukajtis }
11625c28e83SPiotr Jasiukajtis
11725c28e83SPiotr Jasiukajtis /* for .1640625 < x < M, */
11825c28e83SPiotr Jasiukajtis n = ix >> 20;
11925c28e83SPiotr Jasiukajtis if (n < 0x402) { /* x < 8 */
12025c28e83SPiotr Jasiukajtis i = (((ix >> 12) & 0xff) | 0x100) >> (0x401 - n);
12125c28e83SPiotr Jasiukajtis j = i - 10;
12225c28e83SPiotr Jasiukajtis x = fabs(x);
12325c28e83SPiotr Jasiukajtis v = x - _TBL_sincosx[j];
12425c28e83SPiotr Jasiukajtis if (((j - 181) ^ (j - 201)) < 0) {
12525c28e83SPiotr Jasiukajtis /* near pi, sin(x) = sin(pi-x) */
12625c28e83SPiotr Jasiukajtis p = PI_H - x;
12725c28e83SPiotr Jasiukajtis i = ix - 0x400921fb;
12825c28e83SPiotr Jasiukajtis x = p + PI_L;
12925c28e83SPiotr Jasiukajtis if ((i | ((lx - 0x54442D00) & 0xffffff00)) == 0) {
13025c28e83SPiotr Jasiukajtis /* very close to pi */
13125c28e83SPiotr Jasiukajtis x = p + PI_L0;
13225c28e83SPiotr Jasiukajtis return ((hx >= 0)? x + PI_L1 : -(x + PI_L1));
13325c28e83SPiotr Jasiukajtis }
13425c28e83SPiotr Jasiukajtis z = x * x;
13525c28e83SPiotr Jasiukajtis if (((ix - 0x40092000) >> 11) == 0) {
13625c28e83SPiotr Jasiukajtis /* |pi-x|<2**-8 */
13725c28e83SPiotr Jasiukajtis w = PI_L + (z * x) * (PP1 + z * PP2);
13825c28e83SPiotr Jasiukajtis } else {
13925c28e83SPiotr Jasiukajtis w = PI_L + (z * x) * ((P1 + z * P2) +
14025c28e83SPiotr Jasiukajtis (z * z) * (P3 + z * P4));
14125c28e83SPiotr Jasiukajtis }
14225c28e83SPiotr Jasiukajtis return ((hx >= 0)? p + w : -p - w);
14325c28e83SPiotr Jasiukajtis }
14425c28e83SPiotr Jasiukajtis s = v * v;
14525c28e83SPiotr Jasiukajtis if (((j - 382) ^ (j - 402)) < 0) {
14625c28e83SPiotr Jasiukajtis /* near 2pi, sin(x) = sin(x-2pi) */
14725c28e83SPiotr Jasiukajtis p = x - PI2_H;
14825c28e83SPiotr Jasiukajtis i = ix - 0x401921fb;
14925c28e83SPiotr Jasiukajtis x = p - PI2_L;
15025c28e83SPiotr Jasiukajtis if ((i | ((lx - 0x54442D00) & 0xffffff00)) == 0) {
15125c28e83SPiotr Jasiukajtis /* very close to 2pi */
15225c28e83SPiotr Jasiukajtis x = p - PI2_L0;
15325c28e83SPiotr Jasiukajtis return ((hx >= 0)? x - PI2_L1 : -(x - PI2_L1));
15425c28e83SPiotr Jasiukajtis }
15525c28e83SPiotr Jasiukajtis z = x * x;
15625c28e83SPiotr Jasiukajtis if (((ix - 0x40192000) >> 10) == 0) {
15725c28e83SPiotr Jasiukajtis /* |x-2pi|<2**-8 */
15825c28e83SPiotr Jasiukajtis w = (z * x) * (PP1 + z * PP2) - PI2_L;
15925c28e83SPiotr Jasiukajtis } else {
16025c28e83SPiotr Jasiukajtis w = (z * x) * ((P1 + z * P2) +
16125c28e83SPiotr Jasiukajtis (z * z) * (P3 + z * P4)) - PI2_L;
16225c28e83SPiotr Jasiukajtis }
16325c28e83SPiotr Jasiukajtis return ((hx >= 0)? p + w : -p - w);
16425c28e83SPiotr Jasiukajtis }
16525c28e83SPiotr Jasiukajtis j <<= 1;
16625c28e83SPiotr Jasiukajtis w = _TBL_sincos[j+1];
16725c28e83SPiotr Jasiukajtis z = _TBL_sincos[j];
16825c28e83SPiotr Jasiukajtis p = v + (v * s) * (PP1 + s * PP2);
16925c28e83SPiotr Jasiukajtis q = s * (QQ1 + s * QQ2);
17025c28e83SPiotr Jasiukajtis v = w * p + z * q;
17125c28e83SPiotr Jasiukajtis return ((hx >= 0)? z + v : -z - v);
17225c28e83SPiotr Jasiukajtis }
17325c28e83SPiotr Jasiukajtis
17425c28e83SPiotr Jasiukajtis if (ix >= 0x7ff00000) /* sin(Inf or NaN) is NaN */
17525c28e83SPiotr Jasiukajtis return (x / x);
17625c28e83SPiotr Jasiukajtis
17725c28e83SPiotr Jasiukajtis /* argument reduction needed */
17825c28e83SPiotr Jasiukajtis n = __rem_pio2(x, y);
17925c28e83SPiotr Jasiukajtis switch (n & 3) {
18025c28e83SPiotr Jasiukajtis case 0:
18125c28e83SPiotr Jasiukajtis return (__k_sin(y[0], y[1]));
18225c28e83SPiotr Jasiukajtis case 1:
18325c28e83SPiotr Jasiukajtis return (__k_cos(y[0], y[1]));
18425c28e83SPiotr Jasiukajtis case 2:
18525c28e83SPiotr Jasiukajtis return (-__k_sin(y[0], y[1]));
18625c28e83SPiotr Jasiukajtis default:
18725c28e83SPiotr Jasiukajtis return (-__k_cos(y[0], y[1]));
18825c28e83SPiotr Jasiukajtis }
18925c28e83SPiotr Jasiukajtis }
190