xref: /netbsd-src/lib/libm/src/k_sin.c (revision aa30599e066edca03ae9611c8115625435305425)
113618394Sjtc /* @(#)k_sin.c 5.1 93/09/24 */
213618394Sjtc /*
313618394Sjtc  * ====================================================
413618394Sjtc  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
513618394Sjtc  *
613618394Sjtc  * Developed at SunPro, a Sun Microsystems, Inc. business.
713618394Sjtc  * Permission to use, copy, modify, and distribute this
813618394Sjtc  * software is freely granted, provided that this notice
913618394Sjtc  * is preserved.
1013618394Sjtc  * ====================================================
1113618394Sjtc  */
1213618394Sjtc 
13dd7adfbfSlukem #include <sys/cdefs.h>
14d1f06e0bSjtc #if defined(LIBM_SCCS) && !defined(lint)
15*aa30599eSwiz __RCSID("$NetBSD: k_sin.c,v 1.11 2002/05/26 22:01:53 wiz Exp $");
16bc3f7bf6Sjtc #endif
17bc3f7bf6Sjtc 
1813618394Sjtc /* __kernel_sin( x, y, iy)
1913618394Sjtc  * kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
2013618394Sjtc  * Input x is assumed to be bounded by ~pi/4 in magnitude.
2113618394Sjtc  * Input y is the tail of x.
2213618394Sjtc  * Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
2313618394Sjtc  *
2413618394Sjtc  * Algorithm
2513618394Sjtc  *	1. Since sin(-x) = -sin(x), we need only to consider positive x.
2613618394Sjtc  *	2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0.
2713618394Sjtc  *	3. sin(x) is approximated by a polynomial of degree 13 on
2813618394Sjtc  *	   [0,pi/4]
2913618394Sjtc  *		  	         3            13
3013618394Sjtc  *	   	sin(x) ~ x + S1*x + ... + S6*x
3113618394Sjtc  *	   where
3213618394Sjtc  *
3313618394Sjtc  * 	|sin(x)         2     4     6     8     10     12  |     -58
3413618394Sjtc  * 	|----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x  +S6*x   )| <= 2
3513618394Sjtc  * 	|  x 					           |
3613618394Sjtc  *
3713618394Sjtc  *	4. sin(x+y) = sin(x) + sin'(x')*y
3813618394Sjtc  *		    ~ sin(x) + (1-x*x/2)*y
3913618394Sjtc  *	   For better accuracy, let
4013618394Sjtc  *		     3      2      2      2      2
4113618394Sjtc  *		r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
4213618394Sjtc  *	   then                   3    2
4313618394Sjtc  *		sin(x) = x + (S1*x + (x *(r-y/2)+y))
4413618394Sjtc  */
4513618394Sjtc 
468346e333Sjtc #include "math.h"
478346e333Sjtc #include "math_private.h"
4813618394Sjtc 
4913618394Sjtc static const double
5013618394Sjtc half =  5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
5113618394Sjtc S1  = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */
5213618394Sjtc S2  =  8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */
5313618394Sjtc S3  = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */
5413618394Sjtc S4  =  2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */
5513618394Sjtc S5  = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */
5613618394Sjtc S6  =  1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */
5713618394Sjtc 
58*aa30599eSwiz double
__kernel_sin(double x,double y,int iy)59*aa30599eSwiz __kernel_sin(double x, double y, int iy)
6013618394Sjtc {
6113618394Sjtc 	double z,r,v;
62b0c9d092Sjtc 	int32_t ix;
638346e333Sjtc 	GET_HIGH_WORD(ix,x);
648346e333Sjtc 	ix &= 0x7fffffff;			/* high word of x */
6513618394Sjtc 	if(ix<0x3e400000)			/* |x| < 2**-27 */
6613618394Sjtc 	   {if((int)x==0) return x;}		/* generate inexact */
6713618394Sjtc 	z	=  x*x;
6813618394Sjtc 	v	=  z*x;
6913618394Sjtc 	r	=  S2+z*(S3+z*(S4+z*(S5+z*S6)));
7013618394Sjtc 	if(iy==0) return x+v*(S1+z*r);
7113618394Sjtc 	else      return x-((z*(half*y-v*r)-y)-v*S1);
7213618394Sjtc }
73