Add arch-agnostic fls() fallback
authorMathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Tue, 22 Feb 2011 22:29:31 +0000 (17:29 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Tue, 22 Feb 2011 22:29:31 +0000 (17:29 -0500)
In preparation for ARM port.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
include/ust/processor.h

index 8a01a8868c19385b6990957693dd2915ce203f15..6ee44ddde599f3bd8eb5e24d90295c800168e1be 100644 (file)
@@ -214,9 +214,7 @@ static inline int fls(int x)
 
 #define _ASM_PTR ".long "
 
-#endif /* below is code for x86-64 */
-
-#ifdef __x86_64
+#elif defined(__x86_64)
 
 struct registers {
        int padding; /* 4 bytes */
@@ -425,9 +423,7 @@ static inline int fls(int x)
 
 #define _ASM_PTR ".quad "
 
-#endif /* x86_64 */
-
-#ifdef __PPC__
+#elif defined(__PPC__)
 
 struct registers {
 };
@@ -447,6 +443,37 @@ static __inline__ int fls(unsigned int x)
 #define _ASM_PTR ".long "
 #define save_registers(a)
 
-#endif /* __PPC__ */
+#else /* arch-agnostic */
+
+static __inline__ int fls(unsigned int x)
+{
+       int r = 32;
+
+       if (!x)
+               return 0;
+       if (!(x & 0xFFFF0000U)) {
+               x <<= 16;
+               r -= 16;
+       }
+       if (!(x & 0xFF000000U)) {
+               x <<= 8;
+               r -= 8;
+       }
+       if (!(x & 0xF0000000U)) {
+               x <<= 4;
+               r -= 4;
+       }
+       if (!(x & 0xC0000000U)) {
+               x <<= 2;
+               r -= 2;
+       }
+       if (!(x & 0x80000000U)) {
+               x <<= 1;
+               r -= 1;
+       }
+       return r;
+}
+
+#endif
 
 #endif /* UST_PROCESSOR_H */
This page took 0.026029 seconds and 4 git commands to generate.