/****************************************************************************/
/*	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	float	tancot( float 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 float coeff[] = {

	+7.853982640485e-01,
	+1.614872919014e-01,
	+3.988596249406e-02,
	+9.765389928971e-03,
	+2.913392732152e-03,
	+1.099389159457e-04,
	+4.397510322798e-04
};

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

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

/**
*	Tangent function.
*
*	@param	x		The value to calculate the tangent of, in radians
*	@retval	NaN		The argument was NaN or outside of +/- 1E8.
*	@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
*/

float	tanf( float x )
{
	return( tancot( x, 0 ) );
}

/**
*	Cotangent function.
*
*	@param	x		The value to calculate the cotangent of, in radians
*	@retval	NaN		The argument was NaN or outside of +/- 1E8.
*	@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 floats 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.
*/

float	cotf( float 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().
*	For explanation, see tancot.c, the only difference is that we use
*	Cody-Waite reduction instead of Payne-Hanek and we have a 6th order
*	polynom instead of a 14th order one.
*/

static	float	tancot( float x, int m )
{
int		o, s;

	s = signbit( x );
	x = _blm_CodyWaite( x, &o );
	if ( o < 0 ) return( x );
	if ( o & 2 ) s = ! s;
	if ( o & 1 ) x = 1.0f - x;
	x = x * polynomf( x * x, coeff, 6 );
	o ^= m;
	if ( o == 1 || o == 2 || o == 5 || o == 6 ) x = 1.0f / x;
	return( s ? -x : +x );
}

