[PATCH] D69334: [MathExtras] Add intrinsic for the PopulationCounter in Visual Studio

Ehud Katz via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 23 04:51:08 PDT 2019


ekatz created this revision.
ekatz added reviewers: lattner, chandlerc, craig.topper.
ekatz added a project: LLVM.
Herald added a subscriber: llvm-commits.

The Visual Studio __popcnt and __popcnt64 intrinsics should be used the same way the GNU __builtin_popcount is already used.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D69334

Files:
  llvm/include/llvm/Support/MathExtras.h


Index: llvm/include/llvm/Support/MathExtras.h
===================================================================
--- llvm/include/llvm/Support/MathExtras.h
+++ llvm/include/llvm/Support/MathExtras.h
@@ -35,6 +35,8 @@
 unsigned char _BitScanForward64(unsigned long *_Index, unsigned __int64 _Mask);
 unsigned char _BitScanReverse(unsigned long *_Index, unsigned long _Mask);
 unsigned char _BitScanReverse64(unsigned long *_Index, unsigned __int64 _Mask);
+unsigned int __popcnt(unsigned int);
+unsigned __int64 __popcnt64(unsigned __int64);
 }
 #endif
 
@@ -525,6 +527,9 @@
     static_assert(SizeOfT <= 4, "Not implemented!");
 #if defined(__GNUC__)
     return __builtin_popcount(Value);
+#elif defined(_MSC_VER) && _MSC_VER >= 1500 &&                                 \
+    (defined(_M_IX86) || defined(_M_X64))
+    return __popcnt(Value);
 #else
     uint32_t v = Value;
     v = v - ((v >> 1) & 0x55555555);
@@ -538,6 +543,8 @@
   static unsigned count(T Value) {
 #if defined(__GNUC__)
     return __builtin_popcountll(Value);
+#elif defined(_MSC_VER) && _MSC_VER >= 1500 && defined(_M_X64)
+    return (unsigned)__popcnt64(Value);
 #else
     uint64_t v = Value;
     v = v - ((v >> 1) & 0x5555555555555555ULL);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D69334.226116.patch
Type: text/x-patch
Size: 1221 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191023/a74cff34/attachment.bin>


More information about the llvm-commits mailing list