From fd2893bf6df8e6dba6d5b237a715180a557212d9 Mon Sep 17 00:00:00 2001 From: Andrzej Kapolka Date: Wed, 4 Jun 2014 18:23:31 -0700 Subject: [PATCH] Get rid of the log calls entirely; use the same code that we use for the ID streamer. --- libraries/metavoxels/src/Bitstream.cpp | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/libraries/metavoxels/src/Bitstream.cpp b/libraries/metavoxels/src/Bitstream.cpp index dc74594816..30d34580d7 100644 --- a/libraries/metavoxels/src/Bitstream.cpp +++ b/libraries/metavoxels/src/Bitstream.cpp @@ -47,11 +47,19 @@ IDStreamer::IDStreamer(Bitstream& stream) : _bits(1) { } -void IDStreamer::setBitsFromValue(int value) { - _bits = 1; - while (value >= (1 << _bits) - 1) { - _bits++; +static int getBitsForHighestValue(int highestValue) { + // if this turns out to be a bottleneck, there are fancier ways to do it (get the position of the highest set bit): + // http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogObvious + int bits = 0; + while (highestValue != 0) { + bits++; + highestValue >>= 1; } + return bits; +} + +void IDStreamer::setBitsFromValue(int value) { + _bits = getBitsForHighestValue(value + 1); } IDStreamer& IDStreamer::operator<<(int value) { @@ -719,10 +727,6 @@ Bitstream& Bitstream::operator<(const TypeStreamer* streamer) { return *this; } -static int getBitsForHighestValue(int highestValue) { - return (highestValue == 0) ? 0 : 1 + (int)(log((double)highestValue) / log(2.0)); -} - Bitstream& Bitstream::operator>(TypeReader& reader) { QByteArray typeName; *this >> typeName;