English Amiga Board


Go Back   English Amiga Board > Coders > Coders. Asm / Hardware

 
 
Thread Tools
Old 16 January 2018, 10:33   #1
sandruzzo
Registered User
 
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,281
Sqrt

Hi All,

I'm plaing a little bit with raycasting stuff, and I need a way to perform asqrt on 68000. Can someone help me here?
sandruzzo is offline  
Old 16 January 2018, 10:45   #2
chb
Registered User
 
Join Date: Dec 2014
Location: germany
Posts: 439
Long thread from the Atari forum:
http://www.atari-forum.com/viewtopic.php?t=4984

From "Dr. Dobb's Toolbook of 68000 Programming":
https://github.com/jefftranter/68000...squareroot.asm

But they might or might not fit your requirements for speed and precision.
chb is offline  
Old 16 January 2018, 10:46   #3
sandruzzo
Registered User
 
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,281
Quote:
Originally Posted by chb View Post
Long thread from the Atari forum:
http://www.atari-forum.com/viewtopic.php?t=4984

From "Dr. Dobb's Toolbook of 68000 Programming":
https://github.com/jefftranter/68000...squareroot.asm

Thanks
sandruzzo is offline  
Old 16 January 2018, 11:43   #4
chb
Registered User
 
Join Date: Dec 2014
Location: germany
Posts: 439
Probably for a game a table-based approach is preferable, but are you sure you really need a sqrt for the raycaster?
chb is offline  
Old 16 January 2018, 12:42   #5
sandruzzo
Registered User
 
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,281
Quote:
Originally Posted by chb View Post
Probably for a game a table-based approach is preferable, but are you sure you really need a sqrt for the raycaster?
According to this tutorial yes. You need it!

http://lodev.org/cgtutor/raycasting.html
sandruzzo is offline  
Old 16 January 2018, 13:07   #6
chb
Registered User
 
Join Date: Dec 2014
Location: germany
Posts: 439
Funny, actually because of that tutorial I was asking, as he does not use sqrts in the code AFAICS - except for one instance where he computes deltaDistX rep. deltaDistY and does not apply the simplification he mentions in the text, probably simply because he did not update that piece of code.
chb is offline  
Old 16 January 2018, 14:18   #7
sandruzzo
Registered User
 
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,281
Quote:
Originally Posted by chb View Post
Funny, actually because of that tutorial I was asking, as he does not use sqrts in the code AFAICS - except for one instance where he computes deltaDistX rep. deltaDistY and does not apply the simplification he mentions in the text, probably simply because he did not update that piece of code.

Do you have any better tutorial?
sandruzzo is offline  
Old 16 January 2018, 14:23   #8
LaBodilsen
Registered User
 
Join Date: Dec 2017
Location: Denmark
Posts: 179
Quote:
Originally Posted by chb View Post
Funny, actually because of that tutorial I was asking, as he does not use sqrts in the code AFAICS - except for one instance where he computes deltaDistX rep. deltaDistY and does not apply the simplification he mentions in the text, probably simply because he did not update that piece of code.
Exactly.. i startet work on my raycaster following this tutorial before he updated with the simplification, and then when i looked at it again, i was all confused

but as you mention, he replaced the sqrt with a much simpler ABS(1 / rayDirX). which made my work alot easier, as now we can pre-calc all deltaDistX and Y and use a lookup table.
LaBodilsen is offline  
Old 16 January 2018, 14:26   #9
LaBodilsen
Registered User
 
Join Date: Dec 2017
Location: Denmark
Posts: 179
Quote:
Originally Posted by sandruzzo View Post
Do you have any better tutorial?
This one is pretty good to get an understanding of the workings:
http://www.permadi.com/tutorial/raycast/rayc1.html

but the one you are using, is easier to translate to 68k code.
LaBodilsen is offline  
Old 16 January 2018, 14:27   #10
sandruzzo
Registered User
 
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,281
Thanks. I'll get rid of that sqrt!!!
sandruzzo is offline  
Old 16 January 2018, 14:29   #11
sandruzzo
Registered User
 
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,281
Quote:
Originally Posted by LaBodilsen View Post
This one is pretty good to get an understanding of the workings:
http://www.permadi.com/tutorial/raycast/rayc1.html

but the one you are using, is easier to translate to 68k code.
I'll start for the easier, then we'll see!
sandruzzo is offline  
Old 16 January 2018, 14:44   #12
LaBodilsen
Registered User
 
Join Date: Dec 2017
Location: Denmark
Posts: 179
my approach have been to take the C code, and then write lot's of comment lines with 68k code. eg.

while hit = 0
(
IF sideDistX < sideDistY
#CPM.W sideDistY,sideDistX
#BHI.w sideDistY_Smaller
(
sideDistX = sideDistX + deltaDistX
#Add.w deltaDistX,sideDistX
mapX = mapX + stepX
#Add.w StepX,mapX
side = 0
#moveq #0,side
#BRA CheckMap

that way i can still maintain an overview of the process, and quickly see where to improve (this is only my 4th project since i'm trying to re-learn Assembler after 20 years og abcense). atm i'm only working in the powershell ISE editor (to get syntax highlight), until i'm satisfied with the code. then i will start to move it to Asm-pro.
LaBodilsen is offline  
Old 16 January 2018, 21:08   #13
kamelito
Zone Friend
 
kamelito's Avatar
 
Join Date: May 2006
Location: France
Posts: 1,801
Check this book. The author is also working on a book about Doom.
https://www.amazon.com/gp/aw/d/15396...06L&ref=plSrch
kamelito is offline  
Old 16 January 2018, 21:26   #14
jotd
This cat is no more
 
jotd's Avatar
 
Join Date: Dec 2004
Location: FRANCE
Age: 52
Posts: 8,160
ever heard of the fast pseudo-sqrt algorithm? https://stackoverflow.com/questions/...t-optimization
jotd is offline  
Old 16 January 2018, 22:07   #15
chb
Registered User
 
Join Date: Dec 2014
Location: germany
Posts: 439
Quote:
Originally Posted by jotd View Post
ever heard of the fast pseudo-sqrt algorithm? https://stackoverflow.com/questions/...t-optimization
I've known only of the probably more well-known fast inverse square root before, but they look very similar. But they work on floating point values and exploit some tricky relation between the binary representation of a float and an integer, so they need a reasonable fast FPU to work well. On the 68000 almost certainly not interesting, maybe more so for 040 or 060.
chb is offline  
Old 17 January 2018, 00:01   #16
Megol
Registered User
 
Megol's Avatar
 
Join Date: May 2014
Location: inside the emulator
Posts: 377
Well floats are just scaled integers with a funky encoding.
But a lookup table should be faster in this case I think?
Megol is offline  
Old 17 January 2018, 11:31   #17
chb
Registered User
 
Join Date: Dec 2014
Location: germany
Posts: 439
Quote:
Originally Posted by Megol View Post
Well floats are just scaled integers with a funky encoding.
Well, of course that's true in some sense, but the fast sqrt exploits the fact that if you take an integer number i and cast it as an IEEE float (so interpret its binary representation as floating point, not converting anything), you are performing a piecewise linear approximation of log2:
Code:
f(i) \approx L*log2(i)+ L*log2(B + \sigma)
where L= 2^23, B = 127 and \sigma a constant to improve the approximation. Using this relation, dividing (shifting) an integer number by 2 gives log2(0.5*i) = log2(i)^0.5 = sqrt(log2(i)), so the sqrt of our fp number (I am ignoring the B + \sigma term here for simplicity).

That relation was (at last for me) quite surprising when I saw it the first time, especially as some bits of the integer are interpreted as exponent and some as mantissa. Wouldn't have thought there's something useful to do with it.

So for that reason that algorithm isn't of very much use if you want to work with integers, the conversions to float and back and the fp-operations needed will be quite likely much slower than an integer-only algorithm without a fast FPU, and the fast sqrt is also just an usable, but rather rough approximation.

Quote:
Originally Posted by Megol View Post
But a lookup table should be faster in this case I think?
Of course, if you have 16 bit values and can afford the 64k (128k for unsigned int) for the table. But as written above, there is no need for a square root here.
chb is offline  
Old 17 January 2018, 13:15   #18
Megol
Registered User
 
Megol's Avatar
 
Join Date: May 2014
Location: inside the emulator
Posts: 377
Thanks for the explanation! While I have used the "trick" in the past I've never actually looked at how it works.
Megol is offline  
 


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 08:39.

Top

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Page generated in 0.26392 seconds with 14 queries