/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include <math.h>
#include <libm.h>

/****************************************************************************/
/*					Local definitions										*/
/****************************************************************************/

/*
*	Control flags
*/

#define	FLAG_INH		0			// Inherit the signum of the argument
#define	FLAG_INV		1			// Invert the signum of the argument
#define	FLAG_POS		2			// Set the signum to positive
#define	FLAG_NEG		3			// Set the signum to negative
#define	FLAG_FRW		0			// Octant interpreted forward
#define	FLAG_BCK		4			// Octant interpreted backward
#define	FLAG_SIN		0			// Use the sine core
#define	FLAG_COS		8			// Use the cosine core

/****************************************************************************/
/*					Local prototypes										*/
/****************************************************************************/

static double	evalcore( double x, const unsigned char *e );

/****************************************************************************/
/*					Local data												*/
/****************************************************************************/

/*
*	Coefficients for a Chebyshev approximation of sin()
*/

static const double sincoeff[] = {

	+7.853981633974482586707001330328e-01,
	-8.074551218827791905160995433732e-02,
	+2.490394570146897540777785663192e-03,
	-3.657620386131890280923242890704e-05,
	+3.133605048443902394411362643994e-07,
	-1.754797564972814743506122534473e-09,
	+4.107266995647920440219184078767e-12,
	+1.699418383793727116426452994347e-12,
	-4.223288385674095479771494865417e-13
};

/*
*	Coefficients for a Chebyshev approximation of cos()
*/

static const double coscoeff[] = {

	+1.000000000000000000000000000000e+00,
	-3.084251375340424861028372494065e-01,
	+1.585434424381550099018281171914e-02,
	-3.259918869225740397193628546980e-04,
	+3.590860406766894102709026870819e-06,
	-2.461121167444153896273431561199e-08,
	+1.146941971221898962850313018540e-10,
	-3.605011026539422774322744694029e-14,
	-2.052860805322684188382755969018e-13,
	+4.919295736477132632392289490977e-14
};

/*
*	Evaluation control table for sin()
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

static const unsigned char sintable[] = {

	FLAG_SIN | FLAG_INH | FLAG_FRW,			// +/-	  0 -  45
	FLAG_COS | FLAG_INH | FLAG_BCK,			// +/-	 45 -  90
	FLAG_COS | FLAG_INH | FLAG_FRW,			// +/-	 90 - 135
	FLAG_SIN | FLAG_INH | FLAG_BCK,			// +/-	135 - 180
	FLAG_SIN | FLAG_INV | FLAG_FRW,			// +/-	180 - 225
	FLAG_COS | FLAG_INV | FLAG_BCK,			// +/-	225 - 270
	FLAG_COS | FLAG_INV | FLAG_FRW,			// +/-	270 - 315
	FLAG_SIN | FLAG_INV | FLAG_BCK			// +/-	315 - 360
};

/*
*	Evaluation control table for cos()
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

static const unsigned char costable[] = {

	FLAG_COS | FLAG_POS | FLAG_FRW,			// +/-	  0 -  45
	FLAG_SIN | FLAG_POS | FLAG_BCK,			// +/-	 45 -  90
	FLAG_SIN | FLAG_NEG | FLAG_FRW,			// +/-	 90 - 135
	FLAG_COS | FLAG_NEG | FLAG_BCK,			// +/-	135 - 180
	FLAG_COS | FLAG_NEG | FLAG_FRW,			// +/-	180 - 225
	FLAG_SIN | FLAG_NEG | FLAG_BCK,			// +/-	225 - 270
	FLAG_SIN | FLAG_POS | FLAG_FRW,			// +/-	270 - 315
	FLAG_COS | FLAG_POS | FLAG_BCK			// +/-	315 - 360
};

/****************************************************************************/
/*					Public API functions									*/
/****************************************************************************/

/** @addtogroup func */
/** @{ */

/**
*	Sine function.
*
*	@param	x		The value to calculate the sine of, in radians
*	@retval	NaN		The argument was NaN or infinite
*	@retval	sin(x)	Otherwise
*/

double	sin( double x )
{
	return( evalcore( x, sintable ) );
}

/**
*	Cosine function.
*
*	@param	x		The value to calculate the cosine of, in radians
*	@retval	NaN		The argument was NaN or infinite
*	@retval	cos(x)	Otherwise
*/

double	cos( double x )
{
	return( evalcore( x, costable ) );
}

/** @} */

/****************************************************************************/
/*						Local functions										*/
/****************************************************************************/

/*
*	Evaluates the selected function
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

static double	evalcore( double x, const unsigned char *e )
{
int		o, s;

	// We need the signum of the argument

	s = signbit( x );

	// Do the range reduction

	x = _blm_PayneHanek( x, &o );

	// If it was an illegal value, return NaN. The range reduction
	// returns a NaN and sets the octant to <0 in that case.

	if ( o < 0 ) return( x );

	// Select the relevant table entry and calculate the result accordingly

	o = e[ o ];

	switch ( o & ~( FLAG_COS | FLAG_BCK ) ) {

		case FLAG_POS:	s = 0;	break;
		case FLAG_NEG:	s = 1;	break;
		case FLAG_INV:	s = !s;	break;
	}

	if ( o & FLAG_BCK )

		x = 1 - x;

	if ( o & FLAG_COS )

		x = polynom( x * x, coscoeff, 9 );
	else
		x = x * polynom( x * x, sincoeff, 8 );

	return( s ? -x : +x );
}

