From nobody Fri May 8 03:49:55 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 5C9BBC433FE for ; Wed, 11 May 2022 16:04:09 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344240AbiEKQEH (ORCPT ); Wed, 11 May 2022 12:04:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44862 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344229AbiEKQD5 (ORCPT ); Wed, 11 May 2022 12:03:57 -0400 Received: from mail-pj1-x1035.google.com (mail-pj1-x1035.google.com [IPv6:2607:f8b0:4864:20::1035]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 484CE19B3AF for ; Wed, 11 May 2022 09:03:56 -0700 (PDT) Received: by mail-pj1-x1035.google.com with SMTP id e24so2664297pjt.2 for ; Wed, 11 May 2022 09:03:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=vuLyis0cozB0HH3xDqQBXVh/FgY1uFVWgKAFWAeIWvM=; b=hhVPLl9jZ2ypnIaMCtBrwRQ4MD1YdrdEuYR/XyRJ2cz2URMLWcQbvVOHcAcJIUDqqh KlYavh4iDSxiPIVuLaGm7hExlyARUyFBGfw+LZROEaSo4UoNF7xEEmJl5dehfx8+octD OM+wh/70MPIZcCZ+wobJd+lCCLG5SWsICcfjp9jXT9fDNrzTrAAUEc9aZhScirSFlh7b U+0DvC6+FowLArpf4ER7XRHbllK+2yvfz+FcDoZL28yndF6a1i+BSJ7ButQUPGpbs2Ug lDsne26hSlf9dzvCaj+qVPa5QBZCLpW01k4xxvpTBhh3GB/ac7IaC2nEl41WTL19aLZG YWDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=vuLyis0cozB0HH3xDqQBXVh/FgY1uFVWgKAFWAeIWvM=; b=ColCrIhBAjuLrqrzM70PWkLBXvjWiG63+SCw2LZGbj4Sby69skc8Ag0ZI5QaQfBwXU uKaNbpXtUxYqlVWnadwcQYQUs6paP8S8D2KAbE/7fMKerd7w8Dy9jtSQs3qwcCsUo/Tj z0u+WKk9CoZqIGuXdxDMkOFHobPp4sOEia1yrTMU5KUpIz46gdkzA7cVvKdXNyKVlXQ5 Yj25Auhll82BtZsHPvutalFlDvOa0vPtoibenbYH5H7x/JKKiXITu1OLO9T1+DZIWySW ysXvX7WJTPYvFbJpWs8iU78HAmEYK138HDPY1SJ3ajK+MpiyB9hOHDcyTB6ErY4GKwMp v0ow== X-Gm-Message-State: AOAM531BIjDvnYRx8IR4SWevPnWktcKo+g6OzJ3JkIKGmOsRw4+/XRBw ClphRRsrbYh8BoGQKGwLOoM= X-Google-Smtp-Source: ABdhPJxWqrFU+wrf1f3fcmudbr5H+AxqToGCpCuwHOM5wSjMkxBiclhBPlICHx+V9Z0uhmMF3z6nRA== X-Received: by 2002:a17:903:189:b0:15e:9584:fbe7 with SMTP id z9-20020a170903018900b0015e9584fbe7mr26565542plg.65.1652285035170; Wed, 11 May 2022 09:03:55 -0700 (PDT) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id w11-20020a63c10b000000b003c14af50624sm42825pgf.60.2022.05.11.09.03.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 09:03:54 -0700 (PDT) Sender: Vincent Mailhol From: Vincent Mailhol To: 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 , Vincent Mailhol Subject: [PATCH v2 1/2] x86/asm/bitops: ffs: use __builtin_ffs to evaluate constant expressions Date: Thu, 12 May 2022 01:03:18 +0900 Message-Id: <20220511160319.1045812-2-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> References: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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. * Example * Let's consider two dummy functions foo() and bar() as below: | #include | #define CONST 0x01000000 | | unsigned int foo(void) | { | return ffs(CONST); | } | | unsigned int bar(void) | { | return __builtin_ffs(CONST); | } GCC would produce below assembly code: | 0000000000000000 : | 0: ba 00 00 00 01 mov $0x1000000,%edx | 5: b8 ff ff ff ff mov $0xffffffff,%eax | a: 0f bc c2 bsf %edx,%eax | d: 83 c0 01 add $0x1,%eax | 10: c3 ret | 11: 66 66 2e 0f 1f 84 00 data16 cs nopw 0x0(%rax,%rax,1) | 18: 00 00 00 00 | 1c: 0f 1f 40 00 nopl 0x0(%rax) | | 0000000000000020 : | 20: b8 19 00 00 00 mov $0x19,%eax | 25: c3 ret And clang would produce: | 0000000000000000 : | 0: b8 ff ff ff ff mov $0xffffffff,%eax | 5: 0f bc 05 00 00 00 00 bsf 0x0(%rip),%eax # c | c: 83 c0 01 add $0x1,%eax | f: c3 ret | | 0000000000000010 : | 10: b8 19 00 00 00 mov $0x19,%eax | 15: c3 ret For both example, we clearly see the benefit of using __builtin_ffs() instead of the kernel's asm implementation for constant expressions. However, for non constant expressions, the ffs() asm version of the kernel remains better for x86_64 because, contrary to GCC, it doesn't emit the CMOV assembly instruction, c.f. [1] (noticeably, clang is able optimize out the CMOV call). This patch uses the __builtin_constant_p() to select between the kernel's ffs() and the __builtin_ffs() depending on whether the argument is constant or not. As a side benefit, this patch also removes below -Wshadow warning: | ./arch/x86/include/asm/bitops.h:283:28: warning: declaration of 'ffs' sha= dows a built-in function [-Wshadow] | 283 | static __always_inline int ffs(int x) ** Statistics ** On a allyesconfig, before applying this patch...: | $ objdump -d vmlinux.o | grep bsf | wc -l | 3607 ...and after: | $ objdump -d vmlinux.o | grep bsf | wc -l | 792 So, roughly 26.7% of the call to ffs() were using constant expression and were optimized out. (tests done on linux v5.18-rc5 x86_64 using GCC 11.2.1) [1] commit ca3d30cc02f7 ("x86_64, asm: Optimise fls(), ffs() and fls64()") http://lkml.kernel.org/r/20111213145654.14362.39868.stgit@warthog.procyon.o= rg.uk CC: Nick Desaulniers Signed-off-by: Vincent Mailhol Reviewed-by: Nick Desaulniers --- arch/x86/include/asm/bitops.h | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h index a288ecd230ab..6ed979547086 100644 --- a/arch/x86/include/asm/bitops.h +++ b/arch/x86/include/asm/bitops.h @@ -269,18 +269,7 @@ static __always_inline unsigned long __fls(unsigned lo= ng word) #undef ADDR =20 #ifdef __KERNEL__ -/** - * 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. - */ -static __always_inline int ffs(int x) +static __always_inline int variable_ffs(int x) { int r; =20 @@ -310,6 +299,19 @@ static __always_inline int ffs(int x) return r + 1; } =20 +/** + * 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)) + /** * fls - find last set bit in word * @x: the word to search --=20 2.35.1 From nobody Fri May 8 03:49:55 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7D505C433EF for ; Wed, 11 May 2022 16:04:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344295AbiEKQET (ORCPT ); Wed, 11 May 2022 12:04:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46110 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344256AbiEKQEM (ORCPT ); Wed, 11 May 2022 12:04:12 -0400 Received: from mail-pg1-x533.google.com (mail-pg1-x533.google.com [IPv6:2607:f8b0:4864:20::533]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2700E19C3AA for ; Wed, 11 May 2022 09:04:09 -0700 (PDT) Received: by mail-pg1-x533.google.com with SMTP id a19so1966860pgw.6 for ; Wed, 11 May 2022 09:04:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=sender:from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Ia+/x8MZ9E6VPhirjhPy6yDuf0hwRPmpcA5xcDM4PWc=; b=GrAs7bFYRhllSkDU1AB4GkixpiQjuApCo3+JdF+OkbWvQVUM/s4MB4LEm/3A2fwumk Or6ebC96Eobbz4ZkeZdIauQAWhZ1j7wKcooIg9jBl3T5rj+6it/3UxmtldYjyB4AzSSQ pjmAEYYytKjs55ZJHMAPN0arbZiJtHQgYXKSdaF8kJzlwobKQbqdiXIpyJx7xS9eekJF ECFiI/82/12BOUH1csVQGDmw4PqC99ERrc/mA5YoWE1Mg4SDQ2qvqZd3N2bzeRsJeNsx HGPO/ABm+LhhRSfwG2cVkWjl01/MEgMCnErWELrIdRD/D0zVucdiZhmQDGO2vsxugJ/S gedw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:sender:from:to:cc:subject:date:message-id :in-reply-to:references:mime-version:content-transfer-encoding; bh=Ia+/x8MZ9E6VPhirjhPy6yDuf0hwRPmpcA5xcDM4PWc=; b=MhtacQm3N9v9QL7AlbWDRbGjQx6k/s9ZiwDVoFE0pWYC34AaslPrEJQ8yKcOeAJ/Be yh6B0CosoyPPTyNPAEGkKp6q6ht8kW+AzeqzQ/KzKZoR3ljvzfxajp27PcJCtbPDG0ye K1EzfUVi26WWTSnPUJEJo880V7ZOGg1HsDSWQ/E/9/loGJydSGS4hgZXbwF9LCrU0Nq/ CFThJOtNHqn7GUXpTb6+7wiFrpb8vIvbNL13yrXttcuD+adkJRNMjVBWQxPVm//mWE+W qtpvVhmHYnlXza7/rd9/gnSSO0HWvf/tv0Oz4zbJfzQRj5zMK8NYsxQr0YL41LgPQCpj Hvtw== X-Gm-Message-State: AOAM530vk9Lz/tib2HFxeelGWSGfeJA9WNEHWvkfS61+ErIRpcP81mGm 92usnSYmokI3EnPQuLHpzp9P0cuqdlkz5DhJ X-Google-Smtp-Source: ABdhPJygAUxl6K/K6Rudkipc4SNLTGDQMpOuCCcX3+DgENy853haMZz3csLQwlj4mQrXrARdCIQh0Q== X-Received: by 2002:a05:6a00:158c:b0:50f:83c5:c147 with SMTP id u12-20020a056a00158c00b0050f83c5c147mr25398144pfk.44.1652285048355; Wed, 11 May 2022 09:04:08 -0700 (PDT) Received: from localhost.localdomain (124x33x176x97.ap124.ftth.ucom.ne.jp. [124.33.176.97]) by smtp.gmail.com with ESMTPSA id w11-20020a63c10b000000b003c14af50624sm42825pgf.60.2022.05.11.09.04.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 May 2022 09:04:08 -0700 (PDT) Sender: Vincent Mailhol From: Vincent Mailhol To: 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 , Vincent Mailhol Subject: [PATCH v2 2/2] x86/asm/bitops: __ffs,ffz: use __builtin_ctzl to evaluate constant expressions Date: Thu, 12 May 2022 01:03:19 +0900 Message-Id: <20220511160319.1045812-3-mailhol.vincent@wanadoo.fr> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> References: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" __ffs(x) is equivalent to (unsigned long)__builtin_ctzl(x) and ffz(x) is equivalent to (unsigned long)__builtin_ctzl(~x). Because __builting_ctzl() returns an int, a cast to (unsigned long) is necessary to avoid potential warnings on implicit casts. For x86_64, the current __ffs() and ffz() implementations do not produce optimized code when called with a constant expression. On the contrary, the __builtin_ctzl() gets simplified into a single instruction. However, for non constant expressions, the __ffs() and ffz() asm versions of the kernel remains slightly better than the code produced by GCC (it produces a useless instruction to clear eax). This patch uses the __builtin_constant_p() to select between the kernel's __ffs()/ffz() and the __builtin_ctzl() depending on whether the argument is constant or not. ** Statistics ** On a allyesconfig, before applying this patch...: | $ objdump -d vmlinux.o | grep tzcnt | wc -l | 3607 ...and after: | $ objdump -d vmlinux.o | grep tzcnt | wc -l | 2600 So, roughly 27.9% of the call to either __ffs() or ffz() were using constant expression and were optimized out. (tests done on linux v5.18-rc5 x86_64 using GCC 11.2.1) Note: on x86_64, the asm bsf instruction produces tzcnt when used with the ret prefix (which is why we grep tzcnt instead of bsf in above benchmark). c.f. [1] [1] commit e26a44a2d618 ("x86: Use REP BSF unconditionally") http://lkml.kernel.org/r/5058741E020000780009C014@nat28.tlf.novell.com CC: Nick Desaulniers Signed-off-by: Vincent Mailhol --- arch/x86/include/asm/bitops.h | 38 ++++++++++++++++++++++------------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h index 6ed979547086..7cf5374ce403 100644 --- a/arch/x86/include/asm/bitops.h +++ b/arch/x86/include/asm/bitops.h @@ -224,13 +224,7 @@ static __always_inline bool variable_test_bit(long nr,= volatile const unsigned l ? constant_test_bit((nr), (addr)) \ : variable_test_bit((nr), (addr))) =20 -/** - * __ffs - find first set bit in word - * @word: The word to search - * - * Undefined if no bit exists, so code should check against 0 first. - */ -static __always_inline unsigned long __ffs(unsigned long word) +static __always_inline unsigned long __variable_ffs(unsigned long word) { asm("rep; bsf %1,%0" : "=3Dr" (word) @@ -238,13 +232,18 @@ static __always_inline unsigned long __ffs(unsigned l= ong word) return word; } =20 -/** - * ffz - find first zero bit in word - * @word: The word to search - * - * Undefined if no zero exists, so code should check against ~0UL first. - */ -static __always_inline unsigned long ffz(unsigned long word) +/** + * __ffs - find first set bit in word + * @word: The word to search + * + * Undefined if no bit exists, so code should check against 0 first. + */ +#define __ffs(word) \ + (__builtin_constant_p(word) ? \ + (unsigned long)__builtin_ctzl(word) : \ + __variable_ffs(word)) + +static __always_inline unsigned long __variable_ffz(unsigned long word) { asm("rep; bsf %1,%0" : "=3Dr" (word) @@ -252,6 +251,17 @@ static __always_inline unsigned long ffz(unsigned long= word) return word; } =20 +/** + * ffz - find first zero bit in word + * @word: The word to search + * + * Undefined if no zero exists, so code should check against ~0UL first. + */ +#define ffz(word) \ + (__builtin_constant_p(word) ? \ + (unsigned long)__builtin_ctzl(~word) : \ + __variable_ffz(word)) + /* * __fls: find last set bit in word * @word: The word to search --=20 2.35.1 From nobody Fri May 8 03:49:55 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 77E56C6FA82 for ; Tue, 20 Sep 2022 20:00:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230287AbiITUA3 (ORCPT ); Tue, 20 Sep 2022 16:00:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229670AbiITUAZ (ORCPT ); Tue, 20 Sep 2022 16:00:25 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25B163ED69; Tue, 20 Sep 2022 13:00:22 -0700 (PDT) Date: Tue, 20 Sep 2022 20:00:18 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1663704020; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F6KfugY/ZF2k+N60z0DOxutQrzRQ1q7GMoUe0LhivhQ=; b=zELUirONf6R7vie97BPX9AnZ2VjamZSksRRaV08+zTfKtYk2/Bg/N0zCYxoctMLaHRNwgR zqf2SV++qm5sffkAVZE+PcQuBHsONk0Rx7uGF9JOeSY5Ain9WLS9HV4IcdSVE59YGyJjb7 1GMTXDHBimKOhZKrrROP4DcVfBCDg1jVWabN5RxOT2Ktv+78jZw/suO90wyh2BA507sfhJ kpYemMp44QmYb+yb7LkRlRm83vK6q5w9hjHmPVlw83xoPNSk6dQe4mw9B0Rqm6vChvtvAV 1Eju7NA63+0CRnRlyC/6kgrSALfgi3getdc0k1hHEbf8rNtUGKQrDFxLnKSCYQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1663704020; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=F6KfugY/ZF2k+N60z0DOxutQrzRQ1q7GMoUe0LhivhQ=; b=SSpRiuIfu6l4gPSIBu5IBl+nFPBNA80dxHSF/X64Ig5dnegfDauwjhqthaCZm29Jla2LcE HWi8q6N9JChTUcDA== From: "tip-bot2 for Vincent Mailhol" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/asm] x86/asm/bitops: Use __builtin_ctzl() to evaluate constant expressions Cc: Vincent Mailhol , Borislav Petkov , Nick Desaulniers , Yury Norov , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> References: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> MIME-Version: 1.0 Message-ID: <166370401862.401.15713055946708670653.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/asm branch of tip: Commit-ID: fdb6649ab7c142e497539a471e573c2593b9c923 Gitweb: https://git.kernel.org/tip/fdb6649ab7c142e497539a471e573c259= 3b9c923 Author: Vincent Mailhol AuthorDate: Wed, 07 Sep 2022 18:09:35 +09:00 Committer: Borislav Petkov CommitterDate: Tue, 20 Sep 2022 15:35:37 +02:00 x86/asm/bitops: Use __builtin_ctzl() to evaluate constant expressions If x is not 0, __ffs(x) is equivalent to: (unsigned long)__builtin_ctzl(x) And if x is not ~0UL, ffz(x) is equivalent to: (unsigned long)__builtin_ctzl(~x) Because __builting_ctzl() returns an int, a cast to (unsigned long) is necessary to avoid potential warnings on implicit casts. Concerning the edge cases, __builtin_ctzl(0) is always undefined, whereas __ffs(0) and ffz(~0UL) may or may not be defined, depending on the processor. Regardless, for both functions, developers are asked to check against 0 or ~0UL so replacing __ffs() or ffz() by __builting_ctzl() is safe. For x86_64, the current __ffs() and ffz() implementations do not produce optimized code when called with a constant expression. On the contrary, the __builtin_ctzl() folds into a single instruction. However, for non constant expressions, the __ffs() and ffz() asm versions of the kernel remains slightly better than the code produced by GCC (it produces a useless instruction to clear eax). Use __builtin_constant_p() to select between the kernel's __ffs()/ffz() and the __builtin_ctzl() depending on whether the argument is constant or not. ** Statistics ** On a allyesconfig, before...: $ objdump -d vmlinux.o | grep tzcnt | wc -l 3607 ...and after: $ objdump -d vmlinux.o | grep tzcnt | wc -l 2600 So, roughly 27.9% of the calls to either __ffs() or ffz() were using constant expressions and could be optimized out. (tests done on linux v5.18-rc5 x86_64 using GCC 11.2.1) Note: on x86_64, the BSF instruction produces TZCNT when used with the REP prefix (which explain the use of `grep tzcnt' instead of `grep bsf' in above benchmark). c.f. [1] [1] e26a44a2d618 ("x86: Use REP BSF unconditionally") [ bp: Massage commit message. ] Signed-off-by: Vincent Mailhol Signed-off-by: Borislav Petkov Reviewed-by: Nick Desaulniers Reviewed-by: Yury Norov Link: https://lore.kernel.org/r/20220511160319.1045812-1-mailhol.vincent@wa= nadoo.fr --- arch/x86/include/asm/bitops.h | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h index 879238e..2edf684 100644 --- a/arch/x86/include/asm/bitops.h +++ b/arch/x86/include/asm/bitops.h @@ -247,17 +247,30 @@ arch_test_bit_acquire(unsigned long nr, const volatil= e unsigned long *addr) variable_test_bit(nr, addr); } =20 +static __always_inline unsigned long variable__ffs(unsigned long word) +{ + asm("rep; bsf %1,%0" + : "=3Dr" (word) + : "rm" (word)); + return word; +} + /** * __ffs - find first set bit in word * @word: The word to search * * Undefined if no bit exists, so code should check against 0 first. */ -static __always_inline unsigned long __ffs(unsigned long word) +#define __ffs(word) \ + (__builtin_constant_p(word) ? \ + (unsigned long)__builtin_ctzl(word) : \ + variable__ffs(word)) + +static __always_inline unsigned long variable_ffz(unsigned long word) { asm("rep; bsf %1,%0" : "=3Dr" (word) - : "rm" (word)); + : "r" (~word)); return word; } =20 @@ -267,13 +280,10 @@ static __always_inline unsigned long __ffs(unsigned l= ong word) * * Undefined if no zero exists, so code should check against ~0UL first. */ -static __always_inline unsigned long ffz(unsigned long word) -{ - asm("rep; bsf %1,%0" - : "=3Dr" (word) - : "r" (~word)); - return word; -} +#define ffz(word) \ + (__builtin_constant_p(word) ? \ + (unsigned long)__builtin_ctzl(~word) : \ + variable_ffz(word)) =20 /* * __fls: find last set bit in word From nobody Fri May 8 03:49:55 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6CB2DC6FA82 for ; Tue, 20 Sep 2022 20:00:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230337AbiITUAd (ORCPT ); Tue, 20 Sep 2022 16:00:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55262 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229870AbiITUAZ (ORCPT ); Tue, 20 Sep 2022 16:00:25 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 25B8240BF2; Tue, 20 Sep 2022 13:00:22 -0700 (PDT) Date: Tue, 20 Sep 2022 20:00:19 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1663704021; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=td/0Lmoinj5QP/RSW+m2L52+twM1EDigUVdjwGaT5EQ=; b=NxkDySEsXZryEsfWdfAl+vc/sosE0IWQwkHHxySO5r3Xy0tFoAIXFhN0UktjUcK4H/CIum 22XxdPb9zhvAAtutMmbE83Q75mRD8C2flwqLR8fXQz7u9+jiocj3L4pG72Oz/ZaYttGqnO Wn8FHwe3U5wFuatzd1+usLfHewUzKYCnwJn77VH5qu/f2gCcmAl6QIbDqmrmc3lDAH+QXH K08i6rKuFqJiLWZuRnY1bjvjhgATR9Quz3PX9RWvV5sSHGVPA0hKQxoof8tqlVhFgAhpWU BeYq6kwWnbul4NoIx2zTkTVR0b7Websldrjnc5SbbtD2TWFj5ymN+MESiUGBpg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1663704021; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=td/0Lmoinj5QP/RSW+m2L52+twM1EDigUVdjwGaT5EQ=; b=ABA6KXS2pHFFYv2/aCc/wGXC1WCdKD1YlLRLDafGrbT1BZFDxMWIas65sz5atL60gomCVY iX3LDCTzGQUl7VCw== From: "tip-bot2 for Vincent Mailhol" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/asm] x86/asm/bitops: Use __builtin_ffs() to evaluate constant expressions Cc: Vincent Mailhol , Borislav Petkov , Nick Desaulniers , Yury Norov , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> References: <20220511160319.1045812-1-mailhol.vincent@wanadoo.fr> MIME-Version: 1.0 Message-ID: <166370401997.401.11967078990556002751.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/asm branch of tip: Commit-ID: 146034fed6ee75ec09cf8f996165e2296ceae0bb Gitweb: https://git.kernel.org/tip/146034fed6ee75ec09cf8f996165e2296= ceae0bb Author: Vincent Mailhol AuthorDate: Wed, 07 Sep 2022 18:09:34 +09:00 Committer: Borislav Petkov CommitterDate: Tue, 20 Sep 2022 15:31:17 +02:00 x86/asm/bitops: Use __builtin_ffs() to evaluate constant expressions For x86_64, the current ffs() implementation does not produce optimized code when called with a constant expression. On the contrary, the __builtin_ffs() functions of both GCC and clang are able to fold the expression into a single instruction. ** Example ** Consider two dummy functions foo() and bar() as below: #include #define CONST 0x01000000 unsigned int foo(void) { return ffs(CONST); } unsigned int bar(void) { return __builtin_ffs(CONST); } GCC would produce below assembly code: 0000000000000000 : 0: ba 00 00 00 01 mov $0x1000000,%edx 5: b8 ff ff ff ff mov $0xffffffff,%eax a: 0f bc c2 bsf %edx,%eax d: 83 c0 01 add $0x1,%eax 10: c3 ret 0000000000000020 : 20: b8 19 00 00 00 mov $0x19,%eax 25: c3 ret And clang would produce: 0000000000000000 : 0: b8 ff ff ff ff mov $0xffffffff,%eax 5: 0f bc 05 00 00 00 00 bsf 0x0(%rip),%eax # c c: 83 c0 01 add $0x1,%eax f: c3 ret 0000000000000010 : 10: b8 19 00 00 00 mov $0x19,%eax 15: c3 ret Both examples clearly demonstrate the benefit of using __builtin_ffs() instead of the kernel's asm implementation for constant expressions. However, for non constant expressions, the kernel's ffs() asm version remains better for x86_64 because, contrary to GCC, it doesn't emit the CMOV assembly instruction, c.f. [1] (noticeably, clang is able optimize out the CMOV call). Use __builtin_constant_p() to select between the kernel's ffs() and the __builtin_ffs() depending on whether the argument is constant or not. As a side benefit, replacing the ffs() function declaration by a macro also removes below -Wshadow warning: ./arch/x86/include/asm/bitops.h:283:28: warning: declaration of 'ffs' sha= dows a built-in function [-Wshadow] 283 | static __always_inline int ffs(int x) ** Statistics ** On a allyesconfig, before...: $ objdump -d vmlinux.o | grep bsf | wc -l 1081 ...and after: $ objdump -d vmlinux.o | grep bsf | wc -l 792 So, roughly 26.7% of the calls to ffs() were using constant expressions and could be optimized out. (tests done on linux v5.18-rc5 x86_64 using GCC 11.2.1) [1] commit ca3d30cc02f7 ("x86_64, asm: Optimise fls(), ffs() and fls64()") [ bp: Massage commit message. ] Signed-off-by: Vincent Mailhol Signed-off-by: Borislav Petkov Reviewed-by: Nick Desaulniers Reviewed-by: Yury Norov Link: https://lore.kernel.org/r/20220511160319.1045812-1-mailhol.vincent@wa= nadoo.fr --- arch/x86/include/asm/bitops.h | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/arch/x86/include/asm/bitops.h b/arch/x86/include/asm/bitops.h index 0fe9de5..879238e 100644 --- a/arch/x86/include/asm/bitops.h +++ b/arch/x86/include/asm/bitops.h @@ -292,18 +292,7 @@ static __always_inline unsigned long __fls(unsigned lo= ng word) #undef ADDR =20 #ifdef __KERNEL__ -/** - * 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. - */ -static __always_inline int ffs(int x) +static __always_inline int variable_ffs(int x) { int r; =20 @@ -334,6 +323,19 @@ static __always_inline int ffs(int x) } =20 /** + * 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)) + +/** * fls - find last set bit in word * @x: the word to search *