All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tzu-Ching Yang <shingekinocore@gmail.com>
To: Greg Ungerer <gerg@linux-m68k.org>,
	Geert Uytterhoeven <geert@linux-m68k.org>
Cc: linux-m68k@lists.linux-m68k.org,
	Angelo Dureghello <adureghello@baylibre.com>,
	Jean-Michel Hautbois <jeanmichel.hautbois@yoseli.org>,
	Yury Norov <yury.norov@gmail.com>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Arnd Bergmann <arnd@arndb.de>,
	linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH] m68k: coldfire: Use the builtin fls() and __fls() on ISA_A+ and ISA_C
Date: Mon, 23 Nov 2026 21:11:01 +0800	[thread overview]
Message-ID: <20261123131101.639643-1-shingekinocore@gmail.com> (raw)

Let fls() and __fls() use the ff1 instruction on ColdFire ISA_A+ and
ISA_C cores instead of the generic binary search in C, as __ffs() and
ffs() already do. GCC emits ff1 for __builtin_clz() on exactly the cores
for which it defines __mcfisaaplus__ or __mcfisac__, the macros that
already select the ff1 versions of __ffs() and ffs(), so no new
condition is needed. Commit a6260ef84103 ("m68knommu: add ffs and __ffs
plattform which support ISA A+ or ISA C") added the ff1 versions of
__ffs() and ffs() in 2008 and kept the generic fls(); the generic
__fls() followed in 2009.

Use the asm-generic builtin versions of fls() and __fls() when
__mcfisaaplus__ or __mcfisac__ is defined, and keep the generic C
versions for ISA_A, ISA_B and 68000. The bitrev/ff1 asm of __ffs() and
ffs() stays, as this asm is shorter than the code GCC generates for
the builtin versions of __ffs() and ffs().

With GCC 12 to 16, fls() shrinks from 28 instructions to 8 or 9 and
__fls() from 24 to 6. With GCC 8 to 16, the functions of each ISA_A+ or
ISA_C defconfig kernel shrink in total by at least 8676 and at most
13266 bytes (scripts/bloat-o-meter -t), depending on the defconfig and
the compiler. In lib/find_bit.c, only _find_last_bit() changes, from 55
to 35 instructions for m5208evb with GCC 16.

fls(0) still returns 0, and __fls() still returns unsigned int.
__fls(0), which is undefined, now returns ~0 instead of 0. For other
CPUs, the preprocessed source changes only in its line numbers.

Suggested-by: Arnd Bergmann <arnd@arndb.de>
Link: https://lore.kernel.org/all/dbb44e92-0bc5-449d-9e43-1f93362aa6c3@app.fastmail.com/
Signed-off-by: Tzu-Ching Yang <shingekinocore@gmail.com>
---
I have no ColdFire hardware. On QEMU mcf5208evb (ISA_A+), the ffs and
bitops KUnit tests pass before and after with GCC 8.3 to 16.2. In
find_bit_benchmark on that machine, with the bitmaps drawn from a fixed
seed so that the kernels before and after use the same bitmaps, and the
executed instructions counted by a TCG plugin, find_last_bit goes from
52.3 to 38.3 instructions per iteration on the random bitmap with GCC
16, and bitmap_find_next_zero_area_off, which calls find_last_bit(),
from 3302 to 2864; the tests of the other helpers do not change. The
ISA_C kernels were only built, as QEMU has no ISA_C machine.

Under QEMU user mode, with the kernel's flags for -mcpu=5208, 532x and
54455 and each GCC at -O2 and -Os, old and new fls(), __fls() and
fls64() agree on all 2^32 inputs (fls64() in either 32-bit half)
except the undefined __fls(0).

The seven ISA_A+ and ISA_C defconfigs (m5208evb, m5235evb, m5275evb,
m5282evb, m5329evb, m54418evb and stmark2) were built with GCC 8.3 to
16.2 before and after, with the same .config before and after. A W=1
build of these seven and of twenty other ColdFire and m68k defconfigs,
each with at least GCC 8.3, 12.5 and 16.2, has no new warning.

The new out-of-line fls() for m5208evb with GCC 16.2:

	movel %sp@(4),%d0
	tstl %d0
	beqs 1f
	movel %d0,%d1
	ff1 %d1
	moveq #32,%d0
	subl %d1,%d0
1:	rts

Against v7.4-rc1; also applies to the m68knommu and m68k for-next
branches and to next-20261113.

 arch/m68k/include/asm/bitops.h | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/arch/m68k/include/asm/bitops.h b/arch/m68k/include/asm/bitops.h
index e9639e48c6c3c..81923a9fa4494 100644
--- a/arch/m68k/include/asm/bitops.h
+++ b/arch/m68k/include/asm/bitops.h
@@ -506,13 +506,14 @@ static inline __attribute_const__ int ffs(int x)
 	return __ffs(x) + 1;
 }
 
+#include <asm-generic/bitops/builtin-fls.h>
+#include <asm-generic/bitops/builtin-__fls.h>
 #else
 #include <asm-generic/bitops/ffs.h>
 #include <asm-generic/bitops/__ffs.h>
-#endif
-
 #include <asm-generic/bitops/fls.h>
 #include <asm-generic/bitops/__fls.h>
+#endif
 
 #else
 

base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
-- 
2.34.1


             reply	other threads:[~2026-11-23 13:11 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 13:11 Tzu-Ching Yang [this message]
2026-11-25 12:41 ` [PATCH] m68k: coldfire: Use the builtin fls() and __fls() on ISA_A+ and ISA_C Greg Ungerer
2026-11-25 15:35 ` Yury Norov
2026-11-30  7:54 ` adureghello
2026-11-30 12:13   ` Tzu-Ching Yang
2026-11-30 12:25     ` adureghello
2026-12-01 23:16   ` Greg Ungerer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261123131101.639643-1-shingekinocore@gmail.com \
    --to=shingekinocore@gmail.com \
    --cc=adureghello@baylibre.com \
    --cc=arnd@arndb.de \
    --cc=geert@linux-m68k.org \
    --cc=gerg@linux-m68k.org \
    --cc=jeanmichel.hautbois@yoseli.org \
    --cc=linux-arch@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-m68k@lists.linux-m68k.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.