It is currently Fri Feb 22, 2019 5:10 am

 All times are UTC - 7 hours

 Page 1 of 1 [ 1 post ]
 Print view Previous topic | Next topic
Author Message
 Post subject: A fast 16 bit divide by 240 (~80 cycles)Posted: Fri Jan 12, 2018 6:17 pm

Joined: Fri Nov 24, 2017 2:40 pm
Posts: 130
Related thread with a much faster 12 bit divide:
viewtopic.php?f=2&t=16911

So it was nagging at me that there must be a way to get a full 16 bit divide** by 240 using the trick I did in the last thread. (The idea is basically the same as this: http://blog.jgc.org/2012/03/how-to-divide-by-9-really-really-fast.html) I finally came up with something, and it seems to work correctly! (though has only been mildly tested) It's not nearly as fast as the 12 bit version, maybe not even fast enough to be practical when you can just keep separate world space and tile wrapped scroll counters. Either way, it's still many times faster than a generic 16/8 bit division routine.

** Since it's intended to be used for setting scroll registers, I don't bother calculating the upper 4 bits of the quotient. The assumption is that you'll be discarding everything but the LSB anyway.

Code:
; 16 bit unsigned divide by 240.
; Returns the lower nibble of the quotient in x, and the remainder in a.
; Conceptually similar to http://blog.jgc.org/2012/03/how-to-divide-by-9-really-really-fast.html, but in base 16.
.proc div240_quick16
; Dividend is split into nibbles, (A, B, C, D)
; Because 240/16 = 15, we can simplify the problem to dividing ABC by 15.
; Then at the end, we push D in as the low bits of the remainder.

A_B = sreg + 1
C_D = sreg + 0
tmp = A_B ; A_B is read only once, reuse it for scratch.
; x stores the quotient

.code
ldx A_B
txa
lsr
lsr
lsr
lsr ; acc = A
sta tmp
txa
and #\$F ; acc = B
clc
sta tmp ; A + B

; A + B is also the lower few bits of the quotient
; (minus some carry bits from the remainder that happen later)
tax

lda C_D
lsr
lsr
lsr
lsr ; acc = C
clc
adc tmp ; acc = A + B + C

; Divide acc by 15, carry the quotient into x.
; acc is in the range [0, 45], so divide using an unrolled loop.
; Repeating twice is good enough for dividends up to 0xEFFF.
; Could save a few cycles here by rearranging comparisons I guess.
.repeat 2
cmp #15
bcc :+
sbc #15
inx
:
.endrepeat

; acc now has the upper nibble of the remainder and needs to be combined with D.
; Could save 2 cycles with a 4 bit look up table I guess.
asl
asl
asl
asl
sta tmp
lda C_D
and #\$F
ora tmp

rts
.endproc

edit: Some cleanup and comments. Hopefully easier to understand now.

Last edited by slembcke on Fri Jan 12, 2018 7:16 pm, edited 2 times in total.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 1 post ]

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: No registered users and 6 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ NES / Famicom    NESdev    NESemdev    NES Graphics    NES Music    Homebrew Projects       2018 NESdev Competition       2017 NESdev Competition       2016 NESdev Competition       2014 NESdev Competition       2011 NESdev Competition    Newbie Help Center    NES Hardware and Flash Equipment       Reproduction    NESdev International       FCdev       NESdev China       NESdev Middle East Other    General Stuff    Membler Industries    Other Retro Dev       SNESdev       GBDev    Test Forum Site Issues    phpBB Issues    Web Issues    nesdevWiki