linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: David Laight <David.Laight@ACULAB.COM>
To: 'Andrew Morton' <akpm@linux-foundation.org>,
	Nathan Chancellor <nathan@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
	"clang-built-linux@googlegroups.com" 
	<clang-built-linux@googlegroups.com>,
	"terrelln@fb.com" <terrelln@fb.com>
Subject: RE: [PATCH] lib/zstd: Fix bitwise vs logical operators
Date: Thu, 19 Aug 2021 21:22:54 +0000	[thread overview]
Message-ID: <defb9e5133234835950c21511d776fb9@AcuMS.aculab.com> (raw)
In-Reply-To: <20210816180532.6e85c93f84d62dc720d21178@linux-foundation.org>

From: Andrew Morton
> Sent: 17 August 2021 02:06
> 
> On Sat, 14 Aug 2021 17:41:54 -0700 Nathan Chancellor <nathan@kernel.org> wrote:
> 
> > clang warns several times along the lines of:
> >
> > lib/zstd/compress.c:1043:7: warning: bitwise and of boolean expressions; did you mean logical and?
> [-Wbool-operation-and]
> >                 if ((offset_1 > 0) & (ZSTD_read32(ip + 1 - offset_1) == ZSTD_read32(ip + 1))) {
> >                     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> >                                    &&
> >
> > Bitwise ANDs do not short circuit, meaning that the ZSTD_read32 calls
> > will be evaluated even if the first condition is not true. This is not
> > always a problem but it is not a standard way to do conditionals so
> > replace the bitwise ANDs with logical ones to fix the warning and make
> > the code clearer.
> >
> > ...
> >
> > --- a/lib/zstd/compress.c
> > +++ b/lib/zstd/compress.c
> > @@ -1040,7 +1040,7 @@ void ZSTD_compressBlock_fast_generic(ZSTD_CCtx *cctx, const void *src, size_t
> sr
> >  		const BYTE *match = base + matchIndex;
> >  		hashTable[h] = curr; /* update hash table */
> >
> > -		if ((offset_1 > 0) & (ZSTD_read32(ip + 1 - offset_1) == ZSTD_read32(ip + 1))) {
> > +		if ((offset_1 > 0) && (ZSTD_read32(ip + 1 - offset_1) == ZSTD_read32(ip + 1))) {
> 
> yeah, this is a late night party trick which is sometimes used to
> attempt to speed things up by avoiding a branch.  It is perhaps
> beneficial if the LHS is almost always true.  I guess.
> 
> I'd prefer that the code not do this - it's silly, looks wrong and I
> bet it's unmeasurable.
> 
> But I think this code is supposed to be kept in sync with an
> out-of-tree upstream version so this change might be problematic.

Except that in this case you want the short-circuit.
The RH condition is slow and always true when offset_1 is zero.
OTOH subtracting offset_1 makes the lines look odd (out of context).

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


      reply	other threads:[~2021-08-19 21:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-15  0:41 [PATCH] lib/zstd: Fix bitwise vs logical operators Nathan Chancellor
2021-08-16 15:14 ` David Sterba
2021-08-16 16:53   ` Nathan Chancellor
2021-08-16 21:17     ` David Sterba
2021-08-17  1:05 ` Andrew Morton
2021-08-19 21:22   ` David Laight [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=defb9e5133234835950c21511d776fb9@AcuMS.aculab.com \
    --to=david.laight@aculab.com \
    --cc=akpm@linux-foundation.org \
    --cc=clang-built-linux@googlegroups.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=terrelln@fb.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).