Skip to content

Commit

Permalink
Utilize _BitScanReverse in Visual Studio
Browse files Browse the repository at this point in the history
_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.
  • Loading branch information
JayXon committed Apr 9, 2016
1 parent 16e0741 commit 71583df
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/zopfli/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,22 @@ Author: [email protected] (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 <intrin.h>
# 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;
Expand Down

0 comments on commit 71583df

Please sign in to comment.