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

Ruben Van Boxem vanboxem.ruben at gmail.com
Thu Nov 24 03:42:38 PST 2011


2011/11/24 Jean-Daniel Dupas <devlists at shadowlab.org>:
> 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.

That's my fault. After further investigation, you are completely
correct: I thought I found that all x86 processors supported these
instructions, but that was only valid for the _BitScan* functions). I
don't know how to fix this (I'm just a humble hobbyist), so patches
are welcome.

The problem is only theoretical though, as libc++ is unusable with
current (and near-future) versions of MSVC, as their C++11 support is
not good enough for the rest of the library as far as I can tell.
There's problems with decltype and overload resolution (which is
really to compiler-level to work around). The intrinsics in question
are only used for MSVC/non-Clang/non-GCC compiles.

I'm working (or rather, as working) on using a MSVC-based Clang to
further enhance libc++ compatibility with Visual Studio headers, but
haven't gotten to useful parts yet. Help is welcome!

Thanks for the interest,

Ruben

>
> -- Jean-Daniel
>
>
>
>
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev
>
>




More information about the cfe-dev mailing list