From 110de33ca497d94fc4737e5154d3fe781fa84a0a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Piotr=20Da=C5=82ek?= Date: Wed, 31 Aug 2016 17:24:31 +0200 Subject: [PATCH] crush: don't normalize input of crush_ln iteratively MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Use __builtin_clz() supported by GCC and Clang to figure out how many bits we should shift instead of shifting by a bit in a loop until the value gets normalized. Improves performance of this function by up to 3x in worst-case scenario and overall straw2 performance by ~10%. Signed-off-by: Piotr Dałek --- src/crush/mapper.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/crush/mapper.c b/src/crush/mapper.c index 832900f6821bf..6f6b09e5807e1 100644 --- a/src/crush/mapper.c +++ b/src/crush/mapper.c @@ -253,9 +253,13 @@ static __u64 crush_ln(unsigned int xin) /* normalize input */ iexpon = 15; - while (!(x & 0x18000)) { - x <<= 1; - iexpon--; + + // figure out number of bits we need to shift and + // do it in one step instead of iteratively + if (!(x & 0x18000)) { + int bits = __builtin_clz(x & 0x1FFFF) - 16; + x <<= bits; + iexpon = 15 - bits; } index1 = (x >> 8) << 1; -- 2.39.5