Skip to content
This repository was archived by the owner on Oct 14, 2025. It is now read-only.

Commit 71583df

Browse files
committed
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.
1 parent 16e0741 commit 71583df

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/zopfli/util.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,22 @@ Author: [email protected] (Jyrki Alakuijala)
3333
/* __builtin_clz available beginning with GCC 3.4 */
3434
#elif __GNUC__ * 100 + __GNUC_MINOR__ >= 304
3535
# define HAS_BUILTIN_CLZ
36+
/* _BitScanReverse available beginning with Visual Studio 2005 */
37+
#elif _MSC_VER >= 1400
38+
# include <intrin.h>
39+
# define HAS_BITSCANREVERSE
3640
#endif
3741

3842
int ZopfliGetDistExtraBits(int dist) {
39-
#ifdef HAS_BUILTIN_CLZ
4043
if (dist < 5) return 0;
44+
#ifdef HAS_BUILTIN_CLZ
4145
return (31 ^ __builtin_clz(dist - 1)) - 1; /* log2(dist - 1) - 1 */
46+
#elif defined HAS_BITSCANREVERSE
47+
unsigned long index;
48+
_BitScanReverse(&index, dist - 1);
49+
return index - 1;
4250
#else
43-
if (dist < 5) return 0;
44-
else if (dist < 9) return 1;
51+
if (dist < 9) return 1;
4552
else if (dist < 17) return 2;
4653
else if (dist < 33) return 3;
4754
else if (dist < 65) return 4;

0 commit comments

Comments
 (0)