6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Apr 26, 2024 3:41 pm

All times are UTC




Post new topic Reply to topic  [ 22 posts ]  Go to page Previous  1, 2
Author Message
PostPosted: Sun Dec 20, 2020 10:13 am 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10793
Location: England
Interesting - thanks for investigating. Perhaps a single check for size and then incrementing would be a good bandaid.


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 20, 2020 3:26 pm 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1926
Location: Sacramento, CA, USA
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)


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 20, 2020 4:13 pm 
Offline

Joined: Mon May 21, 2018 8:09 pm
Posts: 1462
Here's an alternative form of the log function:
Code:
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


Top
 Profile  
Reply with quote  
PostPosted: Sun Dec 20, 2020 6:08 pm 
Offline

Joined: Mon May 21, 2018 8:09 pm
Posts: 1462
And here's an attempt at an antilog function, mostly just reversing the steps of the logarithm function.
Code:
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 21, 2020 3:24 am 
Offline
User avatar

Joined: Sun Jun 30, 2013 10:26 pm
Posts: 1926
Location: Sacramento, CA, USA
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.

Well, eight more bytes and eight more cycles isn't so bad, if you squint a little:
Code:
    ...
    cmp #$4E
    adc #0
    cmp #$99
    adc #0
    rts

... although, that's gonna push $DD and above a bit higher than you want (I didn't actually run your code, so I don't know for sure).

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


Top
 Profile  
Reply with quote  
PostPosted: Mon Dec 21, 2020 10:38 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8428
Location: Southern California
Here's my effort at an 8-bit inverse LOG2 function. Input and output are in the accumulator.
Code:
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?


Top
 Profile  
Reply with quote  
PostPosted: Tue Dec 22, 2020 10:42 am 
Offline

Joined: Mon May 21, 2018 8:09 pm
Posts: 1462
barrym95838 wrote:
@Chromatix: does your algorithm provide similar results to Garth's (sorry, feeling a bit lazy here tonight)?

The log function should produce exactly the same results, except that it also handles a zero input cleanly (outputting zero as the nearest representation of -Inf). I think there is also a way to improve its accuracy during the right-shifting stage.

The antilog function has a bug, which I need to fix - serves me right for doing it last thing at night without testing…


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 22 posts ]  Go to page Previous  1, 2

All times are UTC


Who is online

Users browsing this forum: No registered users and 20 guests


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

Search for:
Jump to: