6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Thu Sep 26, 2024 11:24 pm

All times are UTC




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: Wed Feb 23, 2011 4:08 am 
Offline

Joined: Tue Nov 18, 2003 8:41 pm
Posts: 250
I have my own version but I'm pretty sure I've seen
better.

But I can't find them.

So I thought I'd ask if any one has or knows of a good
renedering of the algorithm in 6502 code?


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 23, 2011 8:00 am 
Offline

Joined: Wed Oct 06, 2010 9:05 am
Posts: 95
Location: Palma, Spain
Look no further than this very website!


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 23, 2011 9:03 am 
Offline

Joined: Tue Nov 18, 2003 8:41 pm
Posts: 250
RichTW wrote:
Look no further than this very website!


I'm looking for something more akin to this

Code:
r=1
REPEAT
   x=x-r
   r=r+1
   x=x-r
UNTIL x<0
r=r-1


as mentioned here and other place by Garth Wilson

However, what I'm looking for uses the same basic idea but
goes about it a little differently IIRC


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Wed Feb 23, 2011 12:14 pm 
Offline

Joined: Wed Oct 06, 2010 9:05 am
Posts: 95
Location: Palma, Spain
Sorry, I misunderstood what you were asking! There's another implementation of it here, though I think this algorithm is probably a little suboptimal for square roots of large numbers (which have to iterate as many times as the result!).

The other algorithm always iterates exactly 8 times, and so it more-or-less time constant, regardless of the input.


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 24, 2011 2:20 am 
Offline
User avatar

Joined: Thu Mar 11, 2004 7:42 am
Posts: 362
bogax wrote:
I have my own version but I'm pretty sure I've seen
better.


I could've sworn I've seen one as well, but I can't remember where. Evidently, losing one's mind is contagious. :) Best I could come up on the spot is this (which subtracts 2r inside the loop instead of r twice):

Code:
   ldx #$FF
   sec
.1 inx
   inx
   stx tmp
   sbc tmp
   bcs .1
   txa
   lsr


Top
 Profile  
Reply with quote  
PostPosted: Thu Feb 24, 2011 4:26 am 
Offline

Joined: Tue Nov 18, 2003 8:41 pm
Posts: 250
dclxvi wrote:
Best I could come up on the spot is this (which subtracts 2r inside the loop instead of r twice):

Code:
   ldx #$FF
   sec
.1 inx
   inx
   stx tmp
   sbc tmp
   bcs .1
   txa
   lsr


Cool! That's great. Thanks

Much better than mine, which seems like a kludgy mess.

Code:
 ldx #$00
LOOP
 sta num
 dex
 txa
 asl
 adc num
 bcs LOOP
 txa
 eor #$FF


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 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: