[cfe-dev] [libcxx] Win32 port must not use __popcnt intrinsect

Jean-Daniel Dupas devlists at shadowlab.org
Thu Nov 24 02:28:24 PST 2011


Hello,

While browsing win32 support headers of the libcxx, I found these macros:

#define __builtin_popcount __popcnt
#define __builtin_popcountl __popcnt
#define __builtin_popcountll(__i) static_cast<int>(__popcnt64(__i))

While using _BitScanReverse and other built-in is fine, using  __popcnt is not safe at all. Whatever the cpu target is, the compiler will generate a __popcnt instruction, even if it does not support it.

From MSDN documentation:

To determine hardware support for the popcnt instruction, call the __cpuid intrinsic with InfoType=0x00000001 and check bit 23 ofCPUInfo[2] (ECX). This bit is 1 if the instruction is supported, and 0 otherwise. If you run code that uses this intrinsic on hardware that does not support the popcnt instruction, the results are unpredictable.

Where "unpredictable" generally means raising an "invalid instruction" exception.

This macros should be either removed if there are not used, or replaced by a safe implementation.


-- Jean-Daniel




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20111124/a653d94b/attachment.html>


More information about the cfe-dev mailing list