Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Moe

A really nasty 'if' statement...

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to create a function that takes a RECT structure as a parameter, and checks if the mouse position is in it. This function has to be as fast as possible, as it is being called in real time, (30+fps). Here is the code that I came up with: //RECT test function int RectTest (RECT test) { if(cursor_x >= test.left){ if(cursor_y >= test.top){ if(cursor_x <= test.right){ if(cursor_y <= test.bottom) { return(RECTTEST_OK); } } } } else { return(RECTTEST_BAD); } } As you may have guessed, the x position of the mouse is stored in the cursor_x variable, and the y position of the mouse likewise in the cursor_y variable. I have define the RECTTEST_OK and RECTTEST_BAD at the beginning of the program. Here are the errors I get when I try to link/compile: D:\STYDX7IN24\Source\prototype\prototype.cpp(361) : error C2059: syntax error : ''='' D:\STYDX7IN24\Source\prototype\prototype.cpp(366) : error C2059: syntax error : ''='' D:\STYDX7IN24\Source\prototype\prototype.cpp(366) : error C2143: syntax error : missing '';'' before '')'' I can''t seem to figure it out (stupid me:p). Anyone here have an answer/better solution?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
int RecTest(RECT test)
{
if(((cx >= test.left) && (cx <= test.right)) && ((cy >= test.top) && (cy <= test.bottom)))
{
return(RECTTEST_OK);
}

return(RECTTEST_BAD);
}



cant see anything wrong with that code, the errors are most likely due to something missing in the code just before, or a problem with your defines.

Share this post


Link to post
Share on other sites
I beleive that ifs have some problems with speed as I think they are treated as function calls. Logical ands are fast... Don''t quote me on the first bit tho

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-Chris Bennett of Dwarfsoft
"The Philosophers' Stone of Programming Alchemy"
IOL (The list formerly known as NPCAI) - A GDNet production
Our Doc - The future of RPGs
Thanks to all the goblins over in our little Game Design Corner niche
          

Share this post


Link to post
Share on other sites
Here's a cleaner way to write this function:

    
int RectTest (RECT test)
{
if(cursor_x >= test.left)
if(cursor_y >= test.top)
if(cursor_x <= test.right)
if(cursor_y <= test.bottom)
return(RECTTEST_OK);
else
return(RECTTEST_BAD);
}


I've never used so many nested 'ifs' like this, but it should work.

Edited by - Zipster on August 29, 2000 8:00:07 PM

Share this post


Link to post
Share on other sites
Ok, to do my part here: here's a messed up way, but faster (yippie)

        
#define RectTest(R) (cx >= R.left && cx <= R.right && cy >= R.top && cy <= R.bottom ? RECTTEST_OK : RECTTEST_BAD)




Edited by - baskuenen on August 29, 2000 8:22:20 PM

Share this post


Link to post
Share on other sites
I just want to make sure everyone is hip to something...

Logical AND''s and OR''s in C++ have what''s known as short circuit evaluation. This means logical operations can evaluate faster without to do all the work all the time.

What that means if I have an AND statement like:

if ((x==2) && (y==3))
...

If x==2 is false, there is no need to evaluate y==3. The statement will exit out before it even evaluates y==3, because it knows the entire expression will evaluate to false regardless of the value of y.

For an OR example:

if ((x==2) || (y==3))
...

If x==2 is true, then there is not need to evaluate y==3. No matter what the value of y, the entire expression will return true.

To put it another way, the language takes advantage of 2 rules:
1) False logically AND''d with anything else is ALWAYS False.
2) True logically OR''d with anything else is ALWAYS True.

I know this to be the case on C/C++, and Java (although Java has operators without this feature).

So...
I''d throw them in one if statement.

PS> Short circuit evaluation can also mess you up . If your expression contains function calls for instance, those functions might not get called because the if statement short circuited on you. If those function calls would have changed data for instance, that data might not have been changed. Keep it in mind...

Share this post


Link to post
Share on other sites
I didn''t use logical and''s in the first place because I was unsure of the performace hit it would make. Would logical and''s be faster or as fast as using the super-nested if statement?

Share this post


Link to post
Share on other sites
I think I''ll try compiling something and disassembling it just to see. With short circuit evaluation it seems like you would not even need to do the actual AND''s on the CPU.

I''ll check it out...

Share this post


Link to post
Share on other sites
I tried this out with Visual C++ 6.0 in a debug build (which obviously does not matter once you see the disassembly ).

Turns out they don't do AND's .

Here is a code snippet I tried:

        

void main()
{
int x = 5;
int y = 7;
int z = 9;

if (x==5)
{
if (y==7)
{
z = 10;
}
}
}



I then tried this:



void main()
{
if ((x==5) && (y==7))
{
z = 10;
}
}



They both produced the exact same assembly:



0040103D cmp dword ptr [ebp-4],5
00401041 jne main+40h (00401050)
00401043 cmp dword ptr [ebp-8],7
00401047 jne main+40h (00401050)



So in summation:

Again, this is in Visual C++, but I have a feeling it's not exactly MS patent pending or anything. An if statement composed of expressions logically AND'ed together produces the exact same code.

Wonder if I should add this to my list of interview questions designed to torture people? j/k

Edited by - Kentamanos on August 30, 2000 3:04:29 AM

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!