English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

float fAbsX = abs(fX);
if(fX >= 0 && fX <= fD
&& abs(fY) <= fAbsX && abs(fZ) <= fAbsX)
{
...
}

2007-02-23 01:20:57 · 2 answers · asked by Dee . 2 in Computers & Internet Programming & Design

can i reduce the 8 operations (comparisons, anding and abs's) by doing || or bitwise OR or any shiftings << or >> ?

2007-02-23 01:44:57 · update #1

2 answers

--can i reduce the 8 operations (comparisons, anding and abs's) --by doing || or bitwise OR or any shiftings << or >> ?

Shifting would be useless here, using || instead of && bring no gain and bitwise ops are not really applicable here.

Your best bet is use the important feature of if which is to perform lazy evaluation: stop evaluating terms as soon as the global result is known.

The idea is then to arrange the order of the terms in the if to prevent the more costly operations most of the time, and putting the most often triggered conditions before the more specific ones.
Also uses 0.0f instead of 0. 0 in a float operation is converted to a double constant and fx must also be converted to a double to perform the comparaison. 0.0f specify that 0 will be a float constant preventing that extra task.

If fx is less than 0 you called abs(fx) for nothing, since the rest of the expression where fAbs is used is not evaluated. So you may want to check if the first three conditions screen out more than half of the cases. If it's the case you might be better
with something like (no previous fAbsX assignation)
if (fx >= 0.0f && fx <=fD && abs(fy) <= abs(fx) && abs(fz) <= abs(fz) )

Do some tests. Determine which of the first two test screen out more case, and put it first. Do the same with the last two.

Hope it help

2007-02-23 04:39:40 · answer #1 · answered by Rah-Mon Heur 4 · 4 0

I don't see how you could optimize this more. The values tests are done before the tests using the abs() function, so this is good. The only thing you could do is compile with optimization flags, depending on your compiler (e.g. -O3 with cpp).

Also, if abs(fY) and abs(fZ) are used more than once, put them in vars like you did for fX (if fY and fZ don't change).

2007-02-23 09:42:02 · answer #2 · answered by Viv 3 · 2 0

fedest.com, questions and answers