From 5f94e44cde18e3c48bc301438e1fadd73a2db520 Mon Sep 17 00:00:00 2001 From: Tony Hagale Date: Wed, 19 Mar 2014 16:26:45 -0500 Subject: [PATCH] #2386 trying a new static lookup table for numberOfOnes() --- libraries/shared/src/SharedUtil.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/libraries/shared/src/SharedUtil.cpp b/libraries/shared/src/SharedUtil.cpp index e5aded2798..00810653c1 100644 --- a/libraries/shared/src/SharedUtil.cpp +++ b/libraries/shared/src/SharedUtil.cpp @@ -130,14 +130,22 @@ void outputBits(unsigned char byte, QDebug* continuedDebug) { } int numberOfOnes(unsigned char byte) { - return (byte >> 7) - + ((byte >> 6) & 1) - + ((byte >> 5) & 1) - + ((byte >> 4) & 1) - + ((byte >> 3) & 1) - + ((byte >> 2) & 1) - + ((byte >> 1) & 1) - + (byte & 1); + + static const unsigned char nbits[256] = { + 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3, + 4,3,4,4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4, + 4,5,3,4,4,5,4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2, + 3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5, + 4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,1,2,2,3,2,3,3, + 4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3, + 3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5, + 6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6, + 4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5, + 6,5,6,6,7,5,6,6,7,6,7,7,8 + }; + + return nbits[(unsigned char) byte]; + } bool oneAtBit(unsigned char byte, int bitIndex) {