From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.215.170 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796134985; b=Q0rzzaNEnOkm5THB+NHXLBr7ZESKpUdTWa+I4TpkzxSuZ8kQFcsDEYd9YkjYxSqFGfkQ HMh9e8Q7OK2E68fAeNrQz60EGhJNEumW9BufmpuqfAjl7VjMOBi99w4YeaLQBdjrtpxpv GqR/54gNfZDxy1GOG3GjtLGR6VRf8YiXzAqwe8etBF4dnBthpB6oRT7J2WC1enfWeod9K pYsBYhjdf+aJ8ctxYkP/wOyC+5k0PJW++ShqucRzl5Van1gUJSv0s1xIoBd/e0GQv1Qsw dzDHcWX78bauvo5rUxwyshTVdMsx/r34fOmhUhlVfeqb8rEV2+y8gkgUgtn0RHhqDhA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796134985; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Cc:Subject:Date:Message-Id:X-Mailer:MIME-Version: Content-Transfer-Encoding; bh=xB1dPU36bH9oqSUBy/N9jpRCZ3nFDpl96+HKs82Zy9g=; b=kmMtfwJ5dCN6yeenPlKwamoLM7AvHciUPXSXnpK28Hio1F/28h+CvyTmtnjOYW2yQeTV sKfeepdqNvVuET/XDDYPtbHMuL5TdOfUGREFUahaXfAATB+xzb+zoENzKRQ9CMsbRtRTu UVQFTixBjZrOPnsW0pvGq9GjRfflPcHQgeqJGyLLo2UHe26UxzL5dOvjgXfdaCbwUXZR3 oGLQDGRVOHvaIWXqZZCIS/bZZTPSHgRRMZLUOiZFr4sjVMLta+YZKd+vPVOyulslrzkt6 R0YYBsyauvDPWabzSPOL8oFNsrzG9qmwJztrkvx6cDJjGQORFkZkfXxfoEm/iWhvJkA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=uiuIWbXW; arc=none smtp.remote-ip=209.85.215.170 Received: from mail-pg1-f170.google.com (mail-pg1-f170.google.com [209.85.215.170]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B7447606A320 for ; Tue, 1 Dec 2026 14:23:04 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org B7447606A320 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=uiuIWbXW Received: by mail-pg1-f170.google.com with SMTP id 41be03b00d2f7-cd69954c755so8542862a12.3 for ; Tue, 01 Dec 2026 06:23:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1796134984; x=1796739784; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=xB1dPU36bH9oqSUBy/N9jpRCZ3nFDpl96+HKs82Zy9g=; b=uiuIWbXWwIa77aS3ms3tD5n0pPXRhQ+bvKzjnHLxxJrVmALnjJrk9srz4yNowrR25E 21tv43/EdavtYHiMbK2bS9bXkpHJ43ZcK12ibbzRInZ3e1DVPs4qKDAX5qzR7e/O5H5q 3jPgYka7a8cWFULwN2uyRHqzT3tQ9bnTamgYEvZKzxXd8qKSw9Cz7a1L96oUMN58iB2M Is4ow0uz8aE157sp/8YMy4BRmmSxy1gn2iYkwRtmQiOQ6Fj+BcgqsX/2U90Mo/get4Rv UjucqGUxwQpTIeYCZyDebdFN+T04L5oRK2l7E5rWPxt+HYrrS4b5e7W2NIyHeexvf2PC ZcvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796134984; x=1796739784; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=xB1dPU36bH9oqSUBy/N9jpRCZ3nFDpl96+HKs82Zy9g=; b=22xw1UJ3OaMitEAJXN5yLGDfxKqmfGTku7v+KF3dtQKIPoaiTljXjEP5ioQ/ICag0I XlqWSBEta4jt/JRV6g69nvapArJUBfz3N08fkdDVCus8Hw+U4/IHxG7pNSYC8wVp8hzd mpyhmOStoLT7ECCBJihXOOZexyLPXMz75Nv2QUzW6h2rQXlj/j44OSTRyXchOHr6er4w oR5D1EgqUVBKvN4ksSWmzs5YgocOowHRbaph8gdtZGI21QG2AK5I9Xl37/VKLQp6zSbj YfIS/O9svZI4Ch60f4RO9UfpKW6hKapLJGj5myOOjaL+CBbGb9NuDHEV2stbQr+apXzg X7cw== X-Forwarded-Encrypted: i=1; AL+hErEen6B17mQ7W36ynVP3l3VDBW5xA9QjPLQ1oGhubCla/gvkGR2O8IkT7RYI9AbeTMup/aJSomr40bWq@vger.kernel.org X-Gm-Message-State: APuU4Z8AVq7bc74xoOyUXinzzGGxdx982q1k7MYr1WWpkMcGKb8YfhRZ GGzlO9pkIdvBvb4nbOiQ3JoxG8sKXSSsPyO1SjaZsdYFBORB2Lb6r64XIDACIEzV X-Gm-Gg: AZFdTZfq8V6wQ+s+eOZz+LNkd6sEzro9A5WJxMHV7gpq0guJL/KZ2rk+CiEAaXRpDa/ yE5TWWgnFw9iUDE+fcj8DxJdd6hNZxOwQ41BnH4dm4GC1QRfbE5EbR9A9KLp1Z6mpv5MtjiSdLN klo1Y4fObvbSF4K1KPWZmTk1TBcFbLQ/f2fQp1yAq56PJvlVXFpAsPVdojczmb/9bKCjYpyn1gm Zkj8uwrPNadaLwa70kj8QQ5PpytRulG12XBt1tOijmqdngiJYNqnksZb2ylGjiLusB/TnvmSsZ6 cqGjnyXGOIKdIdV3eBpqFPBjMJnxR+0ePcOlxfiFwcMyZJQRDSfFkhwWBIYSMpyoSIiiD94OwFY +Kda28Bl9IlBn9cdB7woAMetCoRxO+w3SfJLxflcKMqjIWzNsOOOA45j/DEu5bvUiyoA70UfDKL T+x5wq0qJfRNYw7STo1QZg1nMK9jnLKawK2WXg2T/bpGewIwgEpvnG/7QJvXfUbTt1OeRhUWiN7 4xge/vbnK2m9gvORh13/xEyRW23sWwRV6I2KJB9b30xAvPMktV8cNG0kyqk+L0/XbpNMFfxUShw mBmv0ozCyoV0TrHafTq5WjsFFaw3qh1Vgw== X-Received: by 2002:a17:90b:39a3:b0:3a0:9c9d:eb18 with SMTP id 98e67ed59e1d1-3b41cb20e66mr41269891a91.54.1796134983607; Tue, 01 Dec 2026 06:23:03 -0800 (PST) Received: from arm-a100.. ([140.112.28.118]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b44b8c2fcesm22196438a91.4.2026.12.01.06.23.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 01 Dec 2026 06:23:03 -0800 (PST) From: Tzu-Ching Yang To: Max Filippov , Chris Zankel Cc: Yury Norov , Rasmus Villemoes , Arnd Bergmann , linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] xtensa: bitops: use the builtins for __ffs(), ffs(), ffz() and __fls() Date: Tue, 1 Dec 2026 22:22:53 +0800 Message-Id: <20261201142253.1986402-1-shingekinocore@gmail.com> X-Mailer: git-send-email 2.34.1 X-Mailing-List: linux-arch@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Let GCC compute __ffs(), ffs(), ffz() and __fls() at compile time when the argument is a constant, on cores with the NSA option, by using the compiler builtins instead of the nsau asm wrappers; GCC implements the builtins with the same nsau instruction on these cores. The nsau wrappers come from the initial port in 2005, commit 9a8fd5589902 ("[PATCH] xtensa: Architecture support for Tensilica Xtensa Part 6"), and from 2009 for __fls(), so they predate the asm-generic builtin versions, which were added in 2012. Use the asm-generic builtin versions of __ffs(), ffs() and __fls() inside the existing XCHAL_HAVE_NSA block, and use the generic ffz(), which is __ffs(~x), with and without XCHAL_HAVE_NSA instead of only without it. fls() keeps its asm, as the builtin version of fls() needs a test for zero that nsau makes unnecessary by returning 32 for 0. With GCC 8 to 16, the functions of each vmlinux tested are smaller in total with this patch than without it, by at least 1346 and at most 10002 bytes (scripts/bloat-o-meter -t; 37 builds on xtensa-for-next: 8 defconfigs, each with at least two of these compilers). With GCC 8 to 13, none of the __ffs()-based functions of lib/find_bit.c that find_bit_benchmark tests executes more instructions than without this patch. With GCC 14 to 16, each of these functions executes one instruction more per call that finds a bit than without this patch (two more in __find_nth_bit() with GCC 15 and 16, one of them a register move), as these compilers need one instruction more than GCC 13 to add __builtin_ctzl() to a value in a register [1]. The generic ffz() lets GCC 10 to 16 inline next_signal() into dequeue_signal(), which they do not do without this patch. For generic_kc705_defconfig with GCC 16.2 for the dc233c core, a call of dequeue_signal() then executes 2 to 22 instructions fewer than without this patch, counted along the disassembly, and dequeue_signal() is 163 bytes larger. On the fd allocation and block flush paths, counted in three builds, a call executes between three instructions fewer and four more than without this patch. ffs(0) still returns 0; __ffs(0), __fls(0) and ffz(~0UL) remain undefined. __ffs() and __fls() now return unsigned int instead of unsigned long, and ffz() unsigned int instead of int, like the asm-generic versions that the !XCHAL_HAVE_NSA case already uses; unsigned int and unsigned long are 32 bits wide on xtensa. ffs() and ffz() become macros, for __builtin_ffs() and __ffs(~(x)). Suggested-by: Arnd Bergmann Link: https://lore.kernel.org/all/affe7da1-41aa-4710-bee0-5e3c2b8a8dfc@app.fastmail.com/ Link: https://gcc.gnu.org/PR129142 [1] Signed-off-by: Tzu-Ching Yang --- I have no xtensa hardware. On QEMU kc705, the ffs and bitops KUnit tests pass before and after, with the dc233c core and GCC 13.5 to 16.2 built for that core, and with the test_kc705_be core and the kernel.org GCC 8.1 to 16.2. Under QEMU user mode, the old and new __ffs(), ffs(), ffz() and __fls() return the same values for every defined 32-bit input with GCC 8 to 16. Max, could you run the ffs and bitops KUnit tests and lib/find_bit_benchmark.c before and after on hardware, for example the KC705 board with dc233c? In virt_defconfig with GCC 16.2 for dc233c, the number of nsau instructions whose operand is a constant (loaded by movi or l32r earlier in the same basic block) goes from 178 without this patch to 9 with it, and napi_pp_put_page() shrinks from 284 to 146 bytes because PP_MAGIC_MASK, which uses __fls() and __ffs(PAGE_OFFSET), now folds. Executed instructions per iteration of four find_bit_benchmark tests on QEMU kc705 with dc233c, counted by a TCG plugin (random bitmap, generic_kc705_defconfig with the benchmark built in, the same bitmaps before and after, written through the QEMU gdbstub): GCC 13.5 GCC 16.2 before after before after ------------------------------------------------------ find_next_bit 30.2 30.2 26.3 27.3 find_first_bit 2587.1 2587.1 2586.1 2587.1 find_nth_bit 17482.5 17482.5 19017.7 19019.7 find_last_bit 32.2 32.2 29.2 29.2 Instructions per call with this patch minus without it (negative: fewer with this patch) on paths that use ffz(), counted along the disassembly: kc705 kc705 iss GCC 16.2 GCC 13.5 kernel.org dc233c dc233c GCC 16.2 --------------------------------------------------------------- dequeue_signal(), five cases -2 to -22 -5 to -26 -3 to -22 get_signal(), no sync. signal +1 0 0 alloc_fd(), fast path -1 -3 +2 blk_flush_complete_seq() -2 to +3 -3 to +4 -2 to +1 flush_end_io() -1 to +1 -1 to +1 +1 to +3 and per request it ends 0 +1 0 kc705 is generic_kc705_defconfig, iss is iss_defconfig. The five cases are no signal pending and one signal in either word of the thread's or the shared pending set. With the kernel.org GCC 10 to 15 (iss_defconfig), the five cases take 1 to 27 instructions fewer than without this patch, except one signal in word 0 of the thread's set with GCC 12.5, which takes 2 more. The builtins need a GCC built for a core with NSA, as the rest of such a kernel already does (entry.S uses nsau); with a GCC built without NSA, this patch fails to link (undefined __ffssi2). I can send one of these two versions instead: - keep the ffz() asm: the paths of the ffz() table stay as without this patch; 32 of the 37 vmlinux are up to 230 bytes smaller than with this patch, the other five 1 to 160 bytes larger; - keep the __ffs() and ffz() asm: lib/find_bit.o stays as in xtensa-for-next with GCC 8 to 16, but __ffs() and ffz() of a constant do not fold; the 37 vmlinux are 1370 to 9972 bytes larger than with this patch (-651 to +86 against xtensa-for-next). Against xtensa-for-next (de1aec038588); also applies to v7.4-rc5 and bitmap-for-next. arch/xtensa/include/asm/bitops.h | 45 +++----------------------------- 1 file changed, 4 insertions(+), 41 deletions(-) diff --git a/arch/xtensa/include/asm/bitops.h b/arch/xtensa/include/asm/bitops.h index f7390b6761e1..ba7e0b61ccdb 100644 --- a/arch/xtensa/include/asm/bitops.h +++ b/arch/xtensa/include/asm/bitops.h @@ -32,36 +32,6 @@ static inline unsigned long __cntlz (unsigned long x) return lz; } -/* - * ffz: Find first zero in word. Undefined if no zero exists. - * bit 0 is the LSB of addr; bit 32 is the LSB of (addr+1). - */ - -static inline int __attribute_const__ ffz(unsigned long x) -{ - return 31 - __cntlz(~x & -~x); -} - -/* - * __ffs: Find first bit set in word. Return 0 for bit 0 - */ - -static inline __attribute_const__ unsigned long __ffs(unsigned long x) -{ - return 31 - __cntlz(x & -x); -} - -/* - * ffs: Find first bit set in word. This is defined the same way as - * the libc and compiler builtin ffs routines, therefore - * differs in spirit from the above ffz (man ffs). - */ - -static inline __attribute_const__ int ffs(unsigned long x) -{ - return 32 - __cntlz(x & -x); -} - /* * fls: Find last (most-significant) bit set in word. * Note fls(0) = 0, fls(1) = 1, fls(0x80000000) = 32. @@ -72,28 +42,21 @@ static inline __attribute_const__ int fls (unsigned int x) return 32 - __cntlz(x); } -/** - * __fls - find last (most-significant) set bit in a long word - * @word: the word to search - * - * Undefined if no set bit exists, so code should check against 0 first. - */ -static inline __attribute_const__ unsigned long __fls(unsigned long word) -{ - return 31 - __cntlz(word); -} +# include +# include +# include #else /* Use the generic implementation if we don't have the nsa/nsau instructions. */ # include # include -# include # include # include #endif +#include #include #if XCHAL_HAVE_EXCLUSIVE base-commit: de1aec038588f12b1faeef9e94abe9608814d39a -- 2.34.1