I want both implementations of __builtin_popcountll

I have some code that I would like to run a quick built-in CPU popcnt instruction (when __builtin_popcountll compiled using the correct flags like g++ -mpopcnt or clang++ -march=corei7 , this happens), but will also be able to return to the code when cpuid shows that the CPU does not support the HW instruction.

Of course, in order to get a return code that I trust, people compilers are implemented correctly (so I don’t need to enter C or asm code for my popcount) I need a separate compilation unit that compiled without the -mpopcnt or -march=corei7 .

Does only compiled code bind in a single way? Are there any built-in compilers or other types of tooltips or other built-in modules that I don't know about what I can use to generate popcount backup code?

+4
source share
2 answers

I don’t know for sure, but the cost of entering the code needed to choose between the popcnt instruction and the backup implementation may have a higher performance rating than just switching to non-popcnt all the time.

To switch to an alternative implementation (perform a switch on the popcnt website), you will need at least the following:

  • Download and check the cpuid bit (CPUID.01H: ECX.POPCNT [Bit 23])
  • Debugging to select popcnt or alternative implementation
  • Potentially save / restore registers needed for alternative implementation.
  • Potentially move data to SIMD registers if an alternate implementation is based on SSE or AVX

I suspect cost prohibits the effective implementation of your inherent description.

+2
source

You can directly call "code rollback". I believe that it is available for standard libraries:

 int __popcountsi2 (int a) int __popcountdi2 (long a) int __popcountti2 (long long a) 
0
source

All Articles