Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1039692iob; Fri, 13 May 2022 20:40:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyIiZ8kkjJZrBaRxj+bgsOkfIighuxHVdsgCykgAnkDjP0h6uEm4l/MH5w1V+9rNZVbsOSv X-Received: by 2002:a05:6000:154b:b0:20c:4ca6:6f18 with SMTP id 11-20020a056000154b00b0020c4ca66f18mr6081164wry.704.1652499645826; Fri, 13 May 2022 20:40:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652499645; cv=none; d=google.com; s=arc-20160816; b=ppdtT0p0xZwpKrGup7P30FRloqWH8cxrbAW8VZbF8T7ULIp1kamlD1w5xwG2ltaSqp h8h9LLOOU1qXoNgiBLDHlGoGUhXBlFHA1DnZgkn3ZxXOf13UZZ1RFHvEVQ4rqXZYf7lv zVBKYFoDe7JPf/7feUdItObRPbIPbL5k1gBfcFXbwid5FF/II9glBOwu8g2TOjTj+3GU rwFcLZPGum9IPxW5Q8SFop9SRzH8LJO9hynjDRhcdVpze+5GQgdMdGxu+ViR70iP+eD1 MgsxzFKDlIGDkdj8KjiSA03KwHUV2DZNGOwoEGBbqrkjMirAIk9cgubYZXBwqETkmkzJ BXow== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=kT23OD19fnHBDjgJve1hWxT+JouavJxyOs0YPduJlJo=; b=zhHKvCACfEqJrsrATx2C2Qqjv42KEP2eFzo3IxUsb1x6fKU66EBQXKArBYBxpkZeYQ Zm6Qd5M2sWMcq+WUbvfnJnR9sKRkVxXZ+Mfeq1AGMCsw5sesDjhraIY8PRAbcz7Nge3j aMDEeMtMuCim1Lz/a5Vmk453ASt/YMuAfKLxQG06mnC5g+hOS6ZjCb6JQ+0mQIXFrPS2 o8vBvqn0izAQUvYPK/HrYBNYIs3fwkxkL6gkXj9BpxurDhbw9gGpufOBGsL9F5ggZn0U /E5BZFfuLzmqn7jilXMmxwS0aMNOCqr+HkClLSY0lYm5IVeQhjvonqw+qd6FC5U27aE5 dZWA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id j39-20020a05600c1c2700b0038c77be9cf5si4191563wms.189.2022.05.13.20.40.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 20:40:45 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 6CA9C32B870; Fri, 13 May 2022 17:15:52 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242764AbiELEaP convert rfc822-to-8bit (ORCPT + 99 others); Thu, 12 May 2022 00:30:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243795AbiELEaN (ORCPT ); Thu, 12 May 2022 00:30:13 -0400 Received: from mail-yw1-f182.google.com (mail-yw1-f182.google.com [209.85.128.182]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 45F2620F9D3 for ; Wed, 11 May 2022 21:29:59 -0700 (PDT) Received: by mail-yw1-f182.google.com with SMTP id 00721157ae682-2f7d621d1caso42644237b3.11 for ; Wed, 11 May 2022 21:29:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=XqBqQ+XRvyAHUyGfBZIABF5Embq5Xn1tLFaDAHrehy0=; b=SjYSlNjradb8xsdLxJrOoO57gFeddQhIN7aSvVVzAr3NT12jd7yBDjBlmIYGMOtqtC 0iDfIr5XOW0Gx8OreHZQEowRFS6Kwyp5bHWwucndMYw28JWVAtgGv7vIj/+5C6bxrQUg 4E508vMZQYXCo+qL/QqnlHZBRV5VXbKuP50S2ZsWbjzgO/rIw3LnwH5VZYkZ6ipQpb9M Qk6UT6e+U43L6L+SB6lbNsOyJr67y5TDV7h1Z2P/yeBPTlO5zYy5LkYTwCOpLDxkfyoi 6TUF9DlS0fefptZLpjkRUTOsVLAg6OSnpTaBdYEkLisKkaAZTQKN328QrLOd7UdpiGtq kdjA== X-Gm-Message-State: AOAM532XDoYwROjJ4bvZLTRNI0HnHteNVD7euhLHM7FkSRpIj1/9CwVy CmIpHTzOH4NDKsBGI3hzzYE8D1IbFW6U1lOOZVA= X-Received: by 2002:a81:34f:0:b0:2f7:bbb1:1576 with SMTP id 76-20020a81034f000000b002f7bbb11576mr28802714ywd.45.1652329798501; Wed, 11 May 2022 21:29:58 -0700 (PDT) MIME-Version: 1.0 References: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> <20220512011855.1189653-1-mailhol.vincent@wanadoo.fr> <20220512011855.1189653-2-mailhol.vincent@wanadoo.fr> <154f41707c58acdac26c3300c5b429f381c45708.camel@perches.com> In-Reply-To: <154f41707c58acdac26c3300c5b429f381c45708.camel@perches.com> From: Vincent MAILHOL Date: Thu, 12 May 2022 13:29:47 +0900 Message-ID: Subject: Re: [PATCH v4 1/2] x86/asm/bitops: ffs: use __builtin_ffs to evaluate constant expressions To: Joe Perches Cc: Nick Desaulniers , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H . Peter Anvin" , Nathan Chancellor , Tom Rix , linux-kernel@vger.kernel.org, llvm@lists.linux.dev, David Howells , Jan Beulich , Christophe JAILLET Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Thu. 12 May 2022 at 12:02, Joe Perches wrote: > On Thu, 2022-05-12 at 10:18 +0900, Vincent Mailhol wrote: > > For x86_64, the current ffs() implementation does not produce > > optimized code when called with a constant expression. On the > > contrary, the __builtin_ffs() function of both GCC and clang is able > > to simplify the expression into a single instruction. > [] > > -static __always_inline int ffs(int x) > > +static __always_inline int variable_ffs(int x) > > { > > int r; > > > > @@ -310,6 +299,19 @@ static __always_inline int ffs(int x) > > return r + 1; > > } > > > > +/** > > + * ffs - find first set bit in word > > + * @x: the word to search > > + * > > + * This is defined the same way as the libc and compiler builtin ffs > > + * routines, therefore differs in spirit from the other bitops. > > + * > > + * ffs(value) returns 0 if value is 0 or the position of the first > > + * set bit if value is nonzero. The first (least significant) bit > > + * is at position 1. > > + */ > > +#define ffs(x) (__builtin_constant_p(x) ? __builtin_ffs(x) : variable_ffs(x)) > > How about not defining another function and using parentheses around > the function definition to avoid the macro expansion like: > > #define ffs(x) (__builtin_constant_p(x) ? __builtin_ffs(x) : ffs(x)) > > and > > static __always_inline int (ffs)(int x) > { > etc... > } Sorry, but I don’t really like this approach. Main issue I see is that this code will emit a -Wshadow warning. And using parentheses around the function definition just seems an obscure hack to me. The variable_foo() gives me less headache. Was this pattern ever used anywhere else in the kernel? Yours sincerely, Vincent Mailhol