Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp1520340pxb; Tue, 26 Oct 2021 10:29:03 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxXblYtR+rqO9hQ03JCi2pPK7Ic2y9VH2DKGTUp4TPlpVtsTeGhSVxUps7PeSuvj94Zz5xF X-Received: by 2002:a62:27c7:0:b0:44d:b86:54f2 with SMTP id n190-20020a6227c7000000b0044d0b8654f2mr26826287pfn.68.1635269343476; Tue, 26 Oct 2021 10:29:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635269343; cv=none; d=google.com; s=arc-20160816; b=AT1j0iUPLGaRQGMA+rs3NC2yCz5SQqlQRkgXekikZuFM9ARz1pMtj/cy4ZfMCftsSI LJ/nj+JdJHGoVgxb6hK+rEF42llDWh/la5Rp5GUqBfSGhm7tNO0P6QPY+dGnT8YG8BaZ xafFWYISSaM77CHuZg3DTs5hkoInAEW5Hiftcj5DXbIXd3U41t6b37KI7mCdqjnVXLBc Ah+57AIaJtjNibVjAn3K7Au7Bxy8l5+pdHyu88omRjWxutPSctCUOotnz4tXLEZvwvXT 1k2kYl8vepVhiwhFHHr3GkRuzjzl/di2G70FK3BLOJAE/lcQLSKs5d5rsO/ufX7zSLN+ G8wA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=83dz9I3yi/wfJEU5b9z2GKjY6nDhByMKdKT60OyNpEI=; b=bW148V9kScdPQ7PoJsfUxmXqHDoGGLZjG80Dep3Q3BP3gauEfvs/sKh5BRtSMacnef 7gAuxAtOUakoo0rhKOohkLF8rjR1mJbvaO+rIxVhxhXXypf1zkJolyOoU1aSo5rmsfMu 3YNrfT8lCZZ67dwSA6czKzH/q4XVDcp9P9ypWPOc6BvBw/LKm/+MzYhvsMEduDZL3oxi SxIuIgPBS1tqVDjLXfCCf9X6VVtj4mVqc72Y/FbOXBlgHwXODH7xVEfFpqPmeYpe/75/ Vbkeo4BPaCR5GD+XDWHRrzl6Y+mN5Hk1peVcaU215/213YSa5TaVIh8nspvqNImi4bok wRDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pcYmtMmA; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id q8si34633740pfu.132.2021.10.26.10.28.50; Tue, 26 Oct 2021 10:29:03 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=pcYmtMmA; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235249AbhJZOFL (ORCPT + 99 others); Tue, 26 Oct 2021 10:05:11 -0400 Received: from mail.kernel.org ([198.145.29.99]:38912 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230119AbhJZOFK (ORCPT ); Tue, 26 Oct 2021 10:05:10 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 53D4260D07; Tue, 26 Oct 2021 14:02:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1635256966; bh=jf046iBN2pgX9ep9EdmfiHHDhGaD9VLYXiqA222lDBE=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=pcYmtMmA+B6622uCVjok524Q7axWOqq/F+FL96MUvTsTW0dyHIQncKyWnjcI3cxRA TJOIcgvOAl38iiVftPP9TCymnjc+5rzfFD41prwn27/kvOQ53iZD6aqUcbNxbwgHmi 11LMU4juimXbloLghA57nk2I45iBw5Yw1fed9Ah0MgNpxu0G4i4LEbSb5BFTbCZL51 pTohkzjOHOfdZc8tcGyR2oZzxmCDYe18Lm57/e6LG4YiFM4KqefcyBgierFYfu3MuW u3sMdOJ8uYZSyBxGDYVVUcZsjdzBtiUd4w5UjBuTELmHALggvfyqbg2xPOBszQkluz Z8fxmnr00/SbA== Date: Tue, 26 Oct 2021 07:02:41 -0700 From: Nathan Chancellor To: David Laight Cc: 'Nick Terrell' , Nick Desaulniers , "linux-kernel@vger.kernel.org" , "llvm@lists.linux.dev" Subject: Re: [PATCH] lib: zstd: Add cast to silence clang's -Wbitwise-instead-of-logical Message-ID: References: <20211021202353.2356400-1-nathan@kernel.org> <4245BD7A-4B12-4172-B4EE-76A99C717C7D@fb.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 26, 2021 at 10:34:31AM +0000, David Laight wrote: > From: Nick Terrell > > Sent: 26 October 2021 02:18 > > > > > On Oct 21, 2021, at 1:23 PM, Nathan Chancellor wrote: > > > > > > A new warning in clang warns that there is an instance where boolean > > > expressions are being used with bitwise operators instead of logical > > > ones: > > > > > > lib/zstd/decompress/huf_decompress.c:890:25: warning: use of bitwise '&' with boolean operands [- > > Wbitwise-instead-of-logical] > > > (BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished) > > > ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ > > > > > > zstd does this frequently to help with performance, as logical operators > > > have branches whereas bitwise ones do not. > ... > > > The first U32 cast is to silence an instance of -Wshorten-64-to-32 > > > because __builtin_expect() returns long so it cannot be moved. > > Isn't enabling that warning completely stupid? > The casts required to silence it could easily cause more problems > - by hiding more important bugs. And seriously affect code readability. Which warning? -Wbitwise-instead-of-logical is included in clang's -Wall and I do not think it should be disabled; this is the first instance of the warning that has been silenced with a cast. -Wshorten-64-to-32 will never be enabled for Linux but zstd is a separate project that can be built for a variety of operating systems so that has to be considered when developing changes for the kernel because the kernel changes need to go upstream eventually if they touch core zstd code, otherwise they will just get blown away on the next import. Specifically, this warning was enabled on iOS: https://github.com/facebook/zstd/pull/2062 > ...c > > > index 05570ed5f8be..5105e59ac04a 100644 > > > --- a/lib/zstd/decompress/huf_decompress.c > > > +++ b/lib/zstd/decompress/huf_decompress.c > > > @@ -886,7 +886,7 @@ HUF_decompress4X2_usingDTable_internal_body( > > > HUF_DECODE_SYMBOLX2_0(op2, &bitD2); > > > HUF_DECODE_SYMBOLX2_0(op3, &bitD3); > > > HUF_DECODE_SYMBOLX2_0(op4, &bitD4); > > > - endSignal = (U32)LIKELY( > > > + endSignal = (U32)LIKELY((U32) > > > (BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished) > > > & (BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished) > > > & (BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished) > > Isn't that the same as: > ((BIT_reload() & BIT_reload() & BIT_reload()) == BIT_DStream_unfinished) > which will generate much better code. > Especially on cpu without 'seteq' instructions. I don't think so. Feel free to double check my math. BIT_reloadDStreamFast() can return either BIT_DStream_unfinished (0) or BIT_DStream_overflow (3). Let's say the second call returns BIT_DStream_overflow but the others return BIT_DStream_unfinished. Current code: (BIT_reloadDStreamFast(&bitD1) == BIT_DStream_unfinished) & (BIT_reloadDStreamFast(&bitD2) == BIT_DStream_unfinished) & (BIT_reloadDStreamFast(&bitD3) == BIT_DStream_unfinished) (BIT_DStream_unfinished == BIT_DStream_unfinished) & (BIT_DStream_overflow == BIT_DStream_unfinished) & (BIT_DStream_unfinished == BIT_DStream_unfinished) (1 & 0 & 1) Final result: 0 Your suggestion: (BIT_reloadDStreamFast(&bitD1) & BIT_reloadDStreamFast(&bitD2) & BIT_reloadDStreamFast(&bitD3)) == BIT_DStream_unfinished (BIT_DStream_unfinished & BIT_DStream_overflow & BIT_DStream_unfinished) == BIT_DStream_unfinished (0 & 3 & 0) == 0 (0) == 0 Final result: 1 Clang 13.0.0 and GCC 11.2.0 appear agree with me: https://godbolt.org/z/M78s1TTEx Cheers, Nathan