## Compact /10 and %10 operations?

**Moderator:** Moderators

### Re: Compact /10 and %10 operations?

I just use 100 byte lookup tables.

### Re: Compact /10 and %10 operations?

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.tokumaru wrote:You'll only get automatic wrap around detection in one direction anyway (either when adding or subtracting).

- Jarhmander
- Formerly ~J-@D!~
**Posts:**511**Joined:**Sun Mar 12, 2006 12:36 am**Location:**Rive nord de Montréal

### Re: Compact /10 and %10 operations?

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

### Re: Compact /10 and %10 operations?

This division routine with remainder is quite fast and needs 100 bytes' worth of tables. (I didn't test it extensively.)pubby wrote:I just use 100 byte lookup tables.

Code: Select all

```
; Start with 0-99 in A.
; 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: combined the routines.

### Re: Compact /10 and %10 operations?

Your division table is 50 bytes long. What happens when x >= 50?

### Re: Compact /10 and %10 operations?

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.calima wrote:Your division table is 50 bytes long. What happens when x >= 50?

### Re: Compact /10 and %10 operations?

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.qalle wrote:This division routine with remainder is quite fast and needs 100 bytes' worth of tables. (I didn't test it extensively.)pubby wrote:I just use 100 byte lookup tables.

Edit: optimized the modulo routine.Code: Select all

`; Start with 0-99 in A. ; 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: combined the routines.

Like, dividing by 12 for example:

Code: Select all

```
; 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
```

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

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

### Re: Compact /10 and %10 operations?

Ah indeed, thanks for pointing that out.