[PATCH] D13414: Define GNU clz builtins equivalents for MSVC

angelsl via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 3 10:45:23 PDT 2015


angelsl created this revision.
angelsl added a reviewer: compnerd.
angelsl added subscribers: compnerd, llvm-commits.

http://reviews.llvm.org/D13414

Files:
  lib/builtins/fp_lib.h

Index: lib/builtins/fp_lib.h
===================================================================
--- lib/builtins/fp_lib.h
+++ lib/builtins/fp_lib.h
@@ -38,6 +38,41 @@
 # endif
 #endif
 
+#ifdef _MSC_VER
+#include <intrin.h>
+
+uint32_t __inline __builtin_ctz(uint32_t value) {
+    uint32_t trailing_zero = 0;
+    if (_BitScanForward(&trailing_zero, value)) {
+        return trailing_zero;
+    } else {
+        return 32;
+    }
+}
+
+uint32_t __inline __builtin_clz(uint32_t value) {
+    uint32_t leading_zero = 0;
+    if (_BitScanReverse(&leading_zero, value)) {
+        return 31 - leading_zero;
+    } else {
+        return 32;
+    }
+}
+
+uint32_t __inline __builtin_clzll(uint64_t value) {
+    uint32_t leading_zero = 0;
+    if (_BitScanReverse64(&leading_zero, value)) {
+        return 63 - leading_zero;
+    } else {
+        return 64;
+    }
+}
+
+uint32_t __inline __builtin_clzl(uint64_t value) {
+    return __builtin_clzll(value);
+}
+#endif
+
 #if defined SINGLE_PRECISION
 
 typedef uint32_t rep_t;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D13414.36442.patch
Type: text/x-patch
Size: 1029 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20151003/e5b469d4/attachment.bin>


More information about the llvm-commits mailing list