From 463834a82ebd26e394b1c3b9607d0b3ac939655b Mon Sep 17 00:00:00 2001 From: Mika Lindqvist Date: Sun, 30 Oct 2016 14:12:42 +0200 Subject: [PATCH] Move Visual C++ workaround for CTZL to arch/x86. --- arch/x86/ctzl.h | 25 +++++++++++++++++++++++++ match.c | 22 ++-------------------- 2 files changed, 27 insertions(+), 20 deletions(-) create mode 100644 arch/x86/ctzl.h diff --git a/arch/x86/ctzl.h b/arch/x86/ctzl.h new file mode 100644 index 00000000..bc9e9bd5 --- /dev/null +++ b/arch/x86/ctzl.h @@ -0,0 +1,25 @@ +#ifndef X86_CTZL_H +#define X86_CTZL_H + +#include +#ifdef X86_CPUID +# include "x86.h" +#endif + +#if defined(_MSC_VER) && !defined(__clang__) +/* This is not a general purpose replacement for __builtin_ctzl. The function expects that value is != 0 + * Because of that assumption trailing_zero is not initialized and the return value of _BitScanForward is not checked + */ +static __forceinline unsigned long __builtin_ctzl(unsigned long value) +{ +#ifdef X86_CPUID + if (x86_cpu_has_tzcnt) + return _tzcnt_u32(value); +#endif + unsigned long trailing_zero; + _BitScanForward(&trailing_zero, value); + return trailing_zero; +} +#endif + +#endif diff --git a/match.c b/match.c index ce93132b..98310a32 100644 --- a/match.c +++ b/match.c @@ -29,9 +29,8 @@ #if defined(_MSC_VER) && !defined(__clang__) -#include -# ifdef X86_CPUID -# include "arch/x86/x86.h" +# if defined(_M_IX86) || defined(_M_AMD64) || defined(_M_IA64) +# include "arch/x86/ctzl.h" # endif #endif @@ -277,23 +276,6 @@ ZLIB_INTERNAL unsigned longest_match(deflate_state *const s, IPos cur_match) { #endif #ifdef std3_longest_match - -#if defined(_MSC_VER) && !defined(__clang__) -/* This is not a general purpose replacement for __builtin_ctzl. The function expects that value is != 0 - * Because of that assumption trailing_zero is not initialized and the return value of _BitScanForward is not checked - */ -static __forceinline unsigned long __builtin_ctzl(unsigned long value) -{ -#ifdef X86_CPUID - if (x86_cpu_has_tzcnt) - return _tzcnt_u32(value); -#endif - unsigned long trailing_zero; - _BitScanForward(&trailing_zero, value); - return trailing_zero; -} -#endif - /* longest_match() with minor change to improve performance (in terms of * execution time). * -- 2.47.2