ENTRY(__do_div64)
@ Test for easy paths first.
subs ip, r4,
bls 9f @ divisor is 0 or 1
tst ip, r4
beq 8f @ divisor is power of 2
@ See if we need to handle upper 32-bit result.
cmp xh, r4
mov yh,
blo 3f
@ Align divisor with upper part of dividend.
@ The aligned divisor is stored in yl preserving the original.
@ The bit position is stored in ip.
clz yl, r4
clz ip, xh
sub yl, yl, ip
mov ip,
mov ip, ip, lsl yl
mov yl, r4, lsl yl
mov yl, r4
mov ip,
1: cmp yl,
cmpcc yl, xh
movcc yl, yl, lsl
movcc ip, ip, lsl
bcc 1b
@ The division loop for needed upper bit positions.
@ Break out early if dividend reaches 0.
2: cmp xh, yl
orrcs yh, yh, ip
subcss xh, xh, yl
movnes ip, ip, lsr
mov yl, yl, lsr
bne 2b
@ See if we need to handle lower 32-bit result.
3: cmp xh,
mov yl,
cmpeq xl, r4
movlo xh, xl
movlo pc, lr
@ The division loop for lower bit positions.
@ Here we shift remainer bits leftwards rather than moving the
@ divisor for comparisons, considering the carry-out bit as well.
mov ip,
4: movs xl, xl, lsl
adcs xh, xh, xh
beq 6f
cmpcc xh, r4
5: orrcs yl, yl, ip
subcs xh, xh, r4
movs ip, ip, lsr
bne 4b
mov pc, lr
@ The top part of remainder became zero. If carry is set
@ (the 33th bit) this is a false positive so resume the loop.
@ Otherwise, if lower part is also null then we are done.
6: bcs 5b
cmp xl,
moveq pc, lr
@ We still have remainer bits in the low part. Bring them up.
clz xh, xl @ we know xh is zero here so...
add xh, xh,
mov xl, xl, lsl xh
mov ip, ip, lsr xh
7: movs xl, xl, lsl
mov ip, ip, lsr
bcc 7b
@ Current remainder is now 1. It is worthless to compare with
@ divisor at this point since divisor can not be smaller than 3 here.
@ If possible, branch for another shift in the division loop.
@ If no bit position left then we are done.
movs ip, ip, lsr
mov xh,
bne 4b
mov pc, lr
8: @ Division by a power of 2: determine what that divisor order is
@ then simply shift values around
clz ip, r4
rsb ip, ip,
mov yl, r4
cmp r4,
mov ip,
movhs yl, yl, lsr
movhs ip,
cmp yl,
movhs yl, yl, lsr
addhs ip, ip,
cmp yl,
movhs yl, yl, lsr
addhs ip, ip,
cmp yl,
addhi ip, ip,
addls ip, ip, yl, lsr
mov yh, xh, lsr ip
mov yl, xl, lsr ip
rsb ip, ip,
ARM( orr yl, yl, xh, lsl ip )
THUMB( lsl xh, xh, ip )
THUMB( orr yl, yl, xh )
mov xh, xl, lsl ip
mov xh, xh, lsr ip
mov pc, lr
@ eq -> division by 1: obvious enough...
9: moveq yl, xl
moveq yh, xh
moveq xh,
moveq pc, lr
@ Division by 0:
str lr, [sp,
bl __div0
@ as wrong as it could be...
mov yl,
mov yh,
mov xh,
ldr pc, [sp],
ENDPROC(__do_div64)