Simple 8bit division routine
Re: Simple 8bit division routine
Interesting - thanks for investigating. Perhaps a single check for size and then incrementing would be a good bandaid.
- barrym95838
- Posts: 2056
- Joined: 30 Jun 2013
- Location: Sacramento, CA, USA
Re: Simple 8bit division routine
And sometimes a decent approximation is more than adequate for the job at hand. I like it.
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!
Mike B. (about me) (learning how to github)
Mike B. (about me) (learning how to github)
Re: Simple 8bit division routine
Here's an alternative form of the log function:
Code: Select all
8BIT_LOG2:
; Count leading zeroes
LDX #8
SEC
: DEX
ROL A
BCC :-
; Insert 3-bit LZ count at top of remainder
STX tmp
LSR tmp
ROR A
LSR tmp
ROR A
LSR tmp
ROR A
; Result in A, clobbered X
RTS
Re: Simple 8bit division routine
And here's an attempt at an antilog function, mostly just reversing the steps of the logarithm function.
Code: Select all
8b_AntiLog2:
; Extract 3-bit LZ count into X
STZ tmp
ASL A
ROL tmp
ASL A
ROL tmp
ASL A
ROL tmp
LDX tmp
; Shift mantissa down
BEQ :++
: LSR A
DEX
BNE :-
; Round up - result in A, clobbered X
: ADC #0
RTS
- barrym95838
- Posts: 2056
- Joined: 30 Jun 2013
- Location: Sacramento, CA, USA
Re: Simple 8bit division routine
GARTHWILSON wrote:
The worst areas were approximately $4E-$6D where adding one count to all these would generally improve accuracy, and $99-$DC where adding two counts would generally improve accuracy.
Code: Select all
...
cmp #$4E
adc #0
cmp #$99
adc #0
rts@Chromatix: does your algorithm provide similar results to Garth's (sorry, feeling a bit lazy here tonight)?
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!
Mike B. (about me) (learning how to github)
Mike B. (about me) (learning how to github)
- GARTHWILSON
- Forum Moderator
- Posts: 8775
- Joined: 30 Aug 2002
- Location: Southern California
- Contact:
Re: Simple 8bit division routine
Here's my effort at an 8-bit inverse LOG2 function. Input and output are in the accumulator.
Because of truncation errors in the LOG2 --> ALOG2 cycle, many of the outputs are less than the input (where ideally they'd be identical).
1 to $3F were correct.
From $40 to $7F, every second one was one count low.
From $80 to $FF, it went $80, 80, 80, 80, 84, 84, 84, 84, 8C, 8C, 8C, 8C, 90, 90 [...] F8, F8, F8, F8, FC, FC, FC, and FC.
Code: Select all
ALOG2: TAY ; (We'll need the input again later.)
AND #1FH ; Look at only the fractional part; but
ORA #20H ; it excludes the 1st '1', so add it.
ASL A ; Now scoot it to the left end to init.
ASL A
STA OUTPUT
TYA ; Get the original input back again.
loop: ADC # 00100000B ; (Note that C is already clear.) Add
BCS end ; 1 to the exponent part until it carries.
LSR OUTPUT ; As long as it doesn't carry, shift over
BRA loop ; and see if you can do it again.
end: LDA OUTPUT
RTS
;-------------Because of truncation errors in the LOG2 --> ALOG2 cycle, many of the outputs are less than the input (where ideally they'd be identical).
1 to $3F were correct.
From $40 to $7F, every second one was one count low.
From $80 to $FF, it went $80, 80, 80, 80, 84, 84, 84, 84, 8C, 8C, 8C, 8C, 90, 90 [...] F8, F8, F8, F8, FC, FC, FC, and FC.
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?
Re: Simple 8bit division routine
barrym95838 wrote:
@Chromatix: does your algorithm provide similar results to Garth's (sorry, feeling a bit lazy here tonight)?
The antilog function has a bug, which I need to fix - serves me right for doing it last thing at night without testing…