16 January 2018, 10:33 | #1 |
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? |
16 January 2018, 10:45 | #2 |
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. |
16 January 2018, 10:46 | #3 | |
Registered User
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,281
|
Quote:
Thanks |
|
16 January 2018, 11:43 | #4 |
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?
|
16 January 2018, 12:42 | #5 | |
Registered User
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,281
|
Quote:
http://lodev.org/cgtutor/raycasting.html |
|
16 January 2018, 13:07 | #6 |
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.
|
16 January 2018, 14:18 | #7 | |
Registered User
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,281
|
Quote:
Do you have any better tutorial? |
|
16 January 2018, 14:23 | #8 | |
Registered User
Join Date: Dec 2017
Location: Denmark
Posts: 179
|
Quote:
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. |
|
16 January 2018, 14:26 | #9 |
Registered User
Join Date: Dec 2017
Location: Denmark
Posts: 179
|
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. |
16 January 2018, 14:27 | #10 |
Registered User
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,281
|
Thanks. I'll get rid of that sqrt!!!
|
16 January 2018, 14:29 | #11 | |
Registered User
Join Date: Feb 2011
Location: Italy/Rome
Posts: 2,281
|
Quote:
|
|
16 January 2018, 14:44 | #12 |
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 (#CPM.W sideDistY,sideDistX #BHI.w sideDistY_Smaller (#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. |
16 January 2018, 21:08 | #13 |
Zone Friend
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 |
16 January 2018, 21:26 | #14 |
This cat is no more
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
|
16 January 2018, 22:07 | #15 | |
Registered User
Join Date: Dec 2014
Location: germany
Posts: 439
|
Quote:
|
|
17 January 2018, 00:01 | #16 |
Registered User
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? |
17 January 2018, 11:31 | #17 |
Registered User
Join Date: Dec 2014
Location: germany
Posts: 439
|
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) 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. 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. |
17 January 2018, 13:15 | #18 |
Registered User
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.
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
|
|