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

#ifndef _LIBM_H
#define _LIBM_H

static unsigned long long _blm_dtoull( double d ) __attribute__ ((const));
static unsigned int _blm_ftoul( float d ) __attribute__ ((const));
static double _blm_ulltod( unsigned long long l ) __attribute__ ((const));
static float _blm_ultof( unsigned l ) __attribute__ ((const));

/*
*	Changes the type of an object from double to long long
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	This function simply takes a double, i.e. a 64 bit object and
*	returns a long long, an other 64 bit object that will have the
*	same bit representation as the double had.
*	Depending on whether you compile for EABI or ELF format, the word order
*	of a double is different. Thus, for EABI targets this function does
*	absolutely nothing, while for ELF targets it swaps the 32-bit words in
*	the 64-bit datum.
*
*	We use this function because if we use the customary union spoofing then
*	gcc generates superfluous memory access instructions. We can't use the
*	other trick, the i = *((unsigned long long *) &d) either, because there
*	is a bug in gcc versions prior to 4.3.0 that makes the compiler to generate
*	totally erroneous code out of that.
*/

static __inline__ unsigned long long _blm_dtoull( double d )
{
unsigned long long l;

	__asm__ volatile ( "\n" : "=r" (l) : "0" (d) );
#ifndef __ARM_EABI__
	return( ( l >> 32 ) | ( l << 32 ) );
#else
	return( l );
#endif
}

/*
*	Changes the type of an object from float to int
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*	The same thing as for the _blm_dtoull but for float numbers.
*/

static __inline__ unsigned int _blm_ftoul( float f )
{
unsigned int l;

	__asm__ volatile ( "\n" : "=r" (l) : "0" (f) );
	return( l );
}

/*
*	Changes a long long int to a double, the opposite of _blm_dtoull()
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

static __inline__ double _blm_ulltod( unsigned long long l )
{
double d;

#ifndef __ARM_EABI__
	l = ( l >> 32 ) | ( l << 32 );
#endif
	__asm__ volatile ( "\n" : "=r" (d) : "0" (l) );
	return( d );
}

/*
*	Changes an unsigned int to a float, the opposite of _blm_ftoul()
*	~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
*/

static __inline__ float _blm_ultof( unsigned l )
{
float	f;

	__asm__ volatile ( "\n" : "=r" (f) : "0" (l) );
	return( f );
}

/*
*	Range reduction for trigonometric functions
*/

double	_blm_PayneHanek( double, int * );
float	_blm_CodyWaite( float, int * );

/****************************************************************************/
/* 			The rest is only for documentation purposes						*/
/****************************************************************************/

#ifdef __DOXYGEN

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

/**
*	Square root function.
*
*	The function calculates the square root of its argument. If the
*	argument is NaN or negative, NaN is returned. Negative 0 is accepted
*	and the returned value is positive 0. If the argument is positive
*	infinity, then positive infinity is returned. Otherwise, the square
*	root of the argument is returned.
*
*	@param	x	The number of which the square root is to be calculated
*
*	@retval	NaN		The argument was NaN or a non-zero negative number
*	@retval	+INF	The argument was positive infinity
*	@retval	+0		The argument was +0 or -0
*	@retval sqrt(x)	Otherwise
*/

double sqrt( double x );

/**
*	Square root function.
*
*	The function calculates the square root of its argument. If the
*	argument is NaN or negative, NaN is returned. Negative 0 is accepted
*	and the returned value is positive 0. If the argument is positive
*	infinity, then positive infinity is returned. Otherwise, the square
*	root of the argument is returned.
*
*	@param	x	The number of which the square root is to be calculated
*
*	@retval	NaN		The argument was NaN or a non-zero negative number
*	@retval	+INF	The argument was positive infinity
*	@retval	+0		The argument was +0 or -0
*	@retval sqrt(x)	Otherwise
*/

float sqrtf( float x );

/** @} */

#endif

#endif

