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

/**
*	Regular exponentiation.
*
*	Raises the second element on the stack to the power of the topmost
*	element, then discards both and pushes the result onto the stack.
*
*	@param	nanu			The natural number machine state descriptor
*
*	@retval	BC_NANU_NANU	The operation succeeded
*	@retval	BC_NANU_SUND	The stack didn't have at least two items on it
*	@retval	BC_NANU_SOVR	Stack overflow occured during calculations
*	@retval	BC_NANU_OMEM	Out of memory pool space
*
*	@par Stack image
*		e a ...  ==> a^e ...
*/

int		BC_NanuRexp( BC_NANU nanu )
{
unsigned char	one = 1;
int				ev, ec, fg;
unsigned int	wc, bc, el, nw;

	// Pop the exponent from the stack

	if ( ( ev = _bc_NanuStkGet( nanu ) ) < 0 )

		return( _bc_NanuError( nanu, ev ) );

	// If the number to exponentiate is 0 or 1 we just return
	// the number as it is. So, we push 1 and do a compare.

	if ( ( ec = BC_NanuPush( nanu, &one, 1 ) ) < 0 ) return( ec );
	if ( ( ec = BC_NanuComp( nanu, BC_NANU_GE ) ) < 0 ) return( ec );
	BC_NanuDrop( nanu );

	if ( ec == 1 ) {

		// Release the exponent and return

		_bc_NanuVarDec( nanu, ev );
		return( BC_NANU_NANU );
	}

	// Get the length of the exponent

	el = _bc_NanuVarLen( nanu, ev );
	fg = 0;

	// Check for the exponent being 0 or 1, which are special cases

	if ( el == 1 && *_bc_NanuVarPtr( nanu, ev ) == 0 ) {

		// We have to return 1

		_bc_NanuVarDec( nanu, ev );
		BC_NanuDrop( nanu );
		BC_NanuPush( nanu, &one, 1 );
		return( BC_NANU_NANU );
	}

	if ( el == 1 && *_bc_NanuVarPtr( nanu, ev ) == 1 ) {

		// We have to return the number itself

		_bc_NanuVarDec( nanu, ev );
		return( BC_NANU_NANU );
	}

	for ( wc = 0 ; wc < el ; wc++ ) {

		// Get the next word of the exponent

		nw = _bc_NanuVarPtr( nanu, ev )[ wc ];

		for ( bc = 33 ; --bc ; ) {

			if ( nw & 1 ) {

				// Do we already have a copy of the number ?

				if ( fg ) {

					// We already have a result. We have to
					// multiply it with the current power

					if ( ( ec = BC_NanuTuck( nanu ) ) ||
						 ( ec = BC_NanuRmul( nanu ) ) ||
						 ( ec = BC_NanuSwap( nanu ) ) ) {

						return( ec );
					}
				}
				else {

					// We don't have a result yet, make a copy of
					// the current power, that's our variable

					if ( ( ec = BC_NanuDupl( nanu ) ) ) {

						return( ec );
					}

					fg = 1;
				}
			}

			nw = nw >> 1;

			// If the word is 0 and this is the last word,
			// then we're done

			if ( ! nw && wc == el - 1 ) break;

			// Otherwise, square the power

			if ( ( ec = BC_NanuDupl( nanu ) ) ||
				 ( ec = BC_NanuRmul( nanu ) ) ) {

				return( ec );
			}
		}
	}

	// Get rid of the running power and the exponent, then return the result

	BC_NanuDrop( nanu );
	_bc_NanuVarDec( nanu, ev );
	return( BC_NANU_NANU );
}

/** @} */

