From 71583dfc4c4670d6be9f4bb2984a9e5814affc59 Mon Sep 17 00:00:00 2001 From: JayXon Date: Fri, 8 Apr 2016 23:23:27 -0700 Subject: [PATCH] Utilize _BitScanReverse in Visual Studio _BitScanReverse is documented here: https://msdn.microsoft.com/en-us/library/fbxyd7zd.aspx This improves speed of a VS2015 x64 build by around 8%. It's slightly slower than __lzcnt, but compatible with old cpu. --- src/zopfli/util.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/zopfli/util.c b/src/zopfli/util.c index 84806aa9..e993a16c 100644 --- a/src/zopfli/util.c +++ b/src/zopfli/util.c @@ -33,15 +33,22 @@ Author: jyrki.alakuijala@gmail.com (Jyrki Alakuijala) /* __builtin_clz available beginning with GCC 3.4 */ #elif __GNUC__ * 100 + __GNUC_MINOR__ >= 304 # define HAS_BUILTIN_CLZ +/* _BitScanReverse available beginning with Visual Studio 2005 */ +#elif _MSC_VER >= 1400 +# include +# define HAS_BITSCANREVERSE #endif int ZopfliGetDistExtraBits(int dist) { -#ifdef HAS_BUILTIN_CLZ if (dist < 5) return 0; +#ifdef HAS_BUILTIN_CLZ return (31 ^ __builtin_clz(dist - 1)) - 1; /* log2(dist - 1) - 1 */ +#elif defined HAS_BITSCANREVERSE + unsigned long index; + _BitScanReverse(&index, dist - 1); + return index - 1; #else - if (dist < 5) return 0; - else if (dist < 9) return 1; + if (dist < 9) return 1; else if (dist < 17) return 2; else if (dist < 33) return 3; else if (dist < 65) return 4;