155682Smarkm
255682Smarkm/* @(#)k_cos.c 1.3 95/01/18 */
355682Smarkm/*
455682Smarkm * ====================================================
555682Smarkm * Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
655682Smarkm *
755682Smarkm * Developed at SunSoft, a Sun Microsystems, Inc. business.
855682Smarkm * Permission to use, copy, modify, and distribute this
955682Smarkm * software is freely granted, provided that this notice
1055682Smarkm * is preserved.
1155682Smarkm * ====================================================
1255682Smarkm */
1355682Smarkm
1455682Smarkm#include <sys/cdefs.h>
1555682Smarkm__FBSDID("$FreeBSD$");
1655682Smarkm
1755682Smarkm/*
1855682Smarkm * __kernel_cos( x,  y )
1955682Smarkm * kernel cos function on [-pi/4, pi/4], pi/4 ~ 0.785398164
2055682Smarkm * Input x is assumed to be bounded by ~pi/4 in magnitude.
2155682Smarkm * Input y is the tail of x.
2255682Smarkm *
2355682Smarkm * Algorithm
2455682Smarkm *	1. Since cos(-x) = cos(x), we need only to consider positive x.
2555682Smarkm *	2. if x < 2^-27 (hx<0x3e400000 0), return 1 with inexact if x!=0.
2655682Smarkm *	3. cos(x) is approximated by a polynomial of degree 14 on
2755682Smarkm *	   [0,pi/4]
2855682Smarkm *		  	                 4            14
2955682Smarkm *	   	cos(x) ~ 1 - x*x/2 + C1*x + ... + C6*x
3055682Smarkm *	   where the remez error is
3155682Smarkm *
3255682Smarkm * 	|              2     4     6     8     10    12     14 |     -58
3355682Smarkm * 	|cos(x)-(1-.5*x +C1*x +C2*x +C3*x +C4*x +C5*x  +C6*x  )| <= 2
3455682Smarkm * 	|    					               |
35178825Sdfr *
3655682Smarkm * 	               4     6     8     10    12     14
3755682Smarkm *	4. let r = C1*x +C2*x +C3*x +C4*x +C5*x  +C6*x  , then
3855682Smarkm *	       cos(x) ~ 1 - x*x/2 + r
3955682Smarkm *	   since cos(x+y) ~ cos(x) - sin(x)*y
4055682Smarkm *			  ~ cos(x) - x*y,
4155682Smarkm *	   a correction term is necessary in cos(x) and hence
4255682Smarkm *		cos(x+y) = 1 - (x*x/2 - (r - x*y))
4355682Smarkm *	   For better accuracy, rearrange to
4455682Smarkm *		cos(x+y) ~ w + (tmp + (r-x*y))
4555682Smarkm *	   where w = 1 - x*x/2 and tmp is a tiny correction term
4655682Smarkm *	   (1 - x*x/2 == w + tmp exactly in infinite precision).
47178825Sdfr *	   The exactness of w + tmp in infinite precision depends on w
4855682Smarkm *	   and tmp having the same precision as x.  If they have extra
4955682Smarkm *	   precision due to compiler bugs, then the extra precision is
5055682Smarkm *	   only good provided it is retained in all terms of the final
5155682Smarkm *	   expression for cos().  Retention happens in all cases tested
5255682Smarkm *	   under FreeBSD, so don't pessimize things by forcibly clipping
5355682Smarkm *	   any extra precision in w.
5455682Smarkm */
5555682Smarkm
5655682Smarkm#include "math.h"
5755682Smarkm#include "math_private.h"
5855682Smarkm
5955682Smarkmstatic const double
6055682Smarkmone =  1.00000000000000000000e+00, /* 0x3FF00000, 0x00000000 */
6155682SmarkmC1  =  4.16666666666666019037e-02, /* 0x3FA55555, 0x5555554C */
6255682SmarkmC2  = -1.38888888888741095749e-03, /* 0xBF56C16C, 0x16C15177 */
6355682SmarkmC3  =  2.48015872894767294178e-05, /* 0x3EFA01A0, 0x19CB1590 */
6455682SmarkmC4  = -2.75573143513906633035e-07, /* 0xBE927E4F, 0x809C52AD */
6555682SmarkmC5  =  2.08757232129817482790e-09, /* 0x3E21EE9E, 0xBDB4B1C4 */
6655682SmarkmC6  = -1.13596475577881948265e-11; /* 0xBDA8FAE9, 0xBE8838D4 */
6755682Smarkm
6855682Smarkmdouble
6955682Smarkm__kernel_cos(double x, double y)
7055682Smarkm{
7155682Smarkm	double hz,z,r,w;
7255682Smarkm
7355682Smarkm	z  = x*x;
7455682Smarkm	w  = z*z;
7555682Smarkm	r  = z*(C1+z*(C2+z*C3)) + w*w*(C4+z*(C5+z*C6));
7655682Smarkm	hz = 0.5*z;
7755682Smarkm	w  = one-hz;
7855682Smarkm	return w + (((one-w)-hz) + (z*r-x*y));
7955682Smarkm}
8055682Smarkm