Here are optimized ffs(3) implementations for * arm64 (superseding the earlier ffs.S) * powerpc * powerpc64
arm64 tested by myself, powerpc tested by cwen@. OK? (Some other archs fell through. sparc64 specifies the popc instruction that can be used for this, but UltraSPARC doesn't implement it. alpha's ctlz was only added late with the CIX extension.) Index: lib/libkern/arch/arm64/ffs.c =================================================================== RCS file: lib/libkern/arch/arm64/ffs.c diff -N lib/libkern/arch/arm64/ffs.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/libkern/arch/arm64/ffs.c 9 Jun 2020 18:09:11 -0000 @@ -0,0 +1,12 @@ +/* $OpenBSD$ */ +/* + * Written by Christian Weisgerber <[email protected]>. + * Public domain. + */ + +int ffs(int x) +{ + x = x & -x; + __asm volatile("clz %w0, %w0" : "+r" (x)); + return (32 - x); +} Index: lib/libkern/arch/powerpc/ffs.c =================================================================== RCS file: lib/libkern/arch/powerpc/ffs.c diff -N lib/libkern/arch/powerpc/ffs.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/libkern/arch/powerpc/ffs.c 9 Jun 2020 18:10:20 -0000 @@ -0,0 +1,12 @@ +/* $OpenBSD$ */ +/* + * Written by Christian Weisgerber <[email protected]>. + * Public domain. + */ + +int ffs(int x) +{ + x = x & -x; + __asm volatile("cntlzw %0, %0" : "+r" (x)); + return (32 - x); +} Index: lib/libkern/arch/powerpc64/ffs.c =================================================================== RCS file: lib/libkern/arch/powerpc64/ffs.c diff -N lib/libkern/arch/powerpc64/ffs.c --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ lib/libkern/arch/powerpc64/ffs.c 9 Jun 2020 18:10:49 -0000 @@ -0,0 +1,12 @@ +/* $OpenBSD$ */ +/* + * Written by Christian Weisgerber <[email protected]>. + * Public domain. + */ + +int ffs(int x) +{ + x = x & -x; + __asm volatile("cntlzw %0, %0" : "+r" (x)); + return (32 - x); +} -- Christian "naddy" Weisgerber [email protected]
