/****************************************************************************/
/*	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 prototypes										*/
/****************************************************************************/

static	double	tancot( double x, int m );

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

/*
*	Coefficients for a Chebyshev approximation of tan().
*	The even coeffs are all 0, they are omitted from the table.
*/

static const double coeff[] = {

	+7.853981633974483096282022398515e-01,
	+1.614910243765540370807810843790e-01,
	+3.984631312354480508458706884689e-02,
	+9.948727524594100601163610894662e-03,
	+2.486838559840725525291804188024e-03,
	+6.216983859125451336219750873846e-04,
	+1.554353146068683984579418208300e-04,
	+3.881396952699252740382900072127e-05,
	+9.832164901698211186020007055402e-06,
	+2.194479249162384349057810086701e-06,
	+9.359429957574623889686029664986e-07,
	-1.712451611031913974253695046192e-07,
	+2.518297947062819730438747302414e-07,
	-7.856689686983978996846549061534e-08,
	+2.074308830627794956626133461370e-08,
};

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

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

/**
*	Tangent function.
*
*	@param	x		The value to calculate the tangent of, in radians
*	@retval	NaN		The argument was NaN.
*	@retval INF		The argument was very close to (but not exactly at) a value
*					of PI/2 + k * PI where k is an integer, the signum of the
*					infinity depends on which side of the singularity was x.
*	@retval	tg(x)	Otherwise
*/

double	tan( double x )
{
	return( tancot( x, 0 ) );
}

/**
*	Cotangent function.
*
*	@param	x		The value to calculate the cotangent of, in radians
*	@retval	NaN		The argument was NaN
*	@retval INF		The argument was very close to, but not exactly at, a value
*					of k * PI where k is an integer, the signum of the infinity
*					depends on which side of the singularity was x.
*
*	@retval  ctg(x)	Otherwise
*
*	@note
*		Since the IEEE-754 doubles can not represent irrational numbers,
*		the argument to this function can never be an exact multiple of
*		PI. Therefore, the function never returns NaN for a finite argument.
*		There is one exception, namely 0. The 0.0 value is indeed representable
*		in IEEE-754 and thus this function could return a NaN for that.
*		However, this implementation returns + or - infinity, depending on
*		the sign of the zero argument. If you rely on the function returning
*		NaN on 0, you have to put a little wrapper around it.
*/

double	cot( double x )
{
	return( tancot( x, 1 ) );
}

/** @} */

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

/*
*	Calculates tan() or cot()
*	~~~~~~~~~~~~~~~~~~~~~~~~~
*	If the 'm' argument is 0, then it works out tan(), if it is 1,
*	then the returned value is cot(). Not other value should be used.
*/

static	double	tancot( double x, int m )
{
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 negative in that case.

	if ( o < 0 ) return( x );

	// The following part uses a couple of identities related to the
	// tangent function. x represents the argument reduced to 0-360 deg,
	// while z represents x further reduced to 0-45 deg. Then:
	//
	//	  0 <= x <  45		tan( x ) = +tan( z );
	//	 45 <= x <  90		tan( x ) = 1 / +tan( 45 - z );
	//	 90 <= x < 135		tan( x ) = 1 / -tan( z );
	//  135 <= x < 180		tan( x ) = -tan( 45 - z );
	//	180 <= x < 225		tan( x ) = +tan( z );
	//	225 <= x < 270		tan( x ) = 1 / +tan( 45 - z );
	//	270 <= x < 315		tan( x ) = 1 / -tan( z );
	//	315 <= x < 360		tan( x ) = -tan( 45 - z );
	//
	//	  0 <= x <  45		cot( x ) = 1 / +tan( z );
	//	 45 <= x <  90		cot( x ) = +tan( 45 - z );
	//	 90 <= x < 135		cot( x ) = -tan( z );
	//  135 <= x < 180		cot( x ) = 1 / -tan( 45 - z );
	//	180 <= x < 225		cot( x ) = 1 / +tan( z );
	//	225 <= x < 270		cot( x ) = +tan( 45 - z );
	//	270 <= x < 315		cot( x ) = -tan( z );
	//	315 <= x < 360		cot( x ) = 1 / -tan( 45 - z );
	//
	//	In our own representation 45 degrees corresponds to 1 and the
	// octant tells us which identity to use.
	// For octants 2, 3, 6 and 7 we have to change the signum of the result.

	if ( o & 2 ) s = ! s;

	// For octants 1, 3, 5 and 7 we have to transform the argument,
	// z' = 45 - z. For all other cases z' = z.

	if ( o & 1 ) x = 1.0 - x;

	// Now evaluate the polynom, i.e. calculate tan( z' )

	x = x * polynom( x * x, coeff, 14 );

	// For octants 1, 2, 5 and 6 we have to transform the result for tangent.
	// For cotangent, it is for octants 0, 3, 4, 7. Note that the two sets
	// only differ in their LSB.

	o ^= m;

	if ( o == 1 || o == 2 || o == 5 || o == 6 ) x = 1.0 / x;

	// Just return the signed result

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

