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

/**
*	Shifting of a number.
*
*	The function shifts the number on the top of the stack to the left
*	by the number of bits specified in the \p shift argument. If the
*	argument is negative, then the number is shifted to the right by
*	the absolute value of \p shift number of bits. Thus, this operation
*	is equivalent to<br>
*	<tt>TOS <- TOS * 2<sup>shift</sup></tt><br>
*	where \p TOS is the top of the stack.
*
*	@param	nanu			The natural number machine state descriptor
*	@param	shift			The number of left shifts (in bits), if negative
*							then shifting will be towards the right.
*
*	@retval	BC_NANU_NANU	Success
*	@retval	BC_NANU_SUND	The stack was empty
*	@retval	BC_NANU_OMEM	Out of memory
*	@retval BC_NANU_ITEM	Out of descriptors
*
*	@par Stack image
*		x ... ==> x*2<sup>shift</sup> ...
*/

int		BC_NanuShft( BC_NANU nanu, int shift )
{
const char 	zero = 0;
int			olen, owrd, ovar;
int			nlen, nwrd, nvar;
unsigned	*optr, *nptr;
unsigned	next, temp;

	// Get the current length of the number.
	// This catches the empty stack error.

	if ( ( olen = BC_NanuSize( nanu ) ) < 0 ) return( olen );

	// If we don't shift or the number is 0, then we have nothing else to do.

	if ( ! shift || ! olen ) return( BC_NANU_NANU );

	if ( shift > 0 ) {

		// Shift to the left. We have to allocate enough space for the
		// shifted version. Let's calculate the length of the final result

		nlen = olen + shift;

		// Get the old variable's handle and parameters

		ovar = _bc_NanuStkGet( nanu );
		owrd = _bc_NanuVarLen( nanu, ovar );
		optr = _bc_NanuVarPtr( nanu, ovar );
		temp = ( nlen + 31 ) / 32;

		// Create a new variable with the given length and get its parameters

		if ( ( nvar = _bc_NanuVarTmp( nanu, ovar, -1, temp ) ) < 0 )

			return( nvar );

		nwrd = _bc_NanuVarLen( nanu, nvar );
		nptr = _bc_NanuVarPtr( nanu, nvar );

		// Now we can start the shifting. Since the old and new variables
		// may overlap, we have to start the shifting from the high end.
		// Calculate the actual shift amount. If it is 0, then we just copy.

		if ( ! ( shift &= 31 ) ) {

			_bc_NanuMove( nptr + nwrd - owrd, optr, owrd );
			_bc_NanuZero( nptr, nwrd - owrd );
		}
		else {

			optr += owrd;
			nptr += nwrd;

			// Now we have two distinct cases. If the shifted version of the
			// most significant word of the old fits in 32 bits, then that is
			// our initial value. If it does not, then we do an extra store.

			temp = *--optr;
			next = temp >> ( 32 - shift );

			if ( next ) {

				*--nptr = next;
				nwrd--;
			}

			temp <<= shift;
			owrd--;
			nwrd--;

			while ( owrd ) {
				next = *--optr;
				*--nptr = temp | next >> ( 32 - shift );
				temp = next << shift;
				nwrd--;
				owrd--;
			}

			*--nptr = temp;
			_bc_NanuZero( nptr - nwrd, nwrd );
		}
	}
	else {

		// Shift to the right

		shift = -shift;

		if ( shift >= olen ) {

			// After the shift we'd have 0

			return( BC_NanuDrop( nanu ), BC_NanuPush( nanu, &zero, 1 ) );
		}

		// We really have to shift. Create the result variable

		nlen = olen - shift;
		temp = ( nlen + 31 ) / 32;
		next = shift >> 5;
		ovar = _bc_NanuStkGet( nanu );
		owrd = _bc_NanuVarLen( nanu, ovar );
		optr = _bc_NanuVarPtr( nanu, ovar ) + next;

		if ( ( nvar = _bc_NanuVarTmp( nanu, ovar, -1, temp ) ) < 0 )

			return( nvar );

		nwrd = _bc_NanuVarLen( nanu, nvar );
		nptr = _bc_NanuVarPtr( nanu, nvar );
		nlen = owrd - next;

		// At this moment owrd and nwrd are the length of the old and
		// new variables in words and nlen is the length of the final
		// result in words. Note that nwrd may be larger than nlen
		// and ovar and nvar might be the same value.

		if ( ! ( shift &= 31 ) ) {

			// We only copy

			_bc_NanuMove( nptr, optr, nlen );
			_bc_NanuZero( nptr + next, nwrd - nlen );
		}
		else {

			temp = *optr++ >> shift;

			while ( --nlen ) {

				next = *optr++;
				temp = temp | ( next << ( 32 - shift ) );
				*nptr++ = temp;
				temp = next >> shift;
				nwrd--;
			}

			if ( temp ) {

				*nptr++ = temp;
				nwrd--;
			}

			_bc_NanuZero( nptr, nwrd );
		}
	}

	_bc_NanuVarNrm( nanu, nvar );
	_bc_NanuStkPut( nanu, nvar );
	_bc_NanuVarDec( nanu, ovar );
	return( BC_NANU_NANU );
}

/** @} */

