[PATCH v2] Add bit operations util header

Поиск
Список
Период
Сортировка
От Jeremy Kerr
Тема [PATCH v2] Add bit operations util header
Дата
Msg-id 1243985601.846636.478412111657.1.gpush@pingu
обсуждение исходный текст
Ответ на Re: [PATCH 1/2] Add bit operations util header  (Tom Lane <tgl@sss.pgh.pa.us>)
Ответы Re: [PATCH v2] Add bit operations util header  (Florian Weimer <fweimer@bfk.de>)
Список pgsql-hackers
Add a utility header for simple bit operatios - bitops.h.

At present, just contains the fls() (find last set bit) function.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>

---
v2: only use inline asm with gcc

---src/include/utils/bitops.h |   53 +++++++++++++++++++++++++++++++++++++++++++++1 file changed, 53 insertions(+)

diff --git a/src/include/utils/bitops.h b/src/include/utils/bitops.h
new file mode 100644
index 0000000..4f2bbc9
--- /dev/null
+++ b/src/include/utils/bitops.h
@@ -0,0 +1,53 @@
+/*-------------------------------------------------------------------------
+ *
+ * bitops.h
+ *      Simple bit operations.
+ *
+ * Portions Copyright (c) 2009, PostgreSQL Global Development Group
+ *
+ * $PostgreSQL$
+ *
+ *-------------------------------------------------------------------------
+ */
+#ifndef BITOPS_H
+#define BITOPS_H
+
+#if defined(__GNUC__) && \
+    (defined(__ppc__) || defined(__powerpc__) || \
+     defined(__ppc64__) || defined (__powerpc64__))
+
+static inline int
+fls(unsigned int x)
+{
+    int lz;
+    asm("cntlz %0,%1" : "=r" (lz) : "r" (x));
+    return 32 - lz;
+}
+
+#else /* !(gcc && powerpc) */
+
+/* Architecture-independent implementations */
+
+/*
+ * fls: find last set bit.
+ *
+ * Returns the 1-based index of the most-significant bit in x. The MSB
+ * is bit number 32, the LSB is bit number 1. If x is zero, returns zero.
+ */
+static inline int
+fls(unsigned int x)
+{
+    int ls = 0;
+
+    while (x != 0)
+    {
+        ls++;
+        x >>= 1;
+    }
+
+    return ls;
+}
+
+#endif
+
+#endif /* BITOPS_H */


В списке pgsql-hackers по дате отправления:

Предыдущее
От: "David E. Wheeler"
Дата:
Сообщение: Re: Managing multiple branches in git
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Managing multiple branches in git