[A83] Re: 1 byte division routine


[Prev][Next][Index][Thread]

[A83] Re: 1 byte division routine




Hey, I just found a shorter version on my HDD:

Fast multiplication and division:

Mul_A_DE:		; HL=A*DE, where A is unsigned and 
DE is signed, in less than 200 cycles!
 ld hl,0
 rra
 jp nc,$+4
 add hl,de
 sla e
 rl d
 rra
 jp nc,$+4
 add hl,de
 sla e
 rl d
 rra
 jp nc,$+4
 add hl,de
 sla e
 rl d
 rra
 jp nc,$+4
 add hl,de
 sla e
 rl d
 rra
 jp nc,$+4
 add hl,de
 sla e
 rl d
 rra
 jp nc,$+4
 add hl,de
 sla e
 rl d
 rra
 jp nc,$+4
 add hl,de
 rra
 ret nc
 add hl,de		; Faster than shifting and adding 
only once
 add hl,de
 ret

Div_Signed_BC_D:	; Division with BC treated as a 
signed integer
 ld a,$f1		; RET is replaced with POP AF
 ld (Div_Ret),a
 ld a,b
 rla
 push af
 jp nc,Div_BC_D
 xor a
 sub c
 ld c,a
 ld a,0
 sbc a,b
 ld b,a
Div_BC_D:		; HL=BC/D (unsigned, D<128) - in 
about 1000 cycles!
 xor a			; Can be called separately as well 
(unlike the core of signed
 ld h,a			; multiplication above)
 ld l,a
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
#include "math_d.inc"
 sla c			; If you substitute this part with 
another include, A will hold the remainder
 rl b
 rla
 cp d
 ccf
 rl l
 rl h
Div_Ret:
 ret
 ld a,$c9		; RET is restored
 ld (Div_Ret),a
 ret nc
 xor a
 sub l
 ld l,a
 ld a,0
 sbc a,h
 ld h,a
 ret

and math_d.inc:

 sla c
 rl b
 rla
 cp d
 push af
 ccf
 rl l
 rl h
 pop af
 jp c,$+4
 sub d

The good side of this division is that its speed is
practically independent of how big the numbers are.
The binary weights are more important.

PG






References: