Xonotic Forums
QuakeC: if chains? - Printable Version

+- Xonotic Forums (https://forums.xonotic.org)
+-- Forum: Creating & Contributing (https://forums.xonotic.org/forumdisplay.php?fid=10)
+--- Forum: Xonotic - Development (https://forums.xonotic.org/forumdisplay.php?fid=12)
+--- Thread: QuakeC: if chains? (/showthread.php?tid=4813)



QuakeC: if chains? - WannabeUser - 03-12-2014

So writing some code i have been reading up on how to not make it perform badly by default and came across this site http://ouns.nexuizninjaz.com/dev_quakec.html#if-chains that says:

Quote:With default compile options (i.e. if the option -flo is not passed to the compiler), boolean expressions are evaluated fully.

They also mention fteqcc so i am wondering if this is something i have to worry about using gmqcc beyond that expressions that can have sideeffects (containing functions or assignments) will have them regardless of order (which i guess cant be changed as some code might rely on the sideeffects). Where i am getting is: Can i write something like if( somevar && someother && yetanother ) assuming the compiler will chose the best possible bytecode (say no possible sideeffects -> if chain - assuming that actually is the most efficient bytecode) or will it be auto performance loss for not writing if( somevar ) if( someother ) if( yetanother ) because the compiler is not clever enough or for some weird technical reason cant optimize it? The gmqcc "documentation" sadly wasnt exactly helpful so i thought it might not hurt to ask.

Somewhat related: Is there a reason to avoid switch? I have seen looooong if-else-if constructs. Is this just a preference of the authors or maybe some historical thing or is there more to it? Switch being closely related to if kinda got me curious.

Also somewhat related: Is this something i should worry about at all? I guess instantly doubting the compilers abilities to optimize stuff is kind of a bad c habit.


RE: QuakeC: if chains? - Mr. Bougo - 03-13-2014

Your reference is not up to date, here's the relevant page in the Xonotic wiki: http://dev.xonotic.org/projects/xonotic/wiki/Introduction_to_QuakeC

It's still based on FTEQCC though. Someone should fix that.

Anyway, gmqcc's manpage (or at least a months-old version of it) mentions
Quote: -fshort-logic
Perform early out in logical AND and OR expressions. The final result will be either a 0 or a 1, see the next flag for more possibilities.
which is what you mean.

Looking at the source code, it seems that SHORT_LOGIC is enabled for -std=gmqcc, which Xonotic uses. Looks like the docs aren't up to date on this.


RE: QuakeC: if chains? - WannabeUser - 03-13-2014

Ouch, how could i miss that? You are right, its just above -fperl-logic which ironically i read at least 3 times thinking "no this really isnt what i am looking for". I guess i owe the gmqcc documentation an apology now. Thanks a lot for clarifying. This would have cost me time on pretty much every single if statement trying to decide if i am going to build this ugly if-if-if construct just to be sure or if i will take the risk of doing it the normal way.