summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--epan/proto.c18
-rw-r--r--wsutil/Makefile.common1
-rw-r--r--wsutil/bits_ctz.h48
3 files changed, 52 insertions, 15 deletions
diff --git a/epan/proto.c b/epan/proto.c
index bd81825512..07c775bd01 100644
--- a/epan/proto.c
+++ b/epan/proto.c
@@ -29,6 +29,8 @@
#include <ctype.h>
#include <glib.h>
#include <float.h>
+
+#include <wsutil/bits_ctz.h>
#include <wsutil/swar.h>
#include "packet.h"
@@ -5603,21 +5605,7 @@ fill_label_number64(field_info *fi, gchar *label_str, gboolean is_signed)
int
hfinfo_bitshift(const header_field_info *hfinfo)
{
- const guint32 bitmask = hfinfo->bitmask;
-
-#if defined(__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
- g_assert(bitmask != 0);
-
- return __builtin_ctz(bitmask);
-#else
- /* From http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
- static const int table[32] = {
- 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
- 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
- };
-
- return table[((guint32)((bitmask & -(gint32)bitmask) * 0x077CB531U)) >> 27];
-#endif
+ return ws_ctz(hfinfo->bitmask);
}
int
diff --git a/wsutil/Makefile.common b/wsutil/Makefile.common
index 7332d97d8e..4d2ab79376 100644
--- a/wsutil/Makefile.common
+++ b/wsutil/Makefile.common
@@ -62,6 +62,7 @@ LIBWSUTIL_SRC = \
# Header files that are not generated from other files
LIBWSUTIL_INCLUDES = \
aes.h \
+ bits_ctz.h \
crash_info.h \
crc6.h \
crc7.h \
diff --git a/wsutil/bits_ctz.h b/wsutil/bits_ctz.h
new file mode 100644
index 0000000000..7fa9cd87ad
--- /dev/null
+++ b/wsutil/bits_ctz.h
@@ -0,0 +1,48 @@
+/*
+ * bitz_ctz.h
+ *
+ * $Id$
+ *
+ * Wireshark - Network traffic analyzer
+ * By Gerald Combs <gerald@wireshark.org>
+ * Copyright 1998 Gerald Combs
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef __WSUTIL_BITS_CTZ_H__
+#define __WSUTIL_BITS_CTZ_H__
+
+#include <glib.h>
+
+static inline int
+ws_ctz(guint32 x)
+{
+#if defined(__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
+ g_assert(x != 0);
+
+ return __builtin_ctz(x);
+#else
+ /* From http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
+ static const int table[32] = {
+ 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
+ 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
+ };
+
+ return table[((guint32)((x & -(gint32)x) * 0x077CB531U)) >> 27];
+#endif
+}
+
+#endif /* __WSUTIL_BITS_CTZ_H__ */