2005-12-03 00:24:45

by Stephen Hemminger

[permalink] [raw]
Subject: [PATCH] fls in asm for i386

There is a single instruction on i386 to find largest set bit;
so it makes sense to use it (like we use bfs for ffs()).

Signed-off-by: Stephen Hemminger <[email protected]>

--- orig/include/asm-i386/bitops.h
+++ new/include/asm-i386/bitops.h
@@ -367,12 +367,6 @@ static inline unsigned long ffz(unsigned
return word;
}

-/*
- * fls: find last bit set.
- */
-
-#define fls(x) generic_fls(x)
-
#ifdef __KERNEL__

/*
@@ -414,6 +408,23 @@ static inline int ffs(int x)
}

/**
+ * fls - find last bit set
+ * @x: the word to search
+ *
+ * This is defined the same way as ffs.
+ */
+static inline int fls(int x)
+{
+ int r;
+
+ __asm__("bsrl %1,%0\n\t"
+ "jnz 1f\n\t"
+ "movl $-1,%0\n"
+ "1:" : "=r" (r) : "rm" (x));
+ return r+1;
+}
+
+/**
* hweightN - returns the hamming weight of a N-bit word
* @x: the word to weigh
*


2005-12-03 00:24:49

by Stephen Hemminger

[permalink] [raw]
Subject: [PATCH] fls in asm for x86_64

Use single instruction for find largest set bit on x86_64.

Signed-off-by: Stephen Hemminger <[email protected]>

--- orig/include/asm-x86_64/bitops.h
+++ new/include/asm-x86_64/bitops.h
@@ -370,6 +370,22 @@ static __inline__ int ffs(int x)
}

/**
+ * fls - find last bit set
+ * @x: the word to search
+ *
+ * This is defined the same way as ffs.
+ */
+static __inline__ int fls(int x)
+{
+ int r;
+
+ __asm__("bsrl %1,%0\n\t"
+ "cmovzl %2,%0"
+ : "=r" (r) : "rm" (x), "r" (-1));
+ return r+1;
+}
+
+/**
* hweightN - returns the hamming weight of a N-bit word
* @x: the word to weigh
*
@@ -407,9 +423,6 @@ static __inline__ int ffs(int x)
#define minix_find_first_zero_bit(addr,size) \
find_first_zero_bit((void*)addr,size)

-/* find last set bit */
-#define fls(x) generic_fls(x)
-
#endif /* __KERNEL__ */

#endif /* _X86_64_BITOPS_H */

2005-12-03 01:44:28

by Andi Kleen

[permalink] [raw]
Subject: Re: [PATCH] fls in asm for x86_64

On Fri, Dec 02, 2005 at 04:24:36PM -0800, Stephen Hemminger wrote:
> Use single instruction for find largest set bit on x86_64.

Thanks queued.

-Andi

2005-12-05 13:48:47

by Pádraig Brady

[permalink] [raw]
Subject: Re: [PATCH] fls in asm for i386

Stephen Hemminger wrote:

>There is a single instruction on i386 to find largest set bit;
>so it makes sense to use it (like we use bfs for ffs()).
>
>
Interesting, I thought this had already been done:
http://lkml.org/lkml/2003/1/28/296
http://lkml.org/lkml/2003/4/29/173

P?draig.