# Help with basic vector and matrix operations library

This topic is 4862 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 ? (whats the difference between performance ?) And does anybody have good i686 assembler tutorial for gcc and Microsoft Visual C++ compiler ? Thanks.

##### Share on other sites
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

##### Share on other sites
If you're interested in seeing some production code, you might download the Doom 3 sdk; the math library is included.

##### Share on other sites
Thanks for the information of DOOM3 SDK. Its really interesting, especially that math stuff in idlib. But i didnt found processor detection. There is a class idSys, but methods are abstract and i didnt found implementation of GetProcessorId function.

##### Share on other sites
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:

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

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
18

• 13
• 10
• 12
• 10
• 13
• ### Forum Statistics

• Total Topics
632998
• Total Posts
3009805
• ### Who's Online (See full list)

There are no registered users currently online

×