[<prev] [next>] [<thread-prev] [day] [month] [year] [list]
Message-ID: <defb9e5133234835950c21511d776fb9@AcuMS.aculab.com>
Date: Thu, 19 Aug 2021 21:22:54 +0000
From: David Laight <David.Laight@...LAB.COM>
To: 'Andrew Morton' <akpm@...ux-foundation.org>,
Nathan Chancellor <nathan@...nel.org>
CC: Nick Desaulniers <ndesaulniers@...gle.com>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"clang-built-linux@...glegroups.com"
<clang-built-linux@...glegroups.com>,
"terrelln@...com" <terrelln@...com>
Subject: RE: [PATCH] lib/zstd: Fix bitwise vs logical operators
From: Andrew Morton
> Sent: 17 August 2021 02:06
>
> On Sat, 14 Aug 2021 17:41:54 -0700 Nathan Chancellor <nathan@...nel.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)
Powered by blists - more mailing lists