Received: by 2002:a05:6a10:5bc5:0:0:0:0 with SMTP id os5csp254486pxb; Wed, 27 Oct 2021 02:22:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwJMycmEsEwGIUUrBFNg96DuTsZtW1CxaxD017rR1MQ4iOox2aEraSb7UfM5LTJEYoGi7sc X-Received: by 2002:a63:6a05:: with SMTP id f5mr23126285pgc.97.1635326559667; Wed, 27 Oct 2021 02:22:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1635326559; cv=none; d=google.com; s=arc-20160816; b=ufB8h6OSex53uUqz9zoo3CcW1AIpuaBA0a/+/BU+EOkFdzoAUgR2pHJ0N4B7Pk1+Ic wjOkAKOzaf/wNpGCA15DrKh+ONY2+nCzcKJXqtgYhjYCiNqyf63315seODnWZNOap3OC c1t1n77BD2ijGREnFJkYpNtdEiEZp22WjB88uKr4lrlmPZfImsWoIXu8Rv2JWcMk5mCR KB40+4S1rOgUgtP4EH7KnFkV3qoVy5ZtlyBOKKcIIEND7wA8ZPKhwixOLxkPHQ262jym YYQuHIRtBf69VVPEofcI/zr1msv2as++0wPGkGqKXMHh43hVk7Sw7lyRgcd1n1W2iQbx pTiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:organization:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=qyLmBvfmPbTqNfq5HHiiDu76l0lSRFqn7XId61IFL+E=; b=ai/2+sIVu6jiZEREIRf/f7fj1URed5ytFUBeG7MDo+gklsCZcQNEs+I6UV0OvZ5q7D 4FRSZegWVu/NVIOxijJ3iFg/KxJt0lU62KJprZu/OIfPO/lI31zXZfrXf8qLSeaeIWa8 y0JZffgVJPP2eGuSm3OIvf4FOyRFJQYknGa7zZ1mG0pC8AaZaHxiI4kqNF4gc/8U3rUK YNbnc2bQrGc9QOd65RqGc90WyA7FbZh6Xhk5C7lbpCDTLUN/uaNX70YoaZtVzi7kqbxp E6mRSrbDzhGUPQpjwkARvn4aXf5ooHA2geqLpMGm/TC2R/BJS0p4PPlG2zD4v7teY8Ng GFKw== 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=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 140si7802846pfy.240.2021.10.27.02.22.00; Wed, 27 Oct 2021 02:22:39 -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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235884AbhJZTYp (ORCPT + 99 others); Tue, 26 Oct 2021 15:24:45 -0400 Received: from mga03.intel.com ([134.134.136.65]:54678 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235162AbhJZTYn (ORCPT ); Tue, 26 Oct 2021 15:24:43 -0400 X-IronPort-AV: E=McAfee;i="6200,9189,10149"; a="229942638" X-IronPort-AV: E=Sophos;i="5.87,184,1631602800"; d="scan'208";a="229942638" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Oct 2021 12:22:19 -0700 X-IronPort-AV: E=Sophos;i="5.87,184,1631602800"; d="scan'208";a="486323056" Received: from smile.fi.intel.com ([10.237.72.184]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Oct 2021 12:22:18 -0700 Received: from andy by smile.fi.intel.com with local (Exim 4.95) (envelope-from ) id 1mfS1G-001IBg-Au; Tue, 26 Oct 2021 22:21:58 +0300 Date: Tue, 26 Oct 2021 22:21:58 +0300 From: Andy Shevchenko To: Yury Norov Cc: Qian Cai , Rasmus Villemoes , linux-kernel@vger.kernel.org Subject: Re: [PATCH] bitmap: simplify GENMASK(size - 1, 0) lines Message-ID: References: <20211026144108.35373-1-quic_qiancai@quicinc.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Organization: Intel Finland Oy - BIC 0357606-4 - Westendinkatu 7, 02160 Espoo Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Oct 26, 2021 at 11:54:16AM -0700, Yury Norov wrote: > On Tue, Oct 26, 2021 at 10:41:08AM -0400, Qian Cai wrote: > > Since "size" is an "unsigned int", the rvalue "size - 1" will still be > > "unsigned int" according to the C standard (3.2.1.5 Usual arithmetic > > conversions). Therefore, GENMASK(size - 1, 0) will always return 0UL. Those > > are also caught by GCC (W=2): > > > > ./include/linux/find.h: In function 'find_first_bit': > > ./include/linux/bits.h:25:22: warning: comparison of unsigned expression in '< 0' is always false [-Wtype-limits] > > 25 | __is_constexpr((l) > (h)), (l) > (h), 0))) > > | ^ > > ./include/linux/build_bug.h:16:62: note: in definition of macro 'BUILD_BUG_ON_ZERO' > > 16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); }))) > > | ^ > > ./include/linux/bits.h:25:3: note: in expansion of macro '__is_constexpr' > > 25 | __is_constexpr((l) > (h)), (l) > (h), 0))) > > | ^~~~~~~~~~~~~~ > > ./include/linux/bits.h:38:3: note: in expansion of macro 'GENMASK_INPUT_CHECK' > > 38 | (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l)) > > | ^~~~~~~~~~~~~~~~~~~ > > ./include/linux/find.h:119:31: note: in expansion of macro 'GENMASK' > > 119 | unsigned long val = *addr & GENMASK(size - 1, 0); > > | ^~~~~~~ > > ./include/linux/bits.h:25:34: warning: comparison of unsigned expression in '< 0' is always false [-Wtype-limits] > > 25 | __is_constexpr((l) > (h)), (l) > (h), 0))) > > | ^ > > ./include/linux/build_bug.h:16:62: note: in definition of macro 'BUILD_BUG_ON_ZERO' > > 16 | #define BUILD_BUG_ON_ZERO(e) ((int)(sizeof(struct { int:(-!!(e)); }))) > > | ^ > > ./include/linux/bits.h:38:3: note: in expansion of macro 'GENMASK_INPUT_CHECK' > > 38 | (GENMASK_INPUT_CHECK(h, l) + __GENMASK(h, l)) > > | ^~~~~~~~~~~~~~~~~~~ > > ./include/linux/find.h:119:31: note: in expansion of macro 'GENMASK' > > 119 | unsigned long val = *addr & GENMASK(size - 1, 0); > > | ^~~~~~~ > > > > Signed-off-by: Qian Cai > > --- > > include/linux/find.h | 28 ++++++++-------------------- > > 1 file changed, 8 insertions(+), 20 deletions(-) > > > > diff --git a/include/linux/find.h b/include/linux/find.h > > index 5bb6db213bcb..5ce2b17aea42 100644 > > --- a/include/linux/find.h > > +++ b/include/linux/find.h > > @@ -115,11 +115,8 @@ unsigned long find_next_zero_bit(const unsigned long *addr, unsigned long size, > > static inline > > unsigned long find_first_bit(const unsigned long *addr, unsigned long size) > > { > > - if (small_const_nbits(size)) { > > - unsigned long val = *addr & GENMASK(size - 1, 0); > > - > > - return val ? __ffs(val) : size; > > - } > > + if (small_const_nbits(size)) > > + return size; > > > > return _find_first_bit(addr, size); > > } > > [...] > > Nice catch! I'm a bit concerned that small_const_nbits() will never > allow GENMASK() to be passed with size == 0, but the patch looks > good to me overall. Can you explain to me how it is supposed to work? For example, x = 0xaa55; size = 5; printf("%lu\n", find_first_bit(&x, size)); In the resulting code we will always have 5 as the result, but is it correct one? -- With Best Regards, Andy Shevchenko