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

#define	PI		3.141592653589793238512808959406186204433f
#define	PI2		1.570796326794896619256404479703093102216f

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

/*
*	Chebyshev approximation coefficients
*/

static const float poly[] = {

	+1.6666657639575913044571905e-01,
	+7.5005713810246336719841920e-02,
	+4.4508709498660264338354333e-02,
	+3.1857188894046587333982144e-02,
	+1.4295256038051056585478716e-02,
	+3.7677138341546115691643682e-02
};

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

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

/**
*	Arcus sine function.
*
*	@param	x		The value of which the arcus sine should be calculated
*	@retval	NaN		The argument was NaN or outside of [-1.0,+1.0].
*	@retval	asin(x)	Otherwise. The returned value is in radian and in the
*					principal range of [-PI/2,+PI/2]
*/

float	asinf( float x )
{
int		s;

	if ( isnan( x ) || x < -1 || x > +1 ) return( nanf( 0 ) );

	s = 0;

	if ( x < 0 ) {

		x = -x;
		s = 1;
	}

	if ( x <= 0.5f ) {

		x = polynomf( x * x, poly, 5 ) * x + x;
	}
	else {

		x = sqrtf( ldexpf( 1 - x, -1 ) );
		x = PI2 - ldexpf( polynomf( x * x, poly, 5 ) * x + x, 1 );
	}

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

/**
*	Arcus cosine function.
*
*	@param	x		The value of which the arcus cosine should be calculated
*	@retval	NaN		The argument was NaN or outside of [-1.0,+1.0].
*	@retval	acos(x)	Otherwise. The returned value is in radian and in the
*					principal range of [0,PI]
*/

float	acosf( float x )
{
int		s;

	if ( isnan( x ) || x < -1 || x > +1 ) return( nanf( 0 ) );

	s = 0;

	if ( x < 0 ) {

		x = -x;
		s = 1;
	}

	if ( x <= 0.5f ) {

		x = PI2 - ( polynomf( x * x, poly, 5 ) * x + x );
	}
	else {

		x = sqrtf( ldexpf( 1 - x, -1 ) );
		x = ldexpf( polynomf( x * x, poly, 5 ) * x + x, 1 );
	}

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

/** @} */

