mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: linux-kernel@horizon.com
To: 76306.1226@compuserve.com
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] only use 48-bit lba when necessary
Date: 18 Apr 2003 03:32:13 -0000	[thread overview]
Message-ID: <20030418033213.32695.qmail@science.horizon.com> (raw)

Matt Mackall wrote:
>> FYI, GCC as of 3.2.3 doesn't yet reduce the if(...) form to branchless
>> code but the & and && versions come out the same with -O2.

And Chuck Ebbert replied:
>   The operands of & can be evaluated in any order, while && requires
> left-to-right and does not evaluate the right operand if the left one
> is false.  Only the simplest cases could possibly generate the same
> code.

The code must execute AS IF the right operand is only evaluated if the left
operand is true.

If an optimizer can prove that evaluating an operand has no side effects
(which a halfway-decent optimizer can usually do for simple expressions),
then it is free to evaluate it in any way that will produce the same
result.

For example, if both operands of && have no side effects, then the compiler
is free to convert

if ( expensive() && cheap() ) {...}

into

if ( cheap() && expensive() ) {...}

although I expect most assume the programmer was sensible enough to
put the most commonly failed tests first.


The compiler is also free to do the same to

if ( !!cheap() & !!expensive() ) {...}

Again, it has to behave AS IF !!expensive() were evaluated in every
case, but if it can prove that there are no side effects, it can
skip expensive() entirely if there appears to be reason to do so.


The upshot of this is that, if the conditions have no side effects and
the compiler is decent, there should be NO DIFFERENCE in the compiled code.

Since it is only legal to convert between && and & if the right operand
has no side effects and does not depend on any side effects of the left
operand, the only time it makes sense to choose one over the other for
any reason but coding style is if A) the code is speed-critical, and B)
you know the conditions are met but you don't think the optimizer can
figure it out.

Which is pretty uncommon.


Regarding style, personally I'm used to C's zero/non-zero boolean tests
and prefer to write "while (p)" and "if (x & MASK)".  Using bare &
between such expressions is more difficult to read for two reasons:
A) the reader has to parse the "!!(...) " or "... != 0" clutter around
   each operand, and
B) the reader has to figure out that the "1 & 2" case never happens.

With |. that can often be avoided, but not with pointer operands.

             reply	other threads:[~2003-04-18  3:20 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-18  3:32 linux-kernel [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-04-18  9:50 Chuck Ebbert
2003-04-18  1:34 Chuck Ebbert
2003-04-18  4:18 ` Matt Mackall
2003-04-18 14:34 ` Timothy Miller
2003-04-04 17:02 Chuck Ebbert
2003-04-17 14:20 ` Matt Mackall
2003-04-17 15:24   ` Timothy Miller
2003-04-17 16:05     ` Matt Mackall
2003-04-17 18:49       ` Timothy Miller
2003-04-04 12:29 Jens Axboe
2003-04-04 13:19 ` Juan Quintela
2003-04-04 13:22   ` Jens Axboe
2003-04-04 15:48     ` Juan Quintela
2003-04-04 15:54       ` Jens Axboe
2003-04-04 17:06         ` John Bradford
2003-04-04 14:40 ` Andries Brouwer
2003-04-04 15:13   ` Jens Axboe

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=20030418033213.32695.qmail@science.horizon.com \
    --to=linux-kernel@horizon.com \
    --cc=76306.1226@compuserve.com \
    --cc=linux-kernel@vger.kernel.org \
    /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

all inboxes | Powered by JetHome®