From 00addf5adc340c08723ab516d7efe3844754d1e3 Mon Sep 17 00:00:00 2001 From: "verwaest@chromium.org" Date: Tue, 4 Jun 2013 15:39:56 +0000 Subject: [PATCH] Replace log2 with MostSignificantBit R=ulan@chromium.org Review URL: https://chromiumcodereview.appspot.com/15994015 git-svn-id: http://v8.googlecode.com/svn/branches/bleeding_edge@14937 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/hydrogen-instructions.cc | 9 +++------ src/utils.h | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/hydrogen-instructions.cc b/src/hydrogen-instructions.cc index 1943cce..4b1093e 100644 --- a/src/hydrogen-instructions.cc +++ b/src/hydrogen-instructions.cc @@ -2328,14 +2328,11 @@ Range* HBitwise::InferRange(Zone* zone) { if (right_upper < 0) right_upper = ~right_upper; if (right_lower < 0) right_lower = ~right_lower; - // Find the highest used bit. - int high = static_cast(log2(left_upper)); - high = Max(high, static_cast(log2(left_lower))); - high = Max(high, static_cast(log2(right_upper))); - high = Max(high, static_cast(log2(right_lower))); + int high = MostSignificantBit( + left_upper | left_lower | right_upper | right_lower); int64_t limit = 1; - limit <<= high + 1; + limit <<= high; int32_t min = (left()->range()->CanBeNegative() || right()->range()->CanBeNegative()) ? static_cast(-limit) : 0; diff --git a/src/utils.h b/src/utils.h index b16730b..5680384 100644 --- a/src/utils.h +++ b/src/utils.h @@ -86,6 +86,25 @@ inline int WhichPowerOf2(uint32_t x) { } +inline int MostSignificantBit(uint32_t x) { + static const int msb4[] = {0, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4}; + int nibble = 0; + if (x & 0xffff0000) { + nibble += 16; + x >>= 16; + } + if (x & 0xff00) { + nibble += 8; + x >>= 8; + } + if (x & 0xf0) { + nibble += 4; + x >>= 4; + } + return nibble + msb4[x]; +} + + // Magic numbers for integer division. // These are kind of 2's complement reciprocal of the divisors. // Details and proofs can be found in: -- 2.7.4