Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: question about branchless code

Author: Tony Werten

Date: 08:07:00 02/27/03

Go up one level in this thread


On February 27, 2003 at 10:14:50, Heiner Marxen wrote:

>On February 27, 2003 at 05:11:18, Tony Werten wrote:
>
>>On February 26, 2003 at 14:58:52, Heiner Marxen wrote:
>>
>>>On February 26, 2003 at 14:04:16, Uri Blass wrote:
>>>
>>>>I had the following code in my program
>>>>
>>>>do
>>>>{
>>>>...
>>>>if (info[target]==EMPTA)
>>>>  target+=8;
>>>>else
>>>>  target=64;
>>>>}
>>>>while (target<64)
>>>>
>>>>I found that changing it to else break did not do my program faster.
>>>
>>>Assuming that the variable "target" is not read after this loop (the value
>>>is "dead"), I would assume that any decent optimizer will produce code
>>>as if there had been a "break" instead of "target=64".
>>>
>>>Obviously, your compiler does so.
>>>You may even inspect the generated assembler for both versions.
>>>When they are just equal you need not even understand the assembly.
>>>
>>>>I think that with break I have branches and without break the compiler can
>>>>translate it to
>>>>
>>>>target=((info[target]==EMPTA)?target+8:64);
>>>>
>>>>My questions are
>>>>1)How can I write the code as branchless code without target=64 that is a waste
>>>>of time?
>>>
>>>I expect it to be optimized away.
>>>Hence it is not really a waste of time.
>>>
>>>I'd suggest you choose the version that best fits your style, i.e. which
>>>is most readable for you.  I would prefer to terminate a loop with a break
>>>instead of manipulating the loop variable to do so in the next iteration.
>>>YMMV.
>>>
>>>
>>>>2)I understood from previous discussion that for some reason ? is considered
>>>>branchless code.
>>>>What is the reason for it.
>>>
>>>I do not consider ?: to be branchless.
>>>In most cases it _does_ generate branches.
>>>
>>>>It seems to me that using ? is the same as using if.
>>>
>>>Yes, I agree.
>>>
>>>>If it is not the case then what is the difference?
>>>
>>>The ?: is an expression.
>>>"if" is a statement.
>>>
>>>Using ?: within a larger expression can avoid repeating the surrounding
>>>part in the then- and else-part of the equivalent if/else code.
>>>Sometimes ?: is just more readable, since it is more compact.
>>>
>>>  x = (a<b) ? a : b;
>>>
>>>vs
>>>
>>>  if (a<b) x=a; else x=b;
>>>
>>>or even
>>>
>>>  if (a<b) {
>>>    x=a;
>>>  }else {
>>>    x=b;
>>>  }
>>>
>>>I clearly prefer the first version.  (Using a min macro may be even better).
>>
>>You should prefer
>>
>>x=b;
>>if a<b x=a;
>
>Actually, I don“t.
>If this is really more efficient, I want the optimizer to do this
>mini-optimization.  The optimizer has to consider what is the best way
>to do it on the local hardware.
>
>In my source I prefer the most readable version.

:))) Have had that discussion a few times before. Problem is that everybody
thinks their version is most readable. Because that's what they're used to. That
doesn't mean most readable for other people though.

Tony

>
>Cheers,
>Heiner



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.