From 2b8e7757cdb8595181a01bb18756d60662c41fde Mon Sep 17 00:00:00 2001
From: Matt Oliver <protogonoi@gmail.com>
Date: Thu, 15 Oct 2015 19:42:31 +1100
Subject: [PATCH 1/2] lavu/intmath.h: Add msvc/icl ctzll optimisations.

Signed-off-by: Matt Oliver <protogonoi@gmail.com>
---
 libavutil/x86/intmath.h | 28 ++++++++++++++++++++++++++++
 1 file changed, 28 insertions(+)

diff --git a/libavutil/x86/intmath.h b/libavutil/x86/intmath.h
index fefad20..613903b 100644
--- a/libavutil/x86/intmath.h
+++ b/libavutil/x86/intmath.h
@@ -60,6 +60,34 @@ static av_always_inline av_const unsigned av_mod_uintp2_bmi2(unsigned a, unsigne
 
 #endif /* __BMI2__ */
 
+#elif defined(_MSC_VER)
+#define ff_ctzll ff_ctzll_c
+static av_always_inline av_const int ff_ctzll_c(long long v)
+{
+#if defined(__INTEL_COMPILER) 
+#if ARCH_X86_64
+    uint64_t c;
+    __asm__("bsfq %1,%0" : "=r" (c) : "r" (v));
+    return c;
+#else
+    return ((uint32_t)v == 0) ? _bit_scan_forward(((uint32_t *)&v)[1]) + 32 : _bit_scan_forward((uint32_t)v);
+#endif
+#else
+    unsigned long c;
+#if ARCH_X86_64
+    _BitScanForward64(&c, v);
+#else
+    if ((uint32_t)v == 0) {
+        _BitScanForward(&c, ((uint32_t *)&v)[1]);
+        c += 32;
+    } else {
+        _BitScanForward(&c, (uint32_t)v);
+    }
+#endif
+    return c;
+#endif
+}
+
 #endif /* __GNUC__ */
 
 #endif /* AVUTIL_X86_INTMATH_H */
-- 
1.9.5.github.0

