Copied versions of dv[iu].s from ../../m68k2/libem.

This commit is contained in:
ceriel 1995-12-05 15:38:55 +00:00
parent ddc1751296
commit f7157ca24c
2 changed files with 154 additions and 57 deletions

View file

@ -5,40 +5,96 @@
.sect .bss .sect .bss
! signed long divide ! signed long divide
!-----------------------------------------------------------------------------
! rewritten by Kai-Uwe Bloem (i5110401@dbstu1.bitnet) for speed.
! #1 01/12/90 initial revision. Minor reduce of shift operations.
! #2 03/07/90 use 68000 divu instruction whereever possible. This change
! makes #1 superflous. (derived from my GNU division routine)
!-----------------------------------------------------------------------------
! Some common cases can be handled in a special, much faster way :
! 1) divisor = 0
! => cause trap, then return to user. Result is undefined
! 2) dividend < divisor
! => quotient = 0, remainder = dividend
! 3) divisor < 0x10000 ( i.e. divisor is only 16 bits wide )
! => quotient and remainder can be calculated quite fast by repeated
! application of 68000 divu operations (ca. 400 cycles)
! 4) otherwise (due to #2, #3 dividend, divisor both wider then 16 bits)
! => do slow division by shift and subtract
!-----------------------------------------------------------------------------
! register usage:
! : d0 divisor
! d1 dividend
! exit : d1 quotient
! d2 remainder
.sect .text .sect .text
.dvi: .dvi:
move.l 4(sp),d2 move.l (sp)+,a1 ! return address
move.l 8(sp),d1 move.l (sp)+,d0 ! divisor
move.l d4,-(sp) move.l (sp)+,d2 ! dividend
move.l d3,a0 ! save d3
move.l d4,-(sp) ! save result sign register
clr.l d4 clr.l d4
tst.l d2 ! divisor tst.l d2
bpl 1f bpl 0f ! dividend is negative ?
neg.l d2 neg.l d2 ! yes - negate
not d4 not.l d4 ! and note negation in d4
1: 0:
tst.l d1 ! dividend tst.l d0
bpl 2f bpl 0f ! divisor is negative ?
neg.l d1 neg.l d0 ! yes - negate
not d4 not.w d4 ! note negation
swap d4 0:
not d4 clr.l d1 ! prepare quotient
swap d4 ! === case 1: divisor = 0
tst.l d0 ! divisor = 0 ?
beq 9f ! yes - divide by zero trap
! === case 2: dividend < divisor
cmp.l d0,d2 ! dividend < divisor ?
bcs 8f ! yes - division already finished
! === case 3: divisor <= 0x0ffff
cmp.l #0x0ffff,d0 ! is divisor only 16 bits wide ?
bhi 2f
move.w d2,d3 ! save dividend.l
clr.w d2 ! prepare dividend.h for divu operation
swap d2
beq 0f ! dividend.h is all zero, no divu necessary
divu d0,d2
0: move.w d2,d1 ! save quotient.h
swap d1
move.w d3,d2 ! divide dividend.l
divu d0,d2 ! (d2.h = remainder of prev divu)
move.w d2,d1 ! save qoutient.l
clr.w d2 ! get remainder
swap d2
bra 8f
! === case 4: divisor and dividend both > 0x0ffff
2: 2:
move.l d1,-(sp) move #32-1,d3 ! loop count
move.l d2,-(sp) 4:
jsr .dvu lsl.l #1,d2 ! shift dividend ...
tst d4 roxl.l #1,d1 ! ... into d1
beq 5f cmp.l d0,d1 ! compare with divisor
neg.l d1 ! quotient bcs 5f
sub.l d0,d1 ! bigger, subtract divisor
add #1,d2 ! note subtraction in result
5: 5:
tst.l d4 dbra d3,4b
bpl 6f exg d1,d2 ! get results in the correct registers
neg.l d0 ! remainder 8:
6: tst.w d4 ! quotient < 0 ?
move.l (sp)+,d4 bpl 0f
move.l (sp)+,a0 neg.l d1 ! yes - negate
add.l #8,sp 0: tst.l d4 ! remainder < 0 ?
move.l a0,-(sp) bpl 0f
rts neg.l d2
0: move.l (sp)+,d4 ! restore d4
move.l a0,d3 ! restore d3
jmp (a1)
.align 2 EIDIVZ = 6
9: move.w #EIDIVZ,-(sp)
jsr .trp

View file

@ -5,36 +5,77 @@
.sect .bss .sect .bss
! unsigned long divide ! unsigned long divide
!-----------------------------------------------------------------------------
! rewritten by Kai-Uwe Bloem (i5110401@dbstu1.bitnet) for speed.
! #1 01/12/90 initial revision. Minor reduce of shift operations.
! #2 03/07/90 use 68000 divu instruction whereever possible. This change
! makes #1 superflous. (derived from my GNU division routine)
!-----------------------------------------------------------------------------
! Some common cases can be handled in a special, much faster way :
! 1) divisor = 0
! => cause trap, then return to user. Result is undefined
! 2) dividend < divisor
! => quotient = 0, remainder = dividend
! 3) divisor < 0x10000 ( i.e. divisor is only 16 bits wide )
! => quotient and remainder can be calculated quite fast by repeated
! application of 68000 divu operations (ca. 400 cycles)
! 4) otherwise (due to #2, #3 dividend, divisor both wider then 16 bits)
! => do slow division by shift and subtract
!-----------------------------------------------------------------------------
! register usage: ! register usage:
! : d2 divisor ! : d0 divisor
! d1 dividend ! d1 dividend
! exit : d1 quotient ! exit : d1 quotient
! d0 remainder ! d2 remainder
.sect .text .sect .text
.dvu: .dvu:
move.l 4(sp),d2 move.l d3,a0 ! save d3
move.l 8(sp),d1 move.l (sp)+,a1 ! return address
move.l d3,-(sp) move.l (sp)+,d0 ! divisor
tst.l d2 move.l (sp)+,d2 ! dividend
bne 0f clr.l d1 ! prepare quotient
move.l #EIDIVZ,-(sp) ! === case 1: divisor = 0
jsr .trp tst.l d0 ! divisor = 0 ?
0: beq 9f ! yes - divide by zero trap
clr.l d0 ! === case 2: dividend < divisor
move.l #31,d3 cmp.l d0,d2 ! dividend < divisor ?
3: bcs 8f ! yes - division already finished
lsl.l #1,d1 ! === case 3: divisor <= 0x0ffff
roxl.l #1,d0 cmp.l #0x0ffff,d0 ! is divisor only 16 bits wide ?
cmp.l d2,d0 bhi 2f
blt 4f move.w d2,d3 ! save dividend.l
sub.l d2,d0 clr.w d2 ! prepare dividend.h for divu operation
add #1,d1 swap d2
beq 0f ! dividend.h is all zero, no divu necessary
divu d0,d2
0: move.w d2,d1 ! save quotient.h
swap d1
move.w d3,d2 ! divide dividend.l
divu d0,d2 ! (d2.h = remainder of prev divu)
move.w d2,d1 ! save qoutient.l
clr.w d2 ! get remainder
swap d2
bra 8f
! === case 4: divisor and dividend both > 0x0ffff
2:
move #32-1,d3 ! loop count
4: 4:
dbf d3,3b lsl.l #1,d2 ! shift dividend ...
move.l (sp)+,d3 roxl.l #1,d1 ! ... into d1
move.l (sp)+,a0 cmp.l d0,d1 ! compare with divisor
add.l #8,sp bcs 5f
move.l a0,-(sp) sub.l d0,d1 ! bigger, subtract divisor
rts add #1,d2 ! note subtraction in result
5:
dbra d3,4b
exg d1,d2 ! get results in the correct registers
8:
move.l a0,d3 ! restore d3
jmp (a1)
.align 2 EIDIVZ = 6
9: move.w #EIDIVZ,-(sp)
jsr .trp