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

/**
*	Extracts the signed integral and fractional parts of a number.
*
*	If the argument \p x is NaN or infinite, then NaN is returned and
*	the location pointed by the \p i argument is untouched. Otherwise
*	the integer part (as obtained by the trunc() function) of \p x
*	is stored at the location pointed by \p i and \p x minus the stored
*	value (that is, the fractional part of the number) is returned.
*	The signum of the integral and fractional parts is the same as
*	that of the \p x argument, as long as the value is nonzero.
*	If either the stored or the returned value is 0, then the signum
*	of that 0 is not guaranteed to match the signum of x.
*
*	@param	x	The number to decompose to integer and fractional parts
*	@param	i	Pointer to a location where the integer part will be
*				stored. If the pointer is NULL, then the integer part
*				will be discarded.
*
*	@return		NaN if the \p x argument was NaN or infinite, the signed
*				fractional part of \p x otherwise.
*/

double	modf( double x, double *i )
{
unsigned long long	m;
unsigned			e;

	m = _blm_dtoull( x );
	e = ( ( m >> 52 ) & 0x7ff );

	if ( e == 0x7ff ) return( nan( 0 ) );

	if ( e > 0x432 ) {

		if ( i ) *i = x;
		return( 0.0 );
	}

	if ( e < 0x3ff ) {

		if ( i ) *i = 0.0;
		return( x );
	}

	m >>= 0x433 - e;
	m <<= 0x433 - e;
	if ( *i ) *i = _blm_ulltod( m );
	return( x - _blm_ulltod( m ) );
}

/** @} */

