k_sin.c revision 97409
1169691Skan/* @(#)k_sin.c 5.1 93/09/24 */
2169691Skan/*
3169691Skan * ====================================================
4169691Skan * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
5169691Skan *
6169691Skan * Developed at SunPro, a Sun Microsystems, Inc. business.
7169691Skan * Permission to use, copy, modify, and distribute this
8169691Skan * software is freely granted, provided that this notice
9169691Skan * is preserved.
10169691Skan * ====================================================
11169691Skan */
12169691Skan
13169691Skan#ifndef lint
14169691Skanstatic char rcsid[] = "$FreeBSD: head/lib/msun/src/k_sin.c 97409 2002-05-28 17:51:46Z alfred $";
15169691Skan#endif
16169691Skan
17169691Skan/* __kernel_sin( x, y, iy)
18169691Skan * kernel sin function on [-pi/4, pi/4], pi/4 ~ 0.7854
19169691Skan * Input x is assumed to be bounded by ~pi/4 in magnitude.
20169691Skan * Input y is the tail of x.
21169691Skan * Input iy indicates whether y is 0. (if iy=0, y assume to be 0).
22169691Skan *
23169691Skan * Algorithm
24169691Skan *	1. Since sin(-x) = -sin(x), we need only to consider positive x.
25169691Skan *	2. if x < 2^-27 (hx<0x3e400000 0), return x with inexact if x!=0.
26169691Skan *	3. sin(x) is approximated by a polynomial of degree 13 on
27169691Skan *	   [0,pi/4]
28169691Skan *		  	         3            13
29169691Skan *	   	sin(x) ~ x + S1*x + ... + S6*x
30169691Skan *	   where
31169691Skan *
32169691Skan * 	|sin(x)         2     4     6     8     10     12  |     -58
33169691Skan * 	|----- - (1+S1*x +S2*x +S3*x +S4*x +S5*x  +S6*x   )| <= 2
34169691Skan * 	|  x 					           |
35169691Skan *
36169691Skan *	4. sin(x+y) = sin(x) + sin'(x')*y
37169691Skan *		    ~ sin(x) + (1-x*x/2)*y
38169691Skan *	   For better accuracy, let
39169691Skan *		     3      2      2      2      2
40169691Skan *		r = x *(S2+x *(S3+x *(S4+x *(S5+x *S6))))
41169691Skan *	   then                   3    2
42169691Skan *		sin(x) = x + (S1*x + (x *(r-y/2)+y))
43169691Skan */
44169691Skan
45169691Skan#include "math.h"
46169691Skan#include "math_private.h"
47169691Skan
48169691Skanstatic const double
49169691Skanhalf =  5.00000000000000000000e-01, /* 0x3FE00000, 0x00000000 */
50169691SkanS1  = -1.66666666666666324348e-01, /* 0xBFC55555, 0x55555549 */
51169691SkanS2  =  8.33333333332248946124e-03, /* 0x3F811111, 0x1110F8A6 */
52169691SkanS3  = -1.98412698298579493134e-04, /* 0xBF2A01A0, 0x19C161D5 */
53169691SkanS4  =  2.75573137070700676789e-06, /* 0x3EC71DE3, 0x57B1FE7D */
54169691SkanS5  = -2.50507602534068634195e-08, /* 0xBE5AE5E6, 0x8A2B9CEB */
55169691SkanS6  =  1.58969099521155010221e-10; /* 0x3DE5D93A, 0x5ACFD57C */
56169691Skan
57169691Skan	double __kernel_sin(double x, double y, int iy)
58169691Skan{
59169691Skan	double z,r,v;
60169691Skan	int32_t ix;
61169691Skan	GET_HIGH_WORD(ix,x);
62169691Skan	ix &= 0x7fffffff;			/* high word of x */
63169691Skan	if(ix<0x3e400000)			/* |x| < 2**-27 */
64169691Skan	   {if((int)x==0) return x;}		/* generate inexact */
65169691Skan	z	=  x*x;
66169691Skan	v	=  z*x;
67169691Skan	r	=  S2+z*(S3+z*(S4+z*(S5+z*S6)));
68169691Skan	if(iy==0) return x+v*(S1+z*r);
69169691Skan	else      return x-((z*(half*y-v*r)-y)-v*S1);
70169691Skan}
71169691Skan