Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_BitScanForward64 intrinsic is not available on x86 #72

Open
db4 opened this issue Apr 3, 2019 · 1 comment
Open

_BitScanForward64 intrinsic is not available on x86 #72

db4 opened this issue Apr 3, 2019 · 1 comment
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.

Comments

@db4
Copy link
Contributor

db4 commented Apr 3, 2019

This function

_BitScanForward64(&r, x);

is not supported by 32-bit MSVC (see https://docs.microsoft.com/en-us/cpp/intrinsics/bitscanforward-bitscanforward64). So I get a link error.

@db4
Copy link
Contributor Author

db4 commented Apr 4, 2019

The following patch should fix the bug:

--- a/src/int_math_stubs.c	2019-02-25 13:55:12.000000000 +0300
+++ b/src/int_math_stubs.c	2019-04-04 18:29:43.760643700 +0300
@@ -12,17 +12,24 @@
 #define __builtin_popcountll __popcnt64
 #define __builtin_popcount   __popcnt
 
-static uint32_t __inline __builtin_clz(uint32_t x)
+static int __inline __builtin_clz(uint32_t x)
 {
   int r = 0;
   _BitScanForward(&r, x);
   return r;
 }
 
-static uint64_t __inline __builtin_clzll(uint64_t x)
+static int __inline __builtin_clzll(uint64_t x)
 {
   int r = 0;
+#ifdef _WIN64
   _BitScanForward64(&r, x);
+#else
+  if (!_BitScanForward(&r, (uint32_t)x) &&
+      _BitScanForward(&r, (uint32_t)(x>>32))) {
+    r += 32;
+  }
+#endif
   return r;
 }

@github-iron github-iron added the forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system. label Sep 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
forwarded-to-js-devs This report has been forwarded to Jane Street's internal review system.
Projects
None yet
Development

No branches or pull requests

2 participants