Skip to contentSkip to conversation

fls in asm for i386

PATCH In review Stephen Hemminger 3 participants 4 messages Dec 3, 2005 – Dec 5, 2005

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 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()).

include/asm-i386/bitops.h +17 -6
@@ -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
  *
#1 lore raw

PATCH 1/1 fls in asm for x86_64

Use single instruction for find largest set bit on x86_64.

include/asm-x86_64/bitops.h +16 -3
@@ -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 */
#2 lore raw

Re: [PATCH] fls in asm for x86_64

Quoted 1 line · Stephen Hemminger

Use single instruction for find largest set bit on x86_64.

Thanks queued.

-Andi