Skip to content

Instantly share code, notes, and snippets.

@gvanem
Last active July 5, 2020 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gvanem/f96c20a1738f8f1d03adc742c0d337b9 to your computer and use it in GitHub Desktop.
Save gvanem/f96c20a1738f8f1d03adc742c0d337b9 to your computer and use it in GitHub Desktop.
A diff for LibFileZilla to compile with clang-cl and for x86. Ref: https://svn.filezilla-project.org/filezilla/libfilezilla/trunk/lib/util.cpp?view=markup
--- a/libfilezilla/lib/util.cpp 2020-07-01 12:30:29
+++ b/libfilezilla/lib/util.cpp 2020-07-04 15:32:17
@@ -125,8 +125,22 @@
uint64_t bitscan(uint64_t v)
{
-#if !FZ_WINDOWS || defined(__MINGW32__) || defined(__MINGW64__)
+#if !FZ_WINDOWS || defined(__MINGW32__) || defined(__MINGW64__) || defined(__clang__)
return __builtin_ctzll(v);
+#elif defined(_M_IX86)
+ /*
+ * Modified from:
+ * https://github.com/google/re2/commit/35febd432d9e6d8630845285c7f29eabd1df7beb
+ */
+ unsigned long i;
+
+ if (static_cast<uint32_t>(v) != 0) {
+ _BitScanForward(&i, static_cast<uint32_t>(v));
+ return static_cast<uint64_t>(i);
+ }
+ _BitScanForward(&i, static_cast<uint32_t>(v >> 32));
+ return static_cast<uint64_t>(i) + 32;
+
#else
unsigned long i;
_BitScanForward64(&i, v);
@@ -137,8 +151,19 @@
uint64_t bitscan_reverse(uint64_t v)
{
-#if !FZ_WINDOWS || defined(__MINGW32__) || defined(__MINGW64__)
+#if !FZ_WINDOWS || defined(__MINGW32__) || defined(__MINGW64__) || defined(__clang__)
return 63 - __builtin_clzll(v);
+#elif defined(_M_IX86)
+ unsigned long i;
+ uint32_t x = static_cast<uint32_t>(v >> 32);
+
+ if (_BitScanReverse(&i, x))
+ return (i + 1 + 32);
+
+ x = static_cast<uint32_t>(v);
+ if (_BitScanReverse(&i, x))
+ return (i + 1);
+ return (0ULL);
#else
unsigned long i;
_BitScanReverse64(&i, v);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment