It is currently Mon Mar 25, 2019 12:39 am

 All times are UTC - 7 hours

 Page 2 of 2 [ 23 posts ] Go to page Previous  1, 2
 Print view Previous topic | Next topic
Author Message
 Post subject: Re: Compact /10 and %10 operations?Posted: Mon Sep 10, 2018 11:48 am

Joined: Thu Mar 31, 2016 11:15 am
Posts: 506
I just use 100 byte lookup tables.

Top

 Post subject: Re: Compact /10 and %10 operations?Posted: Mon Sep 10, 2018 12:01 pm

Joined: Fri Nov 12, 2004 2:49 pm
Posts: 7678
Location: Chexbres, VD, Switzerland
tokumaru wrote:
You'll only get automatic wrap around detection in one direction anyway (either when adding or subtracting).

You won't - automatic overflow detection would work only when using 0-9 or when using 246-255; the second case being less intuitive but not more optimal than the first.

Top

 Post subject: Re: Compact /10 and %10 operations?Posted: Mon Sep 10, 2018 7:17 pm
 Formerly ~J-@D!~

Joined: Sun Mar 12, 2006 12:36 am
Posts: 486
Location: Rive nord de Montréal
I see optimality in having numbers in tiles 246-255, at least for things like score points: you rarely substract points, if ever, in a game. From the look of it, you genuinely optimized your tile for scorekeeping. I find it actually clever.

_________________
((λ (x) (x x)) (λ (x) (x x)))

Top

 Post subject: Re: Compact /10 and %10 operations?Posted: Mon Sep 17, 2018 8:09 pm

Joined: Wed Aug 16, 2017 12:15 am
Posts: 50
pubby wrote:
I just use 100 byte lookup tables.

This division routine with remainder is quite fast and needs 100 bytes' worth of tables. (I didn't test it extensively.)

Code:
; Get the quotient and the remainder of A divided by 10.
; Needs 9 bytes and 14 cycles per operation and 100 bytes for the tables.

lsr  ; keep LSB in carry
tax
lda unsigned_divide_by_5,x
; now A = the quotient
lda unsigned_modulo_5,x
rol  ; get LSB from carry
; now A = the remainder

unsigned_divide_by_5:
.byte 0, 0, 0, 0, 0, 1, 1, 1, 1, 1
.byte 2, 2, 2, 2, 2, 3, 3, 3, 3, 3
.byte 4, 4, 4, 4, 4, 5, 5, 5, 5, 5
.byte 6, 6, 6, 6, 6, 7, 7, 7, 7, 7
.byte 8, 8, 8, 8, 8, 9, 9, 9, 9, 9

unsigned_modulo_5:
.byte 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
.byte 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
.byte 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
.byte 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
.byte 0, 1, 2, 3, 4, 0, 1, 2, 3, 4

Edit: optimized the modulo routine.
Edit: combined the routines.

Top

 Post subject: Re: Compact /10 and %10 operations?Posted: Tue Sep 18, 2018 4:13 am

Joined: Tue Oct 06, 2015 10:16 am
Posts: 902
Your division table is 50 bytes long. What happens when x >= 50?

Top

 Post subject: Re: Compact /10 and %10 operations?Posted: Tue Sep 18, 2018 5:43 am

Joined: Sat Feb 12, 2005 9:43 pm
Posts: 11243
Location: Rio de Janeiro - Brazil
calima wrote:
Your division table is 50 bytes long. What happens when x >= 50?

It won't ever be >= 50, because of the LSR. If the largest number is 99, X will be at most 49. By getting rid of the least significant bit (and putting it back later) the tables can be made half the size.

Top

 Post subject: Re: Compact /10 and %10 operations?Posted: Tue Sep 18, 2018 9:49 am

Joined: Tue Apr 04, 2017 1:22 pm
Posts: 37
Location: Ohio, USA
qalle wrote:
pubby wrote:
I just use 100 byte lookup tables.

This division routine with remainder is quite fast and needs 100 bytes' worth of tables. (I didn't test it extensively.)

Code:
; Get the quotient and the remainder of A divided by 10.
; Needs 9 bytes and 14 cycles per operation and 100 bytes for the tables.

lsr  ; keep LSB in carry
tax
lda unsigned_divide_by_5,x
; now A = the quotient
lda unsigned_modulo_5,x
rol  ; get LSB from carry
; now A = the remainder

unsigned_divide_by_5:
.byte 0, 0, 0, 0, 0, 1, 1, 1, 1, 1
.byte 2, 2, 2, 2, 2, 3, 3, 3, 3, 3
.byte 4, 4, 4, 4, 4, 5, 5, 5, 5, 5
.byte 6, 6, 6, 6, 6, 7, 7, 7, 7, 7
.byte 8, 8, 8, 8, 8, 9, 9, 9, 9, 9

unsigned_modulo_5:
.byte 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
.byte 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
.byte 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
.byte 0, 1, 2, 3, 4, 0, 1, 2, 3, 4
.byte 0, 1, 2, 3, 4, 0, 1, 2, 3, 4

Edit: optimized the modulo routine.
Edit: combined the routines.

Wow, that's a great solution. The same concept should apply when dividing by any number for which a power-of-two is a factor.
Like, dividing by 12 for example:

Code:
; Value from 0-143 in A.

lsr
rol temp
lsr
tax
lda unsigned_divide_by_3, x
; now A = the quotient
lda unsigned_modulo_3, x
rol
lsr temp
rol
; now A = the remainder

unsigned_divide_by_3:
.byte 0, 0, 0, 1, 1, 1, 2, 2, 2
.byte 3, 3, 3, 4, 4, 4, 5, 5, 5
.byte 6, 6, 6, 7, 7, 7, 8, 8, 8
.byte 9, 9, 9, 10, 10, 10, 11, 11, 11

unsigned_modulo_3:
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2
.byte 0, 1, 2, 0, 1, 2, 0, 1, 2

It requires an extra temporary variable (since we initially divide by 4), but it's still a neat concept.

EDIT: Also this website has a ton of math implementations for 6502.

_________________
http://zutanogames.com/ <-- my dev blog

Top

 Post subject: Re: Compact /10 and %10 operations?Posted: Tue Sep 18, 2018 10:33 am

Joined: Tue Oct 06, 2015 10:16 am
Posts: 902
Ah indeed, thanks for pointing that out.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 2 of 2 [ 23 posts ] Go to page Previous  1, 2

 All times are UTC - 7 hours

#### Who is online

Users browsing this forum: DRW and 4 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