<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div>Hello,</div><div><br></div><div>While browsing win32 support headers of the libcxx, I found these macros:</div><div><br></div><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; color: rgb(123, 72, 47); ">#define __builtin_popcount __popcnt</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; color: rgb(123, 72, 47); ">#define __builtin_popcountl __popcnt</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; color: rgb(123, 72, 47); ">#define __builtin_popcountll(__i) static_cast<int>(__popcnt64(__i))</div></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; color: rgb(123, 72, 47); "><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; "><font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="background-color: transparent;">While using </span></font>_BitScanReverse <font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="background-color: transparent;">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.</span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; "><font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="background-color: transparent;"><br></span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; "><font class="Apple-style-span" face="Helvetica" size="3"><span class="Apple-style-span" style="background-color: transparent;">From MSDN documentation:</span></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; "><font class="Apple-style-span" face="Helvetica" size="3"><br></font></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; font: normal normal normal 11px/normal Menlo; "><span class="Apple-style-span" style="font-family: 'Segoe UI', Verdana, Arial; font-size: 13px; ">To determine hardware support for the <span><span class="input">popcnt </span></span>instruction, call the <strong>__cpuid</strong> intrinsic with <span class="parameter" style="font-style: italic; ">InfoType=0x00000001</span> and check bit 23 of<span class="parameter" style="font-style: italic; ">CPUInfo[2] (ECX)</span>. This bit is 1 if the instruction is supported, and 0 otherwise. </span><b><span class="Apple-style-span" style="font-family: 'Segoe UI', Verdana, Arial; font-size: 13px; ">If you run code that uses this intrinsic on hardware that does not support the </span><span class="Apple-style-span" style="font-family: 'Segoe UI', Verdana, Arial; font-size: 13px; "><span><span class="input">popcnt</span></span></span><span class="Apple-style-span" style="font-family: 'Segoe UI', Verdana, Arial; font-size: 13px; "> instruction, the results are unpredictable.</span></b></div><div><br></div><div>Where "unpredictable" generally means raising an "invalid instruction" exception.</div><div><br></div><div>This macros should be either removed if there are not used, or replaced by a safe implementation.</div><div><br></div><div><br></div><div apple-content-edited="true">
<div>-- Jean-Daniel</div><div><br></div><div><br></div><br class="Apple-interchange-newline">
</div>
<br></body></html>