/****************************************************************************/
/*	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 or 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 <nanu-nanu.h>

/** @addtogroup nanu */
/** @{ */

/**
*	Compares the two top elements of the stack.
*
*	Compares the topmost two elements of the stack and returns a flag whether
*	the specified test was true or not. The stack remains unchanged. The
*	following comparisons are supported:
*	- BC_NANU_GT returns true if the second element is larger than the topmost
*	- BC_NANU_GE returns true if the second element is larger than or equal
*				 to the topmost
*	- BC_NANU_EQ returns true if the second element is equal to the topmost
*	- BC_NANU_NE returns true if the second element is not equal to the topmost
*	- BC_NANU_LE returns true if the second element is smaller than or equal
*				 to the topmost
*	- BC_NANU_LT returns true if the second element is smaller than the topmost
*
*
*	@param	nanu			The natural number machine state descriptor
*	@param	test			The comparison to perform
*
*	@retval 1				The test was successful and the result is 'true'
*	@retval 0				The test was successful and the result is 'false'
*	@retval	BC_NANU_SUND	The stack didn't have at least two items on it
*	@retval	BC_NANU_SIZE	The test parameter was not a valid test
*
*	@par Stack image
*		a b ...  ==> a b ...
*/

int		BC_NanuComp( BC_NANU nanu, int test )
{
int			v1, v2, l1, l2;
int			cp;

	if ( ( v1 = _bc_NanuStkSee( nanu, 0 ) ) < 0 )

		return( _bc_NanuError( nanu, v1 ) );

	if ( ( v2 = _bc_NanuStkSee( nanu, 1 ) ) < 0 )

		return( _bc_NanuError( nanu, v2 ) );

	l1 = _bc_NanuVarLen( nanu, v1 );
	l2 = _bc_NanuVarLen( nanu, v2 );

	if ( l1 != l2 ) {

		if ( l1 < l2 )

			cp = -1;
		else
			cp = +1;
	}
	else {

		cp = _bc_NanuComp( _bc_NanuVarPtr( nanu, v1 ),
						   _bc_NanuVarPtr( nanu, v2 ), l1 );
	}

	switch ( test ) {

		case BC_NANU_LT:	return( cp <  0 );
		case BC_NANU_LE:	return( cp <= 0 );
		case BC_NANU_EQ:	return( cp == 0 );
		case BC_NANU_NE:	return( cp != 0 );
		case BC_NANU_GE:	return( cp >= 0 );
		case BC_NANU_GT:	return( cp >  0 );
		default:			return( BC_NANU_SIZE );
	}
}

/** @} */

