util: Better count_1bits().
authorJarno Rajahalme <jrajahalme@nicira.com>
Wed, 27 Nov 2013 20:58:46 +0000 (12:58 -0800)
committerJarno Rajahalme <jrajahalme@nicira.com>
Fri, 6 Dec 2013 20:55:27 +0000 (12:55 -0800)
commitc3cc4d2dd265823828d93278b5d1ead4faad85ba
tree1eab27791136f105a62a6642bc2886e36d08181c
parent52105b67878c83d16ef86f1b70fe8a076fdf69cb
util: Better count_1bits().

Inline, use another well-known algorithm for 64-bit builds, and use
builtins when they are known to be fast at compile time.  A 32-bit
version of the alternate algorithm is slower than the existing
implementation, so the old one is used for 32-bit builds.  Inline
assembler would be a bit faster on 32-bit i7 build, but we use the GCC
builtin for portability.

It should be stressed builds for specific CPUs do not work on others
CPUs, and that OVS build system or runtime does not currently support
CPU detection.

Speed improvement v.s. existing implementation / GCC 4.7
__builtin_popcountll():

i386:         64%  (inlining)                         / 380%
i386 on i7:   240% (inlining + builtin)               / 820%
x86_64:       59%  (inlining + different algorithm)   / 190%
x86_64 on i7: 370% (inlining + builtin)               / 0%

Signed-off-by: Jarno Rajahalme <jrajahalme@nicira.com>
Acked-by: Ben Pfaff <blp@nicira.com>
lib/util.c
lib/util.h