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

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

/** @addtogroup ieee */
/** @{ */

/**
*	Truncate the number to the next integer towards zero.
*
*	The function returns a double that represents the integer
*	that has an absolute value not larger than the that of the
*	argument and is closest to its argument. That is, it simply
*	discards all fractional bits of the argument.
*	The function returns the argument if it was NaN or its absolute
*	value was larger than 2<sup>53</sup>; returns positive 0 if the absolute
*	value of the argument was less than 1.0 and the truncated value
*	otherwise.
*
*	@param	x		The number to be truncated
*	@return			The truncated value
*/

double	trunc( double x )
{
unsigned long long	m;
unsigned			e;

	m = _blm_dtoull( x );
	e = ( ( m >> 52 ) & 0x7ff );
	if ( e > 0x432 ) return( x );
	if ( e < 0x3ff ) return( 0.0 );
	m >>= 0x433 - e;
	m <<= 0x433 - e;
	return( _blm_ulltod( m ) );
}

/** @} */

