@ libgcc1 routines for ARM cpu.
@ Division routines, written by Richard Earnshaw, (rearnsha@armltd.co.uk)
@ Adapted to TOPSY by Silvio Dragone & David Reist, this is a hack for TOPSY
	
/* Copyright (C) 1995, 1996, 1998, 2001 Free Software Foundation, Inc.

This file is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2, or (at your option) any
later version.

In addition to the permissions in the GNU General Public License, the
Free Software Foundation gives you unlimited permission to link the
compiled version of this file with other programs, and to distribute
those programs without any restriction coming from the use of this
file.  (The General Public License restrictions do apply in other
respects; for example, they cover modification of the file, and
distribution when not linked into another program.)

This file is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* As a special exception, if you link this library with other files,
   some of which are compiled with GCC, to produce an executable,
   this library does not by itself cause the resulting executable
   to be covered by the GNU General Public License.
   This exception does not however invalidate any other reasons why
   the executable file might be covered by the GNU General Public License.  */

/* Register functions:
 * dividend = a0
 * divisor  = a1
 * result   = a2
 * overdone = a2
 * curbit   = a3
 */	
	
#define RET	mov
#define RETc(x)	mov##x

#define TYPE(x) .type x,function

#include "asm.h"

.text
		
ENTRY(__udivsi3)
	TYPE 	(__udivsi3)

	cmp	a1, #0
	beq	Ldiv0a
	mov	a3, #1
	mov	a2, #0
	cmp	a0, a1
	bcc	Lgot_a2a
Loop1a:	
	@ Unless the a1 is very big, shift it up in multiples of
	@ four bits, since this is the amount of unwinding in the main
	@ division loop.  Continue shifting until the a1 is 
	@ larger than the a0.
	cmp	a1, #0x10000000
	cmpcc	a1, a0
	movcc	a1, a1, lsl #4
	movcc	a3, a3, lsl #4
	bcc	Loop1a

Lbignuma:
	@ For very big a1s, we must shift it a bit at a time, or
	@ we will be in danger of overflowing.
	cmp	a1, #0x80000000
	cmpcc	a1, a0
	movcc	a1, a1, lsl #1
	movcc	a3, a3, lsl #1
	bcc	Lbignuma

Loop3a:
	@ Test for possible subtractions, and note which bits
	@ are done in the a2.  On the final pass, this may subtract
	@ too much from the a0, but the a2 will be ok, since the
	@ "bit" will have been shifted out at the bottom.
	cmp	a0, a1
	subcs	a0, a0, a1
	orrcs	a2, a2, a3
	cmp	a0, a1, lsr #1
	subcs	a0, a0, a1, lsr #1
	orrcs	a2, a2, a3, lsr #1
	cmp	a0, a1, lsr #2
	subcs	a0, a0, a1, lsr #2
	orrcs	a2, a2, a3, lsr #2
	cmp	a0, a1, lsr #3
	subcs	a0, a0, a1, lsr #3
	orrcs	a2, a2, a3, lsr #3
	cmp	a0, #0			@ Early termination?
	movnes	a3, a3, lsr #4		@ No, any more bits to do?
	movne	a1, a1, lsr #4
	bne	Loop3a
Lgot_a2a:
	mov	r0, a2
	RET	pc, lr

Ldiv0a:
	str	lr, [sp, #-4]!
	bl	__div0
	mov	r0, #0			@ about as wrong as it could be
	ldmia	sp!, {pc}



	

ENTRY(__umodsi3)
	TYPE	(__umodsi3)

	cmp	a1, #0
	beq	Ldiv0b
	mov	a3, #1
	cmp	a0, a1
	RETc(cc)	pc, lr
Loop1b:
	@ Unless the a1 is very big, shift it up in multiples of
	@ four bits, since this is the amount of unwinding in the main
	@ division loop.  Continue shifting until the a1 is 
	@ larger than the a0.
	cmp	a1, #0x10000000
	cmpcc	a1, a0
	movcc	a1, a1, lsl #4
	movcc	a3, a3, lsl #4
	bcc	Loop1b

Lbignumb:
	@ For very big a1s, we must shift it a bit at a time, or
	@ we will be in danger of overflowing.
	cmp	a1, #0x80000000
	cmpcc	a1, a0
	movcc	a1, a1, lsl #1
	movcc	a3, a3, lsl #1
	bcc	Lbignumb

Loop3b:
	@ Test for possible subtractions.  On the final pass, this may 
	@ subtract too much from the a0, so keep track of which
	@ subtractions are done, we can fix them up afterwards...
	mov	a2, #0
	cmp	a0, a1
	subcs	a0, a0, a1
	cmp	a0, a1, lsr #1
	subcs	a0, a0, a1, lsr #1
	orrcs	a2, a2, a3, ror #1
	cmp	a0, a1, lsr #2
	subcs	a0, a0, a1, lsr #2
	orrcs	a2, a2, a3, ror #2
	cmp	a0, a1, lsr #3
	subcs	a0, a0, a1, lsr #3
	orrcs	a2, a2, a3, ror #3
	mov	ip, a3
	cmp	a0, #0			@ Early termination?
	movnes	a3, a3, lsr #4		@ No, any more bits to do?
	movne	a1, a1, lsr #4
	bne	Loop3b

	@ Any subtractions that we should not have done will be recorded in
	@ the top three bits of "overdone".  Exactly which were not needed
	@ are governed by the position of the bit, stored in ip.
	@ If we terminated early, because a0 became zero,
	@ then none of the below will match, since the bit in ip will not be
	@ in the bottom nibble.
	ands	a2, a2, #0xe0000000
	RETc(eq)	pc, lr				@ No fixups needed
	tst	a2, ip, ror #3
	addne	a0, a0, a1, lsr #3
	tst	a2, ip, ror #2
	addne	a0, a0, a1, lsr #2
	tst	a2, ip, ror #1
	addne	a0, a0, a1, lsr #1
	RET	pc, lr

Ldiv0b:
	str	lr, [sp, #-4]!
	bl	__div0
	mov	r0, #0			@ about as wrong as it could be
	ldmia	sp!, {pc}





ENTRY(__divsi3)
	TYPE	(__divsi3)

	eor	ip, a0, a1		@ Save the sign of the a2.
	mov	a3, #1
	mov	a2, #0
	cmp	a1, #0
	rsbmi	a1, a1, #0		@ Loops below use unsigned.
	beq	Ldiv0c
	cmp	a0, #0
	rsbmi	a0, a0, #0
	cmp	a0, a1
	bcc	Lgot_a2c

Loop1c:
	@ Unless the a1 is very big, shift it up in multiples of
	@ four bits, since this is the amount of unwinding in the main
	@ division loop.  Continue shifting until the a1 is 
	@ larger than the a0.
	cmp	a1, #0x10000000
	cmpcc	a1, a0
	movcc	a1, a1, lsl #4
	movcc	a3, a3, lsl #4
	bcc	Loop1c

Lbignumc:
	@ For very big a1s, we must shift it a bit at a time, or
	@ we will be in danger of overflowing.
	cmp	a1, #0x80000000
	cmpcc	a1, a0
	movcc	a1, a1, lsl #1
	movcc	a3, a3, lsl #1
	bcc	Lbignumc

Loop3c:
	@ Test for possible subtractions, and note which bits
	@ are done in the a2.  On the final pass, this may subtract
	@ too much from the a0, but the a2 will be ok, since the
	@ "bit" will have been shifted out at the bottom.
	cmp	a0, a1
	subcs	a0, a0, a1
	orrcs	a2, a2, a3
	cmp	a0, a1, lsr #1
	subcs	a0, a0, a1, lsr #1
	orrcs	a2, a2, a3, lsr #1
	cmp	a0, a1, lsr #2
	subcs	a0, a0, a1, lsr #2
	orrcs	a2, a2, a3, lsr #2
	cmp	a0, a1, lsr #3
	subcs	a0, a0, a1, lsr #3
	orrcs	a2, a2, a3, lsr #3
	cmp	a0, #0			@ Early termination?
	movnes	a3, a3, lsr #4		@ No, any more bits to do?
	movne	a1, a1, lsr #4
	bne	Loop3c
Lgot_a2c:
	mov	r0, a2
	cmp	ip, #0
	rsbmi	r0, r0, #0
	RET	pc, lr

Ldiv0c:
	str	lr, [sp, #-4]!
	bl	__div0
	mov	r0, #0			@ about as wrong as it could be
	ldmia	sp!, {pc}




	
ENTRY(__modsi3)
	TYPE	(__modsi3)

	mov	a3, #1
	cmp	a1, #0
	rsbmi	a1, a1, #0		@ Loops below use unsigned.
	beq	Ldiv0d
	@ Need to save the sign of the a0, unfortunately, we need
	@ ip later on; this is faster than pushing lr and using that.
	str	a0, [sp, #-4]!
	cmp	a0, #0
	rsbmi	a0, a0, #0
	cmp	a0, a1
	bcc	Lgot_a2d

Loop1d:
	@ Unless the a1 is very big, shift it up in multiples of
	@ four bits, since this is the amount of unwinding in the main
	@ division loop.  Continue shifting until the a1 is 
	@ larger than the a0.
	cmp	a1, #0x10000000
	cmpcc	a1, a0
	movcc	a1, a1, lsl #4
	movcc	a3, a3, lsl #4
	bcc	Loop1d

Lbignumd:
	@ For very big a1s, we must shift it a bit at a time, or
	@ we will be in danger of overflowing.
	cmp	a1, #0x80000000
	cmpcc	a1, a0
	movcc	a1, a1, lsl #1
	movcc	a3, a3, lsl #1
	bcc	Lbignumd

Loop3d:
	@ Test for possible subtractions.  On the final pass, this may 
	@ subtract too much from the a0, so keep track of which
	@ subtractions are done, we can fix them up afterwards...
	mov	a2, #0
	cmp	a0, a1
	subcs	a0, a0, a1
	cmp	a0, a1, lsr #1
	subcs	a0, a0, a1, lsr #1
	orrcs	a2, a2, a3, ror #1
	cmp	a0, a1, lsr #2
	subcs	a0, a0, a1, lsr #2
	orrcs	a2, a2, a3, ror #2
	cmp	a0, a1, lsr #3
	subcs	a0, a0, a1, lsr #3
	orrcs	a2, a2, a3, ror #3
	mov	ip, a3
	cmp	a0, #0			@ Early termination?
	movnes	a3, a3, lsr #4		@ No, any more bits to do?
	movne	a1, a1, lsr #4
	bne	Loop3d

	@ Any subtractions that we should not have done will be recorded in
	@ the top three bits of "overdone".  Exactly which were not needed
	@ are governed by the position of the bit, stored in ip.
	@ If we terminated early, because a0 became zero,
	@ then none of the below will match, since the bit in ip will not be
	@ in the bottom nibble.
	ands	a2, a2, #0xe0000000
	beq	Lgot_a2d
	tst	a2, ip, ror #3
	addne	a0, a0, a1, lsr #3
	tst	a2, ip, ror #2
	addne	a0, a0, a1, lsr #2
	tst	a2, ip, ror #1
	addne	a0, a0, a1, lsr #1
Lgot_a2d:
	ldr	ip, [sp], #4
	cmp	ip, #0
	rsbmi	a0, a0, #0
	RET	pc, lr

Ldiv0d:
	str	lr, [sp, #-4]!
	bl	__div0
	mov	r0, #0			@ about as wrong as it could be
	ldmia	sp!, {pc}


	
ENTRY(__div0)
	TYPE	(__div0)

	mov	pc, lr


