xref: /freebsd/lib/msun/src/k_cos.c (revision 0dd5a5603e7a33d976f8e6015620bbc79839c609)
13f708241SDavid Schultz 
23a8617a8SJordan K. Hubbard /*
33a8617a8SJordan K. Hubbard  * ====================================================
43a8617a8SJordan K. Hubbard  * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
53a8617a8SJordan K. Hubbard  *
63f708241SDavid Schultz  * Developed at SunSoft, a Sun Microsystems, Inc. business.
73a8617a8SJordan K. Hubbard  * Permission to use, copy, modify, and distribute this
83a8617a8SJordan K. Hubbard  * software is freely granted, provided that this notice
93a8617a8SJordan K. Hubbard  * is preserved.
103a8617a8SJordan K. Hubbard  * ====================================================
113a8617a8SJordan K. Hubbard  */
123a8617a8SJordan K. Hubbard 
133a8617a8SJordan K. Hubbard /*
143a8617a8SJordan K. Hubbard  * __kernel_cos( x,  y )
153a8617a8SJordan K. Hubbard  * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
163a8617a8SJordan K. Hubbard  * Input x is assumed to be bounded by ~pi/4 in magnitude.
173a8617a8SJordan K. Hubbard  * Input y is the tail of x.
183a8617a8SJordan K. Hubbard  *
193a8617a8SJordan K. Hubbard  * Algorithm
203a8617a8SJordan K. Hubbard  *	1. Since cos(-x) = cos(x), we need only to consider positive x.
213a8617a8SJordan K. Hubbard  *	2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0.
223a8617a8SJordan K. Hubbard  *	3. cos(x) is approximated by a polynomial of degree 14 on
233a8617a8SJordan K. Hubbard  *	   [0,pi/4]
243a8617a8SJordan K. Hubbard  *		  	                 4            14
253a8617a8SJordan K. Hubbard  *	   	cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
263a8617a8SJordan K. Hubbard  *	   where the remez error is
273a8617a8SJordan K. Hubbard  *
283a8617a8SJordan K. Hubbard  * 	|              2     4     6     8     10    12     14 |     -58
293a8617a8SJordan K. Hubbard  * 	|cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x  +C6*x  )| <= 2
303a8617a8SJordan K. Hubbard  * 	|    					               |
313a8617a8SJordan K. Hubbard  *
323a8617a8SJordan K. Hubbard  * 	               4     6     8     10    12     14
333a8617a8SJordan K. Hubbard  *	4. let r = C1*x +C2*x +C3*x +C4*x +C5*x  +C6*x  , then
343b46e988SBruce Evans  *	       cos(x) ~ 1 - x*x/2 + r
353a8617a8SJordan K. Hubbard  *	   since cos(x+y) ~ cos(x) - sin(x)*y
363a8617a8SJordan K. Hubbard  *			  ~ cos(x) - x*y,
373a8617a8SJordan K. Hubbard  *	   a correction term is necessary in cos(x) and hence
383a8617a8SJordan K. Hubbard  *		cos(x+y) = 1 - (x*x/2 - (r - x*y))
393b46e988SBruce Evans  *	   For better accuracy, rearrange to
403b46e988SBruce Evans  *		cos(x+y) ~ w + (tmp + (r-x*y))
413b46e988SBruce Evans  *	   where w = 1 - x*x/2 and tmp is a tiny correction term
423b46e988SBruce Evans  *	   (1 - x*x/2 == w + tmp exactly in infinite precision).
433b46e988SBruce Evans  *	   The exactness of w + tmp in infinite precision depends on w
443b46e988SBruce Evans  *	   and tmp having the same precision as x.  If they have extra
453b46e988SBruce Evans  *	   precision due to compiler bugs, then the extra precision is
463b46e988SBruce Evans  *	   only good provided it is retained in all terms of the final
473b46e988SBruce Evans  *	   expression for cos().  Retention happens in all cases tested
483b46e988SBruce Evans  *	   under FreeBSD, so don't pessimize things by forcibly clipping
493b46e988SBruce Evans  *	   any extra precision in w.
503a8617a8SJordan K. Hubbard  */
513a8617a8SJordan K. Hubbard 
523a8617a8SJordan K. Hubbard #include "math.h"
533a8617a8SJordan K. Hubbard #include "math_private.h"
543a8617a8SJordan K. Hubbard 
553a8617a8SJordan K. Hubbard static const double
563a8617a8SJordan K. Hubbard one =  1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
573a8617a8SJordan K. Hubbard C1  =  4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */
583a8617a8SJordan K. Hubbard C2  = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */
593a8617a8SJordan K. Hubbard C3  =  2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */
603a8617a8SJordan K. Hubbard C4  = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */
613a8617a8SJordan K. Hubbard C5  =  2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */
623a8617a8SJordan K. Hubbard C6  = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */
633a8617a8SJordan K. Hubbard 
6459b19ff1SAlfred Perlstein double
__kernel_cos(double x,double y)6559b19ff1SAlfred Perlstein __kernel_cos(double x, double y)
663a8617a8SJordan K. Hubbard {
673b46e988SBruce Evans 	double hz,z,r,w;
683b46e988SBruce Evans 
693a8617a8SJordan K. Hubbard 	z  = x*x;
709ce87560SBruce Evans 	w  = z*z;
719ce87560SBruce Evans 	r  = z*(C1+z*(C2+z*C3)) + w*w*(C4+z*(C5+z*C6));
72fc84b771SBruce Evans 	hz = 0.5*z;
733b46e988SBruce Evans 	w  = one-hz;
743b46e988SBruce Evans 	return w + (((one-w)-hz) + (z*r-x*y));
753a8617a8SJordan K. Hubbard }
76