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

/*
*	Assembly equivalent of the following C code:
*
*	unsigned _bc_NanuAdd2( unsigned *r, unsigned *a, unsigned *b, unsigned l )
*	{
*	unsigned long long t;
*
*		for ( t = 0 ; l-- ; t >>= 32 ) {
*
*			t += *a++;
*			t += *b++;
*			*r++ = t;
*		}
*
*		return( t );
*	}
*
*	Alas, gcc generates horribly inefficient code from the above, so we
*	resort to assembly here.
*/

	afunc	_bc_NanuAdd2
	stmdb	sp!,{r4}
	mov		r12,#0
	b		1f
0:
	movs	r12,r12,lsl #1			// CY is set to MSB of R12
	ldr		r4,[r1],#4				// R4  = *a++
	ldr		r12,[r2],#4				// R12 = *b++
	adcs	r12,r12,r4				// R12 = LO(R4+R12+CY), CY=HI
	str		r12,[r0],#4				// Store low word of the result
	mov		r12,r12,rrx				// Move the carry into the MSB of R12
1:
	subs	r3,r3,#1
	bpl		0b
	mov		r0,r12,lsr #31			// R12 MSB is the carry out, make it retval
	ldmia	sp!,{r4}
	bx		lr
	.endfunc

/*
*	Assembly equivalent of the following C code:
*
*	unsigned _bc_NanuAdd1( unsigned *r, unsigned *a, unsigned s, unsigned l )
*	{
*	unsigned long long t;
*
*		for ( t = s ; l-- ; t >>= 32 ) {
*
*			t += *a++;
*			*r++ = t;
*		}
*
*		return( t );
*	}
*
*	Alas, gcc generates horribly inefficient code from the above, so we
*	resort to assembly here.
*/

	afunc	_bc_NanuAdd1
	stmdb	sp!,{r4}
	mov		r12,#0
	b		1f
0:
	ldr		r4,[r1],#4
	adds	r2,r2,r4
	str		r2,[r0],#4
	adc		r2,r12,r12
1:
	subs	r3,r3,#1
	bpl		0b
	mov		r0,r2
	ldmia	sp!,{r4}
	bx		lr
	.endfunc

