V7/usr/src/libc/crt/aldiv.s
/ Long quotient
.globl aldiv
.globl csv, cret
aldiv:
jsr r5,csv
mov 8.(r5),r3
sxt r4
bpl 1f
neg r3
1:
cmp r4,6.(r5)
bne hardldiv
mov 4(r5),r1
mov 2(r1),r2
mov (r1),r1
bge 1f
neg r1
neg r2
sbc r1
com r4
1:
mov r4,-(sp)
clr r0
div r3,r0
mov r0,r4 /high quotient
mov r1,r0
mov r2,r1
div r3,r0
bvc 1f
sub r3,r0 / this is the clever part
div r3,r0
tst r1
sxt r1
add r1,r0 / cannot overflow!
1:
mov r0,r1
mov r4,r0
tst (sp)+
bpl 9f
neg r0
neg r1
sbc r0
9:
mov 4.(r5),r2
mov r0,(r2)+
mov r1,(r2)
jmp cret
/ The divisor is known to be >= 2^15 so only 16 cycles are needed.
hardldiv:
clr -(sp)
mov 4.(r5),r0
mov 2(r0),r2
mov (r0),r1
bpl 1f
com (sp)
neg r1
neg r2
sbc r1
1:
clr r0
mov 6.(r5),r3
bge 1f
neg r3
neg 8.(r5)
sbc r3
com (sp)
1:
mov $16.,r4
1:
clc
rol r2
rol r1
rol r0
cmp r3,r0
bgt 3f
blt 2f
cmp 8.(r5),r1
blos 2f
3:
sob r4,1b
br 1f
2:
sub 8.(r5),r1
sbc r0
sub r3,r0
inc r2
sob r4,1b
1:
mov r2,r1
clr r0
tst (sp)+
beq 1f
neg r0
neg r1
sbc r0
1:
mov 4.(r5),r2
mov r0,(r2)+
mov r1,(r2)
jmp cret