Re: [PATCH] security: Use || instead of | for boolean expressions

From: Al Viro
Date: Thu Jun 02 2016 - 13:38:13 EST


On Fri, Jun 03, 2016 at 12:53:07AM +0800, Rui Teng wrote:

> Of cause, it is only a sample on x86, but even if the generated code is
> not the same, the logical will be better than bitwise.
> Because (a || b || c) means (a != 0 || b != 0 || c != 0), once a != 0,
> the whole expression will be true(short-circuit evaluation).
> and (a | b | c) means calculate the bitwise first and check the result
> in the end. And since the args are all integer, there is no need to
> avoid any short-circuit.

Not obvious at all. Comparison of the cost of two OR plus one conditional
branch vs. that of "short-circuited" variant is almost certainly going to be
in favour of compiler using bitwise operations anyway. At the very least
you'll need to examine the first value, so even in the fastest case it's
test + branch taken. The rest is going to be worse and the whole thing is
going to be not fun for the pipeline either, not to mention higher icache
footprint, etc.

So I would be quite surprised if cc(1) would use short-circuit there,
whichever form you use in the source.