Help with basic vector and matrix operations library

Started by
3 comments, last by GameDev.net 18 years, 9 months ago
Hello, i need to implement library that does some basic operations with vectors and matrices. Could somebody help me, how to implement it ? I mean, not how to do multiplying of matrices or vectors, but if to implementit as c inline functions or do a class or do it in assembler. What is faster ? Class or inline c functions ? (what`s the difference between performance ?) And does anybody have good i686 assembler tutorial for gcc and Microsoft Visual C++ compiler ? Thanks.
Advertisement
well, taking operator+ as an example, you have a few things to take into account.

First, you will be doing it alot, all over, it's very short and very simple, and it is likely to find its way into inner loops - it is an excellent candidate for inlining, so inline it.

The next issue is that this function can be written as a member of the class or as a separate function. There's no performance difference unless you make it virtual so put it where it belongs. I'm in favour of keeping it separate - it doesn't need to be a class member to work. It doesn't even need to be a friend. Keeping it separate also makes it consistent with functions such as operator*(float, vector), which can't be a member function (you can't write operator*(vector) as a member of float).

That has nothing to do with optimization, it's just where it belongs...

Don't do it in assembler. Not yet anyway. If you get a game working and profile it and vector addition is a bottle neck, then maybe just possibly consider it, but bare in mind that the optimizer is likely to do a better job of optimizing an inline version than you are of optimizing some assembler.

Finally, look at the fluidstudios library for some ideas:
http://www.fluidstudios.com/pub/FluidStudios/VectorMath/Fluid_Studios_Matrix_Template_Class.zip
If you're interested in seeing some production code, you might download the Doom 3 sdk; the math library is included.
Thanks for the information of DOOM3 SDK. It`s really interesting, especially that math stuff in idlib. But i didn`t found processor detection. There is a class idSys, but methods are abstract and i didn`t found implementation of GetProcessorId function.
If you want it fast ignore what Squirm said, assembly is the only way. But use SSE so you can do mulitple things at once. Check out this for an example of how much faster normalization is done in ASM w/ SSE:

http://www.3dbuzz.com/vbforum/showthread.php?t=104753

If the link for some reason doesn't work (qwest is dying on me so I'm not sure it links properly) go to 3dbuzz.com, on the left side click on "C++" then on "C/C++ Algorithms" and finally the sticky "howto: inline...".

There's also links to learning ASM, which isn't all that hard too. Read the intel documents and you can get all the CPU info you could ever dream of having too. It's included and free. Hope this helps.

-Mike

This topic is closed to new replies.

Advertisement