aboutsummaryrefslogtreecommitdiffstats
path: root/wsutil
diff options
context:
space:
mode:
authorEvan Huus <eapache@gmail.com>2013-08-26 16:23:55 +0000
committerEvan Huus <eapache@gmail.com>2013-08-26 16:23:55 +0000
commit304bbabbbee3ede70ca6e4f55938df3e5d41bad9 (patch)
tree0c8e9681241d16d7d23fd869c214160e8805f362 /wsutil
parent2a0b65b827512f928fff63939a5cb0a14768f673 (diff)
From Jiri Engelthaler via
https://bugs.wireshark.org/bugzilla/show_bug.cgi?id=9055 Add support for bitmask set for signed integer types. svn path=/trunk/; revision=51522
Diffstat (limited to 'wsutil')
-rw-r--r--wsutil/CMakeLists.txt1
-rw-r--r--wsutil/Makefile.common2
-rw-r--r--wsutil/swar.c49
-rw-r--r--wsutil/swar.h32
4 files changed, 84 insertions, 0 deletions
diff --git a/wsutil/CMakeLists.txt b/wsutil/CMakeLists.txt
index d40e2cc630..7b9093c22e 100644
--- a/wsutil/CMakeLists.txt
+++ b/wsutil/CMakeLists.txt
@@ -60,6 +60,7 @@ set(WSUTIL_FILES
str_util.c
rc4.c
report_err.c
+ swar.c
tempfile.c
type_util.c
u3.c
diff --git a/wsutil/Makefile.common b/wsutil/Makefile.common
index ba181e95d6..7332d97d8e 100644
--- a/wsutil/Makefile.common
+++ b/wsutil/Makefile.common
@@ -52,6 +52,7 @@ LIBWSUTIL_SRC = \
sha1.c \
strnatcmp.c \
str_util.c \
+ swar.c \
rc4.c \
report_err.c \
tempfile.c \
@@ -85,6 +86,7 @@ LIBWSUTIL_INCLUDES = \
pint.h \
rc4.h \
report_err.h \
+ swar.h \
tempfile.h \
type_util.h \
u3.h
diff --git a/wsutil/swar.c b/wsutil/swar.c
new file mode 100644
index 0000000000..c784ea42f9
--- /dev/null
+++ b/wsutil/swar.c
@@ -0,0 +1,49 @@
+/*
+ * swar.c
+ *
+ * $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.
+ *
+ */
+
+#include "config.h"
+
+#include <glib.h>
+
+#include "swar.h"
+
+/*
+ * The variable-precision SWAR algorithm is an interesting way to count
+ * the number of bits set in an integer. While its performance is very
+ * good (two times faster than gcc's __builtin_popcount [1] and
+ * 16 instructions when compiled with gcc -O3)
+ * http://playingwithpointers.com/swar.html
+ */
+
+int swar_count_bits(const guint32 bitmask)
+{
+ int bits = bitmask;
+
+ bits = bits - ((bits >> 1) & 0x55555555);
+ bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
+ bits = (bits + (bits >> 4)) & 0x0F0F0F0F;
+
+ return (bits * 0x01010101) >> 24;
+}
diff --git a/wsutil/swar.h b/wsutil/swar.h
new file mode 100644
index 0000000000..6541308fed
--- /dev/null
+++ b/wsutil/swar.h
@@ -0,0 +1,32 @@
+/*
+ * swar.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 __SWAR_H__
+#define __SWAR_H__
+
+#include "ws_symbol_export.h"
+
+WS_DLL_PUBLIC int swar_count_bits(const guint32 bitmask);
+
+#endif /* __SWAR_H__ */