• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
jyk

Fast sqrt

13 posts in this topic

I've been looking through the various threads on this forum about fast sqrt and inverse sqrt approximations, and have also been looking at the relevant code in the Doom 3 sdk, and have a couple of questions. 1. I understand enough about the methods used to know that it relies on the specifics of 32 bit IEEE floating point representation. So I assume that means the code breaks if the representation changes? What happens on 64 bit machines like the Mac G5? 2. And, is it worth it? Or would you generally be better off (and close to as fast) using the standard functions, which I assume are gauranteed to be portable and consistent from platform to platform? (Please excuse me if the question seems naive, but it involves some areas I don't know much about, i.e. floating point architecture, etc.)
0

Share this post


Link to post
Share on other sites
there should be no need for it nowadays. and it's a fast inverse sqrt. Use the normal stuff, and see if it bottleneck your application. Nothing prevents you to have a float finvsqrt(float x) { ... } function in your math lib, then the implementation is left to you and the platform it's running on (so SSE/sqrt+div/Carmack for PC, and whatever for Macs).

I don't think yo ushould worry about it too much :)
0

Share this post


Link to post
Share on other sites
Look

float InvSqrt (float x)
{
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i >> 1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}



And i got it from
here

Yes, it breaks when the representation changes. use the #defines!

You usually never need it. But if you need a lot of sqrts, use the above code.
From,
Nice coder
0

Share this post


Link to post
Share on other sites
it typically takes LOTS of sqrt calls to mean anything in terms of slowdown.

I suggest to all people that have a problem with using sqrt that they put a loop in their code somewhere that keeps calling sqrt. You can typically put it in thousands of times before you even see a FPS drop.
0

Share this post


Link to post
Share on other sites
I messed around with the functions a little and did some completely unscientific tests (if I remember correctly, in earlier threads on the subject people did some very rigorous comparisons).

I compared three versions: 1 / sqrtf(), the code from the above post, and the code from Doom 3. Doom 3 uses the same principal, but calculates the seed on the fly using some constants and a lookup table.

Amazingly enough (and unless I messed up somewhere) DoomInvSqrt() returned exactly the same results as 1 / sqrtf(). So no accuracy problems there. And Q3InvSqrt() was plenty close.

I just did a brute-force test - 10,000,000 calls to each function. The Q3 and Doom versions were about 1.8 times as fast as 1 / sqrt().

I would use the Doom version, but I imagine the code is copyrighted, and I don't understand it well enough to recreate it for myself. But I suppose the other code is fair game.
0

Share this post


Link to post
Share on other sites
can you copyright just a few lines of code? its probablly just an implementation of something discovered 200 years ago, if not longer ago.
0

Share this post


Link to post
Share on other sites
Yes, I believe both versions (Q3 and Doom) use Newton's method. However, the Doom version uses some intricate and specific bit manipulation using lookup tables to find the seed. That's what I'm suspecting may be copyrighted.
0

Share this post


Link to post
Share on other sites
I did a benchmark on various float-type sqrt routines, and the fastest one was the 'sqrtf' in MSVC's standard library (equal to inline assembly). The post is here somewhere on gamedev, but I have no idea where.

The only way you'll get speedup is using the inverse square root formula to actually calculate the inverse square root, and iirc the win was barely one.

Well, you could also get a win using one of the functions that returns much less precision than sqrtf, but in that case you're not really comparing like things.
0

Share this post


Link to post
Share on other sites
There's a really cool paper about this fast sqrt trick, where everything is derived. It's from the thread posted here by Nice Coder.

invsqrt
0

Share this post


Link to post
Share on other sites
I'm going to close the thread, since the topic has been discussed in immense detail throughout the archives.

In addition to the other thread created within the past few days on the same subject, I found many, many, MANY discussions on fast sqt dating back to 2001. I don't see much new in this thread. If anyone has a compelling argument why the thread should remain open, please send me a private message and make your case strongly. The topic of fast sqrt has been covered to death and I will double check any argument to reopen the thread against the archives to see if the argument holds water.
0

Share this post


Link to post
Share on other sites
OK,

I reopened the thread just to post this example from superpig. It seems like a good contribution that may be useful. The thread is closing back immediately.

Quote:
From superpig via private message
SSE has both SQRT and RSQRT instructions, but you don't really get much benefit unless you're doing four of them at once. Say you want to get the lengths of four vectors, stored as a structure of arrays:


__declspec(align(16)) struct blockOfFourVectors
{
float xValues[4];
float yValues[4];
float zValues[4];
float lengths[4];
}

blockOfFourVectors data;

__asm
{
movaps xmm0, [data + 0x00] ; load x components
movaps xmm1, [data + 0x10] ; load y components
movaps xmm2, [data + 0x20] ; load z components

; square each component
mulaps xmm0, xmm0
mulaps xmm1, xmm1
mulaps xmm2, xmm2

; sum them into xmm0
addps xmm0, xmm1
addps xmm0, xmm2

; sqrt to get length
sqrtps xmm0, xmm0

; save out
movaps [data + 0x30], xmm0
}


That would calc all four lengths into data.lengths. For just a single vector it's not really worthwhile (and unless you store that vector in a SoA, would require a load of shuffling).


0

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  
Followers 0