MSWSP: parse_CColumnGroupArray() etc.
[wireshark-wip.git] / wsutil / bits_count_ones.h
blob82f88bc43c5d7bb0cff00ddf626757c1f1a8c788
1 /*
2 * bits_count_ones.h
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #ifndef __WSUTIL_BITS_COUNT_ONES_H__
27 #define __WSUTIL_BITS_COUNT_ONES_H__
29 #include "config.h"
31 #include <glib.h>
34 * The variable-precision SWAR algorithm is an interesting way to count
35 * the number of bits set in an integer. While its performance is very
36 * good (two times faster than gcc's __builtin_popcount [1] and
37 * 16 instructions when compiled with gcc -O3)
38 * http://playingwithpointers.com/swar.html
41 static inline int
42 ws_count_ones(const guint32 x)
44 int bits = x;
46 bits = bits - ((bits >> 1) & 0x55555555);
47 bits = (bits & 0x33333333) + ((bits >> 2) & 0x33333333);
48 bits = (bits + (bits >> 4)) & 0x0F0F0F0F;
50 return (bits * 0x01010101) >> 24;
53 #endif /* __WSUTIL_BITS_COUNT_ONES_H__ */