/****************************************************************************/
/*	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 <misc/multiply.h>
#include <misc/bits.h>

/****************************************************************************/
/*					Public API functions									*/
/****************************************************************************/

/** @addtogroup misc_mult */
/** @{ */

/**
*	Prepares for a signed 32-bit division by a constant.
*
*	This function is the signed counterpart of the udiv32prep() function,
*	see the explanation there. The difference is that the divisor, which
*	is the argument of this function, as well as the divident and quotient,
*	which are the argument and return value of the sdiv32() function,
*	respectively, are signed 32 bit integers.
*
*	@param	div		The value that you want to divide with.
*					It must <b>not</b> be 0 or 0x80000000. If it is, then
*					the returned value will be such that sdiv32() will
*					return 0 regardless of its actual argument.
*
*	@return			A 64-bit object that can be passed to sdiv32() so that it
*					will divide its argument by <tt>div</tt>.
*/

long long sdiv32prep( int div )
{
unsigned	s, l, m;

	if ( ! div || div == (int) 0x80000000 ) return( 0 );

	s = 0;

	if ( div < 0 ) {

		s = 0x80000000;
		div = -div;
	}

	l = 32 - ldzcnt( div - 1 );
	l = l ? l : 1;
	m = 1 << ( l - 1 );
	m = ( (unsigned long long) m << 32 ) / div;
	return( ( (long long) m << 32 ) | ( s + l - 1 ) );
}

/**
*	Signed 32-bit division by a constant.
*
*	This function divides its second argument by a constant and returns the
*	quotient. The divisor constant is determined by the first argument and
*	must have been obtained by a call to the sdiv32prep() function.
*
*	@param	divisor		The 64-bit data structure calculated by the
*						sdiv32prep() function, representing a 32-bit divisor.
*
*	@param	dividend	The 32-bit number to divide.
*
*	@return				The 32-bit quotient.
*/

int		sdiv32( long long divisor, int dividend );

/** @} */


