Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp6351505iob; Tue, 10 May 2022 16:49:10 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwUOgL0cd5331bW48/s7cowPNucdQDNwpHlJbIkH9zymCxmn26Bsnn4nWaGRudniHUJYANV X-Received: by 2002:a17:90b:164a:b0:1dc:981d:f197 with SMTP id il10-20020a17090b164a00b001dc981df197mr2340517pjb.228.1652226549858; Tue, 10 May 2022 16:49:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652226549; cv=none; d=google.com; s=arc-20160816; b=oGPRemvp3BSAr24KI9HMM3UYViBC8xLrGCqZuuee+02FokWmh11uaSQBouwfaSqB4d y5cU6LD6YJQUNTLFTo0LU09KDoDwd+Pu7ix9NrCltHzOoRwuW5izzTQ2xcDKnk9XLRUw hjnsKpRkwXC6AnyvzQMMREQLiPaLWITO989J1/D7YFkal674YeZTRfa3u0VsqnGcfYgw NkOci5ugJvN0o9v9CDoG3OplWOUTz//rrzE3cD1sMlWqOdD7+kHWS14gh2RJnmut8pte CA2vfg4GPh/c69qBMu0QtqV2el/IWcmiR7mDxFJAdQ/6u/uclOz0AshVtBQ17XrXkopb AY9w== 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=yhVqDTExWoz6ItXB09D+IajghtNf4zxV3xnD5/jJ9ho=; b=sz/WcrdD6H51E0dTMOjGvwZLlhnmtPRwEH/GxZK+ftvDF3EM4vFLp1+7zm3sGmlOj3 TplgIQP03QPibU8Ro/C9fz6Nv3hS+DoNgJM+TKHHaE+Pe5EsK75lAIY9PLnsDAzBSTr2 5/eSB9/9d80oZRFu0kp0QbZyENkSNE6ATyZHdyQOlqr4hgTH2jEvj3cY+sZ9p9H8X9fx xN86YTpSfRH5f/onqC7s7c1SCuZ1cYoShXZURv/fSMPlwfNpxh825RvS9+rt23rJsc+a YgfMQC8kHR/cSRajQw2qOxJyLbfLnrqWa+AT1EufPY8bV9tdEXn0pRR+AmE51Do7ry2Y MYnA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=YtU3JpNg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f14-20020a63510e000000b003ab4cd521ccsi820196pgb.539.2022.05.10.16.48.58; Tue, 10 May 2022 16:49:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b=YtU3JpNg; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244263AbiEJTLj (ORCPT + 99 others); Tue, 10 May 2022 15:11:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37572 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239684AbiEJTLc (ORCPT ); Tue, 10 May 2022 15:11:32 -0400 Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E2AB32532C0 for ; Tue, 10 May 2022 12:11:30 -0700 (PDT) Received: by mail-il1-x132.google.com with SMTP id f5so11986906ilj.13 for ; Tue, 10 May 2022 12:11:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=yhVqDTExWoz6ItXB09D+IajghtNf4zxV3xnD5/jJ9ho=; b=YtU3JpNg0d4+TwtvZUMQoB8QGjkNFayCqDkhoy5HhfzjqkvZZlaHweYW4LZr2tevSq oCP8b4Eu6Rhjr4a9IsQ6QbKdW4YPgjDXS9PEBJ0wuOgnusmSUohF2R5oEnWBfjA/dOTI b9vz3uqPUeogLKXkrjlqdzTmz3x95hKu4QjZoyuBnSfMBwN3f0G5F4ezPCB19pUXACmi tKzg+22dP+rnB7n+MLTiuats9FGitXRL2rll2xmvQi9dDkXUjOIVsC9AYs83X661WEah Bz8AfZo1A9pbr3pIEhwqi/MhLrQxH/PYAIolC6+sbgz11qo94NcLkQzfTbdN+rJW0iyp +AKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=yhVqDTExWoz6ItXB09D+IajghtNf4zxV3xnD5/jJ9ho=; b=Zgjoq8lUPdELH9BM03qZmcLi2whpVXt8UlR1K2WBM43+XKoN7XSIP/eRj+YlO/ayr0 Ts7Z5Z6FfkT81yX+GQPDjpWuN+vRzeQUmrfgZdcHx2ur45VvdKQ3WdPS1lxaBn3l6jBL SF9T2Zccc6RJ+h/EZ4sxP1Mls+B04/7rSmSVaG3dLDvtZTATp8ZKyLJyKs988AlbRHgo 4y2BkJ5kLdSMAWXK5wyA5Fmm7SO7alWSIhhtpylxvE+tzAXzKGjAISjXBsRWMmolQ7ci /m10M9BccVTnsUW75ImEfZDpUNWYEuAUua0LYfSuM2QY5W+gGhp+UOwSx1GJJGYR9j3M 6uZg== X-Gm-Message-State: AOAM532pmBrB/R1sL5JxXk83AtBA8tKLQUb4tULoc8ZbjsaMUL6jJzC6 /bcvP7eEwA/0uumLUNexEzc= X-Received: by 2002:a92:7309:0:b0:2ca:8c9c:752d with SMTP id o9-20020a927309000000b002ca8c9c752dmr10040741ilc.151.1652209890213; Tue, 10 May 2022 12:11:30 -0700 (PDT) Received: from localhost ([12.28.44.171]) by smtp.gmail.com with ESMTPSA id g100-20020a02856d000000b0032dba44218asm994547jai.166.2022.05.10.12.11.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 10 May 2022 12:11:29 -0700 (PDT) Date: Tue, 10 May 2022 12:11:27 -0700 From: Yury Norov To: David Laight Cc: 'Alexei Starovoitov' , Andy Shevchenko , Greg Kroah-Hartman , Joe Perches , Julia Lawall , =?utf-8?B?TWljaGHFgiBNaXJvc8WCYXc=?= , Nicholas Piggin , Nicolas Palix , Peter Zijlstra , Rasmus Villemoes , Matti Vaittinen , LKML , Alexei Starovoitov , Andrew Morton , Chris Zankel , Christophe Leroy , "Eric W . Biederman" , Kumar Kartikeya Dwivedi , Max Filippov , Toke =?iso-8859-1?Q?H=F8iland-J=F8rgensen?= , "linux-xtensa@linux-xtensa.org" Subject: Re: [PATCH 08/22] bitops: introduce MANY_BITS() macro Message-ID: References: <20220510154750.212913-1-yury.norov@gmail.com> <20220510154750.212913-9-yury.norov@gmail.com> <3be064fe804845e4aeaca8b1d45ddf0a@AcuMS.aculab.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <3be064fe804845e4aeaca8b1d45ddf0a@AcuMS.aculab.com> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, May 10, 2022 at 05:54:13PM +0000, David Laight wrote: > From: Alexei Starovoitov > > Sent: 10 May 2022 17:51 > ... > > +/* Return: nonzero if 2 or more bits are set */ > > +#define MANY_BITS(n) ((n) & ((n) - 1)) > > You can't have a macro that expands its argument twice. Yes, I'll fix it. > ... > > > static inline __attribute__((const)) > > > bool is_power_of_2(unsigned long n) > > > { > > > - return (n != 0 && ((n & (n - 1)) == 0)); > > > + return n != 0 && !MANY_BITS(n); > > > } > > > > Please don't. Open coded version is much easier to read. To me the human-readable version is easier to read. Still, if you thing that n & (n - 1) is simpler, what for this function is needed at all? > Especially if you remove all the spare parenthesis. > return n && !(n & (n - 1)); > > I bet a lot of callers know the value is non-zero. > > I suspect you'll find at least one caller that uses > is_power_of_2() assuming it is !(n & (n - 1)) and > so is wrong for zero. Another thing is that despite __attribute__(const), gcc sometimes doesn't recognize it as constant expression, and people have to workaround it. XTENSA is the example for 1st case, and for the 2nd: arch/powerpc/mm/init-common.c: unsigned long minalign = max(MAX_PGTABLE_INDEX_SIZE + 1, HUGEPD_SHIFT_MASK + 1); /* It would be nice if this was a BUILD_BUG_ON(), but at the * moment, gcc doesn't seem to recognize is_power_of_2 * as a constant expression, so so much for that. */ BUG_ON(!is_power_of_2(minalign)); This convinced me that we need a simple macro that is decoupled with pow_2 semantics and can be used in another macros like BUILD_BUG_ON(). Thanks, Yury