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

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

/**
*	Changes a signum of a double to that of an other double.
*
*	The returned value will have the same absolute value as the first
*	argument and the same signum as the second argument. If the first
*	argument is NaN, then the returned value will be NaN, if the second
*	argument is NaN, then the behaviour is undefined.
*
*	@param	a	The number of the absolute value is used
*	@param	b	The number of which the signum is used
*
*	@return		A value that has the absolute value of \p a and
*				the signum of \p b.
*/

double	copysign( double a, double b )
{
unsigned long long	x;

	// What you have to do is simple. You have a 64-bit datum
	// and you have to set its most significant bit to the
	// most significant bit of an other 64-bit datum. That's all.
	// The stuff below should compile to exactly 4 ARM instructions
	// or to about 6-8 THUMB instructions. Now gcc 4.4.0 seems to
	// spend a helluva lot of time trying to work out if you attempt
	// to dereference a NULL pointer and creating code to punish you
	// if you did, even if it is a perfectly legit operation on your
	// hardware. Apparently, the time spent there was taken from the
	// code generation time, because instead of 4 instructions the
	// assembly is 17 instructions long, including saving registers
	// on the stack, loading 0 to a register and then OR that zero
	// with an other register, loading 0xffffffff to a register and
	// the AND-ing it with an other register and moving registers
	// around for no apparent reason. Admittedly, 17 ARM instructions
	// is actually a great improvement over 4.0.2's 36 instructions,
	// all of which you do really need if you want to justify the 72
	// bytes stack usage in a function that should compile to this:
	//
	// 		tst		r3,r3
	//		orrmi	r1,r1,#0x80000000
	//		bicpl	r1,r1,#0x80000000
	//		bx		lr
	//
	// Mind you, it's understandable. ARM is an obscure target, it's
	// only been around for maybe a quarter of a century and is not an
	// important architecture considering that it merely runs practically
	// every PDA, mobile phone, MP3 player and handheld game console on
	// the planet, so who cares...

	x = _blm_dtoull( a );

	if ( (long long) _blm_dtoull( b ) < 0 )

		return( _blm_ulltod( x | ( 1ull << 63 ) ) );
	else
		return( _blm_ulltod( x & ~ ( 1ull << 63 ) ) );
}

/** @} */

