Jump to content
brpocock

Fast divide by seven?

Recommended Posts

Simple thing that's killing me.

Any suggestions for a fast "divide by seven" routine?

HMPx/HMOVE whacks give either 7 or 8 clocks of motion. Divide by 8 is easy - bit shift - but division by seven is killing me. I can live with doing a dex / sbc loop, but that isn't nice.

Math and I are not friends.

Share this post


Link to post
Share on other sites
[quote name='brpocock' post='1368116' date='Tue Sep 11, 2007 1:16 AM']Any suggestions for a fast "divide by seven" routine?[/quote]

Rather than doing a divide-by-seven, I'd suggest doing something like:
[code] ldx displacement ; 0-159
lda smallmove,x
sta HMPx
...
sta HMOVE
lda bigmove,x
sta HMPx ; Use this value ten times
...
sta HMOVE
...
sta HMOVE
...
sta HMOVE
...
sta HMOVE ; etc. ten times total for the bigmove,x value[/code]
This way you only have two hmove values to worry about, and you always 'whack' things the same number of times.

Share this post


Link to post
Share on other sites
This is actually for the "judgement" code to decide sprite drawing-lists ... I have the amount of displacement between two sprites to be entered into the drawing list, and am judging the number of scanlines required to perform the move. Incidentally, this is including a RESPx "shortcut" that allows bumping the position effectively up to 48 clocks (see my blog for details).

I'm tinkering with pretending to move 8 clocks and then throwing in an extra count, which could taint the results, but it's just to decide the best slot in the list for a sprite, so I'm wondering if an approximation might work.

The actual motion code works out something like the above; I (sometimes) hit RESPx (at one of two fixed positions) and then whack HMOVE until I think the sprite is where I want it.

The judgement code rewrite has invalidated the sprite drawing in the kernel loop at the moment, so until I refactor that to match, this is all gut instinct.

Share this post


Link to post
Share on other sites
In the interests of getting it to Just Work At All, I'm dropping this logic actually, but thanks to you both for many useful suggestions.

My revised idea, rethinking a few things based on Thomas's comments on my blog, is to simplify the drawing list somewhat by only drawing a full sprite or not drawing it at all, which terribly simplifies the logic in the judgement routine, and the revised mess follows.

An ideal divide by seven would make the judgement code more accurate, but I'm being pessimistic and doing a divide by eight, and possibly ruling out some slots which otherwise could have held sprites, in favour of more reasonable execution times, and the fact that my local time is 3:30am and complex math is never my strong suit to begin with.

I suppose I could resort to a div7 table, as well :-( I think the valid range is only 0..49 or so, depending on how my RESPx hits end up aligning in the kernel, so at least it's a smallish table. Even smallish tables are starting to hurt me, though.

I'll dump some things to my blog in a moment, rather than polluting this abortive thread.

Share this post


Link to post
Share on other sites
Dividing by 7 is a bitch, and there's not going to be a fast and short way to do it. A table is the only way to do it in a kernel.

However, posting requests like this may get us 6502 coders thinking.

Here's a useless attempt. 40 cycles, 25 bytes. Works from 0-63 (64+ is possible with more shifting and adding)
[code] lda value
lsr
lsr
lsr
sta temp
lda value
and #%11111000
clc
sbc temp
clc
sbc value
eor #$ff
lsr
lsr
lsr
clc
adc temp[/code]

Share this post


Link to post
Share on other sites
[quote name='batari' post='1368152' date='Tue Sep 11, 2007 4:30 AM']Here's a useless attempt. 40 cycles, 25 bytes.[/quote]

OK, already better than my attempts. I'm still [i]mystified,[/i] where do you even get the ideas for these shortcuts?

Share this post


Link to post
Share on other sites
The divide by 15 modification I mentioned above[code] tay
and #$07
sta tmpVar
tya
lsr
lsr
lsr
tay
clc
adc tmpVar
.loop:
cmp #$07
bcc .skipIny
sbc #$07
iny
bne .loop

.skipIny:[/code]
Basically it divides by 8 and then corrects the result. Works from 0..255. Edited by Thomas Jentzsch

Share this post


Link to post
Share on other sites
I seem to recall that December 1984 issue of Apple Assembly line had a quirky routine for dividing any value up to 255 by 7 that appears to be faster than Batari's

[quote][ Hint: 1/7 = 1/8 + 1/64 + 1/512 + 1/4096 + ... ]

[code]DIVIDE_BY_SEVEN:
LDA BYTE
LSR
LSR
LSR
ADC BYTE
ROR
LSR
LSR
ADC BYTE
ROR
LSR
LSR
RTS[/code][/quote]

Share this post


Link to post
Share on other sites
Very nice. Here's the [url="http://homepage.mac.com/bobsc/aal/1984/aal8412.html#a5"]original article[/url]. It also includes a compact divide by three, and the infamous divide by 15. This technique can be adapted for all n in (2**n)-1, so variants would work for divide by 3, 7, 15, 31, and 63.

[code] DIVIDE.BY.THREE
LDA BYTE
LSR
LSR
ADC BYTE
ROR
LSR
ADC BYTE
ROR
LSR
ADC BYTE
ROR
LSR
ADC BYTE
ROR
LSR
RTS[/code]

Does anyone know any tricks like this for dividing by 5? Maybe some kind of (2**n)+1 equivalent? If you can divide by 5, you're only a shift away from dividing by 10. :cool:

Share this post


Link to post
Share on other sites
[quote name='TROGDOR' post='1661333' date='Sat Jan 17, 2009 4:05 AM']Very nice. Here's the [url="http://homepage.mac.com/bobsc/aal/1984/aal8412.html#a5"]original article[/url]. It also includes a compact divide by three, and the infamous divide by 15. This technique can be adapted for all n in (2**n)-1, so variants would work for divide by 3, 7, 15, 31, and 63.

[code] DIVIDE.BY.THREE
LDA BYTE
LSR
LSR
ADC BYTE
ROR
LSR
ADC BYTE
ROR
LSR
ADC BYTE
ROR
LSR
ADC BYTE
ROR
LSR
RTS[/code]

Does anyone know any tricks like this for dividing by 5? Maybe some kind of (2**n)+1 equivalent? If you can divide by 5, you're only a shift away from dividing by 10. :cool:[/quote]TO divide by 10, just do an LSR, CLC, ADC BYTE, then divide by 15.

Share this post


Link to post
Share on other sites
[quote name='batari' post='1661341' date='Sat Jan 17, 2009 2:42 AM']TO divide by 10, just do an LSR, CLC, ADC BYTE, then divide by 15.[/quote]
That's a good suggestion batari, but it has restrictions. That's (x * 1.5) / 15 = x / 10. However, it only works for values up to 170. 171 * 1.5 = 256.5, which would cause an overflow.

Share this post


Link to post
Share on other sites
I couldn't help but find great intrest in the topic and wonder if anyone has concidered using Vedic mathemetic routines to try and enhance the mathematic process on the atari? here is a link to some of the info and a bit on the types of processes involved Vedic Mathematics

The link is in the reading:



Multiply a two-figure number by 11
This couldn’t be easier — you simply put the sum of the first and last figure inbetween.

72 x 11 = 7 (7+2) 2 = 792

If the sum of the two figures is greater than 9 you simply carry the first figure of the sum and add it to to first figure of your number.

79 x 11 = 7 (7+9) 9 = 7 (16) 9 = 869

Dividing by 9
Here the answer for a two-figure number is the first figure as the result and the sum of the two figures the remainder.

23 / 9
result 2
remainder 2+3 = 5

For dividing three-figure numbers you need to add the sum of the first two numbers to the first figure and the total sum of the three numbers is your remainder.

134 / 9
result 1 (1+3) = 14
remainder (1+3+4) = 8

www.vedicmaths.org

Share this post


Link to post
Share on other sites
[quote name='roland p' post='1662580' date='Mon Jan 19, 2009 2:07 AM']Are there fast 16bit div 8bit routines?[/quote]
It doesn't have a 16-bit/8-bit routine, but here are some good references to [url="http://codebase64.org/doku.php?id=base:6502_6510_maths"]6502/6510 math routines[/url].
--Selgus

Share this post


Link to post
Share on other sites
[quote name='grafixbmp' post='1662591' date='Mon Jan 19, 2009 1:44 AM']I couldn't help but find great intrest in the topic and wonder if anyone has concidered using Vedic mathemetic routines to try and enhance the mathematic process on the atari? here is a link to some of the info and a bit on the types of processes involved Vedic Mathematics

The link is in the reading:



Multiply a two-figure number by 11
This couldn’t be easier — you simply put the sum of the first and last figure inbetween.

72 x 11 = 7 (7+2) 2 = 792

If the sum of the two figures is greater than 9 you simply carry the first figure of the sum and add it to to first figure of your number.

79 x 11 = 7 (7+9) 9 = 7 (16) 9 = 869

Dividing by 9
Here the answer for a two-figure number is the first figure as the result and the sum of the two figures the remainder.

23 / 9
result 2
remainder 2+3 = 5

For dividing three-figure numbers you need to add the sum of the first two numbers to the first figure and the total sum of the three numbers is your remainder.

134 / 9
result 1 (1+3) = 14
remainder (1+3+4) = 8

www.vedicmaths.org[/quote]

Well, thats interesting stuff, but seems like it would work best for people dealing with numerals on paper. Digits are not stored that way inside a computer.

Share this post


Link to post
Share on other sites
There is also another process a friend of mine introduced me to once, but can't quite remember what it was called. It was either pure hex or true hex or something to that effect. It might be more adept at being used on a 6502 since it deals with hex values. But while I collect some more info on the subject, here is a part of the processes used in vedic for multiplication. It may not work well for a processor but it is still nice to know. It deals with the fact that the math is done with base 10 and uses it as so...

Suppose you need 8 x 7

8 is 2 below 10 and 7 is 3 below 10.
Think of it like this:

The answer is 56.
The diagram below shows how you get it.

You subtract crosswise 8-3 or 7 - 2 to get 5,
the first figure of the answer.
And you multiply vertically: 2 x 3 to get 6,
the last figure of the answer.


I'll get back after I see what I can find on the hex stuff. Edited by grafixbmp

Share this post


Link to post
Share on other sites
I'm sure the multiplication topics will help someone, but in general, multiplication is easy. Division is not.

This is my first attempt at division by 10, which accounts for numbers larger an 170 by setting the MSB in two instances.
[code] lda byte
lsr
clc
adc byte
php
php
sta temp
ror
lsr
lsr
lsr
adc temp
ror
plp
bcc .1
ora #$80
.1
lsr
lsr
lsr
adc temp
ror
plp
bcc .2
ora #$80
.2
lsr
lsr
lsr[/code]
I'm sure this can be improved. Edited by batari

Share this post


Link to post
Share on other sites
My attempt at a fast divide by 10 (no branches, but trashes X) :-

[code] lda byte
lsr
lsr
lsr
lsr
sta temp
lsr
tax
clc
adc temp
sta temp
txa
lsr
tax
clc
adc temp
sta temp
txa
lsr
lsr
clc
adc temp[/code]

Share this post


Link to post
Share on other sites
[quote name='GroovyBee' post='1662968' date='Mon Jan 19, 2009 5:47 PM']My attempt at a fast divide by 10 (no branches, but trashes X) :-

[code] lda byte
lsr
lsr
lsr
lsr
sta temp
lsr
tax
clc
adc temp
sta temp
txa
lsr
tax
clc
adc temp
sta temp
txa
lsr
lsr
clc
adc temp[/code][/quote]
I just tried it, and it doesn't work for all values 0-255. For example, if byte=10, the result is zero.

Share this post


Link to post
Share on other sites
[quote name='batari' post='1662869' date='Mon Jan 19, 2009 2:18 PM']I'm sure the multiplication topics will help someone, but in general, multiplication is easy. Division is not.[/quote]

For medium-sized numbers, there's a huge difference. For really big numbers (crypto-sized), the difference isn't as significant, since the number of steps required to approximate 1/x to a certain number of bits is roughly proportional to the log of the number of bits; once one has a good estimate for the reciprocal of a dividend, the number of multiply/subtract operations is related to the amount of error in the approximated reciprocal.

I haven't looked to see to what extent this trick is used in RSA crypto implementations, but if it isn't used, it should be.

Share this post


Link to post
Share on other sites
Hello Batari,


I tried to improve the code you posted, and I think I've saved 5 cycles, and 1 register (comparing the two used to hold the processer status, vs a second temp that could be in the same location) at a cost of 2 bytes. Though I haven't fully tested the code (and everytime I do that it bites me in the ass!), but I've been trying this for hours now and I'm tired. I think it works.


[code];divide by 10
;numbers > 170 (straight thru) take 60 cycles, numbers <= 170 (branched) take 62 cycles

lda byte ; 3
lsr ; 2
clc ; 2
adc byte ; 3
sta temp2; 3
ror ; 2
lsr ; 2
sta temp1; 3
lsr ; 2
lsr ; 2
adc temp2; 3
ror ; 2
lsr ; 2
lsr ; 2
lsr ; 2
bit temp1; 3
bvc .skip; 2³
ora #$10 ; 2
.skip:
adc temp2; 3
ror ; 2
lsr ; 2
lsr ; 2
lsr ; 2
bit temp1; 3
bvc .exit; 2³
ora #$10 ; 2
.exit:[/code]


I've been trying for hours to somehow double up the code and save a bunch of bytes, but every time I do something takes way longer. I hope that someone can improve this code much more. There's got to be some sick way of doing this with some illegal ops or something. :lol:


Edited for a last minute fix too! Edited by Omegamatrix

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...