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

/**
*	Multiplies a double with an integer power of 2.
*
*	The function takes the float-point number \p x and the integer \p p
*	and returns <tt>x * 2<sup>p</sup></tt>.
*
*	@param	x	The double to be multiplied
*	@param	p	The integer power of 2
*
*	@retval	NaN		if x was NaN
*	@retval	0.0		if x was 0 or if p is such a large negative number that
*					the result can not be represented in a double
*	@retval	INF		if x was infinity or if p is such a large positive number
*					that the result can not be represented in a double
*	@retval	x*2^p	otherwise
*/

double	ldexp( double x, int p )
{
unsigned long long	m, t;
int					e, s;

	if ( isnan( x ) || isinf( x ) || ! x || ! p ) return( x );

	m = _blm_dtoull( x );
	e = ( ( m >> 52 ) & 0x7ff );
	s = m >> 63;
	m = m & ( ( 1ull << 52 ) - 1 );

	if ( ! e ) {

		// This is a denormalised number. Normalise it,
		// but delete the implicit bit.

		while ( ! ( m & ( 1ull << 53 ) ) ) {

			m <<= 1;
			e--;
		}

		m &=  ~( 1ull << 53 );
	}

	if ( p > 2046 - e ) {

		// The result overflows, return infinity

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

	if ( p < -e - 51 ) {

		// The result underflows, return (signed) 0

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

	e += p;

	if ( e < 0 ) {

		// We have a denormalised result. We have to shift it to the right.
		// and we have to properly round it. We round according to the
		// round to nearest rules.

		m  |= 1ull << 53;
		t   = m << ( 64 + e );
		m >>= -e;
		e   = 0;

		// Now m is the shifted new mantissa and p is whatever we shifted
		// out, shifted up so that {m,t} form a 128 bit number. We have
		// also set e to 0, which is the correct exponent for denormalised
		// numbers. So, the rounding bits are: t & ( 1<< 63) is G,
		// t & (1<<62) is R and t & ( ( 1 << 62 ) - 1) is S.

		if ( t >> 63 ) {

			// G is set, we do wound. Check if we round up.

			if ( ( m & 1 ) || ( t << 1 ) ) {

				// Rounding up.

				m++;

				// If after the rounding we have a 52nd bit, we have to
				// make it a normalised number after all

				if ( m & ( 1ull << 53 ) ) {

					m &= ~( 1ull << 53 );
					e++;
				}
			}
		}
	}

	// Put it together and return it

	return( _blm_ulltod( ( (unsigned long long) s << 63 ) |
						 ( (unsigned long long) e << 52 ) | m ) );
}

/** @} */

