Received: by 2002:a6b:500f:0:0:0:0:0 with SMTP id e15csp1015811iob; Fri, 13 May 2022 19:43:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyvpCbQhq36iWG5X16MEk9ZyVrv6wYkUhDlKSp5jDX5Zdjo9zhclmBLr3UmcbON5XY/xyDE X-Received: by 2002:adf:f543:0:b0:20a:e059:2f80 with SMTP id j3-20020adff543000000b0020ae0592f80mr6170127wrp.495.1652496226549; Fri, 13 May 2022 19:43:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1652496226; cv=none; d=google.com; s=arc-20160816; b=PKIxD9/JcW8YoKFQM6gbNgejsBGWdd2F3fdtcaddHdgm3mIXCMC9FCZv4kKlqYvRL1 ZHxgYwtc4Sff+ocQSyKCuXaAqBZE/LmSLGDFnEoIs92b8RedIulyXcbLRlVLf+LwkhdX gn8ff2NpNKuVhAVLFQfjs7YaRacGZIrFfI5Q7oVLvcEwm5GMJPmmN9qOo6USnpPprEiz Io7FO1wPFW7EuVAUMFyD4RyqayYcv5fsQulLsBvovS7Nk39iwuSyViM5igkIMWAFnMf+ D3I+E6b/G4xd32ryzcXdNnYdH76wpXAAw5KsIgB6gmFpIQ16EZvF8OdB1MLuAMnzB8Xv 8FHA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:date:cc:to:from:subject :message-id; bh=C64GDHO9J6LtMi9AATdLwOEL+DcYTzgDuEv6C4O/wCc=; b=SFCis1HTu4ZIMraHvsBKrmLBBYbpXDqWiv1/mnSMfqWRvcS1JPcOPDkG+fuXmqQN3S oB7j+AzvOnZ6HeSKU5dTQQXXTaJKljAmkF1c3aiXxeVI2bOru6EmYyeWFvWWhajCmwD/ bv9PNRuKhGPJXlP6OAFZdKFvHHxhtlizAgGyX9SXLpgHWKWflsu9kriSg7KTWniHAPgp 0iMOpqpE7iYDtQP/82vQ9LTbWQ6csiyNPwlIkz8rSJAElURAEK2dkVh4pOuOrb0jG3AP xfkFArN3MxpKYiCgmqW8FcGRhUZe2Ecvpss1EhkRYi5QXVlr/8fIpHDj1EAEKfo/LNx0 CD8w== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id d13-20020a05600c34cd00b0039447ab4d17si4033818wmq.52.2022.05.13.19.43.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 May 2022 19:43:46 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 1E505354583; Fri, 13 May 2022 16:31:42 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242617AbiELDCi (ORCPT + 99 others); Wed, 11 May 2022 23:02:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57088 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239781AbiELDCf (ORCPT ); Wed, 11 May 2022 23:02:35 -0400 Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4A0E46668A for ; Wed, 11 May 2022 20:02:34 -0700 (PDT) Received: from omf09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id E7D8B36F; Thu, 12 May 2022 03:02:31 +0000 (UTC) Received: from [HIDDEN] (Authenticated sender: joe@perches.com) by omf09.hostedemail.com (Postfix) with ESMTPA id EDF332002B; Thu, 12 May 2022 03:02:28 +0000 (UTC) Message-ID: <154f41707c58acdac26c3300c5b429f381c45708.camel@perches.com> Subject: Re: [PATCH v4 1/2] x86/asm/bitops: ffs: use __builtin_ffs to evaluate constant expressions From: Joe Perches To: Vincent Mailhol , Nick Desaulniers , Thomas Gleixner , Ingo Molnar , Borislav Petkov Cc: 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 Date: Wed, 11 May 2022 20:02:28 -0700 In-Reply-To: <20220512011855.1189653-2-mailhol.vincent@wanadoo.fr> References: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> <20220512011855.1189653-1-mailhol.vincent@wanadoo.fr> <20220512011855.1189653-2-mailhol.vincent@wanadoo.fr> Content-Type: text/plain; charset="ISO-8859-1" User-Agent: Evolution 3.40.4-1ubuntu2 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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,UNPARSEABLE_RELAY autolearn=no autolearn_force=no version=3.4.6 X-Rspamd-Server: rspamout07 X-Rspamd-Queue-Id: EDF332002B X-Stat-Signature: s3ny7ydgms8u5bhpy6dseb6xt83hmmrk X-Session-Marker: 6A6F6540706572636865732E636F6D X-Session-ID: U2FsdGVkX1/YVM8TxruqLdoykQoTANU1GScIYiXrWm4= X-HE-Tag: 1652324548-95630 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, 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... }