Print this page
5288 x86 bitmap inline asm functions duplicate code

@@ -34,83 +34,64 @@
 extern "C" {
 #endif
 
 #if !defined(__lint) && defined(__GNUC__)
 
+#if defined(__amd64)
+#define __SUF   "q"
+#elif defined(__i386)
+#define __SUF   "l"
+#else
+#error "port me"
+#endif
+
 extern __GNU_INLINE int
 highbit(ulong_t i)
 {
-        long __value = -1l;
+        long value = -1l;
 
-#if defined(__amd64)
-        __asm__(
-            "bsrq       %1,%0"
-            : "+r" (__value)
-            : "r" (i)
-            : "cc");
-#elif defined(__i386)
         __asm__(
-            "bsrl       %1,%0"
-            : "+r" (__value)
+            "bsr" __SUF " %1,%0"
+            : "+r" (value)
             : "r" (i)
             : "cc");
-#else
-#error  "port me"
-#endif
-        return ((int)(__value + 1));
+
+        return ((int)(value + 1));
 }
 
 extern __GNU_INLINE int
 lowbit(ulong_t i)
 {
-        long __value = -1l;
+        long value = -1l;
 
-#if defined(__amd64)
-        __asm__(
-            "bsfq       %1,%0"
-            : "+r" (__value)
-            : "r" (i)
-            : "cc");
-#elif defined(__i386)
         __asm__(
-            "bsfl       %1,%0"
-            : "+r" (__value)
+            "bsf" __SUF " %1,%0"
+            : "+r" (value)
             : "r" (i)
             : "cc");
-#else
-#error  "port me"
-#endif
-        return ((int)(__value + 1));
+
+        return ((int)(value + 1));
 }
 
 extern __GNU_INLINE uint_t
 atomic_btr32(uint32_t *memory, uint_t bitnum)
 {
-        uint8_t __value;
+        uint8_t value;
 
-#if defined(__amd64)
-        __asm__ __volatile__(
-            "lock;"
-            "btrl %2, (%0);"
-            "setc %1"
-            : "+r" (memory), "+r" (__value)
-            : "ir" (bitnum)
-            : "cc");
-#elif defined(__i386)
         __asm__ __volatile__(
             "lock;"
-            "btrl %2, (%0);"
+            "btrl %2,%0;"
             "setc %1"
-            : "+r" (memory), "=r" (__value)
+            : "+m" (*memory), "=r" (value)
             : "ir" (bitnum)
             : "cc");
-#else
-#error  "port me"
-#endif
-        return ((uint_t)__value);
+
+        return ((uint_t)value);
 }
 
+#undef __SUF
+
 #endif  /* !__lint && __GNUC__ */
 
 #ifdef  __cplusplus
 }
 #endif