Fastest Divide by 6

The place for codemasters or beginners to talk about programming any language for the Spectrum.
User avatar
Nomad
Manic Miner
Posts: 600
Joined: Thu Dec 28, 2017 12:38 pm

Re: Fastest Divide by 6

Post by Nomad » Mon Feb 05, 2018 3:08 pm

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. :lol: 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. :lol:
0 x

Jonathan
Berk
Posts: 7
Joined: Sun Feb 18, 2018 6:55 pm

Re: Fastest Divide by 6

Post by Jonathan » Sun Feb 18, 2018 7:12 pm

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
0 x

User avatar
RMartins
Manic Miner
Posts: 331
Joined: Thu Nov 16, 2017 3:26 pm

Re: Fastest Divide by 6

Post by RMartins » Sun Feb 18, 2018 11:31 pm

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.
0 x

Wall_Axe
Microbot
Posts: 164
Joined: Mon Nov 13, 2017 11:13 pm

Re: Fastest Divide by 6

Post by Wall_Axe » Mon Feb 19, 2018 6:00 pm

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
0 x

User avatar
RMartins
Manic Miner
Posts: 331
Joined: Thu Nov 16, 2017 3:26 pm

Re: Fastest Divide by 6

Post by RMartins » Mon Feb 19, 2018 7:20 pm

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)
0 x

Wall_Axe
Microbot
Posts: 164
Joined: Mon Nov 13, 2017 11:13 pm

Re: Fastest Divide by 6

Post by Wall_Axe » Mon Feb 19, 2018 9:00 pm

ohh thanks
0 x

antoniovillena
Berk
Posts: 10
Joined: Sat Dec 09, 2017 9:07 pm

Re: Fastest Divide by 6

Post by antoniovillena » Mon Feb 19, 2018 10:01 pm

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
1 x

Post Reply