Jump to content

Recommended Posts

Posted

I am taking a C++ class and at the same time, I am dissecting Quake 3's InvSqrt() function. Anyway, I have two questions, both of them are compiler related.

 

For my C++, I have an assignment where I need to write a program that will take another program's (or perhaps its own) source and reformat it. I am a bit more advanced than my peers and I want to implement support for things that the assignment does not require, such as for loops and switch statements. Because of that, my professor suggested that I write a scanner to extract the tokens from the source code and then reassembly them in a nice neat manner.* I am not familiar with compiler theory (although I did learn a bit of formal language theory in my discrete mathematics class), so would someone be so kind as to point me in the right direction so I can get started?

 

Since I have began dissecting Quake 3's InvSqrt() code, I have improved it (although most of the things I did were done by other people before me, I just essentially put them toegether) to make it more accurate and easier to understand, hopefully without compromising any of its speed by using a union (someone at gamedev.net beat me to this, according to his numbers, the code is 20%-33% faster with a union, depending on whether or not you account for the loop overhead that running the same code in a loop a hundred million times imposed) to make it faster, using a better constant (provided by Chris Lomont), adding a second Newton approximation and using appropriate commenting (i.e. I am clearly indicating in the source code that the initial guess is a Taylor Series approximation and that the steps following it are Newton approximations) to make it easier to understand. With these changes, its output is almost identical to the output of the built-in functions (i.e. 1.0 / sqrt(x)) and is suitable for replacing instances of the built-in functions (of the form 1.0 / sqrt(x)) in just about all applications on 32-bit systems (64-bit systems might need another Newton approximation, although most applications will not need the extra precision).

 

Now, I am curious as to how fast my new function is compared to the original function Quake function and I would like to work out the actual number of cycles necessary to compute it, the original function and the built-in functions. I have disassembled the code in Visual Studio (and some of the things Visual Studio is doing do not make any sense to me, as it appears to be send stuff in addition to the result back to main memory) but I cannot find any documentation on how many cycles instructions the Intel Core 2 Duo. I also am having trouble finding out whether or not the built-in sqrt() function is an assembly instruction or actual code. Would someone please point me in the correct direction on this?

 

 

More...

 

View All Our Microsoft Related Feeds

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...