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

/*
*	The function returns the number of 1 bits in a 32-bit word.
*
*	unsigned bitcnt( unsigned x )
*	{
*		x  = ( x & 0x55555555 ) + ( ( x >> 1 ) & 0x55555555 );
*		x  = ( x & 0x33333333 ) + ( ( x >> 2 ) & 0x33333333 );
*		x += x >> 4;
*		x &= 0x0f0f0f0f;
*		x += x >> 8;
*		x += x >> 16;
*		x &= 63;
*		return( x );
*	}
*/

	afunc	bitcnt
	mov		r3,#0x11
	orr		r3,r3,r3,lsl #8
	orr		r3,r3,r3,lsl #16
	orr		r1,r3,r3,lsl #2
	and		r2,r1,r0,lsr #1
	and		r0,r0,r1
	add		r0,r0,r2
	orr		r1,r3,r3,lsl #1
	and		r2,r1,r0,lsr #2
	and		r0,r0,r1
	add		r0,r0,r2
	add		r0,r0,r0,lsr #4
	and		r0,r0,#0x0000000f
	and		r0,r0,#0x00000f00
	and		r0,r0,#0x000f0000
	and		r0,r0,#0x0f000000
	add		r0,r0,r0,lsr #8
	add		r0,r0,r0,lsr #16
	and		r0,r0,#63
	bx		lr

