* Re: [PATCH] m68k: coldfire: Use the builtin fls() and __fls() on ISA_A+ and ISA_C
2026-11-23 13:11 [PATCH] m68k: coldfire: Use the builtin fls() and __fls() on ISA_A+ and ISA_C Tzu-Ching Yang
@ 2026-11-25 12:41 ` Greg Ungerer
2026-11-25 15:35 ` Yury Norov
2026-11-30 7:54 ` adureghello
2 siblings, 0 replies; 6+ messages in thread
From: Greg Ungerer @ 2026-11-25 12:41 UTC (permalink / raw)
To: Tzu-Ching Yang, Geert Uytterhoeven
Cc: linux-m68k, Angelo Dureghello, Jean-Michel Hautbois, Yury Norov,
Rasmus Villemoes, Arnd Bergmann, linux-arch, linux-kernel
Hi Tzu-Ching,
On 23/11/26 23:11, Tzu-Ching Yang wrote:
> 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>
Looks good to me. I tested it on a ColdFire 5271, which is ISA_A+, and it
checked out good. I don't have my 54418 board handy at the moment (its ISA_C)
but when I get a chance I will double check that too.
Applied to the for-next branch of the m68knommu git tree.
Thanks
Greg
> ---
> 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
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] m68k: coldfire: Use the builtin fls() and __fls() on ISA_A+ and ISA_C
2026-11-23 13:11 [PATCH] m68k: coldfire: Use the builtin fls() and __fls() on ISA_A+ and ISA_C Tzu-Ching Yang
2026-11-25 12:41 ` Greg Ungerer
@ 2026-11-25 15:35 ` Yury Norov
2026-11-30 7:54 ` adureghello
2 siblings, 0 replies; 6+ messages in thread
From: Yury Norov @ 2026-11-25 15:35 UTC (permalink / raw)
To: Tzu-Ching Yang
Cc: Greg Ungerer, Geert Uytterhoeven, linux-m68k, Angelo Dureghello,
Jean-Michel Hautbois, Yury Norov, Rasmus Villemoes,
Arnd Bergmann, linux-arch, linux-kernel
On Mon, Nov 23, 2026 at 09:11:01PM +0800, Tzu-Ching Yang wrote:
> 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>
Acked-by: Yury Norov <ynorov@nvidia.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
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] m68k: coldfire: Use the builtin fls() and __fls() on ISA_A+ and ISA_C
2026-11-23 13:11 [PATCH] m68k: coldfire: Use the builtin fls() and __fls() on ISA_A+ and ISA_C Tzu-Ching Yang
2026-11-25 12:41 ` Greg Ungerer
2026-11-25 15:35 ` Yury Norov
@ 2026-11-30 7:54 ` adureghello
2026-11-30 12:13 ` Tzu-Ching Yang
2 siblings, 1 reply; 6+ messages in thread
From: adureghello @ 2026-11-30 7:54 UTC (permalink / raw)
To: Tzu-Ching Yang
Cc: Greg Ungerer, Geert Uytterhoeven, linux-m68k,
Jean-Michel Hautbois, Yury Norov, Rasmus Villemoes,
Arnd Bergmann, linux-arch, linux-kernel
Hi Tzu-Ching,
it works on mcf54415/stanmrk2 board.
Tested-by: Angelo Dureghello <adureghello@baylibre.com>
With your patch
Loading Environment from SPIFlash... SF: Detected is25lp128 with page
size 256 Bytes, erase size 4 KiB, total 16 MiB
OK
In: uart@fc060000
Out: uart@fc060000
Err: uart@fc060000
Hit any key to stop autoboot: 0
3117512 bytes read in 528 ms (5.6 MiB/s)
789901 bytes read in 140 ms (5.4 MiB/s)
## Booting kernel from Legacy Image at 42000000 ...
Image Name: mainline kernel
Created: 2026-11-30 7:36:30 UTC
Image Type: M68K Linux Kernel Image (uncompressed)
Data Size: 3117448 Bytes = 3 MiB
Load Address: 40001000
Entry Point: 40001000
Verifying Checksum ... OK
Without the patch
Loading Environment from SPIFlash... SF: Detected is25lp128 with page
size 256 Bytes, erase size 4 KiB, total 16 MiB
OK
In: uart@fc060000
Out: uart@fc060000
Err: uart@fc060000
Hit any key to stop autoboot: 0
3125704 bytes read in 530 ms (5.6 MiB/s)
789901 bytes read in 140 ms (5.4 MiB/s)
## Booting kernel from Legacy Image at 42000000 ...
Image Name: mainline kernel
Created: 2026-11-30 7:38:38 UTC
Image Type: M68K Linux Kernel Image (uncompressed)
Data Size: 3125640 Bytes = 3 MiB
Load Address: 40001000
Entry Point: 40001000
Verifying Checksum ... OK
@Greg:
i am currently working on:
- dma global pool on mcf54415 + MMU, right now seems working,
- edma and dspi changes to be tested depending on global pool.
Working all over your last readX/writeX changes (including your edma
and dspi changes).
So got some patches ready, but still have the dspi tx timeout issue
to solve.
Regards,
angelo
On Mon, Nov 23, 2026 at 09:11:01PM +0800, Tzu-Ching Yang wrote:
> 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
>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] m68k: coldfire: Use the builtin fls() and __fls() on ISA_A+ and ISA_C
2026-11-30 7:54 ` adureghello
@ 2026-11-30 12:13 ` Tzu-Ching Yang
2026-11-30 12:25 ` adureghello
0 siblings, 1 reply; 6+ messages in thread
From: Tzu-Ching Yang @ 2026-11-30 12:13 UTC (permalink / raw)
To: Angelo Dureghello
Cc: Greg Ungerer, Geert Uytterhoeven, linux-m68k,
Jean-Michel Hautbois, Yury Norov, Rasmus Villemoes,
Arnd Bergmann, linux-arch, linux-kernel
On Mon, 30 Nov 2026, Angelo Dureghello wrote:
> it works on mcf54415/stanmrk2 board.
>
> Tested-by: Angelo Dureghello <adureghello@baylibre.com>
Hi Angelo,
Thank you for testing on ISA_C hardware.
Best regards,
Tzu-Ching
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] m68k: coldfire: Use the builtin fls() and __fls() on ISA_A+ and ISA_C
2026-11-30 12:13 ` Tzu-Ching Yang
@ 2026-11-30 12:25 ` adureghello
0 siblings, 0 replies; 6+ messages in thread
From: adureghello @ 2026-11-30 12:25 UTC (permalink / raw)
To: Tzu-Ching Yang
Cc: Greg Ungerer, Geert Uytterhoeven, linux-m68k,
Jean-Michel Hautbois, Yury Norov, Rasmus Villemoes,
Arnd Bergmann, linux-arch, linux-kernel
YW,
forgot to add gcc info of the compiled kernel:
Using built-in specs.
COLLECT_GCC=/opt/toolchains/m68k/gcc-15.2.0-nolibc/m68k-linux/bin/m68k-linux-gcc
COLLECT_LTO_WRAPPER=/opt/toolchains/m68k/gcc-15.2.0-nolibc/m68k-linux/bin/../libexec/gcc/m68k-linux/15.2.0/lto-wrapper
Target: m68k-linux
Configured with: /home/arnd/git/gcc/configure --host=x86_64-linux-gnu
--build=aarch64-pc-linux-gnu --target=m68k-linux --enable-targets=all
--prefix=/home/arnd/cross/x86_64/gcc-15.2.0-nolibc/m68k-linux
--enable-languages=c --without-headers --disable-bootstrap
--disable-nls --disable-threads --disable-shared --disable-libmudflap
--disable-libssp --disable-libgomp --disable-decimal-float
--disable-libquadmath --disable-libatomic --disable-libcc1
--disable-libmpx --enable-checking=release
--with-static-standard-libraries --disable-plugin
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 15.2.0 (GCC)
Regards,
angelo
On Mon, Nov 30, 2026 at 08:13:59PM +0800, Tzu-Ching Yang wrote:
> On Mon, 30 Nov 2026, Angelo Dureghello wrote:
> > it works on mcf54415/stanmrk2 board.
> >
> > Tested-by: Angelo Dureghello <adureghello@baylibre.com>
>
> Hi Angelo,
>
> Thank you for testing on ISA_C hardware.
>
> Best regards,
> Tzu-Ching
^ permalink raw reply [flat|nested] 6+ messages in thread