Skip to main content
gamedev.net
🔒 Locked

Compiler optimalization

Started by DaBono 7 replies 900+ views
Original Post
DaBono
DaBono
Hi all, I have a piece of code that calculates something (radial lens distortion, for those who are interested). It looks like this:
double x = (u - m_cx) / m_fx;
double y = (v - m_cy) / m_fy;
double r2 = (x*x) + (y*y);
rhs[0] = -(u + (u-m_cx)*(m_k1*r2 + m_k2*(r2*r2) + 2*m_p1*y + m_p2*(r2/x + 2*x)) - ud);
rhs[1] = -(v + (v-m_cy)*(m_k1*r2 + m_k2*(r2*r2) + 2*m_p2*x + m_p1*(r2/y + 2*y)) - vd);
As you can see, the formulas contain quite some of the same terms. How far can I trust that the compiler will optimize this calculation for me? Or do I benefit from creating some extra variables for e.g. the 'm_k1*r2 + m_k2*(r2*r2)'-term?
Spoonbender
Spoonbender
Quote:
Original post by DaBono
As you can see, the formulas contain quite some of the same terms.
How far can I trust that the compiler will optimize this calculation for me? Or do I benefit from creating some extra variables for e.g. the 'm_k1*r2 + m_k2*(r2*r2)'-term?


Well, technically, you can't. Most modern compilers should be able to do this for you, but you can't be 100% sure.
In any case, why notsplit the terms out into separate variables? Would be more readable *and* you'd be make sure the terms aren't computed more than once.
Kambiz
Kambiz
The formulas for x and y, rhs[0] and rhs[1] are similar so you should be able to use SSE2 on intel and 3DNow on AMD.
Maybe VectorC(this is a c/c++ compiler) could optimize the code.
SiCrane
SiCrane
In general you should write your code so it is maximally understandable by people, and not worry about how it may influence the generation of machine code. In this instance any decent optimizing compiler will create the necessary temporaries to take advantage of common subexpressions.

edit: bad grammar

[Edited by - SiCrane on October 16, 2005 10:38:32 AM]
DaBono
DaBono
Ok, then I will stick to this. These formulas are the ones in each text book, so I guess they qualify as 'the most readable' (or better, recognizable).
They were however used in quite a tight loop, which made me worry if these common subexpressions were optimized. I think GCC and MSVC++ are indeed 'decent optimizing compilers', so all my problems are solved [wink].
Code-R
Code-R
to double check, you can always check out the asm output.
GnuVince
GnuVince
Personally, I would put the common expressions in variables (and try and give them meaninful names, not just x). But that wouldn't be because of a speed consideration, but because of a readability consideration. If you profile your code, I'm pretty sure that this isn't the bottleneck of your application, therefore using temporary variables should only be to more clearly communicate your intention to other programmers.
iMalc
iMalc
Try both, and profile - you choose then. If you need help profiling, just ask.
Mantear
Mantear
Simply create a unit test to test your code, and run it through both optimized and un-optimized. It's really the only way to tell. I've seen turning on optimization break many things (although they tend to be hardware/driver related most of the time) or compiler bugs creep in with them on, so if you plan on using optimization, turn it on early in the project. It's MUCH easier to detect an incorrect optimization error if you keep checking your results via unit tests as you go, instead of coding the whole thing up, turning it on, and finding out that it's broken.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.