<div class="gmail_quote">2011/10/10 Konstantin Tokarev <span dir="ltr"><<a href="mailto:annulen@yandex.ru">annulen@yandex.ru</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<br>
<br>
10.10.2011, 18:29, "David A. Greene" <<a href="mailto:greened@obbligato.org">greened@obbligato.org</a>>:<br>
<div class="im">> Justin Holewinski <<a href="mailto:justin.holewinski@gmail.com">justin.holewinski@gmail.com</a>> writes:<br>
><br>
>>  int globalIndexY2 = get_group_id(1)*186 + 6*get_local_id(1) + 2 + 1;<br>
>>  bool valid2       = validX && globalIndexY2 >= 4 && globalIndexY2 < 3910;<br>
>><br>
>>  Clang, even at -O0, is performing short-circuit evaluation of these<br>
>>  expressions, resulting in a fair number of branch instructions being<br>
>>  generated.<br>
><br>
> It has to.  This is the semantics of C.  Short-circuiting is used to<br>
> defend against all sorts of undefined behavior in real code.<br>
<br>
</div>More precisely, && and || are sequence points (though in C++ they may not be<br>
sequence points if respective operator is overloaded)<br></blockquote><div><br></div><div>Sequence points don't really come into play in a meaningful way in code like that given above.  In the example given, each expression is without side effects, and none of the latter expressions are dependent on the predecessors (no "ptr != NULL && *ptr == value" sorts of things).</div>
<div><br></div><div>noting that boolean values are either exactly 1 or 0, this statement:</div><div>  bool valid2       = validX && globalIndexY2 >= 4 && globalIndexY2 < 3910;</div><div><div>is equivalent to this statement:</div>
<div>  bool valid2       = validX & globalIndexY2 >= 4 & globalIndexY2 < 3910;</div></div><div>assuming no operator overloading or anything else crazy going on.</div><div><br></div><div>There may be some non-trivial work involved in converting the >= and < tests into boolean values depending on the architecture, which can seriously affect the value of such a transformation.  Also worth noting is that globalIndexY2 is known to be in a register from the previous line of code and so there is no load instruction or any chance of a cache miss being avoided by a short-circuit branch, so it really boils down to the cost of the extra instructions vs the cost of the eliminated branches.</div>
</div>-- <br>Sean Middleditch<br>