From: Al Viro <viro@ZenIV.linux.org.uk>
To: Rui Teng <rui.teng@linux.vnet.ibm.com>
Cc: "Serge E. Hallyn" <serge@hallyn.com>,
serge.hallyn@canonical.com, james.l.morris@oracle.com,
linux-security-module@vger.kernel.org,
linux-kernel@vger.kernel.org,
Andy Lutomirski <luto@amacapital.net>
Subject: Re: [PATCH] security: Use || instead of | for boolean expressions
Date: Thu, 2 Jun 2016 18:37:55 +0100 [thread overview]
Message-ID: <20160602173755.GI14480@ZenIV.linux.org.uk> (raw)
In-Reply-To: <e8347493-f52a-8d67-ac09-efb5f2a39ece@linux.vnet.ibm.com>
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.
prev parent reply other threads:[~2016-06-02 17:38 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-06-01 6:03 [PATCH] security: Use || instead of | for boolean expressions Rui Teng
2016-06-02 14:13 ` Serge E. Hallyn
2016-06-02 16:53 ` Rui Teng
2016-06-02 17:37 ` Al Viro [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160602173755.GI14480@ZenIV.linux.org.uk \
--to=viro@zeniv.linux.org.uk \
--cc=james.l.morris@oracle.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=luto@amacapital.net \
--cc=rui.teng@linux.vnet.ibm.com \
--cc=serge.hallyn@canonical.com \
--cc=serge@hallyn.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).