src/libm/k_sin.c
 changeset 2756 a98604b691c8 child 3162 dc1eb82ffdaa
```     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/libm/k_sin.c	Mon Sep 15 06:33:23 2008 +0000
1.3 @@ -0,0 +1,86 @@
1.4 +/* @(#)k_sin.c 5.1 93/09/24 */
1.5 +/*
1.6 + * ====================================================
1.8 + *
1.9 + * Developed at SunPro, a Sun Microsystems, Inc. business.
1.10 + * Permission to use, copy, modify, and distribute this
1.11 + * software is freely granted, provided that this notice
1.12 + * is preserved.
1.13 + * ====================================================
1.14 + */
1.15 +
1.16 +#if defined(LIBM_SCCS) && !defined(lint)
1.17 +static char rcsid[] = "\$NetBSD: k_sin.c,v 1.8 1995/05/10 20:46:31 jtc Exp \$";
1.18 +#endif
1.19 +
1.20 +/* __kernel_sin( x, y, iy)
1.21 + * kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
1.22 + * Input x is assumed to be bounded by ~pi/4 in magnitude.
1.23 + * Input y is the tail of x.
1.24 + * Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
1.25 + *
1.26 + * Algorithm
1.27 + *	1. Since sin(-x) = -sin(x), we need only to consider positive x.
1.28 + *	2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0.
1.29 + *	3. sin(x) is approximated by a polynomial of degree 13 on
1.30 + *	   [0,pi/4]
1.31 + *		  	         3            13
1.32 + *	   	sin(x) ~ x + S1*x + ... + S6*x
1.33 + *	   where
1.34 + *
1.35 + * 	|sin(x)         2     4     6     8     10     12  |     -58
1.36 + * 	|----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x  +S6*x   )| <= 2
1.37 + * 	|  x 					           |
1.38 + *
1.39 + *	4. sin(x+y) = sin(x) + sin'(x')*y
1.40 + *		    ~ sin(x) + (1-x*x/2)*y
1.41 + *	   For better accuracy, let
1.42 + *		     3      2      2      2      2
1.43 + *		r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
1.44 + *	   then                   3    2
1.45 + *		sin(x) = x + (S1*x + (x *(r-y/2)+y))
1.46 + */
1.47 +
1.48 +#include "math.h"
1.49 +#include "math_private.h"
1.50 +
1.51 +#ifdef __STDC__
1.52 +static const double
1.53 +#else
1.54 +static double
1.55 +#endif
1.56 +  half = 5.00000000000000000000e-01,    /* 0x3FE00000, 0x00000000 */
1.57 +    S1 = -1.66666666666666324348e-01,   /* 0xBFC55555, 0x55555549 */
1.58 +    S2 = 8.33333333332248946124e-03,    /* 0x3F811111, 0x1110F8A6 */
1.59 +    S3 = -1.98412698298579493134e-04,   /* 0xBF2A01A0, 0x19C161D5 */
1.60 +    S4 = 2.75573137070700676789e-06,    /* 0x3EC71DE3, 0x57B1FE7D */
1.61 +    S5 = -2.50507602534068634195e-08,   /* 0xBE5AE5E6, 0x8A2B9CEB */
1.62 +    S6 = 1.58969099521155010221e-10;    /* 0x3DE5D93A, 0x5ACFD57C */
1.63 +
1.64 +#ifdef __STDC__
1.65 +double attribute_hidden
1.66 +__kernel_sin(double x, double y, int iy)
1.67 +#else
1.68 +double attribute_hidden
1.69 +__kernel_sin(x, y, iy)
1.70 +     double x, y;
1.71 +     int iy;                    /* iy=0 if y is zero */
1.72 +#endif
1.73 +{
1.74 +    double z, r, v;
1.75 +    int32_t ix;
1.76 +    GET_HIGH_WORD(ix, x);
1.77 +    ix &= 0x7fffffff;           /* high word of x */
1.78 +    if (ix < 0x3e400000) {      /* |x| < 2**-27 */
1.79 +        if ((int) x == 0)
1.80 +            return x;
1.81 +    }                           /* generate inexact */
1.82 +    z = x * x;
1.83 +    v = z * x;
1.84 +    r = S2 + z * (S3 + z * (S4 + z * (S5 + z * S6)));
1.85 +    if (iy == 0)
1.86 +        return x + v * (S1 + z * r);
1.87 +    else
1.88 +        return x - ((z * (half * y - v * r) - y) - v * S1);
1.89 +}
```