Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp903493pxb; Thu, 19 Aug 2021 14:23:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyv3hv/va4nMPxFBd/C3hR8PljMAqGt9sbAfxnCui2Mv40LxI5FAhskIOWh0GLuKkW+SaIe X-Received: by 2002:a02:ca06:: with SMTP id i6mr14503472jak.81.1629408231803; Thu, 19 Aug 2021 14:23:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629408231; cv=none; d=google.com; s=arc-20160816; b=xb2Aw0oPRmNOsG04IvrGORo9tNsRRoodh7mGbSLj6O0EojlbdmQnCqpZPyjsO04V2y MeGuDF4ANLYNI4+ut1yIAuLSNrpEjmCOm0EpWX+Gwn9gGKKNRvRWM4kQmenGfu6U5sdc tQ/ZPrWhFVM7Mz1+jxGeeLbkMRu2wlv4h1+vAK9fBvD1bzlnmskmb2Qy02IshxN9bb+R 0f787WLKuFaU4/wt8whVjN8FQxWmMVxh5bfGlKi8gdiGpuuH2HqlG8bzjCSySYKyGly9 GJB7J3GrC1cOXOrYqzgZsIn6TO++A2t47QPg1cxi8JMTDVUwTv1UjmA7HFI6fNtTJKUz 7M2Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :mime-version:accept-language:in-reply-to:references:message-id:date :thread-index:thread-topic:subject:cc:to:from; bh=m/ZlCLBLgHmgjaFrxk0RNqUu6kKkPRx0gygSbqv5R3M=; b=t1zJJVGw+VZXZHJZrXZB7CG8rCt/qxi+37yVUlAdgVfAeqMfJ7vLSMZd6T6h7jPgoW RIuis0uNpql0lopZtvJljat1iJ/shoKVQgcuyR16NRoV2lq7OuO67IJwki7R6rNo7I+N eU+emRGaRYxSRiWgnXAUVMgx6jQevXPLcP7m2iGCzk+/p/ddkRYKgsiGoXsYx2jmXenJ XSt/+7T7o4KdRBLrBHvIpVPSfo0NPdxV2tLm4WC4gCAilbT85yChjGmwde4K/AloT2SZ jcqd7fgh0o36lVzFHspOKeo2mOV/S1wz7fSL81Ig1HjApvrwfmYoA9tV3FZVhMAyyIHS drJQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l15si5412075ios.94.2021.08.19.14.23.38; Thu, 19 Aug 2021 14:23:51 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=aculab.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233250AbhHSVXf convert rfc822-to-8bit (ORCPT + 99 others); Thu, 19 Aug 2021 17:23:35 -0400 Received: from eu-smtp-delivery-151.mimecast.com ([185.58.85.151]:39061 "EHLO eu-smtp-delivery-151.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230112AbhHSVXe (ORCPT ); Thu, 19 Aug 2021 17:23:34 -0400 Received: from AcuMS.aculab.com (156.67.243.121 [156.67.243.121]) (Using TLS) by relay.mimecast.com with ESMTP id uk-mta-4-jK_kyUcYM325kxK8jwd6cg-1; Thu, 19 Aug 2021 22:22:56 +0100 X-MC-Unique: jK_kyUcYM325kxK8jwd6cg-1 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) by AcuMS.aculab.com (fd9f:af1c:a25b:0:994c:f5c2:35d6:9b65) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Thu, 19 Aug 2021 22:22:55 +0100 Received: from AcuMS.Aculab.com ([fe80::994c:f5c2:35d6:9b65]) by AcuMS.aculab.com ([fe80::994c:f5c2:35d6:9b65%12]) with mapi id 15.00.1497.023; Thu, 19 Aug 2021 22:22:55 +0100 From: David Laight To: 'Andrew Morton' , Nathan Chancellor CC: Nick Desaulniers , "linux-kernel@vger.kernel.org" , "clang-built-linux@googlegroups.com" , "terrelln@fb.com" Subject: RE: [PATCH] lib/zstd: Fix bitwise vs logical operators Thread-Topic: [PATCH] lib/zstd: Fix bitwise vs logical operators Thread-Index: AQHXkwP9hKCn1U30S0yWYygV6tj6Nat7Wbtg Date: Thu, 19 Aug 2021 21:22:54 +0000 Message-ID: References: <20210815004154.1781834-1-nathan@kernel.org> <20210816180532.6e85c93f84d62dc720d21178@linux-foundation.org> In-Reply-To: <20210816180532.6e85c93f84d62dc720d21178@linux-foundation.org> Accept-Language: en-GB, en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=C51A453 smtp.mailfrom=david.laight@aculab.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: aculab.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Andrew Morton > Sent: 17 August 2021 02:06 > > On Sat, 14 Aug 2021 17:41:54 -0700 Nathan Chancellor 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)