From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752176AbcDOPVh (ORCPT ); Fri, 15 Apr 2016 11:21:37 -0400 Received: from m12-16.163.com ([220.181.12.16]:42157 "EHLO m12-16.163.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752021AbcDOPVg (ORCPT ); Fri, 15 Apr 2016 11:21:36 -0400 From: zengzhaoxiu@163.com To: linux-kernel@vger.kernel.org Cc: Zeng Zhaoxiu , Steven Miao , adi-buildroot-devel@lists.sourceforge.net Subject: [PATCH V3] blackfin: optimize ffz, __ffs, ffs, __fls, and fls functions Date: Fri, 15 Apr 2016 23:20:45 +0800 Message-Id: <1460733645-77406-1-git-send-email-zengzhaoxiu@163.com> X-Mailer: git-send-email 2.5.5 In-Reply-To: <1460706724-15953-1-git-send-email-zengzhaoxiu@163.com> References: <1460706724-15953-1-git-send-email-zengzhaoxiu@163.com> X-CM-TRANSID: EMCowAAXCvDZBhFXe0VGAg--.8426S3 X-Coremail-Antispam: 1Uf129KBjvJXoWxXFyUtrW8GF13GFW3Ar47urg_yoWruryrpF sYk3s5JrWDta48XFWakr1vyFW3AFs3GrnxJrWa93s7JF1Uta1DAF92gr1DXw1DGrWvva4a vrZrGFy5GF47XaDanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07jxyCJUUUUU= X-Originating-IP: [14.215.39.229] X-CM-SenderInfo: p2hqw6xkdr5xrx6rljoofrz/1tbiNR9MgFSING8l6wAAsv Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Zeng Zhaoxiu blackfin has popcount instruction (ONES), we can do the efficient computing (ffz, __ffs, ffs, __fls, and fls) use this instruction. Adapted from "https://en.wikipedia.org/wiki/Find_first_set" and arch/ia64/include/asm/bitops.h. Changes to V2: - Fix build errors Changes to V1: - Use hweight32 instead __arch_hweight32 Signed-off-by: Zeng Zhaoxiu --- arch/blackfin/include/asm/bitops.h | 144 ++++++++++++++++++++++++++----------- 1 file changed, 104 insertions(+), 40 deletions(-) diff --git a/arch/blackfin/include/asm/bitops.h b/arch/blackfin/include/asm/bitops.h index b298b65..8380d1b 100644 --- a/arch/blackfin/include/asm/bitops.h +++ b/arch/blackfin/include/asm/bitops.h @@ -9,20 +9,115 @@ #include -#include -#include -#include -#include -#include -#include - #ifndef _LINUX_BITOPS_H #error only can be included directly #endif -#include -#include +/* + * hweightN: returns the hamming weight (i.e. the number + * of bits set) of a N-bit word + */ + +static inline unsigned int __arch_hweight32(unsigned int w) +{ + unsigned int res; + + __asm__ ("%0.l = ONES %1;" + "%0 = %0.l (Z);" + : "=d" (res) : "d" (w)); + return res; +} + +static inline unsigned int __arch_hweight64(__u64 w) +{ + return __arch_hweight32((unsigned int)(w >> 32)) + + __arch_hweight32((unsigned int)w); +} + +static inline unsigned int __arch_hweight16(unsigned int w) +{ + return __arch_hweight32(w & 0xffff); +} + +static inline unsigned int __arch_hweight8(unsigned int w) +{ + return __arch_hweight32(w & 0xff); +} + #include + +/** + * ffz - find the first zero bit in a long word + * @x: The long word to find the bit in + * + * Returns the bit-number (0..31) of the first (least significant) zero bit. + * Undefined if no zero exists, so code should check against ~0UL first... + */ +static inline unsigned long ffz(unsigned long x) +{ + return hweight32(x & (~x - 1)); +} + +/** + * ffs - find first bit set + * @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 above ffz (man ffs). + */ +static inline int ffs(int x) +{ + if (!x) + return 0; + return hweight32(x ^ (x - 1)); +} + +/** + * __ffs - find first bit in word. + * @x: The word to search + * + * Undefined if no bit exists, so code should check against 0 first. + */ +static inline unsigned long __ffs(unsigned long x) +{ + return hweight32(~x & (x - 1)); +} + +/* + * Find the last (most significant) bit set. Returns 0 for x==0 and + * bits are numbered from 1..32 (e.g., fls(9) == 4). + */ +static inline int fls(int x) +{ + if (!x) + return 0; + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + return hweight32(x); +} + +/* + * Find the last (most significant) bit set. Undefined for x==0. + * Bits are numbered from 0..31 (e.g., __fls(9) == 3). + */ +static inline unsigned long __fls(unsigned long x) +{ + x |= x >> 1; + x |= x >> 2; + x |= x >> 4; + x |= x >> 8; + x |= x >> 16; + return hweight32(x) - 1; +} + +#include +#include + +#include #include #include @@ -106,35 +201,4 @@ static inline int test_and_change_bit(int nr, volatile unsigned long *addr) /* Needs to be after test_bit and friends */ #include -/* - * hweightN: returns the hamming weight (i.e. the number - * of bits set) of a N-bit word - */ - -static inline unsigned int __arch_hweight32(unsigned int w) -{ - unsigned int res; - - __asm__ ("%0.l = ONES %1;" - "%0 = %0.l (Z);" - : "=d" (res) : "d" (w)); - return res; -} - -static inline unsigned int __arch_hweight64(__u64 w) -{ - return __arch_hweight32((unsigned int)(w >> 32)) + - __arch_hweight32((unsigned int)w); -} - -static inline unsigned int __arch_hweight16(unsigned int w) -{ - return __arch_hweight32(w & 0xffff); -} - -static inline unsigned int __arch_hweight8(unsigned int w) -{ - return __arch_hweight32(w & 0xff); -} - #endif /* _BLACKFIN_BITOPS_H */ -- 2.5.5