/****************************************************************************/
/*	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 <chips/ARM/func.h>

/*
*	Division by constant. At entry R0 contains the two shift amounts,
*	sh2 in bits 8-1 and sh1 in bit 0. The multiplier is in R1 and R2
*	is the number to divide. We do the following:
*
*	unsigned	udiv32( int sh1, int sh2, unsigned mul, unsigned x )
*	{
*	unsigned	t;
*
*		t = ( (unsigned long long) x * mul ) >> 32;
*		return( ( t + ( ( x - t ) >> sh1 ) ) >> sh2 );
*	}
*
*	The math details of the algorithm and the way sh1, sh2 and mul are
*	calculated can be found in the article:
*
*	Torbjörn Granlund, Peter L. Montgomery:
*	Division by Invariant Integers using Multiplication
*/

	afunc	udiv32
	umull	r1,r3,r1,r2
	movs	r0,r0,lsr #1
	subcs	r2,r2,r3
	addcs	r3,r3,r2,lsr #1
	mov		r0,r3,lsr r0
	bx		lr

