Page 1 of 2
Re: Fastest Divide by 6
Posted: Mon Feb 05, 2018 3:08 pm
by Nomad
Kweepa wrote: ↑Mon Feb 05, 2018 2:42 pm
Ooh, interesting! I went straight to Amazon to buy that book... then decided that maybe the pdf was good enough. $95.60?!
That was my thought also.. I don't mind supporting authors when they are not wringing every dollar they can from you.
That is my argument with a lot of academics - they want to be published but how many people ever get to read what they write? Probably a handful of libraries that are on grants buy these expensive books and its read by at most a few hundred people.
If they published with a regular publisher (assuming it was good enough to be picked up) or even if it was self published it could be released for a reasonable about and get a much wider circulation. There are tons of books like that just gathering dust with interesting approaches that could be useful but they are never going to be read by anyone that would actually put it to practical use its a great shame.
Springer are a good example, they have a computer science series that runs into the 7000 volume mark now but nobody I know outside of a university has heard of it, But then things have started to change recently because these publishers were forced to move into the 21 century and give subscribers a better deal.
anyway rant over.
Re: Fastest Divide by 6
Posted: Sun Feb 18, 2018 7:12 pm
by Jonathan
For the CPC version of AGD I just had to knock together a quick and simple routine to divide a screen coordinate held in the accumulator by 5. The routine I wrote was this:
Code: Select all
; Divide y coordinate by 5 to give block position.
div5 push bc ; store bc.
ld b,0 ; reset result.
cp 80 ; more than 80?
jr c,div5a ; no.
sub 80 ; subtract 80.
set 4,b ; set bit for 16.
div5a cp 40 ; more than 40?
jr c,div5b ; no.
sub 40 ; subtract 40.
set 3,b ; set bit for 8.
div5b cp 20 ; more than 20?
jr c,div5c ; no.
sub 20 ; subtract 20.
set 2,b ; set bit for 4.
div5c cp 10 ; more than 10?
jr c,div5d ; no.
sub 10 ; subtract 10.
set 1,b ; set bit for 2.
div5d cp 5 ; more than 5?
jr c,div5e ; no.
inc b ; set bit for 1.
div5e ld a,b ; get result.
pop bc ; restore bc.
ret
Re: Fastest Divide by 6
Posted: Sun Feb 18, 2018 11:31 pm
by RMartins
If I'm not mistaken, that's the usual way to make a divison, bit by bit, except that in this case, it's optimized to match division by 5, since consecutive multiples of 5 are being checked.
Re: Fastest Divide by 6
Posted: Mon Feb 19, 2018 6:00 pm
by Wall_Axe
why is the number being compared to 10,20,40?
for divide by 6 i was thinking, first see if the number is less than six, if yes,return zero
if no, add one to the result
then take away six from the number
then shift right until the 1st bit is set...so you are reducing the number down to the smallest
then just take away six until you are left with a number less than six
and adjust the answer with how many times you shifted right
Re: Fastest Divide by 6
Posted: Mon Feb 19, 2018 7:20 pm
by RMartins
Wall_Axe wrote: ↑Mon Feb 19, 2018 6:00 pm
why is the number being compared to 10,20,40?
...
Those are just multiples of 5, that match the specific bit that will be set, if condition holds true.
40 / 5 = 8 (bit3)
20 / 5 = 4 (bit2)
10 / 5 = 2 (bit1)
Re: Fastest Divide by 6
Posted: Mon Feb 19, 2018 9:00 pm
by Wall_Axe
ohh thanks
Re: Fastest Divide by 6
Posted: Mon Feb 19, 2018 10:01 pm
by antoniovillena
That's faster:
Code: Select all
LD H, 0
LD D, H ; DE = HL
LD E,A
ADD A,A
LD L,A
ADD HL, HL ; x04
ADD HL, DE ; x05
ADD HL, HL ; x10
ADD HL, HL ; x20
ADD HL, DE ; x21
ADD HL, HL ; x42
ADD HL, DE ; x43
Ast A. Moore wrote: ↑Sun Dec 31, 2017 6:52 pm
Well, if your range is up to 77, then this will be 3 T states faster (dividend goes into A):
Code: Select all
LD H, 0
LD D, H ; DE = HL
ld e,a
add a,a
ld l,a
ADD HL, HL ; x04
ADD HL, DE ; x05
ADD HL, HL ; x10
ADD HL, DE ; x11
ADD HL, HL ; x22
ADD HL, HL ; x44
SBC HL, DE ; x43