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

/*
*	Fixpoint square root routine.
*
*	The input is a 32-bit number in 1.31 format. It must be less than
*	2.0000. The result is in the same format.
*	If you want to know how it works, look at sqrt.S, that contains an
*	explanation of the algorithm. (Note that sqrtf.S uses a completely
*	different algorithm, so don't look at that one.)
*/

	afunc	sqrt32
	movs	r12,r0
	movmi	r0,r0,lsr #2
	mov		r1,#0
	mov		r2,#0x20000000
0:
	.set	ct,0
	.rept	6
	add		r3,r1,r2,lsr #ct
	subs	r0,r0,r3
	addcs	r1,r3,r2,lsr #ct
	addcc	r0,r0,r3
	add		r0,r0,r0
	.set	ct,ct+1
	.endr
	movs	r2,r2,lsr #6
	bne		0b
	tst		r12,r12
	movmi	r0,r1,lsl #1
	movpl	r0,r1
	bx		lr

