FRV: Fix fls() to handle bit 31 being set correctly [try #2]
PATCH 1/7 FRV: Fix fls() to handle bit 31 being set correctly [try #2]
From: David Howells <[email protected]>
Fix FRV fls() to handle bit 31 being set correctly (it should return 32 not 0).
---
include/asm-frv/bitops.h | 21 +++++++++++++++++---- 1 files changed, 17 insertions(+), 4 deletions(-)
include/asm-frv/bitops.h +17 -4
@@ -161,16 +161,29 @@ #include <asm-generic/bitops/ffs.h>#include <asm-generic/bitops/__ffs.h> #include <asm-generic/bitops/find.h> -/* -* fls: find last bit set. +/** +* fls - find last bit set +* @x: the word to search +* +* This is defined the same way as ffs: +* - return 32..1 to indicate bit 31..0 most significant bit set +* - return 0 to indicate no bits set */ #define fls (x) \ ({ \ int bit; \ \ - asm("scan %1,gr0,%0" : "=r"(bit) : "r"(x)); \ + asm(" subcc %1,gr0,gr0,icc0 \n" \ + " ckne icc0,cc4 \n" \ + " cscan.p %1,gr0,%0 ,cc4,#1 \n" \ + " csub %0,%0,%0 ,cc4,#0 \n" \ + " csub %2,%0,%0 ,cc4,#1 \n" \ + : "=&r"(bit) \ + : "r"(x), "r"(32) \ + : "icc0", "cc4" \ + ); \ \ - bit ? 33 - bit : bit; \ + bit; \ })#include <asm-generic/bitops/fls64.h>
PATCH 2/7 FRV: Implement fls64() [try #2]
From: David Howells <[email protected]>
Implement fls64() for FRV without recource to conditional jumps.
---
include/asm-frv/bitops.h | 42 +++++++++++++++++++++++++++++++++++++++++- 1 files changed, 41 insertions(+), 1 deletions(-)
include/asm-frv/bitops.h +41 -1
@@ -186,7 +186,47 @@ ({ \ bit; \ }) -#include <asm-generic/bitops/fls64.h> +/** +* fls64 - find last bit set in a 64-bit value +* @n: the value to search +* +* This is defined the same way as ffs: +* - return 64..1 to indicate bit 63..0 most significant bit set +* - return 0 to indicate no bits set +*/ +static inline __attribute__((const)) +int fls64 (u64 n) +{ +union { +u64 ll; +struct {u32 h, l; }; + } _; +int bit, x, y; + + _.ll = n; + + asm(" subcc.p %3,gr0,gr0,icc0 \n" +" subcc %4,gr0,gr0,icc1 \n" +" ckne icc0,cc4 \n" +" ckne icc1,cc5 \n" +" norcr cc4,cc5,cc6 \n" +" csub.p %0,%0,%0 ,cc6,1 \n" +" orcr cc5,cc4,cc4 \n" +" andcr cc4,cc5,cc4 \n" +" cscan.p %3,gr0,%0 ,cc4,0 \n" +" setlos #64,%1 \n" +" cscan.p %4,gr0,%0 ,cc4,1 \n" +" setlos #32,%2 \n" +" csub.p %1,%0,%0 ,cc4,0 \n" +" csub %2,%0,%0 ,cc4,1 \n" + :"=&r" (bit),"=r" (x),"=r" (y) + :"0r" (_.h),"r" (_.l) + :"icc0" ,"icc1" ,"cc4" ,"cc5" ,"cc6" + ); +return bit; + +} +#include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/hweight.h>
PATCH 3/7 FRV: Optimise ffs() [try #2]
In reply to David HowellsFrom: David Howells <[email protected]>
Optimise ffs(x) by using fls(x & x - 1) which we optimise to use the SCAN instruction.
---
include/asm-frv/bitops.h | 33 +++++++++++++++++++++++++++++++-- 1 files changed, 31 insertions(+), 2 deletions(-)
include/asm-frv/bitops.h +31 -2
@@ -157,8 +157,6 @@ (__builtin_constant_p(nr) ? \ __constant_test_bit((nr),(addr)) : \ __test_bit((nr),(addr))) -#include <asm-generic/bitops/ffs.h> -#include <asm-generic/bitops/__ffs.h> #include <asm-generic/bitops/find.h> /**
@@ -227,6 +225,37 @@ int fls64(u64 n) } +/** +* ffs - find first bit set +* @x: the word to search +* +* - return 32..1 to indicate bit 31..0 most least significant bit set +* - return 0 to indicate no bits set +*/ +static inline __attribute__((const)) +int ffs (int x) +{ +/* Note: (x & -x) gives us a mask that is the least significant +* (rightmost) 1-bit of the value in x. +*/ +return fls (x & -x); +} + +/** +* __ffs - find first bit set +* @x: the word to search +* +* - return 31..0 to indicate bit 31..0 most least significant bit set +* - if no bits are set in x, the result is undefined +*/ +static inline __attribute__((const)) +int __ffs (unsigned long x) +{ +int bit; + asm("scan %1,gr0,%0" :"=r" (bit) :"r" (x & -x)); +return 31 - bit; +} +#include <asm-generic/bitops/sched.h> #include <asm-generic/bitops/hweight.h>
PATCH 4/7 Implement a general integer log2 facility in the kernel [try #2]
In reply to David HowellsFrom: David Howells <[email protected]>
This facility provides three entry points:
ilog2() Log base 2 of unsigned long ilog2_u32() Log base 2 of u32 ilog2_u64() Log base 2 of u64
These facilities can either be used inside functions on dynamic data:
int do_something(long q)
{
...;
y = ilog2(x)
...;
}Or can be used to statically initialise global variables with constant values:
unsigned n = ilog2(27);
When performing static initialisation, the compiler will report "error:
initializer element is not constant" if asked to take a log of zero or of something not reducible to a constant. They treat negative numbers as unsigned.
When not dealing with a constant, they fall back to using fls() which permits them to use arch-specific log calculation instructions - such as BSR on x86/x86_64 or SCAN on FRV - if available.
---
fs/ext2/super.c | 6 - fs/ext3/super.c | 6 - include/asm-frv/bitops.h | 44 +++++++++++ include/linux/kernel.h | 9 -- include/linux/log2.h | 188 ++++++++++++++++++++++++++++++++++++++++++++++ mm/page_alloc.c | 4 - 6 files changed, 239 insertions(+), 18 deletions(-)
fs/ext2/super.c +2 -4
@@ -548,8 +548,6 @@ static int ext2_check_descriptors (strucreturn 1 ; } -#define log2 (n) ffz(~(n)) -/* * Maximal file size. There is a direct, and {,double-,triple-}indirect * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
@@ -784,9 +782,9 @@ static int ext2_fill_super(struct super_ sbi->s_sbh = bh; sbi->s_mount_state =le16_to_cpu (es->s_state); sbi->s_addr_per_block_bits = -log2 (EXT2_ADDR_PER_BLOCK (sb)); +ilog2 (EXT2_ADDR_PER_BLOCK (sb)); sbi->s_desc_per_block_bits = -log2 (EXT2_DESC_PER_BLOCK (sb)); +ilog2 (EXT2_DESC_PER_BLOCK (sb));if (sb->s_magic !=EXT2_SUPER_MAGIC ) goto cantfind_ext2;
fs/ext3/super.c +2 -4
@@ -1294,8 +1294,6 @@ #endif sb->s_flags = s_flags;/* Restore MS_RDONLY status */ } -#define log2 (n) ffz(~(n)) -/* * Maximal file size. There is a direct, and {,double-,triple-}indirect * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
@@ -1543,8 +1541,8 @@ static int ext3_fill_super (struct super sbi->s_desc_per_block = blocksize /sizeof (struct ext3_group_desc ); sbi->s_sbh = bh; sbi->s_mount_state =le16_to_cpu (es->s_state); - sbi->s_addr_per_block_bits =log2 (EXT3_ADDR_PER_BLOCK (sb)); - sbi->s_desc_per_block_bits =log2 (EXT3_DESC_PER_BLOCK (sb)); + sbi->s_addr_per_block_bits =ilog2 (EXT3_ADDR_PER_BLOCK (sb)); + sbi->s_desc_per_block_bits =ilog2 (EXT3_DESC_PER_BLOCK (sb));for (i=0 ; i <4 ; i++) sbi->s_hash_seed[i] =le32_to_cpu (es->s_hash_seed[i]); sbi->s_def_hash_version = es->s_def_hash_version;
include/asm-frv/bitops.h +44 -0
50 lines not on this page · raw message
include/linux/kernel.h +1 -8
@@ -13,6 +13,7 @@ #include <linux/stddef.h>#include <linux/types.h> #include <linux/compiler.h> #include <linux/bitops.h> +#include <linux/log2.h> #include <asm/byteorder.h> #include <asm/bug.h>
@@ -154,14 +155,6 @@ #endifunsigned long int_sqrt (unsigned long ); -static inline int __attribute_pure__ long_log2(unsigned long x) -{ -int r =0 ; -for (x >>=1 ; x >0 ; x >>=1 ) - r++; -return r; -} -static inline unsigned long __attribute_const__ roundup_pow_of_two (unsigned long x) {
include/linux/log2.h new file +188 -0
188 lines not on this page · raw message
mm/page_alloc.c +2 -2
@@ -2420,7 +2420,7 @@ void *__init alloc_large_system_hash(conif (numentries > max) numentries = max; - log2qty =long_log2 (numentries); + log2qty =ilog2 (numentries); do { size = bucketsize << log2qty;
@@ -2442,7 +2442,7 @@ void *__init alloc_large_system_hash(conprintk ("%s hash table entries: %d (order: %d, %lu bytes)\n" , tablename, (1U << log2qty), -long_log2 (size) -PAGE_SHIFT , +ilog2 (size) -PAGE_SHIFT , size);if (_hash_shift)
PATCH 5/7 Alter get_order() so that it can make use of ilog2() on a constant [try #2]
In reply to David HowellsFrom: David Howells <[email protected]>
Alter get_order() so that it can make use of ilog2() on a constant to produce a constant value, retaining the ability for an arch to override it in the non-const case.
---
include/asm-generic/page.h | 14 -------------- include/linux/log2.h | 45 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 14 deletions(-)
include/asm-generic/page.h +0 -14
@@ -6,20 +6,6 @@ #ifndef __ASSEMBLY__#include <linux/compiler.h> -/* Pure 2^n version of get_order */ -static __inline__ __attribute_const__ int get_order(unsigned long size) -{ -int order; - - size = (size -1 ) >> (PAGE_SHIFT -1 ); - order =-1 ; -do { - size >>=1 ; - order++; - }while (size); -return order; -} -#endif /* __ASSEMBLY__ */ #endif /* __KERNEL__ */
include/linux/log2.h +45 -0
61 lines not on this page · raw message
PATCH 6/7 Alter roundup_pow_of_two() so that it can make use of ilog2() on a constant [try #2]
In reply to David HowellsFrom: David Howells <[email protected]>
Alter roundup_pow_of_two() so that it can make use of ilog2() on a constant to produce a constant value, retaining the ability for an arch to override it in the non-const case.
This permits the function to be used to initialise variables.
---
include/linux/kernel.h | 6 ------ include/linux/log2.h | 26 ++++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-)
include/linux/kernel.h +0 -6
@@ -155,12 +155,6 @@ #endifunsigned long int_sqrt (unsigned long ); -static inline unsigned long -__attribute_const__ roundup_pow_of_two (unsigned long x) -{ -return 1UL <<fls_long (x -1 ); -} -extern int printk_ratelimit (void );extern int __printk_ratelimit (int ratelimit_jiffies,int ratelimit_burst);
include/linux/log2.h +26 -0
@@ -73,6 +73,15 @@ #endif }#endif +/* +* round up to nearest power of two +*/ +static inline __attribute__((const)) +unsigned long __roundup_pow_of_two (unsigned long n) +{ +return 1UL <<fls_long (n -1 ); +} +/** * ilog2_u32 - log of base 2 of 32-bit unsigned value * @n - parameter
@@ -230,4 +239,21 @@ ( \ __get_order(n, PAGE_SHIFT) \ ) +/** +* roundup_pow_of_two - round the given value up to nearest power of two +* @n - parameter +* +* round the given balue up to the nearest power of two +* - the result is undefined when n == 0 +* - this can be used to initialise global variables from constant data +*/ +#define roundup_pow_of_two (n) \ +( \ + __builtin_constant_p(n) ? ( \ + (n == 1) ? 0 : \ + (1UL << (ilog2((n) - 1) + 1)) \ + ) : \ + __roundup_pow_of_two(n) \ + ) +#endif /* _LINUX_LOG2_H */
PATCH 7/7 Provide ilog2() fallbacks for powerpc [try #2]
In reply to David HowellsFrom: David Howells <[email protected]>
Provide ilog2() fallbacks for powerpc for 32-bit numbers and 64-bit numbers on ppc64.
---
include/asm-powerpc/bitops.h | 23 ++++++++++++++++++++++- 1 files changed, 22 insertions(+), 1 deletions(-)
include/asm-powerpc/bitops.h +22 -1
@@ -190,7 +190,8 @@ #include <asm-generic/bitops/non-atomic.* Return the zero-based bit position (LE, not IBM bit numbering) of * the most significant 1-bit in a double word. */ -static __inline__int __ilog2 (unsigned long x) +static __inline__ __attribute__((const)) +int __ilog2 (unsigned long x) {int lz;
@@ -198,6 +199,26 @@ static __inline__ int __ilog2(unsigned lreturn BITS_PER_LONG -1 - lz; } +#define ARCH_HAS_ILOG2_U32 +static inline __attribute__((const)) +int __ilog2_u32 (u32 n) +{ +int bit; + asm ("cntlzw %0,%1" :"=r" (bit) :"r" (n)); +return 31 - bit; +} + +#ifdef __powerpc64__ +#define ARCH_HAS_ILOG2_U64 +static inline __attribute__((const)) +int __ilog2_u64 (u32 n) +{ +int bit; + asm ("cntlzd %0,%1" :"=r" (bit) :"r" (n)); +return 63 - bit; +} +#endif +/* * Determines the bit position of the least significant 0 bit in the * specified double word. The returned bit position will be